Beispiel #1
0
        public async Task <MessageServiceResultDto> SendMessageAsync(long senderId, long conversationId, string messageText, AttachmentDto[] attachments = null)
        {
            var conversationUsersIds = (await _conversationUserService.GetConversationUsers(conversationId)).Select((cu) => cu.UserId).ToList();

            if (conversationUsersIds.Count > 0 && await _blackListService.CheckAnyBlocked(senderId, conversationUsersIds))
            {
                return(Fail(MessageFailCauses.UserBlocked));
            }

            var createdAttachments = new List <Attachment>();

            if (attachments != null)
            {
                try
                {
                    var attachmentLoadAsync = attachments.Select(a => _attachmentContentProvider.DidUpload(a) ? Task.CompletedTask : _attachmentContentProvider.WaitForContentLoading(a));
                    Task.WaitAll(attachmentLoadAsync.ToArray());
                    createdAttachments = (await _attachmentRepository.CreateManyAsync(
                                              attachments.Select((a) => _mapper.Map <AttachmentDto, Attachment>(a))
                                              )).ToList();
                }
                catch
                {
                    return(Fail(MessageFailCauses.AttachmentSaveFailed));
                }
            }

            var newMsg = await _messageRepository.CreateAsync(new Message
            {
                SenderId       = senderId,
                ConversationId = conversationId,
                Attachments    = createdAttachments,
                CreatedAt      = _timeProvider.NowUtc()
            });

            OnMessageSent(MapToDto(newMsg));

            var methodResult = Ok(newMsg);

            return(methodResult);
        }
Beispiel #2
0
 private async Task <bool> DidAnyoneBlock(IEnumerable <long> userIds, long maybeBlockedId)
 {
     return(await _blackListService.CheckAnyBlocked(maybeBlockedId, userIds));
 }