diff options
Diffstat (limited to 'internal')
-rw-r--r-- | internal/feed/cache/v1.go | 35 | ||||
-rw-r--r-- | internal/feed/filter/filter.go | 4 | ||||
-rw-r--r-- | internal/feed/item.go | 13 | ||||
-rw-r--r-- | internal/feed/parse.go | 3 | ||||
-rw-r--r-- | internal/imap/connection.go | 4 |
5 files changed, 33 insertions, 26 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 } diff --git a/internal/feed/filter/filter.go b/internal/feed/filter/filter.go index 6377944..bdf5993 100644 --- a/internal/feed/filter/filter.go +++ b/internal/feed/filter/filter.go @@ -2,8 +2,8 @@ package filter import ( "github.com/Necoro/gofeed" - "github.com/antonmedv/expr" - "github.com/antonmedv/expr/vm" + "github.com/expr-lang/expr" + "github.com/expr-lang/expr/vm" ) type Filter struct { diff --git a/internal/feed/item.go b/internal/feed/item.go index 79a971c..4482a5c 100644 --- a/internal/feed/item.go +++ b/internal/feed/item.go @@ -4,6 +4,7 @@ import ( "encoding/base64" "encoding/json" "fmt" + "slices" "strings" "time" @@ -11,7 +12,6 @@ import ( "github.com/google/uuid" "github.com/Necoro/feed2imap-go/pkg/config" - "github.com/Necoro/feed2imap-go/pkg/util" ) type feedImage struct { @@ -20,6 +20,12 @@ type feedImage struct { name string } +type ItemID uuid.UUID + +func newItemID() ItemID { + return ItemID(uuid.New()) +} + type Item struct { *gofeed.Item // access fields implicitly Feed *gofeed.Feed // named explicitly to not shadow common fields with Item @@ -27,9 +33,9 @@ type Item struct { Body string TextBody string UpdateOnly bool + ID ItemID reasons []string images []feedImage - ID uuid.UUID } func (item *Item) DateParsed() *time.Time { @@ -65,7 +71,7 @@ func (item *Item) FeedLink() string { } func (item *Item) AddReason(reason string) { - if !util.Contains(item.reasons, reason) { + if !slices.Contains(item.reasons, reason) { item.reasons = append(item.reasons, reason) } } @@ -77,6 +83,7 @@ func (item *Item) addImage(img []byte, mime string, name string) int { } func (item *Item) clearImages() { + clear(item.images) item.images = []feedImage{} } diff --git a/internal/feed/parse.go b/internal/feed/parse.go index a471b2d..bc796d3 100644 --- a/internal/feed/parse.go +++ b/internal/feed/parse.go @@ -6,7 +6,6 @@ import ( "os/exec" "github.com/Necoro/gofeed" - "github.com/google/uuid" "github.com/Necoro/feed2imap-go/internal/http" ) @@ -59,7 +58,7 @@ func (feed *Feed) Parse() error { feed.feed = parsedFeed feed.items = make([]Item, len(parsedFeed.Items)) for idx, feedItem := range parsedFeed.Items { - feed.items[idx] = Item{Feed: parsedFeed, feed: feed, Item: feedItem, ID: uuid.New()} + feed.items[idx] = Item{Feed: parsedFeed, feed: feed, Item: feedItem, ID: newItemID()} } return cleanup() } diff --git a/internal/imap/connection.go b/internal/imap/connection.go index 8c0f59c..201ca7d 100644 --- a/internal/imap/connection.go +++ b/internal/imap/connection.go @@ -3,6 +3,7 @@ package imap import ( "errors" "fmt" + "slices" "strings" "time" @@ -11,7 +12,6 @@ import ( imapClient "github.com/emersion/go-imap/client" "github.com/Necoro/feed2imap-go/pkg/log" - "github.com/Necoro/feed2imap-go/pkg/util" ) type client struct { @@ -125,7 +125,7 @@ func (conn *connection) ensureFolder(folder Folder) error { } switch { - case found == 0 || (found == 1 && util.Contains(mbox.Attributes, imap.NoSelectAttr)): + case found == 0 || (found == 1 && slices.Contains(mbox.Attributes, imap.NoSelectAttr)): return conn.createFolder(folder.str) case found == 1: conn.mailboxes.add(mbox) |