1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
package cache
import (
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"fmt"
"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"
)
const (
v1Version Version = 1
startFeedId uint64 = 1
maxCacheSize = 1000
)
type feedId uint64
func (id feedId) String() string {
return strconv.FormatUint(uint64(id), 16)
}
func idFromString(s string) feedId {
id, _ := strconv.ParseUint(s, 16, 64)
return feedId(id)
}
type v1Cache struct {
Ids map[feed.Descriptor]feedId
NextId uint64
Feeds map[feedId]*cachedFeed
}
type cachedFeed struct {
feed *feed.Feed
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
Items []cachedItem
newItems []cachedItem
}
type itemHash [sha256.Size]byte
func (h itemHash) String() string {
return hex.EncodeToString(h[:])
}
type cachedItem struct {
Guid string
Title string
Link string
Date time.Time
UpdatedCache time.Time
Hash itemHash
ID uuid.UUID
deleted bool
}
func (item cachedItem) String() string {
return fmt.Sprintf(`{
ID: %s
Title: %q
Guid: %q
Link: %q
Date: %s
Hash: %s
}`,
base64.RawURLEncoding.EncodeToString(item.ID[:]),
item.Title, item.Guid, item.Link, util.TimeFormat(item.Date), item.Hash)
}
func (cf *cachedFeed) Checked(withFailure bool) {
cf.currentCheck = time.Now()
if withFailure {
cf.NumFailures++
} else {
cf.NumFailures = 0
}
}
func (cf *cachedFeed) Commit() {
if cf.newItems != nil {
cf.Items = cf.newItems
cf.newItems = nil
}
cf.LastCheck = cf.currentCheck
}
func (cf *cachedFeed) Failures() int {
return cf.NumFailures
}
func (cf *cachedFeed) Last() time.Time {
return cf.LastCheck
}
func (cf *cachedFeed) Feed() *feed.Feed {
return cf.feed
}
func (cache *v1Cache) Version() Version {
return v1Version
}
func (cache *v1Cache) Info() string {
descriptors := make([]feed.Descriptor, len(cache.Ids))
i := 0
for descr := range cache.Ids {
descriptors[i] = descr
i++
}
sort.Slice(descriptors, func(i, j int) bool {
return descriptors[i].Name < descriptors[j].Name
})
b := strings.Builder{}
for _, descr := range descriptors {
id := cache.Ids[descr]
feed := cache.Feeds[id]
b.WriteString(fmt.Sprintf("%3s: %s (%s) (%d items)\n", id.String(), descr.Name, descr.Url, len(feed.Items)))
}
return b.String()
}
func (cache *v1Cache) SpecificInfo(i interface{}) string {
id := idFromString(i.(string))
b := strings.Builder{}
feed := cache.Feeds[id]
for descr, fId := range cache.Ids {
if id == fId {
b.WriteString(descr.Name)
b.WriteString(" -- ")
b.WriteString(descr.Url)
b.WriteByte('\n')
break
}
}
b.WriteString(fmt.Sprintf(`
Last Check: %s
Num Failures: %d
Num Items: %d
`,
util.TimeFormat(feed.LastCheck),
feed.NumFailures,
len(feed.Items)))
for _, item := range feed.Items {
b.WriteString("\n--------------------\n")
b.WriteString(item.String())
}
return b.String()
}
func newV1Cache() *v1Cache {
cache := v1Cache{
Ids: map[feed.Descriptor]feedId{},
Feeds: map[feedId]*cachedFeed{},
NextId: startFeedId,
}
return &cache
}
func (cache *v1Cache) transformToCurrent() (Impl, error) {
return cache, nil
}
func (cache *v1Cache) getItem(id feedId) *cachedFeed {
feed, ok := cache.Feeds[id]
if !ok {
feed = &cachedFeed{}
cache.Feeds[id] = feed
}
feed.id = id
return feed
}
func (cache *v1Cache) cachedFeed(f *feed.Feed) CachedFeed {
fDescr := f.Descriptor()
id, ok := cache.Ids[fDescr]
if !ok {
var otherId feed.Descriptor
changed := false
for otherId, id = range cache.Ids {
if otherId.Name == fDescr.Name {
log.Warnf("Feed %s seems to have changed URLs: new '%s', old '%s'. Updating.",
fDescr.Name, fDescr.Url, otherId.Url)
changed = true
break
} else if otherId.Url == fDescr.Url {
log.Warnf("Feed with URL '%s' seems to have changed its name: new '%s', old '%s'. Updating.",
fDescr.Url, fDescr.Name, otherId.Name)
changed = true
break
}
}
if changed {
delete(cache.Ids, otherId)
} else {
id = feedId(cache.NextId)
cache.NextId++
}
cache.Ids[fDescr] = id
}
cf := cache.getItem(id)
cf.feed = f
f.SetExtID(id)
return cf
}
func (cf *cachedFeed) cachedItem(item *feed.Item) cachedItem {
var ci cachedItem
ci.ID = item.ID
ci.Title = item.Item.Title
ci.Link = item.Item.Link
if item.DateParsed() != nil {
ci.Date = *item.DateParsed()
}
ci.Guid = item.Item.GUID
contentByte := []byte(item.Item.Description + item.Item.Content)
ci.Hash = sha256.Sum256(contentByte)
return ci
}
func (item *cachedItem) similarTo(other *cachedItem, ignoreHash bool) bool {
return other.Title == item.Title &&
other.Link == item.Link &&
other.Date.Equal(item.Date) &&
(ignoreHash || other.Hash == item.Hash)
}
func (cf *cachedFeed) markItemDeleted(index int) {
cf.Items[index].deleted = true
}
func (cf *cachedFeed) Filter(items []feed.Item, ignoreHash, alwaysNew bool) []feed.Item {
if len(items) == 0 {
return items
}
cacheItems := make(map[cachedItem]*feed.Item, len(items))
for idx := range items {
i := &items[idx]
ci := cf.cachedItem(i)
// remove complete duplicates on the go
cacheItems[ci] = i
}
log.Debugf("%d items after deduplication", len(cacheItems))
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 {
item.UpdateOnly = true
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)
}
filtered = append(filtered, *item)
cacheadd = append(cacheadd, ci)
}
seen := func(oldIdx int) {
ci := cf.Items[oldIdx]
cf.markItemDeleted(oldIdx)
cacheadd = append(cacheadd, ci)
}
CACHE_ITEMS:
for ci, item := range cacheItems {
log.Debugf("Now checking %s", ci)
if ci.Guid != "" {
for idx, oldItem := range cf.Items {
if oldItem.Guid == ci.Guid {
log.Debugf("Guid matches with: %s", oldItem)
if !oldItem.similarTo(&ci, ignoreHash) {
item.AddReason("guid (upd)")
app(item, ci, &idx)
} else {
log.Debugf("Similar, ignoring item %s", base64.RawURLEncoding.EncodeToString(oldItem.ID[:]))
seen(idx)
}
continue CACHE_ITEMS
}
}
log.Debug("Found no matching GUID, including.")
item.AddReason("guid")
app(item, ci, nil)
continue
}
for idx, oldItem := range cf.Items {
if oldItem.similarTo(&ci, ignoreHash) {
log.Debugf("Similarity matches, ignoring: %s", oldItem)
seen(idx)
continue CACHE_ITEMS
}
if oldItem.Link == ci.Link {
if alwaysNew {
log.Debugf("Link matches, but `always-new`.")
item.AddReason("always-new")
continue
}
log.Debugf("Link matches, updating: %s", oldItem)
item.AddReason("link (upd)")
app(item, ci, &idx)
continue CACHE_ITEMS
}
}
log.Debugf("No match found, inserting.")
item.AddReason("new")
app(item, ci, nil)
}
log.Debugf("%d items after filtering", len(filtered))
cf.newItems = append(cacheadd, filterItems(cf.Items)...)
return filtered
}
func filterItems(items []cachedItem) []cachedItem {
var n int
if len(items) < maxCacheSize {
n = len(items)
} else {
n = maxCacheSize
}
copiedItems := make([]cachedItem, 0, n)
for _, item := range items {
if !item.deleted {
copiedItems = append(copiedItems, item)
if len(copiedItems) >= n {
break
}
}
}
return copiedItems
}
|