コード例 #1
0
 private static void InitialiseDependencies(ILogger logger)
 {
     _logger         = logger;
     _linkProcessor  = new LinkProcessor(logger);
     _reportPoster   = new RouteReportPoster(logger);
     _storageManager = new RouteStorageManager(logger);
 }
コード例 #2
0
ファイル: LinkProcessorTests.cs プロジェクト: tonto7973/xim
        public async Task Process_CompleteSuccessfully_WhenOutgoingLinkQueueFound()
        {
            const string     linkName        = "abcd";
            const string     entity          = "entity";
            ISecurityContext securityContext = Substitute.For <ISecurityContext>();
            IEntityLookup    entityLookup    = Substitute.For <IEntityLookup>();
            ILoggerProvider  loggerProvider  = Substitute.For <ILoggerProvider>();

            Entities.IEntity fakeEntity = Substitute.For <Entities.IEntity>();
            var deliveryQueue           = new DeliveryQueue();
            var linkProcessor           = new LinkProcessor(securityContext, entityLookup, loggerProvider);

            entityLookup.Find(Arg.Any <string>()).Returns(fakeEntity);
            securityContext.IsAuthorized(Arg.Any <Connection>()).Returns(true);
            fakeEntity.DeliveryQueue.Returns(deliveryQueue);
            Session session = await TestAmqpHost.OpenAndLinkProcessorAsync(linkProcessor);

            try
            {
                var receiver = new ReceiverLink(session, linkName, entity);
                deliveryQueue.Enqueue(new Delivery(new Message {
                    Properties = new Properties {
                        MessageId = "msgid6746"
                    }
                }));

                Message message = await receiver.ReceiveAsync();

                message.Properties.MessageId.ShouldBe("msgid6746");
            }
            finally
            {
                await session.Connection.CloseAsync();
            }
        }
コード例 #3
0
        public async Task <IActionResult> Create([Bind("Id,HomeFileId,HomeFileName,RemoteFileName,RemoteBaseUri,AcceptFrom,SendTo")] LinkedFile linkedFile)
        {
            if (ModelState.IsValid)
            {
                if (!linkedFile.RemoteBaseUri.EndsWith('/'))
                {
                    linkedFile.RemoteBaseUri = linkedFile.RemoteBaseUri.TrimEnd(' ') + "/";
                }

                LinkProcessor lp = new LinkProcessor(_context);

                if (await lp.Test(linkedFile.RemoteBaseUri))
                {
                    _context.Add(linkedFile);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
                else
                {
                    return(RedirectToAction(nameof(Error)));
                }
            }
            return(View(linkedFile));
        }
コード例 #4
0
ファイル: LinkProcessorTests.cs プロジェクト: tonto7973/xim
        public async Task Process_CompleteWithError_WhenOutgoingLinkQueueNotFound()
        {
            const string     linkName        = "abcd";
            const string     entity          = "entity";
            ISecurityContext securityContext = Substitute.For <ISecurityContext>();
            IEntityLookup    entityLookup    = Substitute.For <IEntityLookup>();
            ILoggerProvider  loggerProvider  = Substitute.For <ILoggerProvider>();

            Entities.IEntity fakeEntity = Substitute.For <Entities.IEntity>();
            var linkProcessor           = new LinkProcessor(securityContext, entityLookup, loggerProvider);

            fakeEntity.DeliveryQueue.Returns((DeliveryQueue)null);
            entityLookup.Find(Arg.Any <string>()).Returns(fakeEntity);
            securityContext.IsAuthorized(Arg.Any <Connection>()).Returns(true);
            AmqpException exception = null;
            Session       session   = await TestAmqpHost.OpenAndLinkProcessorAsync(linkProcessor);

            try
            {
                var receiver = new ReceiverLink(session, linkName, entity);

                Func <Task> action = async() => await receiver.ReceiveAsync();

                linkProcessor.ShouldSatisfyAllConditions(
                    () => exception = action.ShouldThrow <AmqpException>(),
                    () => exception.Error.Condition.ShouldBe((Symbol)ErrorCode.NotFound),
                    () => exception.Error.Description.ShouldBe("Queue not found.")
                    );
            }
            finally
            {
                await session.Connection.CloseAsync();
            }
        }
コード例 #5
0
        public static async Task <string> DeleteLinked(ApplicationDbContext db, NoteHeader nh)
        {
            // Check for linked notefile(s)

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

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

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

                        LinkProcessor lp = new LinkProcessor(db);

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

            return("Ok");
        }
コード例 #6
0
        public async Task <bool> Put(Stringy uri)
        {
            LinkProcessor lp   = new LinkProcessor(null);
            bool          test = await lp.Test2(uri.value);

            return(test);
        }
コード例 #7
0
 public void Setup()
 {
     _robots        = Robots.Load(string.Empty);
     _processedList = new List <string>
     {
         "http://gocardless.com/test2"
     };
     _toProcessList = new Stack <string>();
     _masterDomain  = "gocardless.com";
     _absolutePath  = "http://gocardless.com";
     _linkProcessor = new LinkProcessor(_robots, _processedList, _toProcessList, _masterDomain, _absolutePath);
 }
コード例 #8
0
        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();
            }
        }
