private async void HandleDirectText(Message message) { var sender = message.GetDirectTextNickname(); var content = message.GetDirectTextContent(); var conversation = PassiveConversationService.Conversations.FirstOrDefault(c => c.Participants.Select(p => p.Nickname).Contains(sender)) ?? await ConversationService.AddConversation(ConversationType.Passive, UserService.LoggedUser.Nickname, sender); var conversationControl = ConversationControls.FirstOrDefault(p => p.PassiveConversation == conversation); if (conversationControl == null) { conversationControl = new ConversationControl { PassiveConversation = conversation }; Dispatcher.Invoke(() => { ConversationList.Items.Add(conversationControl); }); } if (SelectedConversationControl.PassiveConversation != conversationControl.PassiveConversation) { conversationControl.Notify(); } var messageDisplay = MessageDisplays.FirstOrDefault(m => m.Conversation == conversation); if (messageDisplay == null) { messageDisplay = new MessageDisplayControl(conversation); Dispatcher.Invoke(() => { MessageDisplays.Add(messageDisplay); }); return; } Dispatcher.Invoke(() => { var msg = new TextMessage(content, DateTime.Now, UserService.GetUser(sender)); conversation.Messages.Add(msg); messageDisplay.AddMessageControl(msg); }); }
private void HandleDirectText(Client sender, Message message) { if (sender == null) { return; } var targetNickname = message.GetDirectTextNickname(); var targetClient = GetClient(targetNickname); if (targetClient == null) { Debug($"Undelivered message from {sender.Nickname} to {targetNickname}, target client offline."); return; } using (var database = new RozmawiatorDb()) { var conversation = database.Conversations.FirstOrDefault( c => c.ConversationParticipants.Any(cp => cp.User.UserName == targetNickname)); var senderUser = database.Users.FirstOrDefault(u => u.UserName == sender.Nickname); if (senderUser == null) { SendAsClient(sender, targetClient, message); return; } if (conversation == null) { var targetUser = database.Users.FirstOrDefault(u => u.UserName == targetNickname); if (targetUser == null) { SendAsClient(sender, targetClient, message); return; } conversation = new Database.Entities.Conversation { Type = ConversationType.Passive, ConversationParticipants = new[] { new ConversationParticipant { User = senderUser }, new ConversationParticipant { User = targetUser } } }; database.Conversations.Add(conversation); } var msg = new Database.Entities.Message { Conversation = conversation, Content = message.GetDirectTextContent(), Timestamp = DateTime.Now, Sender = senderUser }; database.Messages.Add(msg); database.SaveChanges(); } var forwardMessage = new Message().DirectText(sender.Nickname, message.GetDirectTextContent()); //SendAsClient(sender, targetClient, message); SendAsClient(sender, targetClient, forwardMessage); }