-
Notifications
You must be signed in to change notification settings - Fork 5
/
tag.go
531 lines (415 loc) · 10 KB
/
tag.go
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
package sncli
import (
"errors"
"strings"
"github.com/jonhadfield/gosn-v2/cache"
"github.com/jonhadfield/gosn-v2/common"
"github.com/jonhadfield/gosn-v2/items"
)
type tagNotesInput struct {
session *cache.Session
matchTitle string
matchText string
matchTags []string
matchNoteUUIDs []string
newTags []string
syncToken string
replace bool
}
// create tags if they don't exist
// get all notes and tags.
func tagNotes(i tagNotesInput) (err error) {
// create tags if they don't exist
ati := addTagsInput{
session: i.session,
tagTitles: i.newTags,
replace: i.replace,
}
if _, err = addTags(ati); err != nil {
return err
}
// get notes and tags
getNotesFilter := items.Filter{
Type: common.SNItemTypeNote,
}
getTagsFilter := items.Filter{
Type: common.SNItemTypeTag,
}
filters := []items.Filter{getNotesFilter, getTagsFilter}
itemFilter := items.ItemFilters{
MatchAny: true,
Filters: filters,
}
syncInput := cache.SyncInput{
Session: i.session,
}
// get all notes and tags from db
var so cache.SyncOutput
so, err = Sync(syncInput, true)
if err != nil {
return err
}
var allPersistedItems cache.Items
if err = so.DB.All(&allPersistedItems); err != nil {
return
}
var gItems items.Items
gItems, err = allPersistedItems.ToItems(i.session)
if err != nil {
return
}
gItems.Filter(itemFilter)
var allTags []*items.Tag
var allNotes []*items.Note
// create slices of notes and tags
for _, item := range gItems {
if item.IsDeleted() {
continue
}
if item.GetContentType() == common.SNItemTypeTag {
allTags = append(allTags, item.(*items.Tag))
}
if item.GetContentType() == common.SNItemTypeNote {
allNotes = append(allNotes, item.(*items.Note))
}
}
typeUUIDs := make(map[string][]string)
// loop through all notes and create a list of those that
// match the note title or exist in note text
for _, note := range allNotes {
switch {
case StringInSlice(note.UUID, i.matchNoteUUIDs, false):
typeUUIDs[common.SNItemTypeNote] = append(typeUUIDs[common.SNItemTypeNote], note.UUID)
case strings.TrimSpace(i.matchTitle) != "" && strings.Contains(strings.ToLower(note.Content.GetTitle()), strings.ToLower(i.matchTitle)):
typeUUIDs[common.SNItemTypeNote] = append(typeUUIDs[common.SNItemTypeNote], note.UUID)
case strings.TrimSpace(i.matchText) != "" && strings.Contains(strings.ToLower(note.Content.GetText()), strings.ToLower(i.matchText)):
typeUUIDs[common.SNItemTypeNote] = append(typeUUIDs[common.SNItemTypeNote], note.UUID)
}
}
if len(typeUUIDs[common.SNItemTypeNote]) == 0 {
return errors.New("note not found")
}
// update existing (and just created) tags to reference matching uuids
// determine which TAGS need updating and create list to sync back to server
var tagsToPush items.Tags
for _, t := range allTags {
// if tag title is in ones to add then update tag with new references
if StringInSlice(t.Content.GetTitle(), i.newTags, true) {
// does it need updating
updatedTag, changed := upsertTagReferences(*t, typeUUIDs)
if changed {
tagsToPush = append(tagsToPush, updatedTag)
}
}
}
if len(tagsToPush) > 0 {
if err = cache.SaveTags(so.DB, i.session, tagsToPush, true); err != nil {
return err
}
pii := cache.SyncInput{
Session: i.session,
}
so, err = Sync(pii, true)
if err != nil {
return err
}
if err = so.DB.Close(); err != nil {
return err
}
return nil
}
return nil
}
func (i *TagItemsConfig) Run() error {
return tagNotes(tagNotesInput{
matchTitle: i.FindTitle,
matchText: i.FindText,
matchTags: []string{i.FindTag},
newTags: i.NewTags,
session: i.Session,
})
}
func (i *AddTagsInput) Run() (output AddTagsOutput, err error) {
ati := addTagsInput{
tagTitles: i.Tags,
parent: i.Parent,
parentUUID: i.ParentUUID,
session: i.Session,
replace: i.Replace,
}
var ato addTagsOutput
ato, err = addTags(ati)
if err != nil {
return AddTagsOutput{}, err
}
output.Added = ato.added
output.Existing = ato.existing
return output, err
}
func (i *GetItemsConfig) Run() (items items.Items, err error) {
var so cache.SyncOutput
si := cache.SyncInput{
Session: i.Session,
}
so, err = Sync(si, true)
if err != nil {
return items, err
}
var allPersistedItems cache.Items
err = so.DB.All(&allPersistedItems)
if err != nil {
return
}
err = so.DB.Close()
if err != nil {
return
}
items, err = allPersistedItems.ToItems(i.Session)
if err != nil {
return
}
items.FilterAllTypes(i.Filters)
return items, err
}
func (i *GetTagConfig) Run() (items items.Items, err error) {
var so cache.SyncOutput
si := cache.SyncInput{
Session: i.Session,
}
so, err = Sync(si, true)
if err != nil {
return items, err
}
var allPersistedItems cache.Items
err = so.DB.All(&allPersistedItems)
if err != nil {
return
}
err = so.DB.Close()
if err != nil {
return
}
items, err = allPersistedItems.ToItems(i.Session)
if err != nil {
return
}
items.Filter(i.Filters)
return items, err
}
func (i *DeleteTagConfig) Run() (noDeleted int, err error) {
noDeleted, err = deleteTags(i.Session, i.TagTitles, i.TagUUIDs)
return noDeleted, err
}
func deleteTags(session *cache.Session, tagTitles []string, tagUUIDs []string) (noDeleted int, err error) {
deleteTagsFilter := items.Filter{
Type: common.SNItemTypeTag,
}
filters := []items.Filter{deleteTagsFilter}
deleteFilter := items.ItemFilters{
MatchAny: true,
Filters: filters,
}
syncInput := cache.SyncInput{
Session: session,
}
// load db
var so cache.SyncOutput
so, err = Sync(syncInput, true)
if err != nil {
return 0, err
}
defer func() {
_ = so.DB.Close()
}()
var tags items.Items
// get items from db
var allPersistedItems cache.Items
err = so.DB.All(&allPersistedItems)
if err != nil {
return
}
var gItems items.Items
gItems, err = allPersistedItems.ToItems(session)
if err != nil {
return
}
tags = gItems
tags.Filter(deleteFilter)
if len(tags) == 0 {
return 0, nil
}
var tagsToDelete items.Items
for _, tag := range tags {
if tag.IsDeleted() {
continue
}
var gTag *items.Tag
if tag.GetContentType() == common.SNItemTypeTag {
gTag = tag.(*items.Tag)
} else {
continue
}
if StringInSlice(gTag.GetUUID(), tagUUIDs, true) ||
StringInSlice(gTag.Content.Title, tagTitles, true) {
gTag.Deleted = true
tagsToDelete = append(tagsToDelete, gTag)
}
}
var eTagsToDelete items.EncryptedItems
eTagsToDelete, err = tagsToDelete.Encrypt(session.Session, session.DefaultItemsKey)
if err != nil {
return 0, err
}
if len(eTagsToDelete) == 0 {
return 0, nil
}
if err = cache.SaveEncryptedItems(so.DB, eTagsToDelete, true); err != nil {
return
}
pii := cache.SyncInput{
Session: session,
Close: true,
}
_, err = Sync(pii, true)
if err != nil {
return
}
noDeleted = len(tagsToDelete)
return noDeleted, nil
}
type addTagsInput struct {
session *cache.Session
tagTitles []string
parent string
parentUUID string
replace bool
}
type addTagsOutput struct {
added []string
existing []string
}
func addTags(ati addTagsInput) (ato addTagsOutput, err error) {
// get all tags
addTagsFilter := items.Filter{
Type: common.SNItemTypeTag,
}
filters := []items.Filter{addTagsFilter}
addFilter := items.ItemFilters{
MatchAny: true,
Filters: filters,
}
putItemsInput := cache.SyncInput{
Session: ati.session,
}
var so cache.SyncOutput
so, err = Sync(putItemsInput, true)
if err != nil {
return
}
var allPersistedItems cache.Items
err = so.DB.All(&allPersistedItems)
if err != nil {
return
}
var gItems items.Items
gItems, err = allPersistedItems.ToItems(ati.session)
if err != nil {
return
}
if err = so.DB.Close(); err != nil {
return
}
gItems.Filter(addFilter)
var allTags items.Tags
var parentRef items.ItemReferences
for _, item := range gItems {
if item.IsDeleted() {
continue
}
if item.GetContentType() == common.SNItemTypeTag {
tag := item.(*items.Tag)
allTags = append(allTags, *tag)
if tag.Content.GetTitle() == ati.parent || tag.GetUUID() == ati.parentUUID {
if parentRef != nil {
return ato, errors.New("multiple parent tags found, specify by UUID")
}
itemRef := items.ItemReference{
UUID: tag.GetUUID(),
ContentType: common.SNItemTypeTag,
ReferenceType: "TagToParentTag",
}
parentRef = items.ItemReferences{itemRef}
}
}
}
if ati.parent != "" && len(parentRef) == 0 {
return ato, errors.New("parent tag not found by title")
}
if ati.parentUUID != "" && len(parentRef) == 0 {
return ato, errors.New("parent tag not found by UUID")
}
var tagsToAdd items.Tags
for _, tag := range ati.tagTitles {
if tagExists(allTags, tag) {
ato.existing = append(ato.existing, tag)
continue
}
newTag, _ := items.NewTag(tag, parentRef)
tagsToAdd = append(tagsToAdd, newTag)
ato.added = append(ato.added, tag)
}
if len(tagsToAdd) > 0 {
so, err = Sync(putItemsInput, true)
if err != nil {
return
}
var eTagsToAdd items.EncryptedItems
eTagsToAdd, err = tagsToAdd.Encrypt(ati.session.Gosn())
if err != nil {
return
}
err = cache.SaveEncryptedItems(so.DB, eTagsToAdd, true)
if err != nil {
return
}
so, err = Sync(putItemsInput, true)
if err != nil {
return
}
err = so.DB.Close()
if err != nil {
return
}
}
return ato, err
}
func upsertTagReferences(tag items.Tag, typeUUIDs map[string][]string) (items.Tag, bool) {
// create item reference
var newReferences []items.ItemReference
var changed bool
for k, v := range typeUUIDs {
for _, ref := range v {
if !referenceExists(tag, ref) {
newReferences = append(newReferences, items.ItemReference{
ContentType: k,
UUID: ref,
})
}
}
}
if len(newReferences) > 0 {
changed = true
newContent := tag.Content
newContent.UpsertReferences(newReferences)
tag.Content = newContent
}
return tag, changed
}
func tagExists(existing []items.Tag, find string) bool {
for _, tag := range existing {
if tag.Content.GetTitle() == find {
return true
}
}
return false
}