This repository has been archived by the owner on Dec 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
items.go
1272 lines (1012 loc) · 30.8 KB
/
items.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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package gosn
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math"
"net/http"
"reflect"
"strconv"
"strings"
"time"
"gopkg.in/matryer/try.v1"
)
// Item describes a decrypted item
type Item struct {
UUID string
Content ClientStructure
ContentType string
Deleted bool
CreatedAt string
UpdatedAt string
ContentSize int
}
// returns a new, typeless item
func newItem() *Item {
now := time.Now().UTC().Format(timeLayout)
return &Item{
UUID: GenUUID(),
CreatedAt: now,
UpdatedAt: now,
}
}
// NewNote returns an Item of type Note without content
func NewNote() *Item {
item := newItem()
item.ContentType = "Note"
return item
}
// NewTag returns an Item of type Tag without content
func NewTag() *Item {
item := newItem()
item.ContentType = "Tag"
return item
}
// NewComponent returns an Item of type Component without content
func NewComponent() *Item {
item := newItem()
item.ContentType = "SN|Component"
return item
}
// NewNoteContent returns an empty Note content instance
func NewNoteContent() *NoteContent {
c := &NoteContent{}
c.SetUpdateTime(time.Now().UTC())
return c
}
// NewTagContent returns an empty Tag content instance
func NewTagContent() *TagContent {
c := &TagContent{}
c.SetUpdateTime(time.Now().UTC())
return c
}
// NewTagContent returns an empty Tag content instance
func NewComponentContent() *ComponentContent {
c := &ComponentContent{}
c.SetUpdateTime(time.Now().UTC())
return c
}
// ClientStructure defines behaviour of a Component Item's content entry
type ComponentClientStructure interface {
// return name
GetName() string
// return active status
GetActive() bool
// get item associations
GetItemAssociations() []string
// get item disassociations
GetItemDisassociations() []string
// associate items
AssociateItems(items []string)
// disassociate items
DisassociateItems(items []string)
}
// ClientStructure defines behaviour of a Component Item's content entry
type NoteClientStructure interface {
// set text
SetText(input string)
// return text
GetText() string
// get last update time
}
// ClientStructure defines behaviour of an Item's content entry
type ClientStructure interface {
References() ItemReferences
// update or insert item references
UpsertReferences(input ItemReferences)
// set references
SetReferences(input ItemReferences)
// return title
GetTitle() string
// set title
SetTitle(input string)
// get last update time
GetUpdateTime() (time.Time, error)
// set last update time
SetUpdateTime(time.Time)
// get appdata
GetAppData() AppDataContent
// set appdata
SetAppData(data AppDataContent)
// client structure methods for Note
NoteClientStructure
// client structure methods for Component
ComponentClientStructure
}
type syncResponse struct {
Items EncryptedItems `json:"retrieved_items"`
SavedItems EncryptedItems `json:"saved_items"`
Unsaved EncryptedItems `json:"unsaved"`
SyncToken string `json:"sync_token"`
CursorToken string `json:"cursor_token"`
}
// AppTagConfig defines expected configuration structure for making Tag related operations
type AppTagConfig struct {
Email string
Token string
FindText string
FindTag string
NewTags []string
Debug bool
}
// GetItemsInput defines the input for retrieving items
type GetItemsInput struct {
Session Session
SyncToken string
CursorToken string
OutType string
BatchSize int // number of items to retrieve
PageSize int // override default number of items to request with each sync call
Debug bool
}
// GetItemsOutput defines the output from retrieving items
// It contains slices of items based on their state
// see: https://standardfile.org/ for state details
type GetItemsOutput struct {
Items EncryptedItems // items new or modified since last sync
SavedItems EncryptedItems // dirty items needing resolution
Unsaved EncryptedItems // items not saved during sync
SyncToken string
Cursor string
}
const retryScaleFactor = 0.25
func resizeForRetry(in *GetItemsInput) {
switch {
case in.BatchSize != 0:
in.BatchSize = int(math.Ceil(float64(in.BatchSize) * retryScaleFactor))
case in.PageSize != 0:
in.PageSize = int(math.Ceil(float64(in.PageSize) * retryScaleFactor))
default:
in.PageSize = int(math.Ceil(float64(PageSize) * retryScaleFactor))
}
}
type EncryptedItems []EncryptedItem
func (ei EncryptedItems) Decrypt(Mk, Ak string, debug bool) (o DecryptedItems, err error) {
debugPrint(debug, fmt.Sprintf("Decrypt | decrypting %d items", len(ei)))
for _, eItem := range ei {
var item DecryptedItem
if eItem.EncItemKey != "" {
var decryptedEncItemKey string
decryptedEncItemKey, err = decryptString(eItem.EncItemKey, Mk, Ak, eItem.UUID)
if err != nil {
return
}
itemEncryptionKey := decryptedEncItemKey[:len(decryptedEncItemKey)/2]
itemAuthKey := decryptedEncItemKey[len(decryptedEncItemKey)/2:]
var decryptedContent string
decryptedContent, err = decryptString(eItem.Content, itemEncryptionKey, itemAuthKey, eItem.UUID)
if err != nil {
return
}
item.Content = decryptedContent
}
item.UUID = eItem.UUID
item.Deleted = eItem.Deleted
item.ContentType = eItem.ContentType
item.UpdatedAt = eItem.UpdatedAt
item.CreatedAt = eItem.CreatedAt
o = append(o, item)
}
return o, err
}
func (ei EncryptedItems) DecryptAndParse(Mk, Ak string, debug bool) (o Items, err error) {
debugPrint(debug, fmt.Sprintf("DecryptAndParse | items: %d", len(ei)))
var di DecryptedItems
di, err = ei.Decrypt(Mk, Ak, debug)
if err != nil {
return
}
o, err = di.Parse()
return
}
// GetItems retrieves items from the API using optional filters
func GetItems(input GetItemsInput) (output GetItemsOutput, err error) {
giStart := time.Now()
defer func() {
debugPrint(input.Debug, fmt.Sprintf("GetItems | duration %v", time.Since(giStart)))
}()
if !input.Session.Valid() {
err = fmt.Errorf("session is invalid")
return
}
var sResp syncResponse
debugPrint(input.Debug, fmt.Sprintf("GetItems | PageSize %d", input.PageSize))
// retry logic is to handle responses that are too large
// so we can reduce number we retrieve with each sync request
start := time.Now()
rErr := try.Do(func(attempt int) (bool, error) {
debugPrint(input.Debug, fmt.Sprintf("GetItems | attempt %d", attempt))
var rErr error
sResp, rErr = getItemsViaAPI(input)
if rErr != nil && strings.Contains(strings.ToLower(rErr.Error()), "too large") {
debugPrint(input.Debug, fmt.Sprintf("GetItems | %s", rErr.Error()))
initialSize := input.PageSize
resizeForRetry(&input)
debugPrint(input.Debug, fmt.Sprintf("GetItems | failed to retrieve %d items "+
"at a time so reducing to %d", initialSize, input.PageSize))
}
return attempt < 3, rErr
})
if rErr != nil {
return output, rErr
}
elapsed := time.Since(start)
debugPrint(input.Debug, fmt.Sprintf("GetItems | took %v to get all items", elapsed))
postStart := time.Now()
output.Items = sResp.Items
output.Items.DeDupe()
output.Unsaved = sResp.Unsaved
output.Unsaved.DeDupe()
output.SavedItems = sResp.SavedItems
output.SavedItems.DeDupe()
output.Cursor = sResp.CursorToken
output.SyncToken = sResp.SyncToken
// strip any duplicates (https://github.com/standardfile/rails-engine/issues/5)
postElapsed := time.Since(postStart)
debugPrint(input.Debug, fmt.Sprintf("GetItems | post processing took %v", postElapsed))
debugPrint(input.Debug, fmt.Sprintf("GetItems | sync token: %+v", stripLineBreak(output.SyncToken)))
return output, err
}
// PutItemsInput defines the input used to put items
type PutItemsInput struct {
Items EncryptedItems
SyncToken string
Session Session
Debug bool
}
// PutItemsOutput defines the output from putting items
type PutItemsOutput struct {
ResponseBody syncResponse
}
func (i *Items) Validate() error {
var updatedTime time.Time
var err error
// TODO finish item validation
for _, item := range *i {
// validate content if being added
if !item.Deleted {
if stringInSlice(item.ContentType, []string{"Tag", "Note"}, true) {
updatedTime, err = item.Content.GetUpdateTime()
if err != nil {
return err
}
switch {
case item.Content.GetTitle() == "":
err = fmt.Errorf("failed to create \"%s\" due to missing title: \"%s\"",
item.ContentType, item.UUID)
case updatedTime.IsZero():
err = fmt.Errorf("failed to create \"%s\" due to missing content updated time: \"%s\"",
item.ContentType, item.Content.GetTitle())
case item.CreatedAt == "":
err = fmt.Errorf("failed to create \"%s\" due to missing created at date: \"%s\"",
item.ContentType, item.Content.GetTitle())
}
if err != nil {
return err
}
}
}
}
return err
}
func (i *Items) Encrypt(Mk, Ak string, debug bool) (e EncryptedItems, err error) {
e, err = encryptItems(i, Mk, Ak, debug)
return
}
// PutItems validates and then syncs items via API
func PutItems(i PutItemsInput) (output PutItemsOutput, err error) {
piStart := time.Now()
defer func() {
debugPrint(i.Debug, fmt.Sprintf("PutItems | duration %v", time.Since(piStart)))
}()
if !i.Session.Valid() {
err = fmt.Errorf("session is invalid")
return
}
debugPrint(i.Debug, fmt.Sprintf("PutItems | putting %d items", len(i.Items)))
// for each page size, send to push and get response
syncToken := stripLineBreak(i.SyncToken)
var savedItems []EncryptedItem
// put items in big chunks, default being page size
for x := 0; x < len(i.Items); x += PageSize {
var finalChunk bool
var lastItemInChunkIndex int
// if current big chunk > num encrypted items then it's the last
if x+PageSize >= len(i.Items) {
lastItemInChunkIndex = len(i.Items) - 1
finalChunk = true
} else {
lastItemInChunkIndex = x + PageSize
}
debugPrint(i.Debug, fmt.Sprintf("PutItems | putting items: %d to %d", x+1, lastItemInChunkIndex+1))
bigChunkSize := (lastItemInChunkIndex - x) + 1
fullChunk := i.Items[x : lastItemInChunkIndex+1]
var subChunkStart, subChunkEnd int
subChunkStart = x
subChunkEnd = lastItemInChunkIndex
// initialise running total
totalPut := 0
// keep trying to push chunk of encrypted items in reducing subChunk sizes until it succeeds
maxAttempts := 20
try.MaxRetries = 20
for {
rErr := try.Do(func(attempt int) (bool, error) {
var rErr error
// if chunk is too big to put then try with smaller chunk
var encItemJSON []byte
itemsToPut := i.Items[subChunkStart : subChunkEnd+1]
encItemJSON, _ = json.Marshal(itemsToPut)
var s []EncryptedItem
s, syncToken, rErr = putChunk(i.Session, encItemJSON, i.Debug)
if rErr != nil && strings.Contains(strings.ToLower(rErr.Error()), "too large") {
subChunkEnd = resizePutForRetry(subChunkStart, subChunkEnd, len(encItemJSON))
}
if rErr == nil {
savedItems = append(savedItems, s...)
totalPut += len(itemsToPut)
}
debugPrint(i.Debug, fmt.Sprintf("PutItems | attempt: %d of %d", attempt, maxAttempts))
return attempt < maxAttempts, rErr
})
if rErr != nil {
err = errors.New("failed to put all items")
return
}
// if it's not the last of the chunk then continue with next subChunk
if totalPut < bigChunkSize {
subChunkStart = subChunkEnd + 1
subChunkEnd = lastItemInChunkIndex
continue
}
// if it's last of the full chunk, then break
if len(fullChunk) == lastItemInChunkIndex {
break
}
if totalPut == len(fullChunk) {
break
}
} // end infinite for loop for subset
if finalChunk {
break
}
} // end looping through encrypted items
output.ResponseBody.SyncToken = syncToken
output.ResponseBody.SavedItems = savedItems
return output, err
}
func resizePutForRetry(start, end, numBytes int) int {
preShrink := end
// reduce to 90%
multiplier := 0.90
// if size is over 2M then be more aggressive and half
if numBytes > 2000000 {
multiplier = 0.50
}
end = int(math.Ceil(float64(end) * multiplier))
if end <= start {
end = start + 1
}
if preShrink == end && preShrink > 1 {
end--
}
return end
}
func putChunk(session Session, encItemJSON []byte, debug bool) (savedItems []EncryptedItem, syncToken string, err error) {
reqBody := []byte(`{"items":` + string(encItemJSON) +
`,"sync_token":"` + stripLineBreak(syncToken) + `"}`)
var syncRespBodyBytes []byte
syncRespBodyBytes, err = makeSyncRequest(session, reqBody, debug)
if err != nil {
return
}
// get item results from API response
var bodyContent syncResponse
bodyContent, err = getBodyContent(syncRespBodyBytes)
if err != nil {
return
}
// Get new items
syncToken = stripLineBreak(bodyContent.SyncToken)
savedItems = bodyContent.SavedItems
return
}
type EncryptedItem struct {
UUID string `json:"uuid"`
Content string `json:"content"`
ContentType string `json:"content_type"`
EncItemKey string `json:"enc_item_key"`
Deleted bool `json:"deleted"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
type DecryptedItem struct {
UUID string `json:"uuid"`
Content string `json:"content"`
ContentType string `json:"content_type"`
Deleted bool `json:"deleted"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
type DecryptedItems []DecryptedItem
type UpdateItemRefsInput struct {
Items Items // Tags
ToRef Items // Items To Reference
}
type UpdateItemRefsOutput struct {
Items Items // Tags
}
func UpdateItemRefs(i UpdateItemRefsInput) UpdateItemRefsOutput {
var updated Items // updated tags
for _, item := range i.Items {
var refs ItemReferences
for _, tr := range i.ToRef {
ref := ItemReference{
UUID: tr.UUID,
ContentType: tr.ContentType,
}
refs = append(refs, ref)
}
item.Content.UpsertReferences(refs)
updated = append(updated, item)
}
return UpdateItemRefsOutput{
Items: updated,
}
}
func (noteContent *NoteContent) SetReferences(newRefs ItemReferences) {
noteContent.ItemReferences = newRefs
}
func (tagContent *TagContent) SetReferences(newRefs ItemReferences) {
tagContent.ItemReferences = newRefs
}
func (tagContent *TagContent) UpsertReferences(newRefs ItemReferences) {
for _, newRef := range newRefs {
var found bool
for _, existingRef := range tagContent.ItemReferences {
if existingRef.UUID == newRef.UUID {
found = true
}
}
if !found {
tagContent.ItemReferences = append(tagContent.ItemReferences, newRef)
}
}
}
func (noteContent *NoteContent) UpsertReferences(newRefs ItemReferences) {
for _, newRef := range newRefs {
var found bool
for _, existingRef := range noteContent.ItemReferences {
if existingRef.UUID == newRef.UUID {
found = true
}
}
if !found {
noteContent.ItemReferences = append(noteContent.ItemReferences, newRef)
}
}
}
func makeSyncRequest(session Session, reqBody []byte, debug bool) (responseBody []byte, err error) {
var request *http.Request
request, err = http.NewRequest(http.MethodPost, session.Server+syncPath, bytes.NewBuffer(reqBody))
if err != nil {
return
}
request.Header.Set("Content-Type", "application/json")
request.Header.Set("Authorization", "Bearer "+session.Token)
var response *http.Response
start := time.Now()
response, err = httpClient.Do(request)
elapsed := time.Since(start)
debugPrint(debug, fmt.Sprintf("makeSyncRequest | request took: %v", elapsed))
if err != nil {
return
}
defer func() {
if err := response.Body.Close(); err != nil {
debugPrint(debug, fmt.Sprintf("makeSyncRequest | failed to close body closed"))
}
debugPrint(debug, fmt.Sprintf("makeSyncRequest | response body closed"))
}()
switch response.StatusCode {
case 413:
err = errors.New("payload too large")
return
}
if response.StatusCode > 400 {
debugPrint(debug, fmt.Sprintf("makeSyncRequest | sync of %d req bytes failed with: %s", len(reqBody), response.Status))
return
}
if response.StatusCode >= 200 && response.StatusCode < 300 {
debugPrint(debug, fmt.Sprintf("makeSyncRequest | sync of %d req bytes succeeded with: %s", len(reqBody), response.Status))
}
readStart := time.Now()
responseBody, err = ioutil.ReadAll(response.Body)
debugPrint(debug, fmt.Sprintf("makeSyncRequest | response read took %+v", time.Since(readStart)))
if err != nil {
return
}
debugPrint(debug, fmt.Sprintf("makeSyncRequest | response size %d bytes", len(responseBody)))
return responseBody, err
}
func getItemsViaAPI(input GetItemsInput) (out syncResponse, err error) {
// determine how many items to retrieve with each call
var limit int
switch {
case input.BatchSize > 0:
debugPrint(input.Debug, fmt.Sprintf("getItemsViaAPI |input.BatchSize: %d", input.BatchSize))
// batch size must be lower than or equal to page size
limit = input.BatchSize
case input.PageSize > 0:
debugPrint(input.Debug, fmt.Sprintf("getItemsViaAPI | input.PageSize: %d", input.PageSize))
limit = input.PageSize
default:
debugPrint(input.Debug, fmt.Sprintf("getItemsViaAPI | default - limit: %d", PageSize))
limit = PageSize
}
debugPrint(input.Debug, fmt.Sprintf("getItemsViaAPI | using limit: %d", limit))
var requestBody []byte
// generate request body
switch {
case input.CursorToken == "":
requestBody = []byte(`{"limit":` + strconv.Itoa(limit) + `}`)
case input.CursorToken == "null":
debugPrint(input.Debug, "getItemsViaAPI | cursor is null")
requestBody = []byte(`{"limit":` + strconv.Itoa(limit) +
`,"items":[],"sync_token":"` + input.SyncToken + `\n","cursor_token":null}`)
case input.CursorToken != "":
rawST := input.SyncToken
input.SyncToken = stripLineBreak(rawST)
newST := stripLineBreak(input.SyncToken)
requestBody = []byte(`{"limit":` + strconv.Itoa(limit) +
`,"items":[],"sync_token":"` + newST + `\n","cursor_token":"` + stripLineBreak(input.CursorToken) + `\n"}`)
}
// make the request
debugPrint(input.Debug, fmt.Sprintf("getItemsViaAPI | making request: %s", stripLineBreak(string(requestBody))))
msrStart := time.Now()
responseBody, err := makeSyncRequest(input.Session, requestBody, input.Debug)
msrEnd := time.Since(msrStart)
debugPrint(input.Debug, fmt.Sprintf("getItemsViaAPI | makeSyncRequest took: %v", msrEnd))
if err != nil {
return
}
// get encrypted items from API response
var bodyContent syncResponse
bodyContent, err = getBodyContent(responseBody)
if err != nil {
return
}
out.Items = bodyContent.Items
out.SavedItems = bodyContent.SavedItems
out.Unsaved = bodyContent.Unsaved
out.SyncToken = bodyContent.SyncToken
out.CursorToken = bodyContent.CursorToken
if input.BatchSize > 0 {
return
}
if bodyContent.CursorToken != "" && bodyContent.CursorToken != "null" {
var newOutput syncResponse
input.SyncToken = out.SyncToken
input.CursorToken = out.CursorToken
input.PageSize = limit
newOutput, err = getItemsViaAPI(input)
if err != nil {
return
}
out.Items = append(out.Items, newOutput.Items...)
out.SavedItems = append(out.Items, newOutput.SavedItems...)
out.Unsaved = append(out.Items, newOutput.Unsaved...)
} else {
return out, err
}
out.CursorToken = ""
return out, err
}
// ItemReference defines a reference from one item to another
type ItemReference struct {
// unique identifier of the item being referenced
UUID string `json:"uuid"`
// type of item being referenced
ContentType string `json:"content_type"`
}
type OrgStandardNotesSNDetail struct {
ClientUpdatedAt string `json:"client_updated_at"`
}
type AppDataContent struct {
OrgStandardNotesSN OrgStandardNotesSNDetail `json:"org.standardnotes.sn"`
}
type NoteContent struct {
Title string `json:"title"`
Text string `json:"text"`
ItemReferences ItemReferences `json:"references"`
AppData AppDataContent `json:"appData"`
}
func (noteContent *NoteContent) GetUpdateTime() (time.Time, error) {
if noteContent.AppData.OrgStandardNotesSN.ClientUpdatedAt == "" {
return time.Time{}, fmt.Errorf("notset")
}
return time.Parse(timeLayout, noteContent.AppData.OrgStandardNotesSN.ClientUpdatedAt)
}
func (tagContent *TagContent) GetUpdateTime() (time.Time, error) {
if tagContent.AppData.OrgStandardNotesSN.ClientUpdatedAt == "" {
return time.Time{}, fmt.Errorf("notset")
}
return time.Parse(timeLayout, tagContent.AppData.OrgStandardNotesSN.ClientUpdatedAt)
}
func (noteContent *NoteContent) SetUpdateTime(uTime time.Time) {
noteContent.AppData.OrgStandardNotesSN.ClientUpdatedAt = uTime.Format(timeLayout)
}
func (tagContent *TagContent) SetUpdateTime(uTime time.Time) {
tagContent.AppData.OrgStandardNotesSN.ClientUpdatedAt = uTime.Format(timeLayout)
}
func (noteContent *NoteContent) GetTitle() string {
return noteContent.Title
}
func (noteContent *NoteContent) SetTitle(title string) {
noteContent.Title = title
}
func (tagContent *TagContent) SetTitle(title string) {
tagContent.Title = title
}
func (noteContent *NoteContent) GetText() string {
return noteContent.Text
}
func (noteContent *NoteContent) SetText(text string) {
noteContent.Text = text
}
func (tagContent *TagContent) GetText() string {
// Tags only have titles, so empty string
return ""
}
func (tagContent *TagContent) SetText(text string) {
// not implemented
}
func (tagContent *TagContent) GetName() string {
return "not implemented"
}
func (tagContent *TagContent) GetActive() bool {
// not implemented
return false
}
func (tagContent *TagContent) TextContains(findString string, matchCase bool) bool {
// Tags only have titles, so always false
return false
}
func (tagContent *TagContent) GetTitle() string {
return tagContent.Title
}
func (tagContent *TagContent) References() ItemReferences {
var output ItemReferences
return append(output, tagContent.ItemReferences...)
}
func (tagContent *TagContent) GetAppData() AppDataContent {
return tagContent.AppData
}
func (noteContent *NoteContent) GetAppData() AppDataContent {
return noteContent.AppData
}
func (noteContent *NoteContent) SetAppData(data AppDataContent) {
noteContent.AppData = data
}
func (tagContent *TagContent) SetAppData(data AppDataContent) {
tagContent.AppData = data
}
func (noteContent *NoteContent) References() ItemReferences {
var output ItemReferences
return append(output, noteContent.ItemReferences...)
}
func (noteContent *NoteContent) GetActive() bool {
// not implemented
return false
}
func (noteContent *NoteContent) GetName() string {
return "not implemented"
}
func (noteContent *NoteContent) AddItemAssociations() string {
return "not implemented"
}
func (tagContent *TagContent) GetItemAssociations() []string {
panic("not implemented")
}
func (tagContent *TagContent) GetItemDisassociations() []string {
panic("not implemented")
}
func (noteContent *NoteContent) GetItemAssociations() []string {
panic("not implemented")
}
func (noteContent *NoteContent) GetItemDisassociations() []string {
panic("not implemented")
}
func (cc *ComponentContent) GetItemAssociations() []string {
return cc.AssociatedItemIds
}
func (cc *ComponentContent) GetItemDisassociations() []string {
return cc.DissociatedItemIds
}
type TagContent struct {
Title string `json:"title"`
ItemReferences ItemReferences `json:"references"`
AppData AppDataContent `json:"appData"`
}
type ComponentContent struct {
LegacyURL string `json:"legacy_url"`
HostedURL string `json:"hosted_url"`
LocalURL string `json:"local_url"`
ValidUntil string `json:"valid_until"`
OfflineOnly string `json:"offlineOnly"`
Name string `json:"name"`
Area string `json:"area"`
PackageInfo interface{} `json:"package_info"`
Permissions interface{} `json:"permissions"`
Active interface{} `json:"active"`
AutoUpdateDisabled string `json:"autoupdateDisabled"`
ComponentData interface{} `json:"componentData"`
DissociatedItemIds []string `json:"disassociatedItemIds"`
AssociatedItemIds []string `json:"associatedItemIds"`
ItemReferences ItemReferences `json:"references"`
AppData AppDataContent `json:"appData"`
}
func (cc *ComponentContent) UpsertReferences(input ItemReferences) {
panic("implement me")
}
func (cc *ComponentContent) SetReferences(input ItemReferences) {
panic("implement me")
}
func (noteContent *NoteContent) AssociateItems(newItems []string) {
}
func (tagContent *TagContent) AssociateItems(newItems []string) {
}
func (noteContent *NoteContent) DisassociateItems(newItems []string) {
}
func (tagContent *TagContent) DisassociateItems(newItems []string) {
}
func (cc *ComponentContent) AssociateItems(newItems []string) {
// add to associated item ids
for _, newRef := range newItems {
var existingFound bool
var existingDFound bool
for _, existingRef := range cc.AssociatedItemIds {
if existingRef == newRef {
existingFound = true
}
}
for _, existingDRef := range cc.DissociatedItemIds {
if existingDRef == newRef {
existingDFound = true
}
}
// add reference if it doesn't exist
if !existingFound {
cc.AssociatedItemIds = append(cc.AssociatedItemIds, newRef)
}
// remove reference (from disassociated) if it does exist in that list
if existingDFound {
cc.DissociatedItemIds = removeStringFromSlice(newRef, cc.DissociatedItemIds)
}
}
}
func removeStringFromSlice(inSt string, inSl []string) (outSl []string) {
for _, si := range inSl {
if inSt != si {
outSl = append(outSl, si)
}
}
return
}
func (cc *ComponentContent) DisassociateItems(itemsToRemove []string) {
// remove from associated item ids
for _, delRef := range itemsToRemove {
var existingFound bool
for _, existingRef := range cc.AssociatedItemIds {
if existingRef == delRef {
existingFound = true
}