Skip to content

Commit

Permalink
chore(youtube): Move iframe param to struct field
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Jun 24, 2024
1 parent d43bcd1 commit 73cc039
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 15 deletions.
6 changes: 4 additions & 2 deletions internal/youtube/channel/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func New(service *youtube.Service, id string) Channel {
type Channel struct {
Service *youtube.Service
ID string
Iframe bool
}

var ErrInvalid = errors.New("invalid channel")
Expand All @@ -40,12 +41,13 @@ func (p Channel) Meta(ctx context.Context) (*youtube.Channel, error) {
return resp.Items[0], nil
}

func (p Channel) Feed(ctx context.Context, noIframe bool) (*feeds.Feed, error) {
func (p Channel) Feed(ctx context.Context) (*feeds.Feed, error) {
meta, err := p.Meta(ctx)
if err != nil {
return nil, err
}

pl := playlist.New(p.Service, meta.ContentDetails.RelatedPlaylists.Uploads)
return pl.Feed(ctx, noIframe)
pl.Iframe = p.Iframe
return pl.Feed(ctx)
}
4 changes: 3 additions & 1 deletion internal/youtube/channel/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import (
func Handler(service *youtube.Service) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
identifier := chi.URLParam(r, "id")
iframe := r.Context().Value(middleware.IframeKey).(bool)
ch := New(service, identifier)
ch.Iframe = iframe

f, err := ch.Feed(r.Context(), r.Context().Value(middleware.NoIframeKey).(bool))
f, err := ch.Feed(r.Context())
if err != nil {
if errors.Is(err, ErrInvalid) {
http.Error(w, "404 channel not found", http.StatusNotFound)
Expand Down
2 changes: 1 addition & 1 deletion internal/youtube/middleware/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package middleware
type ContextKey uint8

const (
NoIframeKey ContextKey = iota
IframeKey ContextKey = iota
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"net/http"
)

func NoIframe(next http.Handler) http.Handler {
func IframeParam(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
params := r.URL.Query()
noIframe := params.Has("no_iframe") || params.Has("disable_iframe")
r = r.WithContext(context.WithValue(r.Context(), NoIframeKey, noIframe))
iframe := !params.Has("no_iframe") && !params.Has("disable_iframe")
r = r.WithContext(context.WithValue(r.Context(), IframeKey, iframe))
next.ServeHTTP(w, r)
}

Expand Down
4 changes: 2 additions & 2 deletions internal/youtube/playlist/feed_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

type Item youtube.PlaylistItemSnippet

func (i Item) FeedItem(noIframe bool) (*feeds.Item, error) {
func (i Item) FeedItem(iframe bool) (*feeds.Item, error) {
published, err := time.Parse(time.RFC3339, i.PublishedAt)
if err != nil {
return nil, err
Expand All @@ -21,7 +21,7 @@ func (i Item) FeedItem(noIframe bool) (*feeds.Item, error) {
var description strings.Builder
if err := tmpl.DescriptionTmpl.Execute(&description, map[string]any{
"Item": i,
"Iframe": !noIframe,
"Iframe": iframe,
}); err != nil {
return nil, err
}
Expand Down
4 changes: 3 additions & 1 deletion internal/youtube/playlist/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import (
func Handler(service *youtube.Service) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
identifier := chi.URLParam(r, "id")
iframe := r.Context().Value(middleware.IframeKey).(bool)
plist := New(service, identifier)
plist.Iframe = iframe

f, err := plist.Feed(r.Context(), r.Context().Value(middleware.NoIframeKey).(bool))
f, err := plist.Feed(r.Context())
if err != nil {
if errors.Is(err, ErrInvalid) {
http.Error(w, "404 playlist not found", http.StatusNotFound)
Expand Down
9 changes: 5 additions & 4 deletions internal/youtube/playlist/playlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func New(service *youtube.Service, id string) Playlist {
type Playlist struct {
Service *youtube.Service
ID string
Iframe bool
}

var ErrInvalid = errors.New("invalid playlist")
Expand All @@ -43,7 +44,7 @@ func (p Playlist) Meta(ctx context.Context) (*youtube.PlaylistSnippet, error) {
return resp.Items[0].Snippet, nil
}

func (p Playlist) Feed(ctx context.Context, noIframe bool) (*feeds.Feed, error) {
func (p Playlist) Feed(ctx context.Context) (*feeds.Feed, error) {
meta, err := p.Meta(ctx)
if err != nil {
return nil, err
Expand All @@ -63,7 +64,7 @@ func (p Playlist) Feed(ctx context.Context, noIframe bool) (*feeds.Feed, error)
Created: time.Now(),
}

feed.Items, err = p.FeedItems(ctx, noIframe)
feed.Items, err = p.FeedItems(ctx)
if err != nil {
return feed, err
}
Expand Down Expand Up @@ -109,7 +110,7 @@ func (p Playlist) Items(ctx context.Context) ([]*Item, error) {
return items, nil
}

func (p Playlist) FeedItems(ctx context.Context, noIframe bool) ([]*feeds.Item, error) {
func (p Playlist) FeedItems(ctx context.Context) ([]*feeds.Item, error) {
items, err := p.Items(ctx)
if err != nil {
return nil, err
Expand All @@ -118,7 +119,7 @@ func (p Playlist) FeedItems(ctx context.Context, noIframe bool) ([]*feeds.Item,
feedItems := make([]*feeds.Item, 0, len(items))

for _, item := range items {
feedItem, err := item.FeedItem(noIframe)
feedItem, err := item.FeedItem(p.Iframe)
if err != nil {
return feedItems, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/youtube/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func Routes(r chi.Router, conf config.YouTube) error {
}

r.Group(func(r chi.Router) {
r.Use(middleware.NoIframe)
r.Use(middleware.IframeParam)
r.Get("/youtube/channel/{id}", channel.Handler(service))
r.Get("/youtube/playlist/{id}", playlist.Handler(service))
})
Expand Down

0 comments on commit 73cc039

Please sign in to comment.