Пример #1
0
        private static void PrepareForUpload(ISimpleJsonRest web, APIBodySync body, StandardFileTag tag, List <StandardFileNote> allNotes, APIResultAuthorize token, StandardNoteConfig cfg, bool delete)
        {
            var jsnContent = new ContentTag
            {
                title      = tag.Title,
                references = allNotes
                             .Where(n => n.InternalTags.Any(it => it.UUID == tag.UUID))
                             .Select(n => new APIResultContentRef {
                    content_type = "Note", uuid = n.ID
                })
                             .ToList(),
            };

            Debug.Assert(tag.UUID != null, "tag.UUID != null");

            var cdNote = StandardNoteCrypt.EncryptContent(token.version, web.SerializeJson(jsnContent), tag.UUID.Value, token.masterkey, token.masterauthkey);

            body.items.Add(new APIBodyItem
            {
                content_type = "Tag",
                uuid         = tag.UUID.Value,
                enc_item_key = cdNote.enc_item_key,
                auth_hash    = cdNote.auth_hash,
                content      = cdNote.enc_content,
                deleted      = delete,
            });
        }
Пример #2
0
 private static void ReplaceOrAdd(List <StandardFileTag> list, StandardFileTag old, StandardFileTag repl)
 {
     for (var i = 0; i < list.Count; i++)
     {
         if (list[i] == old)
         {
             list[i] = repl; return;
         }
     }
     list.Add(repl);
 }
Пример #3
0
        private static void PrepareForUpload(ISimpleJsonRest web, APIBodySync body, StandardFileNote note, List <StandardFileTag> tags, APIResultAuthorize token, StandardNoteConfig cfg, bool delete)
        {
            var appdata = new Dictionary <string, Dictionary <string, object> >();

            SetAppDataBool(appdata, APPDATA_PINNED, note.IsPinned);
            SetAppDataBool(appdata, APPDATA_LOCKED, note.IsLocked);

            var jsnContent = new ContentNote
            {
                title      = note.InternalTitle,
                text       = note.Text,
                references = new List <APIResultContentRef>(),
                appData    = appdata,
            };

            foreach (var itertag in note.InternalTags.ToList())
            {
                var itag = itertag;

                if (itag.UUID == null)
                {
                    var newTag = tags.FirstOrDefault(e => e.Title == itag.Title);
                    if (newTag == null)
                    {
                        newTag = new StandardFileTag(Guid.NewGuid(), itag.Title);
                        tags.Add(newTag);
                    }

                    note.UpgradeTag(itag, newTag);
                    itag = newTag;
                }

                Debug.Assert(itag.UUID != null, "itag.UUID != null");
                jsnContent.references.Add(new APIResultContentRef {
                    content_type = "Tag", uuid = itag.UUID.Value
                });
            }

            var cdNote = StandardNoteCrypt.EncryptContent(token.version, web.SerializeJson(jsnContent), note.ID, token.masterkey, token.masterauthkey);

            body.items.Add(new APIBodyItem
            {
                content_type = "Note",
                uuid         = note.ID,
                created_at   = note.CreationDate,
                enc_item_key = cdNote.enc_item_key,
                auth_hash    = cdNote.auth_hash,
                content      = cdNote.enc_content,
                deleted      = delete,
            });
        }
Пример #4
0
        private static void PrepareTagForUpload(ISimpleJsonRest web, APIBodySync body, ref List <APIRawBodyItem> bodyraw, StandardFileTag tag, APIResultAuthorize token, bool delete)
        {
            var objContent = new ContentTag
            {
                title      = tag.Title,
                references = tag
                             .References
                             .Select(n => new APIResultContentRef {
                    content_type = "Note", uuid = n
                })
                             .ToList(),
            };

            Debug.Assert(tag.UUID != null, "tag.UUID != null");

            var jsonContent = web.SerializeJson(objContent);

            var cryptData = StandardNoteCrypt.EncryptContent(token.version, jsonContent, tag.UUID.Value, token.masterkey, token.masterauthkey);

            body.items.Add(new APIBodyItem
            {
                content_type = "Tag",
                uuid         = tag.UUID.Value,
                enc_item_key = cryptData.enc_item_key,
                auth_hash    = cryptData.auth_hash,
                content      = cryptData.enc_content,
                deleted      = delete,
            });
            bodyraw.Add(new APIRawBodyItem
            {
                content_type = "Tag",
                uuid         = tag.UUID.Value,
                content      = jsonContent,
                deleted      = delete,
            });
        }
