diff --git a/api/reddit/check_subreddit.go b/api/reddit/check_subreddit.go index db611fa..cbbd941 100644 --- a/api/reddit/check_subreddit.go +++ b/api/reddit/check_subreddit.go @@ -7,6 +7,7 @@ import ( "net/http" "time" + "github.com/tigorlazuardi/redmage/pkg/caller" "github.com/tigorlazuardi/redmage/pkg/errs" ) @@ -22,6 +23,8 @@ func (reddit *Reddit) CheckSubreddit(ctx context.Context, params CheckSubredditP ctx, span := tracer.Start(ctx, "*Reddit.CheckSubreddit") defer span.End() + ctx = caller.WithContext(ctx, caller.New(2)) + url := fmt.Sprintf("https://reddit.com/%s/%s.json?limit=1", params.SubredditType, params.Subreddit) req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody) if err != nil { diff --git a/api/reddit/client.go b/api/reddit/client.go index dd421f0..f8a8ab6 100644 --- a/api/reddit/client.go +++ b/api/reddit/client.go @@ -4,6 +4,7 @@ import ( "net/http" "github.com/tigorlazuardi/redmage/config" + "github.com/tigorlazuardi/redmage/pkg/log" ) type Client interface { @@ -25,6 +26,22 @@ func (ro roundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) func createRoundTripper(cfg *config.Config) roundTripperFunc { return func(r *http.Request) (*http.Response, error) { r.Header.Set("User-Agent", cfg.String("download.useragent")) - return http.DefaultTransport.RoundTrip(r) + resp, err := http.DefaultTransport.RoundTrip(r) + if err != nil { + log.New(r.Context()). + Err(err). + Errorf("reddit: %s %s", r.Method, r.URL) + return resp, err + } + if resp.StatusCode >= 400 { + log.New(r.Context()). + Errorf("reddit: %s %s %d", r.Method, r.URL, resp.StatusCode) + return resp, err + } + if resp.StatusCode >= 200 && resp.StatusCode < 300 { + log.New(r.Context()). + Infof("reddit: %s %s %d", r.Method, r.URL, resp.StatusCode) + } + return resp, err } } diff --git a/pkg/caller/caller.go b/pkg/caller/caller.go index b7ded1a..80b8113 100644 --- a/pkg/caller/caller.go +++ b/pkg/caller/caller.go @@ -1,6 +1,7 @@ package caller import ( + "context" "log/slog" "os" "runtime" @@ -70,3 +71,14 @@ func From(pc uintptr) Caller { c.Frame, _ = runtime.CallersFrames([]uintptr{pc}).Next() return c } + +type contextKey struct{} + +func WithContext(ctx context.Context, caller Caller) context.Context { + return context.WithValue(ctx, contextKey{}, caller) +} + +func FromContext(ctx context.Context) (Caller, bool) { + call, ok := ctx.Value(contextKey{}).(Caller) + return call, ok +} diff --git a/pkg/log/log.go b/pkg/log/log.go index b121077..7e1cdf3 100644 --- a/pkg/log/log.go +++ b/pkg/log/log.go @@ -273,6 +273,9 @@ func (entry *Entry) getCaller() caller.Caller { if entry.caller.PC != 0 { return entry.caller } + if call, ok := caller.FromContext(entry.ctx); ok { + return call + } return caller.New(4) } diff --git a/rest/subreddits/create.http b/rest/subreddits/create.http index 470dc87..4ff3d4a 100644 --- a/rest/subreddits/create.http +++ b/rest/subreddits/create.http @@ -2,7 +2,7 @@ POST http://localhost:8080/api/v1/subreddits HTTP/1.1 Host: localhost:8080 { - "name": "CultureImpact", + "name": "AnimeLandscapes", "enable_schedule": 1, "schedule": "@daily", "countback": 300 diff --git a/rest/subreddits/start.http b/rest/subreddits/start.http index 7644c21..36d7049 100644 --- a/rest/subreddits/start.http +++ b/rest/subreddits/start.http @@ -3,5 +3,5 @@ Host: localhost:8080 Content-Type: application/json { - "subreddit": "CultureImpact" + "subreddit": "AnimeLandscapes" } diff --git a/views/homeview/homeview.templ b/views/homeview/homeview.templ index bf0ca73..b5db451 100644 --- a/views/homeview/homeview.templ +++ b/views/homeview/homeview.templ @@ -24,11 +24,11 @@ templ home(_ *views.Context, data Data) {

Recently Added

- for _, deviceMapValue := range data.RecentlyAddedImages { -

{ deviceMapValue.device.Name }

- for _, subreddit := range deviceMapValue.subreddits { -

{ subreddit.subreddit.Name }

- @RecentlyAddedImageList(subreddit.images, 0) + for _, recently := range data.RecentlyAddedImages { +

{ recently.Device.Name }

+ for _, subreddit := range recently.Subreddits { +

{ subreddit.Subreddit.Name }

+ @RecentlyAddedImageList(subreddit.Images, 0) } }
diff --git a/views/homeview/homeview_data.go b/views/homeview/homeview_data.go index 97539e2..ea86983 100644 --- a/views/homeview/homeview_data.go +++ b/views/homeview/homeview_data.go @@ -1,6 +1,9 @@ package homeview import ( + "slices" + "strings" + "github.com/tigorlazuardi/redmage/api" "github.com/tigorlazuardi/redmage/models" ) @@ -11,38 +14,74 @@ type Data struct { Error error } -type RecentlyAddedImages = map[int32]deviceMapValue - -type deviceMapValue struct { - device *models.Device - subreddits map[int32]subredditMapValue -} +type RecentlyAddedImages = []RecentlyAddedImage type subredditMapValue struct { subreddit *models.Subreddit images []*models.Image } +type RecentlyAddedImage struct { + Device *models.Device + Subreddits []Subreddit +} + +type Subreddit struct { + Subreddit *models.Subreddit + Images models.ImageSlice +} + func NewRecentlyAddedImages(images models.ImageSlice) RecentlyAddedImages { - r := make(RecentlyAddedImages) + r := make(RecentlyAddedImages, 0, len(images)) for _, image := range images { if image.R.Device == nil || image.R.Subreddit == nil { continue } - if _, ok := r[image.R.Device.ID]; !ok { - r[image.R.Device.ID] = deviceMapValue{ - device: image.R.Device, - subreddits: make(map[int32]subredditMapValue), + var deviceFound bool + for i, ra := range r { + if ra.Device.ID == image.R.Device.ID { + deviceFound = true + var subredditFound bool + for j, subreddit := range r[i].Subreddits { + if subreddit.Subreddit.ID == image.R.Subreddit.ID { + subredditFound = true + r[i].Subreddits[j].Images = append(r[i].Subreddits[j].Images, image) + } + } + if !subredditFound { + r[i].Subreddits = append(r[i].Subreddits, Subreddit{ + Subreddit: image.R.Subreddit, + Images: models.ImageSlice{image}, + }) + } } } - if _, ok := r[image.R.Device.ID].subreddits[image.R.Subreddit.ID]; !ok { - r[image.R.Device.ID].subreddits[image.R.Subreddit.ID] = subredditMapValue{} - } - images := append(r[image.R.Device.ID].subreddits[image.R.Subreddit.ID].images, image) - r[image.R.Device.ID].subreddits[image.R.Subreddit.ID] = subredditMapValue{ - subreddit: image.R.Subreddit, - images: images, + if !deviceFound { + r = append(r, RecentlyAddedImage{ + Device: image.R.Device, + Subreddits: []Subreddit{ + { + Subreddit: image.R.Subreddit, + Images: models.ImageSlice{image}, + }, + }, + }) } } + + for _, r := range r { + slices.SortFunc(r.Subreddits, func(left, right Subreddit) int { + leftName := strings.ToLower(left.Subreddit.Name) + rightName := strings.ToLower(right.Subreddit.Name) + return strings.Compare(leftName, rightName) + }) + } + + slices.SortFunc(r, func(left, right RecentlyAddedImage) int { + leftName := strings.ToLower(left.Device.Name) + rightName := strings.ToLower(right.Device.Name) + return strings.Compare(leftName, rightName) + }) + return r }