private static async Task <ContactItem> GetContactItem(ILogger _logger, ISiteMap siteMap, UserService userService, BoxId fromBoxId, ChatDbModel chatDbModel = null) { var contact = new ContactItem(); if (!string.IsNullOrEmpty(fromBoxId?.Id)) { if (fromBoxId.Type == TypeBox.User) { var userDb = await userService.FindApplicationUserByIdAsync(fromBoxId.Id); contact.FullName = userDb.FullName; contact.Id = fromBoxId.Id; contact.Type = TypeBox.User; } else if (fromBoxId.Type == TypeBox.Site) { contact.FullName = await siteMap.GetSiteNameAsync(fromBoxId.Id); contact.Id = fromBoxId.Id; contact.Type = TypeBox.Site; } else if (fromBoxId.Type == TypeBox.UserNotAuthenticated) { InitContactNotAUthenticated(_logger, chatDbModel, contact); } } else { InitContactNotAUthenticated(_logger, chatDbModel, contact); } return(contact); }
public async Task SaveChatAsync(ChatDbModel chatDbModel) { if (chatDbModel.Messages != null) { foreach (var messageDbModel in chatDbModel.Messages) { SetMessageId(messageDbModel); } } await _chats.InsertOneAsync(chatDbModel); }
private static void InitContactNotAUthenticated(ILogger _logger, ChatDbModel chatDbModel, ContactItem contact) { try { var message = SendMessageCommand.FindUserNotAUthenticatedInfo(chatDbModel); if (message != null) { contact.FullName = message.FullName; contact.Id = message.Email; contact.Type = TypeBox.UserNotAuthenticated; } } catch (Exception exception) { _logger.LogError(exception, "Exception deserialize message ContactItem json"); } }
internal static MessageSiteNotAuthenticated FindUserNotAUthenticatedInfo(ChatDbModel chatDbModel) { if (chatDbModel == null || chatDbModel.Messages == null) { return(null); } foreach (var messageDbModel in chatDbModel.Messages) { var messageType = messageDbModel.MessageType; if (messageDbModel.MessageType == SiteNotAuthenticated) { return((MessageSiteNotAuthenticated)GetMessage(messageType, messageDbModel.Message)); } } return(null); }
public static async Task <ChatItem> MapChatItem(ILogger _logger, ISiteMap siteMap, UserService userService, ChatDbModel chat, string userId) { var messages = chat.Messages; var chatItem = new ChatItem(); chatItem.Id = chat.Id; chatItem.CreatedDate = chat.CreateDate; var lastMessageDate = chat.Messages.OrderByDescending(m => m.CreateDate).Select(m => m.CreateDate).FirstOrDefault(); chatItem.LastMessageDate = lastMessageDate; chatItem.Title = chat.Title; chatItem.Messages = new List <MessageItem>(); chatItem.Readed = chat.LastReads != null && chat.LastReads.Count(b => b.Id.Id == userId && b.Id.Type == TypeBox.User) > 0; chatItem.To = new List <ContactItem>(); foreach (var to in chat.To) { var contactTo = await GetContactItem(_logger, siteMap, userService, to.Id, chat); chatItem.To.Add(contactTo); } foreach (var message in messages) { var messageItem = new MessageItem(); messageItem.CreatedDate = chat.CreateDate; messageItem.LastMessageDate = chat.UpdateDate; messageItem.Title = chat.Title; var fromBoxId = message.FromId; var contact = await GetContactItem(_logger, siteMap, userService, fromBoxId, chat); messageItem.From = contact; messageItem.Message = message.Message; messageItem.MessageType = message.MessageType; messageItem.CreatedDate = message.CreateDate; chatItem.Messages.Add(messageItem); } return(chatItem); }
protected override async Task ActionAsync() { var data = Input.Data; var messageJson = data.MessageJson; var messageType = data.Type; var message = GetMessage(messageType, messageJson); var createdDate = DateTime.Now; var messageDbModel = new MessageDbModel(); messageDbModel.CreateDate = createdDate; messageDbModel.FromId = data.From; messageDbModel.Source = data.Source; messageDbModel.Message = data.MessageJson; messageDbModel.MessageType = data.Type; IList <To> toList = new List <To> { new To() { Id = data.To }, new To() { Id = data.From } }; string chatId; ChatDbModel chatDbModel; Read read = null; // TODO control input if (!string.IsNullOrEmpty(Input.UserId)) { read = new Read() { CreateDate = createdDate, Id = data.From }; } if (string.IsNullOrEmpty(data.ChatId)) { var reads = new List <Read>(); if (read != null) { reads.Add(read); } chatDbModel = new ChatDbModel(); chatDbModel.CreateDate = createdDate; chatDbModel.UpdateDate = createdDate; chatDbModel.Title = ((MessageSiteAuthenticated)message).Title; chatDbModel.Reads = reads; chatDbModel.LastReads = reads; chatDbModel.To = toList; chatDbModel.Messages = new List <MessageDbModel> { messageDbModel }; chatDbModel.NumberMessages = 1; await _messageService.SaveChatAsync(chatDbModel); chatId = chatDbModel.Id; } else { await _messageService.SaveMessageAsync(data.ChatId, messageDbModel, read); chatDbModel = await _messageService.GetChatAsync(data.ChatId); toList = chatDbModel.To; chatId = chatDbModel.Id; } Result.Data = new SendMessageResult() { Chat = await ListMessageCommand.MapChatItem(_logger, _siteMap, _userService, chatDbModel, Input.UserId) }; ApplicationUser senderUserDb = null; if (!string.IsNullOrEmpty(data.From.Id) && data.From.Type == TypeBox.User) { senderUserDb = await _userService.FindApplicationUserByIdAsync(data.From.Id); } var siteId = toList.Where(t => t.Id.Type == TypeBox.Site).Select(t => t.Id.Id).FirstOrDefault(); var site = await _siteMap.SiteUrlAsync(siteId); var siteName = site.Name; var siteUrl = site.Url; var isReply = data.Type == "Reply"; foreach (var to in toList) { if (to.Id.Type == TypeBox.Site && !string.IsNullOrEmpty(to.Id.Id)) { var users = await _userService.UserByRoleAsync(siteId); foreach (var userDb in users) { await SendMessageReceiver(siteName, siteUrl, userDb, message, senderUserDb, messageType, chatDbModel, true); } } else if (to.Id.Type == TypeBox.User && !string.IsNullOrEmpty(to.Id.Id)) { var userDb = await _userService.FindApplicationUserByIdAsync(to.Id.Id); await SendMessageReceiver(siteName, siteUrl, userDb, message, senderUserDb, messageType, chatDbModel, false); } /*else if (to.Id.Type == TypeBox.UserNotAuthenticated) * { * var messageUserNotAuthenticated = FindUserNotAUthenticatedInfo(chatDbModel); * await * SendMessageReceiver(siteName, siteUrl, null, messageUserNotAuthenticated, senderUserDb, messageType, chatDbModel, false); * }*/ } await SendMessageSender(messageType, message, siteName, siteUrl, isReply, senderUserDb, data, chatId, chatDbModel); }
private async Task SendMessageReceiver(string siteName, string siteUrl, ApplicationUser userDb, MessageReply messageReply, ApplicationUser senderUserDb, string messageType, ChatDbModel chatDbModel, bool isAdmin) { if (senderUserDb == null || senderUserDb.Id != userDb.Id) { var messageReceiverMailModel = new MessageReceiverMailModel(); messageReceiverMailModel.SiteName = siteName; messageReceiverMailModel.SiteUrl = siteUrl; messageReceiverMailModel.UserName = userDb.FullName; messageReceiverMailModel.Message = FormatMessageForEmail(messageReply.Message); messageReceiverMailModel.Title = chatDbModel.Title; var messageSource = "/administration"; if (!isAdmin) { messageSource = "/utilisateur"; } var messageUrl = UrlHelper.Concat(siteUrl, messageSource + "/messages/message/" + chatDbModel.Id); messageReceiverMailModel.MessageUrl = messageUrl; if (messageType == SiteNotAuthenticated) { var msg = (MessageSiteNotAuthenticated)messageReply; messageReceiverMailModel.Sender = new SenderModel() { Email = msg.Email, FullName = msg.FullName, Phone = msg.Phone, IsNotAuthenticated = true }; } else if (senderUserDb != null) { messageReceiverMailModel.Sender = new SenderModel() { FullName = senderUserDb.FullName, IsNotAuthenticated = false }; } await SendEmailAsync(messageReceiverMailModel, userDb.Email); } }
private async Task SendMessageSender(string messageType, MessageReply message, string siteName, string siteUrl, bool isReply, ApplicationUser senderUserDb, SendMessageInput data, string chatId, ChatDbModel chatDbModel) { if (messageType == SiteNotAuthenticated) { var msg = (MessageSiteNotAuthenticated)message; var messageReceiverMailModel = new MessageReceiverMailModel(); messageReceiverMailModel.SiteName = siteName; messageReceiverMailModel.SiteUrl = siteUrl; messageReceiverMailModel.UserName = msg.FullName; messageReceiverMailModel.Title = msg.Title; messageReceiverMailModel.Message = FormatMessageForEmail(message.Message); messageReceiverMailModel.IsReply = isReply; messageReceiverMailModel.Sender = new SenderModel() { IsNotAuthenticated = true, FullName = msg.FullName, Email = msg.Email, Phone = msg.Phone }; await SendEmailSenderAsync(messageReceiverMailModel, msg.Email); } else if (senderUserDb != null) { var messageReceiverMailModel = new MessageReceiverMailModel(); messageReceiverMailModel.SiteName = siteName; messageReceiverMailModel.SiteUrl = siteUrl; var messageSource = "/administration"; if (data.Source == "User") { messageSource = "/utilisateur"; } var messageUrl = UrlHelper.Concat(siteUrl, messageSource + "/messages/message/" + chatId); messageReceiverMailModel.MessageUrl = messageUrl; messageReceiverMailModel.UserName = senderUserDb.FullName; messageReceiverMailModel.Message = FormatMessageForEmail(message.Message); messageReceiverMailModel.Title = chatDbModel.Title; messageReceiverMailModel.Sender = new SenderModel() { IsNotAuthenticated = false, FullName = senderUserDb.FullName }; messageReceiverMailModel.IsReply = isReply; await SendEmailSenderAsync(messageReceiverMailModel, senderUserDb.Email); } }