Пример #5
0
        private static IEnumerable <StandardFileTag> GetTagsInNeedOfUpdate(List <StandardFileTag> _allTags, List <StandardFileNote> notesUpload, List <StandardFileNote> notesDeleted)
        {
            var result  = new List <StandardFileTag>();
            var allTags = _allTags.ToList();

            // [1] New Tags in Note
            foreach (var note in notesUpload)
            {
                foreach (var noteTag in note.InternalTags)
                {
                    if (noteTag.UUID == null)
                    {
                        continue;
                    }

                    var realTag = allTags.FirstOrDefault(t => t.UUID == noteTag.UUID);

                    if (realTag == null)                     // create new tag
                    {
                        var addtag = new StandardFileTag(noteTag.UUID, noteTag.Title, Enumerable.Repeat(note.ID, 1));
                        allTags.Add(addtag);
                        result.Add(addtag);
                    }
                    else
                    {
                        if (realTag.ContainsReference(note))
                        {
                            // tag already contains ref - all ok
                        }
                        else
                        {
                            // tag does not contain ref - update
                            var addtag = new StandardFileTag(realTag.UUID, realTag.Title, realTag.References.Concat(Enumerable.Repeat(note.ID, 1)));
                            ReplaceOrAdd(allTags, realTag, addtag);
                            ReplaceOrAdd(result, realTag, addtag);
                        }
                    }
                }
            }

            // [2] Tags that ref now-deleted notes
            foreach (var note in notesDeleted)
            {
                foreach (var noteTag in note.InternalTags)
                {
                    if (noteTag.UUID == null)
                    {
                        continue;
                    }

                    var realTag = allTags.FirstOrDefault(t => t.UUID == noteTag.UUID);

                    if (realTag != null && realTag.ContainsReference(note))
                    {
                        // tag does still refrence note - remove ref
                        var addtag = new StandardFileTag(realTag.UUID, realTag.Title, realTag.References.Except(Enumerable.Repeat(note.ID, 1)));
                        ReplaceOrAdd(allTags, realTag, addtag);
                        ReplaceOrAdd(result, realTag, addtag);
                    }
                }
            }

            // [3] Removed Tags in note
            foreach (var tag in allTags.ToList())
            {
                if (tag.UUID == null)
                {
                    continue;
                }

                foreach (var tagref in tag.References)
                {
                    var note = notesUpload.FirstOrDefault(n => tagref == n.ID);
                    if (note == null)
                    {
                        // ref links to a note that was not changed -- I guess its ok (?)
                    }
                    else
                    {
                        if (note.ContainsTag(tag.UUID.Value))
                        {
                            // note contains tags (and tag contains note) -- nothing changed - all ok
                        }
                        else
                        {
                            // note no longer contains tag - update tag
                            var addtag = new StandardFileTag(tag.UUID, tag.Title, tag.References.Except(Enumerable.Repeat(note.ID, 1)));
                            ReplaceOrAdd(allTags, tag, addtag);
                            ReplaceOrAdd(result, tag, addtag);
                        }
                    }
                }
            }

            return(result);
        }
Пример #6
0
        public void UpgradeTag(StandardFileTag told, StandardFileTag tnew)
        {
            int idx = _internalTags.IndexOf(told);

            _internalTags[idx] = tnew;
        }