private static void SetMessageId(MessageDbModel messageDbModel) { if (string.IsNullOrEmpty(messageDbModel.Id)) { messageDbModel.Id = Guid.NewGuid().ToString(); } }
public async Task SaveMessageAsync(string chatId, MessageDbModel messageDbModel, Read readDbModel = null) { var update = Builders <ChatDbModel> .Update .Set("UpdateDate", messageDbModel.CreateDate) .Inc("NumberMessages", 1) .Push("Messages", messageDbModel); if (readDbModel != null) { update = update.Push("Reads", readDbModel); update = update.Push("LastReads", readDbModel); } var builder = Builders <ChatDbModel> .Filter; var filter = builder.Eq(x => x.Guid, new Guid(chatId)); await _chats.UpdateOneAsync(filter, update, new UpdateOptions() { IsUpsert = false }); }
public VoidCommandResponse Handle(SaveNewMessageCommand command) { var model = new MessageDbModel { AccountId = command.AccountId, MessageGroupId = command.GroupId, IsStopped = false, EndTime = command.EndTime, ImportancyFactor = command.ImportancyFactor, IsEmergencyText = command.IsEmergencyText, Message = command.Message, MessageRegime = command.MessageRegime, OrderNumber = command.OrderNumber, StartTime = command.StartTime }; context.Set <MessageDbModel>().Add(model); context.SaveChanges(); return(new VoidCommandResponse()); }
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); }