private async Task ProcessLinkedNotes()
        {
            List <LinkQueue> items = await _db.LinkQueue.Where(p => p.Enqueued == false).ToListAsync();

            foreach (LinkQueue item in items)
            {
                LinkProcessor lp = new LinkProcessor(_db);
                BackgroundJob.Enqueue(() => lp.ProcessLinkAction(item.Id));
                item.Enqueued = true;
                _db.Update(item);
            }
            if (items.Count > 0)
            {
                await _db.SaveChangesAsync();
            }
        }
Esempio n. 2
0
        public static async Task <NoteHeader> CreateNote(ApplicationDbContext db, UserManager <IdentityUser> userManager, NoteHeader nh, string body, string tags, string dMessage, bool send, bool linked)
        {
            if (nh.ResponseOrdinal == 0)  // base note
            {
                nh.NoteOrdinal = await NextBaseNoteOrdinal(db, nh.NoteFileId, nh.ArchiveId);
            }

            if (!linked)
            {
                nh.LinkGuid = Guid.NewGuid().ToString();
            }

            if (!send) // indicates an import operation / adjust time to UCT / assume original was CST = UCT-06, so add 6 hours
            {
                int offset = 6;
                if (nh.LastEdited.IsDaylightSavingTime())
                {
                    offset--;
                }

                Random rand = new Random();
                int    ms   = rand.Next(999);

                nh.LastEdited       = nh.LastEdited.AddHours(offset).AddMilliseconds(ms);
                nh.CreateDate       = nh.LastEdited;
                nh.ThreadLastEdited = nh.CreateDate;
            }

            NoteFile nf = await db.NoteFile
                          .Where(p => p.Id == nh.NoteFileId)
                          .FirstOrDefaultAsync();

            nf.LastEdited      = nh.CreateDate;
            db.Entry(nf).State = EntityState.Modified;
            db.NoteHeader.Add(nh);
            await db.SaveChangesAsync();

            NoteHeader newHeader = nh;

            if (newHeader.ResponseOrdinal == 0)
            {
                newHeader.BaseNoteId      = newHeader.Id;
                db.Entry(newHeader).State = EntityState.Modified;
                await db.SaveChangesAsync();
            }
            else
            {
                NoteHeader baseNote = await db.NoteHeader
                                      .Where(p => p.NoteFileId == newHeader.NoteFileId && p.ArchiveId == newHeader.ArchiveId && p.NoteOrdinal == newHeader.NoteOrdinal && p.ResponseOrdinal == 0)
                                      .FirstOrDefaultAsync();

                newHeader.BaseNoteId      = baseNote.Id;
                db.Entry(newHeader).State = EntityState.Modified;
                await db.SaveChangesAsync();
            }

            NoteContent newContent = new NoteContent()
            {
                NoteHeaderId    = newHeader.Id,
                NoteBody        = body,
                DirectorMessage = dMessage
            };

            db.NoteContent.Add(newContent);
            await db.SaveChangesAsync();

            // deal with tags

            if (tags != null && tags.Length > 1)
            {
                var theTags = Tags.StringToList(tags, newHeader.Id, newHeader.NoteFileId, newHeader.ArchiveId);

                if (theTags.Count > 0)
                {
                    await db.Tags.AddRangeAsync(theTags);

                    await db.SaveChangesAsync();
                }
            }

            // Check for linked notefile(s)

            List <LinkedFile> links = await db.LinkedFile.Where(p => p.HomeFileId == newHeader.NoteFileId && p.SendTo).ToListAsync();

            if (linked || links == null || links.Count < 1)
            {
            }
            else
            {
                foreach (var link in links)
                {
                    if (link.SendTo)
                    {
                        LinkQueue q = new LinkQueue
                        {
                            Activity     = newHeader.ResponseOrdinal == 0 ? LinkAction.CreateBase : LinkAction.CreateResponse,
                            LinkGuid     = newHeader.LinkGuid,
                            LinkedFileId = newHeader.NoteFileId,
                            BaseUri      = link.RemoteBaseUri
                        };

                        db.LinkQueue.Add(q);
                        await db.SaveChangesAsync();

                        LinkProcessor lp = new LinkProcessor(db);

                        BackgroundJob.Enqueue(() => lp.ProcessLinkAction(q.Id));
                    }
                }
            }

            /////

            if (send && !linked)
            {
                await SendNewNoteToSubscribers(db, userManager, newHeader);
            }

            ////

            return(newHeader);
        }
Esempio n. 3
0
        public static async Task <NoteHeader> EditNote(ApplicationDbContext db, UserManager <IdentityUser> userManager, NoteHeader nh, NoteContent nc, string tags)
        {
            NoteHeader eHeader = await GetBaseNoteHeader(db, nh.Id);

            eHeader.LastEdited       = nh.LastEdited;
            eHeader.ThreadLastEdited = nh.ThreadLastEdited;
            eHeader.NoteSubject      = nh.NoteSubject;
            db.Entry(eHeader).State  = EntityState.Modified;

            NoteContent eContent = await GetNoteContent(db, nh.NoteFileId, nh.ArchiveId, nh.NoteOrdinal, nh.ResponseOrdinal);

            eContent.NoteBody        = nc.NoteBody;
            eContent.DirectorMessage = nc.DirectorMessage;
            db.Entry(eContent).State = EntityState.Modified;

            List <Tags> oTags = await GetNoteTags(db, nh.NoteFileId, nh.ArchiveId, nh.NoteOrdinal, nh.ResponseOrdinal, 0);

            db.Tags.RemoveRange(oTags);

            db.UpdateRange(oTags);
            db.Update(eHeader);
            db.Update(eContent);

            await db.SaveChangesAsync();

            // deal with tags

            if (tags != null && tags.Length > 1)
            {
                var theTags = Tags.StringToList(tags, eHeader.Id, eHeader.NoteFileId, eHeader.ArchiveId);

                if (theTags.Count > 0)
                {
                    await db.Tags.AddRangeAsync(theTags);

                    await db.SaveChangesAsync();
                }
            }

            // Check for linked notefile(s)

            List <LinkedFile> links = await db.LinkedFile.Where(p => p.HomeFileId == eHeader.NoteFileId && p.SendTo).ToListAsync();

            if (links == null || links.Count < 1)
            {
            }
            else
            {
                foreach (var link in links)
                {
                    if (link.SendTo)
                    {
                        LinkQueue q = new LinkQueue
                        {
                            Activity     = LinkAction.Edit,
                            LinkGuid     = eHeader.LinkGuid,
                            LinkedFileId = eHeader.NoteFileId,
                            BaseUri      = link.RemoteBaseUri
                        };

                        db.LinkQueue.Add(q);
                        await db.SaveChangesAsync();

                        LinkProcessor lp = new LinkProcessor(db);

                        BackgroundJob.Enqueue(() => lp.ProcessLinkAction(q.Id));
                    }
                }
            }

            return(eHeader);
        }