Exemplo n.º 1
0
        public async Task <ConversationDTO> GetConversationOfUsers(string userId1, string userId2)
        {
            if (!String.IsNullOrWhiteSpace(userId1) && !String.IsNullOrWhiteSpace(userId2))
            {
                var conversationsWithTwoUsersIds = appDb.Conversations
                                                   .Include(u => u.Users)
                                                   .Where(u => u.Users.Count == 2)
                                                   .ToList();


                var user1 = await appDb.Users.Include(u => u.Conversations).Where(u => u.Id == userId1).SingleOrDefaultAsync();

                var user2 = await appDb.Users.Include(u => u.Conversations).Where(u => u.Id == userId2).SingleOrDefaultAsync();

                ConversationComparer conversationComparer = new ConversationComparer();
                Conversation         conversation         = user1.Conversations?.Intersect(user2.Conversations, conversationComparer).Intersect(conversationsWithTwoUsersIds, conversationComparer).FirstOrDefault();

                if (conversation == null)
                {
                    var conversationToAdd = new Conversation();
                    appDb.Conversations.Add(conversationToAdd);
                    await appDb.SaveChangesAsync();

                    appDb.UserConversations.Add(new UserConversation()
                    {
                        Conversation = conversationToAdd,
                        User         = user1
                    });
                    appDb.UserConversations.Add(new UserConversation()
                    {
                        Conversation = conversationToAdd,
                        User         = user2
                    });
                    await appDb.SaveChangesAsync();

                    conversation = conversationToAdd;
                }

                return(new ConversationDTO
                {
                    Id = conversation.Id
                });
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 2
0
        public void ConversationOrderTest()
        {
            foreach (MainWindowModelBase_Accessor model in GetImplementations())
            {
                model.ConversationManager = CreateConversationManager();

                IConversationListItem listItemPrevious = null;

                model.DisplayOptions.ConversationSorting = ConversationSorting.AlphabeticalByContact;
                foreach (IConversationListItem listItem in model.ConversationListItems)
                {
                    if (listItem.Conversation.GetType() == typeof(AggregateConversation))
                    {
                        Assert.IsNull(listItemPrevious);
                        continue;
                    }
                    if (listItemPrevious != null)
                    {
                        Assert.IsTrue(ConversationComparer.AlphabeticalByContact(listItemPrevious.Conversation, listItem.Conversation) <= 0);
                        Assert.AreEqual(listItemPrevious.Conversation, model.PreviousConversation(listItem.Conversation));
                        Assert.AreEqual(listItem.Conversation, model.NextConversation(listItemPrevious.Conversation));
                    }

                    listItemPrevious = listItem;
                }

                listItemPrevious = null;
                model.DisplayOptions.ConversationSorting = ConversationSorting.DescendingByTotalMessages;
                foreach (IConversationListItem listItem in model.ConversationListItems)
                {
                    if (listItem.Conversation.GetType() == typeof(AggregateConversation))
                    {
                        Assert.IsNull(listItemPrevious);
                        continue;
                    }
                    if (listItemPrevious != null)
                    {
                        Assert.IsTrue(ConversationComparer.DescendingByTotalMessages(listItemPrevious.Conversation, listItem.Conversation) <= 0);
                        Assert.AreEqual(listItemPrevious.Conversation, model.PreviousConversation(listItem.Conversation));
                        Assert.AreEqual(listItem.Conversation, model.NextConversation(listItemPrevious.Conversation));
                    }

                    listItemPrevious = listItem;
                }
            }
        }