From fccc25f7a38a532d0434b12867870bbb6e86be4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Thu, 5 Oct 2023 20:28:36 -0700 Subject: [PATCH] Add changed_after and changed_before options to /v1/entries endpoint --- client/client.go | 24 +++++++++++++--- client/model.go | 32 ++++++++++++--------- internal/api/entry.go | 38 ++++++++++++++++--------- internal/googlereader/handler.go | 16 +++++------ internal/storage/entry_query_builder.go | 22 +++++++++++--- 5 files changed, 88 insertions(+), 44 deletions(-) diff --git a/client/client.go b/client/client.go index 18964459..bac7bdf1 100644 --- a/client/client.go +++ b/client/client.go @@ -540,14 +540,30 @@ func buildFilterQueryString(path string, filter *Filter) string { values.Set("after", strconv.FormatInt(filter.After, 10)) } - if filter.AfterEntryID > 0 { - values.Set("after_entry_id", strconv.FormatInt(filter.AfterEntryID, 10)) - } - if filter.Before > 0 { values.Set("before", strconv.FormatInt(filter.Before, 10)) } + if filter.PublishedAfter > 0 { + values.Set("published_after", strconv.FormatInt(filter.PublishedAfter, 10)) + } + + if filter.PublishedBefore > 0 { + values.Set("published_before", strconv.FormatInt(filter.PublishedBefore, 10)) + } + + if filter.ChangedAfter > 0 { + values.Set("changed_after", strconv.FormatInt(filter.ChangedAfter, 10)) + } + + if filter.ChangedBefore > 0 { + values.Set("changed_before", strconv.FormatInt(filter.ChangedBefore, 10)) + } + + if filter.AfterEntryID > 0 { + values.Set("after_entry_id", strconv.FormatInt(filter.AfterEntryID, 10)) + } + if filter.BeforeEntryID > 0 { values.Set("before_entry_id", strconv.FormatInt(filter.BeforeEntryID, 10)) } diff --git a/client/model.go b/client/model.go index 8a9ae17f..2d194a10 100644 --- a/client/model.go +++ b/client/model.go @@ -245,20 +245,24 @@ const ( // Filter is used to filter entries. type Filter struct { - Status string - Offset int - Limit int - Order string - Direction string - Starred string - Before int64 - After int64 - BeforeEntryID int64 - AfterEntryID int64 - Search string - CategoryID int64 - FeedID int64 - Statuses []string + Status string + Offset int + Limit int + Order string + Direction string + Starred string + Before int64 + After int64 + PublishedBefore int64 + PublishedAfter int64 + ChangedBefore int64 + ChangedAfter int64 + BeforeEntryID int64 + AfterEntryID int64 + Search string + CategoryID int64 + FeedID int64 + Statuses []string } // EntryResultSet represents the response when fetching entries. diff --git a/internal/api/entry.go b/internal/api/entry.go index 1044e849..08242d97 100644 --- a/internal/api/entry.go +++ b/internal/api/entry.go @@ -283,28 +283,39 @@ func (h *handler) fetchContent(w http.ResponseWriter, r *http.Request) { } func configureFilters(builder *storage.EntryQueryBuilder, r *http.Request) { - beforeEntryID := request.QueryInt64Param(r, "before_entry_id", 0) - if beforeEntryID > 0 { + if beforeEntryID := request.QueryInt64Param(r, "before_entry_id", 0); beforeEntryID > 0 { builder.BeforeEntryID(beforeEntryID) } - afterEntryID := request.QueryInt64Param(r, "after_entry_id", 0) - if afterEntryID > 0 { + if afterEntryID := request.QueryInt64Param(r, "after_entry_id", 0); afterEntryID > 0 { builder.AfterEntryID(afterEntryID) } - beforeTimestamp := request.QueryInt64Param(r, "before", 0) - if beforeTimestamp > 0 { - builder.BeforeDate(time.Unix(beforeTimestamp, 0)) + if beforePublishedTimestamp := request.QueryInt64Param(r, "before", 0); beforePublishedTimestamp > 0 { + builder.BeforePublishedDate(time.Unix(beforePublishedTimestamp, 0)) } - afterTimestamp := request.QueryInt64Param(r, "after", 0) - if afterTimestamp > 0 { - builder.AfterDate(time.Unix(afterTimestamp, 0)) + if afterPublishedTimestamp := request.QueryInt64Param(r, "after", 0); afterPublishedTimestamp > 0 { + builder.AfterPublishedDate(time.Unix(afterPublishedTimestamp, 0)) } - categoryID := request.QueryInt64Param(r, "category_id", 0) - if categoryID > 0 { + if beforePublishedTimestamp := request.QueryInt64Param(r, "published_before", 0); beforePublishedTimestamp > 0 { + builder.BeforePublishedDate(time.Unix(beforePublishedTimestamp, 0)) + } + + if afterPublishedTimestamp := request.QueryInt64Param(r, "published_after", 0); afterPublishedTimestamp > 0 { + builder.AfterPublishedDate(time.Unix(afterPublishedTimestamp, 0)) + } + + if beforeChangedTimestamp := request.QueryInt64Param(r, "changed_before", 0); beforeChangedTimestamp > 0 { + builder.BeforeChangedDate(time.Unix(beforeChangedTimestamp, 0)) + } + + if afterChangedTimestamp := request.QueryInt64Param(r, "changed_after", 0); afterChangedTimestamp > 0 { + builder.AfterChangedDate(time.Unix(afterChangedTimestamp, 0)) + } + + if categoryID := request.QueryInt64Param(r, "category_id", 0); categoryID > 0 { builder.WithCategoryID(categoryID) } @@ -315,8 +326,7 @@ func configureFilters(builder *storage.EntryQueryBuilder, r *http.Request) { } } - searchQuery := request.QueryStringParam(r, "search", "") - if searchQuery != "" { + if searchQuery := request.QueryStringParam(r, "search", ""); searchQuery != "" { builder.WithSearchQuery(searchQuery) } } diff --git a/internal/googlereader/handler.go b/internal/googlereader/handler.go index 3c88d55f..2312a20e 100644 --- a/internal/googlereader/handler.go +++ b/internal/googlereader/handler.go @@ -1329,10 +1329,10 @@ func (h *handler) handleReadingListStreamHandler(w http.ResponseWriter, r *http. builder.WithOffset(rm.Offset) builder.WithSorting(model.DefaultSortingOrder, rm.SortDirection) if rm.StartTime > 0 { - builder.AfterDate(time.Unix(rm.StartTime, 0)) + builder.AfterPublishedDate(time.Unix(rm.StartTime, 0)) } if rm.StopTime > 0 { - builder.BeforeDate(time.Unix(rm.StopTime, 0)) + builder.BeforePublishedDate(time.Unix(rm.StopTime, 0)) } rawEntryIDs, err := builder.GetEntryIDs() @@ -1367,10 +1367,10 @@ func (h *handler) handleStarredStreamHandler(w http.ResponseWriter, r *http.Requ builder.WithOffset(rm.Offset) builder.WithSorting(model.DefaultSortingOrder, rm.SortDirection) if rm.StartTime > 0 { - builder.AfterDate(time.Unix(rm.StartTime, 0)) + builder.AfterPublishedDate(time.Unix(rm.StartTime, 0)) } if rm.StopTime > 0 { - builder.BeforeDate(time.Unix(rm.StopTime, 0)) + builder.BeforePublishedDate(time.Unix(rm.StopTime, 0)) } rawEntryIDs, err := builder.GetEntryIDs() @@ -1405,10 +1405,10 @@ func (h *handler) handleReadStreamHandler(w http.ResponseWriter, r *http.Request builder.WithOffset(rm.Offset) builder.WithSorting(model.DefaultSortingOrder, rm.SortDirection) if rm.StartTime > 0 { - builder.AfterDate(time.Unix(rm.StartTime, 0)) + builder.AfterPublishedDate(time.Unix(rm.StartTime, 0)) } if rm.StopTime > 0 { - builder.BeforeDate(time.Unix(rm.StopTime, 0)) + builder.BeforePublishedDate(time.Unix(rm.StopTime, 0)) } rawEntryIDs, err := builder.GetEntryIDs() @@ -1449,10 +1449,10 @@ func (h *handler) handleFeedStreamHandler(w http.ResponseWriter, r *http.Request builder.WithOffset(rm.Offset) builder.WithSorting(model.DefaultSortingOrder, rm.SortDirection) if rm.StartTime > 0 { - builder.AfterDate(time.Unix(rm.StartTime, 0)) + builder.AfterPublishedDate(time.Unix(rm.StartTime, 0)) } if rm.StopTime > 0 { - builder.BeforeDate(time.Unix(rm.StopTime, 0)) + builder.BeforePublishedDate(time.Unix(rm.StopTime, 0)) } rawEntryIDs, err := builder.GetEntryIDs() diff --git a/internal/storage/entry_query_builder.go b/internal/storage/entry_query_builder.go index 2a8defd5..f00ca9f6 100644 --- a/internal/storage/entry_query_builder.go +++ b/internal/storage/entry_query_builder.go @@ -51,15 +51,29 @@ func (e *EntryQueryBuilder) WithStarred(starred bool) *EntryQueryBuilder { return e } -// BeforeDate adds a condition < published_at -func (e *EntryQueryBuilder) BeforeDate(date time.Time) *EntryQueryBuilder { +// BeforeChangedDate adds a condition < changed_at +func (e *EntryQueryBuilder) BeforeChangedDate(date time.Time) *EntryQueryBuilder { + e.conditions = append(e.conditions, fmt.Sprintf("e.changed_at < $%d", len(e.args)+1)) + e.args = append(e.args, date) + return e +} + +// AfterChangedDate adds a condition > changed_at +func (e *EntryQueryBuilder) AfterChangedDate(date time.Time) *EntryQueryBuilder { + e.conditions = append(e.conditions, fmt.Sprintf("e.changed_at > $%d", len(e.args)+1)) + e.args = append(e.args, date) + return e +} + +// BeforePublishedDate adds a condition < published_at +func (e *EntryQueryBuilder) BeforePublishedDate(date time.Time) *EntryQueryBuilder { e.conditions = append(e.conditions, fmt.Sprintf("e.published_at < $%d", len(e.args)+1)) e.args = append(e.args, date) return e } -// AfterDate adds a condition > published_at -func (e *EntryQueryBuilder) AfterDate(date time.Time) *EntryQueryBuilder { +// AfterPublishedDate adds a condition > published_at +func (e *EntryQueryBuilder) AfterPublishedDate(date time.Time) *EntryQueryBuilder { e.conditions = append(e.conditions, fmt.Sprintf("e.published_at > $%d", len(e.args)+1)) e.args = append(e.args, date) return e