diff --git a/backend/internal/api/handler.go b/backend/internal/api/handler.go index edbd7dd..d09cc2e 100644 --- a/backend/internal/api/handler.go +++ b/backend/internal/api/handler.go @@ -339,9 +339,7 @@ func (h *Handler) GetAnnotations(w http.ResponseWriter, r *http.Request) { if source != "" { filter.TargetHash = db.HashURL(source) } - if motivation != "" { - filter.Motivations = []string{motivation} - } + filter.Motivations = parseMotivations(motivation) if tag != "" { filter.Tag = tag } @@ -384,6 +382,17 @@ func parseFeedType(s string) db.FeedType { } } +func parseMotivations(motivation string) []string { + switch motivation { + case "": + return nil + case "commenting": + return []string{"commenting", "tagging"} + default: + return []string{motivation} + } +} + func notesByMotivation(notes []service.APINote) (annotations, highlights, bookmarks []service.APINote) { annotations = []service.APINote{} highlights = []service.APINote{} @@ -431,15 +440,10 @@ func (h *Handler) GetFeed(w http.ResponseWriter, r *http.Request) { feedTypeStr := r.URL.Query().Get("type") feedType := parseFeedType(feedTypeStr) - var motivations []string - if motivation != "" { - motivations = []string{motivation} - } - fetchLimit := limit + offset req := service.FeedRequest{ ViewerDID: h.getViewerDID(r), - Motivations: motivations, + Motivations: parseMotivations(motivation), AuthorDID: creator, Tag: tag, FeedType: feedType, @@ -936,7 +940,7 @@ func (h *Handler) GetUserAnnotations(w http.ResponseWriter, r *http.Request) { notes, err := h.noteRepo.List(r.Context(), db.NoteFilter{ AuthorDID: did, - Motivations: []string{"commenting"}, + Motivations: parseMotivations("commenting"), Limit: limit, Offset: offset, }) @@ -1072,10 +1076,11 @@ func (h *Handler) GetUserTargetItems(w http.ResponseWriter, r *http.Request) { urlHash := db.HashURL(source) + motivations := append(parseMotivations("commenting"), "highlighting") notes, err := h.noteRepo.List(r.Context(), db.NoteFilter{ AuthorDID: did, TargetHash: urlHash, - Motivations: []string{"commenting", "highlighting"}, + Motivations: motivations, Limit: limit, Offset: offset, }) diff --git a/backend/internal/api/handler_test.go b/backend/internal/api/handler_test.go new file mode 100644 index 0000000..8a7c608 --- /dev/null +++ b/backend/internal/api/handler_test.go @@ -0,0 +1,27 @@ +package api + +import ( + "reflect" + "testing" +) + +func TestParseMotivationsIncludesTaggedAnnotations(t *testing.T) { + want := []string{"commenting", "tagging"} + if got := parseMotivations("commenting"); !reflect.DeepEqual(got, want) { + t.Fatalf("parseMotivations(commenting) = %v, want %v", got, want) + } +} + +func TestParseMotivationsPreservesOtherFilters(t *testing.T) { + for _, motivation := range []string{"highlighting", "bookmarking", ""} { + t.Run(motivation, func(t *testing.T) { + var want []string + if motivation != "" { + want = []string{motivation} + } + if got := parseMotivations(motivation); !reflect.DeepEqual(got, want) { + t.Fatalf("parseMotivations(%q) = %v, want %v", motivation, got, want) + } + }) + } +}