コード例 #9
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,HomeFileId,HomeFileName,RemoteFileName,RemoteBaseUri,AcceptFrom,SendTo")] LinkedFile linkedFile)
        {
            if (id != linkedFile.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    if (!linkedFile.RemoteBaseUri.EndsWith('/'))
                    {
                        linkedFile.RemoteBaseUri = linkedFile.RemoteBaseUri.TrimEnd(' ') + "/";
                    }

                    LinkProcessor lp = new LinkProcessor(_context);

                    if (await lp.Test(linkedFile.RemoteBaseUri))
                    {
                        _context.Update(linkedFile);
                        await _context.SaveChangesAsync();
                    }
                    else
                    {
                        return(RedirectToAction(nameof(Error)));
                    }
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!LinkedFileExists(linkedFile.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(linkedFile));
        }
コード例 #10
0
ファイル: LinkProcessorTests.cs プロジェクト: tonto7973/xim
        public async Task Process_CompleteWithError_WhenIncomingLinkEntityNotFound()
        {
            const string     linkName        = "abcd";
            const string     entity          = "entity";
            ISecurityContext securityContext = Substitute.For <ISecurityContext>();
            IEntityLookup    entityLookup    = Substitute.For <IEntityLookup>();
            ILoggerProvider  loggerProvider  = Substitute.For <ILoggerProvider>();
            var linkProcessor = new LinkProcessor(securityContext, entityLookup, loggerProvider);

            entityLookup.Find(Arg.Any <string>()).Returns((Entities.IEntity)null);
            securityContext.IsAuthorized(Arg.Any <Connection>()).Returns(true);
            AmqpException exception = null;
            Session       session   = await TestAmqpHost.OpenAndLinkProcessorAsync(linkProcessor);

            try
            {
                var sender  = new SenderLink(session, linkName, entity);
                var message = new Message
                {
                    Properties = new Properties {
                        MessageId = "message1"
                    },
                    BodySection = new Data {
                        Binary = Encoding.UTF8.GetBytes("hello!")
                    }
                };
                Func <Task> action = async() => await sender.SendAsync(message);

                linkProcessor.ShouldSatisfyAllConditions(
                    () => exception = action.ShouldThrow <AmqpException>(),
                    () => exception.Error.Condition.ShouldBe((Symbol)ErrorCode.NotFound),
                    () => exception.Error.Description.ShouldBe("Entity not found.")
                    );
            }
            finally
            {
                await session.Connection.CloseAsync();
            }
        }
コード例 #11
0
ファイル: LinkProcessorTests.cs プロジェクト: tonto7973/xim
        public async Task Process_CompleteSuccessfully_WhenIncomingLinkEntityFound()
        {
            const string     linkName        = "abcd";
            const string     entity          = "myEntity";
            ISecurityContext securityContext = Substitute.For <ISecurityContext>();
            IEntityLookup    entityLookup    = Substitute.For <IEntityLookup>();
            ILoggerProvider  loggerProvider  = Substitute.For <ILoggerProvider>();

            Entities.IEntity fakeEntity = Substitute.For <Entities.IEntity>();
            var linkProcessor           = new LinkProcessor(securityContext, entityLookup, loggerProvider);

            entityLookup.Find(entity).Returns(fakeEntity);
            securityContext.IsAuthorized(Arg.Any <Connection>()).Returns(true);
            Session session = await TestAmqpHost.OpenAndLinkProcessorAsync(linkProcessor);

            try
            {
                var sender  = new SenderLink(session, linkName, entity);
                var message = new Message
                {
                    Properties = new Properties {
                        MessageId = "message173"
                    },
                    BodySection = new Data {
                        Binary = Encoding.UTF8.GetBytes("hello!")
                    }
                };

                await sender.SendAsync(message);

                fakeEntity
                .Received(1)
                .Post(Arg.Is <Message>(m => m.Properties.MessageId == "message173"));
            }
            finally
            {
                await session.Connection.CloseAsync();
            }
        }
コード例 #12
0
ファイル: LinkProcessorTests.cs プロジェクト: tonto7973/xim
        public async Task Process_CompleteWithError_WhenLinkNameEmpty()
        {
            const string     emptyLinkName   = "";
            const string     entity          = "entity";
            ISecurityContext securityContext = Substitute.For <ISecurityContext>();
            IEntityLookup    entityLookup    = Substitute.For <IEntityLookup>();
            ILoggerProvider  loggerProvider  = Substitute.For <ILoggerProvider>();
            var           linkProcessor      = new LinkProcessor(securityContext, entityLookup, loggerProvider);
            AmqpException exception          = null;
            Session       session            = await TestAmqpHost.OpenAndLinkProcessorAsync(linkProcessor);

            try
            {
                var sender  = new SenderLink(session, emptyLinkName, entity);
                var message = new Message
                {
                    Properties = new Properties {
                        MessageId = "message1"
                    },
                    BodySection = new Data {
                        Binary = Encoding.UTF8.GetBytes("hello!")
                    }
                };
                Func <Task> action = async() => await sender.SendAsync(message);

                linkProcessor.ShouldSatisfyAllConditions(
                    () => exception = action.ShouldThrow <AmqpException>(),
                    () => exception.Error.Condition.ShouldBe((Symbol)ErrorCode.InvalidField),
                    () => exception.Error.Description.ShouldBe("Empty link name not allowed.")
                    );
            }
            finally
            {
                await session.Connection.CloseAsync();
            }
        }
コード例 #13
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);
        }
コード例 #14
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);
        }
コード例 #15
0
        public async Task <bool> Post(Stringy uri)
        {
            LinkProcessor lp = new LinkProcessor(null);

            return(await lp.Test(uri.value));
        }