From a895145586ad299bcd4072897f4fcd95fd0f69b9 Mon Sep 17 00:00:00 2001 From: Nazar Kanaev Date: Sat, 30 Sep 2023 23:19:48 +0100 Subject: [PATCH] sort articles before storing --- doc/changelog.txt | 1 + src/storage/item.go | 25 ++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/doc/changelog.txt b/doc/changelog.txt index 948e794..e84c55d 100644 --- a/doc/changelog.txt +++ b/doc/changelog.txt @@ -5,6 +5,7 @@ - (fix) relative article links (thanks to @adazsko for the report) - (fix) atom article links stored in id element (thanks to @adazsko for the report) - (fix) parsing atom feed titles (thanks to @wnh) +- (fix) sorting same-day batch articles (thanks to @lamescholar for the report) # v2.4 (2023-08-15) diff --git a/src/storage/item.go b/src/storage/item.go index d1b8c8f..173a213 100644 --- a/src/storage/item.go +++ b/src/storage/item.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "log" + "sort" "strings" "time" @@ -75,6 +76,25 @@ type MarkFilter struct { Before *time.Time } +type ItemList []Item + +func (list ItemList) Len() int { + return len(list) +} + +func (list ItemList) SortKey(i int) string { + return list[i].Date.Format(time.RFC3339) + "::" + list[i].GUID +} + +func (list ItemList) Less(i, j int) bool { + return list.SortKey(i) < list.SortKey(j) +} + +func (list ItemList) Swap(i, j int) { + list[i], list[j] = list[j], list[i] +} + + func (s *Storage) CreateItems(items []Item) bool { tx, err := s.db.Begin() if err != nil { @@ -84,7 +104,10 @@ func (s *Storage) CreateItems(items []Item) bool { now := time.Now().UTC() - for _, item := range items { + itemsSorted := ItemList(items) + sort.Sort(itemsSorted) + + for _, item := range itemsSorted { _, err = tx.Exec(` insert into items ( guid, feed_id, title, link, date,