예제 #1
0
        private async Task <List <MessageDto> > SetChatMessagesReadAsync(IEnumerable <Guid> messagesId, long chatId, long userId)
        {
            using (MessengerDbContext context = contextFactory.Create())
            {
                var messagesCondition = PredicateBuilder.New <Message>();
                messagesCondition = messagesId.Aggregate(messagesCondition,
                                                         (current, value) => current.Or(message => message.GlobalId == value).Expand());
                var readerUser = await context.ChatUsers
                                 .FirstOrDefaultAsync(opt => !opt.Banned && !opt.Deleted && opt.ChatId == chatId && opt.UserId == userId).ConfigureAwait(false);

                if (readerUser == null)
                {
                    throw new ReadMessageException();
                }

                var lastReadedMessage = await context.Messages
                                        .FirstOrDefaultAsync(opt => opt.GlobalId == readerUser.LastReadedGlobalMessageId && opt.ChatId == chatId).ConfigureAwait(false);

                List <Message> targetMessages = await context.Messages
                                                .Where(messagesCondition)
                                                .Include(message => message.Attachments)
                                                .ToListAsync()
                                                .ConfigureAwait(false);

                if (targetMessages != null)
                {
                    var lastMessage = targetMessages.OrderByDescending(opt => opt.SendingTime).FirstOrDefault();
                    if (lastMessage != null && (lastReadedMessage == null || lastMessage.SendingTime > lastReadedMessage.SendingTime))
                    {
                        await updateChatsService.UpdateLastReadedMessageIdAsync(
                            lastMessage.GlobalId,
                            userId,
                            chatId).ConfigureAwait(false);
                    }

                    targetMessages.ForEach(message =>
                    {
                        message.Read = true;
                    });
                    context.Messages.UpdateRange(targetMessages);
                    await context.SaveChangesAsync().ConfigureAwait(false);

                    return(MessageConverter.GetMessagesDto(targetMessages));
                }
                else
                {
                    throw new ReadMessageException();
                }
            }
        }
예제 #2
0
        private async Task ChatMessageReadAsync()
        {
            List <MessageDto> readedMessages = await updateMessagesService.SetMessagesReadAsync(
                notice.MessagesId,
                notice.ConversationOrUserId,
                ConversationType.Chat,
                notice.ReaderUserId).ConfigureAwait(false);

            var lastMessage = readedMessages.OrderByDescending(opt => opt.SendingTime)
                              .Where(opt => opt.SenderId != notice.ReaderUserId)
                              .FirstOrDefault();

            conversationsNoticeService.SendMessagesReadedNoticeAsync(readedMessages, notice.ConversationOrUserId, ConversationType.Chat, notice.ReaderUserId);
            if (lastMessage != null)
            {
                await updateChatsService.UpdateLastReadedMessageIdAsync(lastMessage.GlobalId, notice.ReaderUserId, notice.ConversationOrUserId).ConfigureAwait(false);
            }
        }