Esempio n. 1
0
        public async Task GetChannelMessagesById()
        {
            var expectedChannelMessage = fillTestDbHelper.Messages.FirstOrDefault(message => message.ChannelId != null);
            var channelMessages        = await loadMessagesService.GetMessagesByIdAsync(
                new List <Guid> {
                expectedChannelMessage.GlobalId
            },
                ConversationType.Channel,
                expectedChannelMessage.ChannelId.Value,
                null);

            Assert.Equal(expectedChannelMessage.GlobalId, channelMessages.FirstOrDefault().GlobalId);
        }
        public async Task SaveForwardedMessages()
        {
            var chat             = fillTestDbHelper.Chats.FirstOrDefault();
            var chatMessage      = fillTestDbHelper.Messages.FirstOrDefault(opt => opt.ChatId == chat.Id);
            var expectedMessages = new List <MessageDto>
            {
                new MessageDto
                {
                    ConversationId   = chat.Id,
                    ConversationType = ConversationType.Chat,
                    SenderId         = chat.ChatUsers.FirstOrDefault().UserId,
                    Text             = "Save messages test",
                    GlobalId         = Guid.NewGuid(),
                    SendingTime      = DateTime.UtcNow.ToUnixTime()
                },
                new MessageDto
                {
                    ConversationId   = chat.Id,
                    ConversationType = ConversationType.Chat,
                    SenderId         = chat.ChatUsers.LastOrDefault().UserId,
                    Text             = "Save messages test",
                    GlobalId         = Guid.NewGuid(),
                    SendingTime      = DateTime.UtcNow.ToUnixTime()
                },
                MessageConverter.GetMessageDto(chatMessage)
            };
            await createMessagesService.SaveForwardedMessagesAsync(expectedMessages);

            var actualMessages = await loadMessagesService.GetMessagesByIdAsync(expectedMessages.Select(opt => opt.GlobalId), ConversationType.Chat, chat.Id, null);

            Assert.True(expectedMessages.Count() == actualMessages.Count());
            Assert.Equal(
                expectedMessages.OrderBy(opt => opt.SendingTime).Select(opt => opt.GlobalId),
                actualMessages.OrderBy(opt => opt.SendingTime).Select(opt => opt.GlobalId));
        }
 private async Task <IEnumerable <MessageDto> > GetDialogMessagesAsync()
 {
     if (request.MessagesId == null)
     {
         return(await loadMessagesService.GetDialogMessagesAsync(
                    request.ConversationId,
                    clientConnection.UserId.GetValueOrDefault(),
                    request.NavigationMessageId,
                    request.AttachmentsTypes,
                    request.Direction.GetValueOrDefault(true),
                    LIMIT).ConfigureAwait(false));
     }
     else
     {
         return(await loadMessagesService.GetMessagesByIdAsync(
                    request.MessagesId,
                    ConversationType.Dialog,
                    request.ConversationId,
                    clientConnection.UserId.GetValueOrDefault()).ConfigureAwait(false));
     }
 }
Esempio n. 4
0
        public async Task <MessagesPageViewModel> GetMessages([FromForm] GetMessagesModel model)
        {
            MessagesPageViewModel responseModel;

            if (model.MessagesIds.IsNullOrEmpty())
            {
                responseModel = await _loadMessagesService.GetMessagesPageAsync(model.ConversationId, model.ConversationType, model.PageNumber.GetValueOrDefault(), messagesLimit);
            }
            else
            {
                var messages = await _loadMessagesService.GetMessagesByIdAsync(model.MessagesIds, model.ConversationType, model.ConversationId, null)
                               .ConfigureAwait(false);

                responseModel = new MessagesPageViewModel {
                    Messages = MessageConverter.GetMessagesVm(messages, null)
                };
            }
            return(responseModel);
        }
        public async Task <bool> CheckEditedMessageAttachmentsAsync(MessageVm message, long userId)
        {
            try
            {
                foreach (var attachment in message.Attachments)
                {
                    switch (attachment.Type)
                    {
                    case AttachmentType.Audio:
                    case AttachmentType.File:
                    case AttachmentType.Picture:
                    case AttachmentType.Video:
                    {
                        if (attachment.Payload is string stringPayload)
                        {
                            var fileInfo = _filesService.GetFileInfoAsync(stringPayload);
                            if (fileInfo == null)
                            {
                                return(false);
                            }
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    break;

                    case AttachmentType.ForwardedMessages:
                    {
                        if (attachment.Payload is ForwardedMessagesInfo messagesInfo)
                        {
                            var messages = await _loadMessagesService.GetMessagesByIdAsync(
                                messagesInfo.MessagesGlobalId,
                                messagesInfo.ConversationType,
                                messagesInfo.ConversationId.GetValueOrDefault(),
                                userId).ConfigureAwait(false);

                            if (messages == null || !messages.Any())
                            {
                                return(false);
                            }
                        }
                        else if (attachment.Payload is List <MessageVm> messages)
                        {
                            await _createMessagesService.SaveForwardedMessagesAsync(MessageConverter.GetMessagesDto(messages)).ConfigureAwait(false);
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    break;

                    case AttachmentType.Poll:
                    {
                        if (attachment.Payload is PollVm poll)
                        {
                            var existingPoll = await _pollsService.GetPollAsync(
                                poll.PollId.GetValueOrDefault(),
                                message.ConversationId.GetValueOrDefault(),
                                message.ConversationType).ConfigureAwait(false);

                            if (existingPoll == null)
                            {
                                poll = await PollConverter.InitPollConversationAsync(poll, message).ConfigureAwait(false);

                                poll.PollId = RandomExtensions.NextGuid();
                                await _pollsService.SavePollAsync(PollConverter.GetPollDto(poll, userId)).ConfigureAwait(false);

                                attachment.Payload = poll;
                            }
                        }
                    }
                    break;

                    case AttachmentType.VoiceMessage:
                    {
                        await ValidateVoiceMessageAttachmentAsync(attachment);
                    }
                    break;
                    }
                }
                return(true);
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex);
                return(false);
            }
        }