diff options
author | René 'Necoro' Neumann <necoro@necoro.eu> | 2020-05-02 18:51:37 +0200 |
---|---|---|
committer | René 'Necoro' Neumann <necoro@necoro.eu> | 2020-05-02 18:51:37 +0200 |
commit | 477241a2c2356c61b7317246040aee50d2a7a81d (patch) | |
tree | 56a040ab9c938d71455366ade90d1c5443440d32 | |
parent | c05a640571b57bd63e67867f9d8122c4e2d1d183 (diff) | |
download | feed2imap-go-477241a2c2356c61b7317246040aee50d2a7a81d.tar.gz feed2imap-go-477241a2c2356c61b7317246040aee50d2a7a81d.tar.bz2 feed2imap-go-477241a2c2356c61b7317246040aee50d2a7a81d.zip |
WIP: Message-Ids
Diffstat (limited to '')
-rw-r--r-- | go.mod | 2 | ||||
-rw-r--r-- | go.sum | 5 | ||||
-rw-r--r-- | internal/feed/cache.go | 1 | ||||
-rw-r--r-- | internal/feed/cache_v1.go | 19 | ||||
-rw-r--r-- | internal/feed/feed.go | 1 | ||||
-rw-r--r-- | internal/feed/mail.go | 9 | ||||
-rw-r--r-- | internal/feed/parse.go | 3 | ||||
-rw-r--r-- | pkg/config/config.go | 5 |
8 files changed, 40 insertions, 5 deletions
@@ -8,6 +8,8 @@ require ( github.com/emersion/go-message v0.11.3-0.20200422153910-8c6ac6b57e3d github.com/gabriel-vasile/mimetype v1.1.0 github.com/google/go-cmp v0.4.0 + github.com/google/uuid v1.1.1 // indirect + github.com/lithammer/shortuuid v3.0.0+incompatible github.com/mmcdole/gofeed v1.0.0-beta2.0.20200331235650-4298e4366be3 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c ) @@ -19,6 +19,11 @@ github.com/gabriel-vasile/mimetype v1.1.0 h1:+ahX+MvQPFve4kO9Qjjxf3j49i0ACdV236k github.com/gabriel-vasile/mimetype v1.1.0/go.mod h1:6CDPel/o/3/s4+bp6kIbsWATq8pmgOisOPG40CJa6To= github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= +github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/lithammer/shortuuid v1.0.0 h1:kdcbvjGVEgqeVeDIRtnANOi/F6ftbKrtbxY+cjQmK1Q= +github.com/lithammer/shortuuid v3.0.0+incompatible h1:NcD0xWW/MZYXEHa6ITy6kaXN5nwm/V115vj2YXfhS0w= +github.com/lithammer/shortuuid v3.0.0+incompatible/go.mod h1:FR74pbAuElzOUuenUHTK2Tciko1/vKuIKS9dSkDrA4w= github.com/martinlindhe/base36 v1.0.0 h1:eYsumTah144C0A8P1T/AVSUk5ZoLnhfYFM3OGQxB52A= github.com/martinlindhe/base36 v1.0.0/go.mod h1:+AtEs8xrBpCeYgSLoY/aJ6Wf37jtBuR0s35750M27+8= github.com/mmcdole/gofeed v1.0.0-beta2.0.20200331235650-4298e4366be3 h1:Wy+ed15cpwtLcJYNiO4Z0wmjZHpNj4q0RsGbsoxWSMA= diff --git a/internal/feed/cache.go b/internal/feed/cache.go index f1dfb77..731eab1 100644 --- a/internal/feed/cache.go +++ b/internal/feed/cache.go @@ -27,6 +27,7 @@ type CachedFeed interface { Checked(withFailure bool) Failures() int Last() time.Time + ID() string filterItems(items []feeditem, ignoreHash bool, alwaysNew bool) []feeditem Commit() } diff --git a/internal/feed/cache_v1.go b/internal/feed/cache_v1.go index bbb8f13..b2813ce 100644 --- a/internal/feed/cache_v1.go +++ b/internal/feed/cache_v1.go @@ -3,8 +3,11 @@ package feed import ( "crypto/sha256" "fmt" + "strconv" "time" + "github.com/lithammer/shortuuid" + "github.com/Necoro/feed2imap-go/pkg/log" "github.com/Necoro/feed2imap-go/pkg/util" ) @@ -16,6 +19,10 @@ const ( type feedId uint64 +func (id feedId) String() string { + return strconv.FormatUint(uint64(id), 16) +} + type v1Cache struct { Ids map[feedDescriptor]feedId NextId uint64 @@ -23,6 +30,7 @@ type v1Cache struct { } type cachedFeed struct { + id feedId // not saved, has to be set on loading LastCheck time.Time currentCheck time.Time NumFailures int // can't be named `Failures` b/c it'll collide with the interface @@ -40,6 +48,7 @@ type cachedItem struct { UpdatedDate time.Time UpdatedCache time.Time Hash itemHash + ID string } func (item cachedItem) String() string { @@ -75,6 +84,10 @@ func (cf *cachedFeed) Last() time.Time { return cf.LastCheck } +func (cf *cachedFeed) ID() string { + return cf.id.String() +} + func (cache *v1Cache) Version() Version { return v1Version } @@ -98,6 +111,7 @@ func (cache *v1Cache) getItem(id feedId) CachedFeed { feed = &cachedFeed{} cache.Feeds[id] = feed } + feed.id = id return feed } @@ -142,6 +156,8 @@ func (cache *v1Cache) findItem(feed *Feed) CachedFeed { func newCachedItem(item feeditem) cachedItem { var ci cachedItem + ci.ID = shortuuid.New() + ci.Title = item.Item.Title ci.Link = item.Item.Link if item.Item.PublishedParsed != nil { @@ -192,6 +208,7 @@ func (cf *cachedFeed) filterItems(items []feeditem, ignoreHash, alwaysNew bool) } filtered = append(filtered, *item) cacheadd = append(cacheadd, ci) + item.itemId = ci.ID } CACHE_ITEMS: @@ -211,6 +228,7 @@ CACHE_ITEMS: log.Debugf("Guid matches with: %s", oldItem) if !oldItem.similarTo(&ci, ignoreHash) { item.addReason("guid (upd)") + ci.ID = oldItem.ID app(item, ci, &idx) } else { log.Debugf("Similar, ignoring") @@ -240,6 +258,7 @@ CACHE_ITEMS: } log.Debugf("Link matches, updating: %s", oldItem) item.addReason("link (upd)") + ci.ID = oldItem.ID app(item, ci, &idx) continue CACHE_ITEMS diff --git a/internal/feed/feed.go b/internal/feed/feed.go index 1a6ef97..9ed44df 100644 --- a/internal/feed/feed.go +++ b/internal/feed/feed.go @@ -35,6 +35,7 @@ type feeditem struct { updateOnly bool reasons []string images []feedImage + itemId string } // Creator returns the name of the creating author. diff --git a/internal/feed/mail.go b/internal/feed/mail.go index 4b0e38b..ebf032a 100644 --- a/internal/feed/mail.go +++ b/internal/feed/mail.go @@ -49,8 +49,9 @@ func buildHeader(feed *Feed, item feeditem, cfg *config.Config) message.Header { h.SetContentType("multipart/alternative", nil) h.SetAddressList("From", fromAdress(feed, item, cfg)) h.SetAddressList("To", address(feed.Name, cfg.DefaultEmail)) - h.Add("X-Feed2Imap-Version", config.Version()) - h.Add("X-Feed2Imap-Reason", strings.Join(item.reasons, ",")) + h.Set("X-Feed2Imap-Version", config.Version()) + h.Set("X-Feed2Imap-Reason", strings.Join(item.reasons, ",")) + h.Set("Message-Id", feed.messageId(item)) { // date date := item.Item.PublishedParsed @@ -227,6 +228,10 @@ func getBody(content, description string, bodyCfg config.Body) string { } } +func (feed *Feed) messageId(item feeditem) string { + return fmt.Sprintf("<feed#%s#%s@%s>", feed.cached.ID(), item.itemId, config.Hostname()) +} + func (feed *Feed) buildBody(item *feeditem) { body := getBody(item.Item.Content, item.Item.Description, feed.Body) diff --git a/internal/feed/parse.go b/internal/feed/parse.go index 1ce6cda..435c0ed 100644 --- a/internal/feed/parse.go +++ b/internal/feed/parse.go @@ -7,6 +7,7 @@ import ( "net/http" "time" + "github.com/lithammer/shortuuid" "github.com/mmcdole/gofeed" "github.com/Necoro/feed2imap-go/pkg/log" @@ -55,7 +56,7 @@ func parseFeed(feed *Feed) error { feed.feed = parsedFeed feed.items = make([]feeditem, len(parsedFeed.Items)) for idx, item := range parsedFeed.Items { - feed.items[idx] = feeditem{Feed: parsedFeed, Item: item} + feed.items[idx] = feeditem{Feed: parsedFeed, Item: item, itemId: shortuuid.New()} } return nil } diff --git a/pkg/config/config.go b/pkg/config/config.go index dfd3d0e..993cd71 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -29,7 +29,7 @@ type GlobalOptions struct { var DefaultGlobalOptions = GlobalOptions{ Timeout: 30, MaxFailures: 10, - DefaultEmail: username() + "@" + hostname(), + DefaultEmail: username() + "@" + Hostname(), Target: "", Parts: []string{"text", "html"}, } @@ -120,7 +120,8 @@ func Load(path string) (*Config, error) { return cfg, nil } -func hostname() (hostname string) { +// Hostname returns the current hostname, or 'localhost' if it cannot be determined +func Hostname() (hostname string) { hostname, err := os.Hostname() if err != nil { hostname = "localhost" |