コード例 #1
0
        private async Task <List <MessageDto> > SetChannelMessagesReadAsync(IEnumerable <Guid> messagesId, long channelId, long readerId)
        {
            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.ChannelUsers.FirstOrDefaultAsync(opt => opt.ChannelId == channelId && opt.UserId == readerId && !opt.Banned && !opt.Deleted).ConfigureAwait(false);

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

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

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

                if (targetMessages.Any())
                {
                    var lastMessage = targetMessages.OrderByDescending(opt => opt.SendingTime).FirstOrDefault();
                    if (lastMessage != null && (lastReadedMessage == null || lastMessage.SendingTime > lastReadedMessage.SendingTime))
                    {
                        await updateChannelsService.UpdateChannelLastReadedMessageAsync(
                            MessageConverter.GetMessageDto(lastMessage),
                            readerId).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 ChannelMessageReadAsync()
        {
            List <MessageDto> readedMessages = await updateMessagesService.SetMessagesReadAsync(
                notice.MessagesId, notice.ConversationOrUserId, ConversationType.Channel, notice.ReaderUserId).ConfigureAwait(false);

            conversationsNoticeService.SendMessagesReadedNoticeAsync(
                readedMessages, notice.ConversationOrUserId, ConversationType.Channel, notice.ReaderUserId);
            var lastMessage = readedMessages.OrderByDescending(opt => opt.SendingTime).FirstOrDefault();

            if (lastMessage != null)
            {
                await updateChannelsService.UpdateChannelLastReadedMessageAsync(lastMessage, notice.ReaderUserId).ConfigureAwait(false);
            }

            UsersConversationsCacheService.Instance.MessagesReadedUpdateConversations(
                MessageConverter.GetMessagesVm(readedMessages, null),
                notice.ConversationOrUserId,
                ConversationType.Channel);
        }