public async Task <MessageDto> CreateMessageAsync(ConversationType conversationType, long conversationId, SystemMessageInfo systemMessageInfo) { List <long> nodesIds = await conversationsService.GetConversationNodesIdsAsync(conversationType, conversationId).ConfigureAwait(false); using (MessengerDbContext context = contextFactory.Create()) { var messageInfoJson = systemMessageInfo.ToJson(); AttachmentDto attachmentDto = new AttachmentDto { Type = AttachmentType.SystemMessage, Payload = messageInfoJson, Hash = GetHash(messageInfoJson) }; MessageDto messageDto = new MessageDto { ConversationId = conversationId, ConversationType = conversationType, Attachments = new List <AttachmentDto> { attachmentDto }, SendingTime = DateTime.UtcNow.ToUnixTime(), GlobalId = Guid.NewGuid(), NodesIds = nodesIds }; Message message = MessageConverter.GetMessage(messageDto); if (conversationType == ConversationType.Dialog) { var mirrorMessage = (MessageDto)messageDto.Clone(); var mirrodDialogId = await loadDialogsService.GetMirrorDialogIdAsync(conversationId); mirrorMessage.ConversationId = mirrodDialogId; await context.Messages.AddAsync(MessageConverter.GetMessage(mirrorMessage)); } await context.Messages.AddAsync(message).ConfigureAwait(false); await context.SaveChangesAsync().ConfigureAwait(false); return(MessageConverter.GetMessageDto(message)); } }
public async void SendSystemMessageNoticeAsync(MessageDto message) { switch (message.ConversationType) { case ConversationType.Dialog: { var mirrorDialog = await loadDialogsService.GetDialogAsync(message.ConversationId).ConfigureAwait(false); var secondMessage = (MessageDto)message.Clone(); secondMessage.ConversationId = mirrorDialog.Id; var firstUserConnections = connectionsService.GetUserClientConnections(mirrorDialog.FirstUserId); var secondUserConnections = connectionsService.GetUserClientConnections(mirrorDialog.SecondUserId); var firstUserNotice = new NewMessageNotice(MessageConverter.GetMessageVm(secondMessage, null), false); var secondUserNotice = new NewMessageNotice(MessageConverter.GetMessageVm(message, null), false); await SendNoticeToClientsAsync(firstUserConnections, firstUserNotice).ConfigureAwait(false); await SendNoticeToClientsAsync(secondUserConnections, secondUserNotice).ConfigureAwait(false); } break; case ConversationType.Chat: { var usersIds = await loadChatsService.GetChatUsersIdAsync(message.ConversationId).ConfigureAwait(false); var notice = new NewMessageNotice(MessageConverter.GetMessageVm(message, null), false); var clientsConnections = connectionsService.GetClientConnections(usersIds); await SendNoticeToClientsAsync(clientsConnections, notice).ConfigureAwait(false); } break; case ConversationType.Channel: { var usersIds = await loadChannelsService.GetChannelUsersIdAsync(message.ConversationId).ConfigureAwait(false); var notice = new NewMessageNotice(MessageConverter.GetMessageVm(message, null), false); var clientsConnections = connectionsService.GetClientConnections(usersIds); await SendNoticeToClientsAsync(clientsConnections, notice).ConfigureAwait(false); } break; } }