diff options
Diffstat (limited to 'internal/feed/cache/v1.go')
-rw-r--r-- | internal/feed/cache/v1.go | 35 |
1 files changed, 18 insertions, 17 deletions
diff --git a/internal/feed/cache/v1.go b/internal/feed/cache/v1.go index 4e3f3e2..aecb242 100644 --- a/internal/feed/cache/v1.go +++ b/internal/feed/cache/v1.go @@ -7,13 +7,12 @@ import ( "encoding/hex" "fmt" "io" + "slices" "sort" "strconv" "strings" "time" - "github.com/google/uuid" - "github.com/Necoro/feed2imap-go/internal/feed" "github.com/Necoro/feed2imap-go/pkg/log" "github.com/Necoro/feed2imap-go/pkg/util" @@ -66,7 +65,7 @@ type cachedItem struct { Date time.Time UpdatedCache time.Time Hash itemHash - ID uuid.UUID + ID feed.ItemID deleted bool } @@ -187,7 +186,7 @@ func (cache *v1Cache) transformTo(v Version) (Impl, error) { } } -func (cache *v1Cache) getItem(id feedId) *cachedFeed { +func (cache *v1Cache) getFeed(id feedId) *cachedFeed { feed, ok := cache.Feeds[id] if !ok { feed = &cachedFeed{} @@ -226,13 +225,13 @@ func (cache *v1Cache) cachedFeed(f *feed.Feed) CachedFeed { cache.Ids[fDescr] = id } - cf := cache.getItem(id) + cf := cache.getFeed(id) cf.feed = f f.SetExtID(id) return cf } -func (cf *cachedFeed) cachedItem(item *feed.Item) cachedItem { +func (cf *cachedFeed) buildCachedItem(item *feed.Item) cachedItem { var ci cachedItem ci.ID = item.ID @@ -268,7 +267,7 @@ func (cf *cachedFeed) Filter(items []feed.Item, ignoreHash, alwaysNew bool) []fe cacheItems := make(map[cachedItem]*feed.Item, len(items)) for idx := range items { i := &items[idx] - ci := cf.cachedItem(i) + ci := cf.buildCachedItem(i) // remove complete duplicates on the go cacheItems[ci] = i @@ -277,14 +276,14 @@ func (cf *cachedFeed) Filter(items []feed.Item, ignoreHash, alwaysNew bool) []fe filtered := make([]feed.Item, 0, len(items)) cacheadd := make([]cachedItem, 0, len(items)) - app := func(item *feed.Item, ci cachedItem, oldIdx *int) { - if oldIdx != nil { + app := func(item *feed.Item, ci cachedItem, oldIdx int) { + if oldIdx > -1 { item.UpdateOnly = true - prevId := cf.Items[*oldIdx].ID + prevId := cf.Items[oldIdx].ID ci.ID = prevId item.ID = prevId - log.Debugf("oldIdx: %d, prevId: %s, item.id: %s", *oldIdx, prevId, item.Id()) - cf.markItemDeleted(*oldIdx) + log.Debugf("oldIdx: %d, prevId: %s, item.id: %s", oldIdx, prevId, item.Id()) + cf.markItemDeleted(oldIdx) } filtered = append(filtered, *item) cacheadd = append(cacheadd, ci) @@ -306,7 +305,7 @@ CACHE_ITEMS: log.Debugf("Guid matches with: %s", oldItem) if !oldItem.similarTo(&ci, ignoreHash) { item.AddReason("guid (upd)") - app(item, ci, &idx) + app(item, ci, idx) } else { log.Debugf("Similar, ignoring item %s", base64.RawURLEncoding.EncodeToString(oldItem.ID[:])) seen(idx) @@ -318,7 +317,7 @@ CACHE_ITEMS: log.Debug("Found no matching GUID, including.") item.AddReason("guid") - app(item, ci, nil) + app(item, ci, -1) continue } @@ -337,7 +336,7 @@ CACHE_ITEMS: } log.Debugf("Link matches, updating: %s", oldItem) item.AddReason("link (upd)") - app(item, ci, &idx) + app(item, ci, idx) continue CACHE_ITEMS } @@ -345,12 +344,14 @@ CACHE_ITEMS: log.Debugf("No match found, inserting.") item.AddReason("new") - app(item, ci, nil) + app(item, ci, -1) } log.Debugf("%d items after filtering", len(filtered)) - cf.newItems = append(cacheadd, filterItems(cf.Items)...) + // only the old items (cf.Items) is filtered and trimmed + // this is to ensure that really all new additions are part of the cache + cf.newItems = slices.Concat(cacheadd, filterItems(cf.Items)) return filtered } |