public Message ConvertToMessage(FbPost post) { var message = new Message { Source = MessageSource.FacebookPost, OriginalId = post.id, SendTime = post.created_time.ToUniversalTime(), Content = post.message, OriginalLink = post.permalink_url, Story = post.story }; if (post.attachments != null) { foreach (var attachment in post.attachments.data) { if (attachment.media != null && attachment.media.image != null) { message.Attachments.Add(ConvertToAttachment(attachment)); } } } return(message); }
public override async Task <FacebookProcessResult> Process(SocialAccount socialAccount, FbHookChange change) { var result = new FacebookProcessResult(NotificationManager); if (IsWallPost(change) && !socialAccount.IfConvertWallPostToConversation) { return(result); } if (!IsWallPost(change) && !socialAccount.IfConvertVisitorPostToConversation) { return(result); } string token = socialAccount.Token; if (IsDuplicatedMessage(MessageSource.FacebookPost, change.Value.PostId)) { return(result); } FbPost post = await FbClient.GetPost(socialAccount.Token, change.Value.PostId); SocialUser sender = await GetOrCreateFacebookUser(socialAccount.Token, post.from.id); Message message = FacebookConverter.ConvertToMessage(post); message.SenderId = sender.Id; if (message.SenderId != socialAccount.Id) { message.ReceiverId = socialAccount.Id; } var conversation = new Conversation { OriginalId = change.Value.PostId, Source = ConversationSource.FacebookVisitorPost, Priority = ConversationPriority.Normal, Status = ConversationStatus.New, Subject = GetSubject(message.Content), LastMessageSenderId = message.SenderId, LastMessageSentTime = message.SendTime }; if (change.Value.Verb == "add" && message.SenderId == socialAccount.Id) { conversation.Source = ConversationSource.FacebookWallPost; conversation.IsHidden = true; } conversation.Messages.Add(message); await AddConversation(socialAccount, conversation); await CurrentUnitOfWork.SaveChangesAsync(); if (!conversation.IsHidden) { result.WithNewConversation(conversation); } return(result); }
public async Task ShouldCreateConversationForWallPost() { // Arrange var fbHookChange = new FbHookChange { Field = "feed", Value = new FbHookChangeValue { Item = "status", PostId = "post_1", Verb = "add", IsPublished = true } }; var fbPost = new FbPost { id = "post_1", from = new FbUser { id = "user_1", name = "test_sender" }, created_time = DateTime.UtcNow, message = "Test_Message" }; var sender = new SocialUser { Id = 888, OriginalId = "user_1" }; var dependencyResolverMock = MockDependencyResolver(fbHookChange, fbPost, sender); var strategy = new NewPostStrategy(dependencyResolverMock.Object); strategy.UnitOfWorkManager = MockUnitOfWorkManager().Object; var socialAccount = new SocialAccount { Id = 888, Token = "token", IfConvertWallPostToConversation = true }; // Act var processResult = await strategy.Process(socialAccount, fbHookChange); // Assert var conversation = processResult.NewConversations.First(); Assert.NotNull(conversation); Assert.Equal(true, conversation.IsHidden); Assert.Equal(ConversationSource.FacebookWallPost, conversation.Source); Assert.Equal(ConversationStatus.New, conversation.Status); Assert.Equal("post_1", conversation.OriginalId); Assert.Equal(ConversationPriority.Normal, conversation.Priority); Assert.Equal(888, conversation.LastMessageSenderId); Assert.Equal(fbPost.created_time, conversation.LastMessageSentTime); Assert.Equal("Test_Message", conversation.Subject); }
private SocialUser GetRecipient(FbPost post, IList <SocialUser> allAccounts) { if (post?.to?.data != null && post.to.data.Any()) { foreach (var to in post.to.data) { var socialAccount = allAccounts.FirstOrDefault(t => t.OriginalId == to.id); if (socialAccount != null) { return(socialAccount); } } } return(null); }
public async Task ShouldCreateMessageForPost() { // Arrange var fbHookChange = new FbHookChange { Field = "feed", Value = new FbHookChangeValue { Item = "post", PostId = "post_1", Verb = "add" } }; var fbPost = new FbPost { id = "post_1", from = new FbUser { id = "user_1", name = "test_sender" }, created_time = DateTime.UtcNow, message = "Test_Message" }; var sender = new SocialUser { Id = 1, OriginalId = "user_1" }; var dependencyResolverMock = MockDependencyResolver(fbHookChange, fbPost, sender); var strategy = new NewPostStrategy(dependencyResolverMock.Object); strategy.UnitOfWorkManager = MockUnitOfWorkManager().Object; var socialAccount = new SocialAccount { Id = 888, Token = "token", IfConvertVisitorPostToConversation = true }; // Act var processResult = await strategy.Process(socialAccount, fbHookChange); // Assert var message = processResult.NewConversations.First().Messages.FirstOrDefault(t => t.OriginalId == "post_1"); Assert.NotNull(message); Assert.Equal(MessageSource.FacebookPost, message.Source); Assert.Equal(fbPost.id, message.OriginalId); Assert.Equal(sender.Id, message.SenderId); Assert.Equal(socialAccount.Id, message.ReceiverId); Assert.Equal(fbPost.message, message.Content); }
public async Task ShouldInsertAndSaveChangesWhenCreateConversation() { // Arrange var fbHookChange = new FbHookChange { Field = "feed", Value = new FbHookChangeValue { Item = "post", PostId = "post_1", Verb = "add" } }; var fbPost = new FbPost { id = "post_1", from = new FbUser { id = "user_1", name = "test_sender" }, created_time = DateTime.UtcNow, message = "Test_Message" }; var sender = new SocialUser { Id = 1, OriginalId = "user_1" }; var dependencyResolverMock = MockDependencyResolver(fbHookChange, fbPost, sender); var conversationServiceMock = new Mock <IConversationService>(); dependencyResolverMock.Setup(t => t.Resolve <IConversationService>()).Returns(conversationServiceMock.Object); var strategy = new NewPostStrategy(dependencyResolverMock.Object); var uowMock = new Mock <IUnitOfWork>(); var uowManagerMock = new Mock <IUnitOfWorkManager>(); uowManagerMock.Setup(t => t.Current).Returns(uowMock.Object); strategy.UnitOfWorkManager = uowManagerMock.Object; var socialAccount = new SocialAccount { Id = 888, Token = "token", IfConvertVisitorPostToConversation = true }; // Act var processResult = await strategy.Process(socialAccount, fbHookChange); // Assert conversationServiceMock.Verify(t => t.InsertAsync(It.IsAny <Conversation>()), "Should add conversation to db."); uowMock.Verify(t => t.SaveChangesAsync(), "Should save changes."); }
public async Task ShouldIgnoreDuplicatedMessage() { // Arrange var fbHookChange = new FbHookChange { Field = "feed", Value = new FbHookChangeValue { Item = "post", PostId = "post_1", Verb = "add" } }; var fbPost = new FbPost { id = "post_1", from = new FbUser { id = "user_1", name = "test_sender" } }; var sender = new SocialUser { Id = 1, OriginalId = "user_1" }; var dependencyResolverMock = MockDependencyResolver(fbHookChange, fbPost, sender); var messageServiceMock = new Mock <IMessageService>(); // make duplicate message messageServiceMock.Setup(t => t.IsDuplicatedMessage(MessageSource.FacebookPost, "post_1")).Returns(true); dependencyResolverMock.Setup(t => t.Resolve <IMessageService>()).Returns(messageServiceMock.Object); var strategy = new NewPostStrategy(dependencyResolverMock.Object); var socialAccount = new SocialAccount { Token = "token", IfConvertVisitorPostToConversation = true }; // Act var processResult = await strategy.Process(socialAccount, fbHookChange); // Assert Assert.False(processResult.NewMessages.Any()); Assert.False(processResult.NewConversations.Any()); Assert.False(processResult.UpdatedConversations.Any()); }
public async Task ShouldNotifyNewConversation() { // Arrange var fbHookChange = new FbHookChange { Field = "feed", Value = new FbHookChangeValue { Item = "post", PostId = "post_1", Verb = "add" } }; var fbPost = new FbPost { id = "post_1", from = new FbUser { id = "user_1", name = "test_sender" }, created_time = DateTime.UtcNow, message = "Test_Message" }; var sender = new SocialUser { Id = 1, OriginalId = "user_1" }; var dependencyResolverMock = MockDependencyResolver(fbHookChange, fbPost, sender); var strategy = new NewPostStrategy(dependencyResolverMock.Object); strategy.UnitOfWorkManager = MockUnitOfWorkManager().Object; var socialAccount = new SocialAccount { Id = 888, Token = "token", IfConvertVisitorPostToConversation = true }; // Act var processResult = await strategy.Process(socialAccount, fbHookChange); // Assert Assert.Equal(1, processResult.NewConversations.Count()); Assert.Equal(0, processResult.UpdatedConversations.Count()); Assert.Equal(0, processResult.NewMessages.Count()); }
private Mock <IDependencyResolver> MockDependencyResolver(FbHookChange change, FbPost fbPost, SocialUser sender) { var fbClientMock = new Mock <IFbClient>(); fbClientMock.Setup(t => t.GetPost("token", change.Value.PostId)).ReturnsAsync(fbPost); var messageServiceMock = new Mock <IMessageService>(); messageServiceMock.Setup(t => t.FindAll()).Returns(new List <Message>().AsQueryable()); var socialUserServiceMock = new Mock <ISocialUserService>(); socialUserServiceMock.Setup(t => t.GetOrCreateFacebookUser("token", fbPost.from.id)).ReturnsAsync(sender); var conversationServiceMock = new Mock <IConversationService>(); var dependencyResolverMock = new Mock <IDependencyResolver>(); dependencyResolverMock.Setup(t => t.Resolve <IFbClient>()).Returns(fbClientMock.Object); dependencyResolverMock.Setup(t => t.Resolve <IMessageService>()).Returns(messageServiceMock.Object); dependencyResolverMock.Setup(t => t.Resolve <ISocialUserService>()).Returns(socialUserServiceMock.Object); dependencyResolverMock.Setup(t => t.Resolve <IConversationService>()).Returns(conversationServiceMock.Object); return(dependencyResolverMock); }