Example #1
0
        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));
            }
        }