Exemplo n.º 1
0
        public async Task GetLastValidDialogMessage()
        {
            var dialog          = fillTestDbHelper.Dialogs.FirstOrDefault();
            var expectedMessage = fillTestDbHelper.Messages
                                  .OrderByDescending(opt => opt.SendingTime)
                                  .FirstOrDefault(opt => !opt.Deleted && opt.DialogId == dialog.Id);
            var actualMessage = await loadMessagesService.GetLastValidDialogMessageAsync(dialog.Id);

            Assert.Equal(expectedMessage.GlobalId, actualMessage.GlobalId);
        }
        private async Task <List <MessageDto> > DeleteDialogMessagesInfoAsync(long dialogId, IEnumerable <Guid> messagesIds, long userId)
        {
            try
            {
                using (MessengerDbContext context = contextFactory.Create())
                {
                    long mirrorDialogId = await _loadDialogsService.GetMirrorDialogIdAsync(dialogId).ConfigureAwait(false);

                    var messagesCondition = PredicateBuilder.New <Message>();
                    messagesCondition = messagesIds.Aggregate(messagesCondition,
                                                              (current, value) => current.Or(opt => opt.GlobalId == value &&
                                                                                             (opt.DialogId == dialogId || opt.DialogId == mirrorDialogId) &&
                                                                                             (opt.SenderId == userId || opt.ReceiverId == userId)).Expand());
                    List <MessageDto> deletedMessages;
                    List <Message>    messages = await context.Messages
                                                 .Include(opt => opt.Attachments)
                                                 .Where(messagesCondition)
                                                 .ToListAsync()
                                                 .ConfigureAwait(false);

                    if (!messages.Any())
                    {
                        return(new List <MessageDto>());
                    }
                    deletedMessages = MessageConverter.GetMessagesDto(messages);
                    Dialog firstDialog = await context.Dialogs.FirstOrDefaultAsync(opt => opt.Id == dialogId).ConfigureAwait(false);

                    Dialog mirrorDialog = await context.Dialogs.FirstOrDefaultAsync(opt => opt.Id == mirrorDialogId).ConfigureAwait(false);

                    foreach (var message in messages)
                    {
                        message.Deleted   = true;
                        message.UpdatedAt = DateTime.UtcNow.ToUnixTime();
                        if (NodeSettings.Configs.Node.PermanentlyDeleting)
                        {
                            message.Attachments = null;
                            message.Replyto     = null;
                            message.SendingTime = 0;
                            message.Text        = null;
                            message.SenderId    = null;
                            message.ReceiverId  = null;
                        }
                    }
                    context.UpdateRange(messages);
                    await context.SaveChangesAsync().ConfigureAwait(false);

                    Message lastMessageFirstDialog = await _loadMessagesService.GetLastValidDialogMessageAsync(dialogId).ConfigureAwait(false);

                    Message lastMessageSecondDialog = await _loadMessagesService.GetLastValidDialogMessageAsync(mirrorDialogId).ConfigureAwait(false);

                    firstDialog.LastMessageId        = lastMessageFirstDialog?.Id ?? null;
                    mirrorDialog.LastMessageId       = lastMessageSecondDialog?.Id ?? null;
                    firstDialog.LastMessageGlobalId  = lastMessageFirstDialog?.GlobalId;
                    mirrorDialog.LastMessageGlobalId = lastMessageSecondDialog?.GlobalId;
                    context.Dialogs.UpdateRange(firstDialog, mirrorDialog);
                    await context.SaveChangesAsync().ConfigureAwait(false);

                    return(deletedMessages);
                }
            }
            catch (Exception ex)
            {
                throw new DeleteMessagesException("An error occurred while deleting messages.", ex);
            }
        }