Ejemplo n.º 1
0
        public async Task <List <MessageVm> > LoadForwardedMessagesAsync(ForwardedMessagesInfo forwarded, long?requestorId)
        {
            var            messagesCondition = PredicateBuilder.New <Message>();
            List <Message> messages          = new List <Message>();

            switch (forwarded.ConversationType)
            {
            case ConversationType.Chat:
            {
                messagesCondition = forwarded.MessagesGlobalId.Aggregate(messagesCondition,
                                                                         (current, value) => current.Or(opt => opt.GlobalId == value && opt.ChatId == forwarded.ConversationId).Expand());
                break;
            }

            case ConversationType.Dialog:
            {
                messagesCondition = forwarded.MessagesGlobalId.Aggregate(messagesCondition,
                                                                         (current, value) => current.Or(opt => opt.GlobalId == value && opt.DialogId == forwarded.ConversationId).Expand());
                break;
            }

            case ConversationType.Channel:
            {
                messagesCondition = forwarded.MessagesGlobalId.Aggregate(messagesCondition,
                                                                         (current, value) => current.Or(opt => opt.GlobalId == value && opt.ChannelId == forwarded.ConversationId).Expand());
            }
            break;

            case ConversationType.Unknown:
            {
                messagesCondition = forwarded.MessagesGlobalId.Aggregate(messagesCondition,
                                                                         (current, value) => current.Or(opt => opt.GlobalId == value && opt.ChannelId == null && opt.DialogId == null && opt.ChatId == null).Expand());
            }
            break;

            default:
                return(null);
            }
            using (MessengerDbContext context = contextFactory.Create())
            {
                messages = await context.Messages
                           .AsNoTracking()
                           .Include(opt => opt.Attachments)
                           .Where(messagesCondition)
                           .ToListAsync().ConfigureAwait(false);

                foreach (var message in messages)
                {
                    if (message.ChannelId != null)
                    {
                        message.SenderId = null;
                    }

                    message.Attachments = message.Attachments.Where(opt => opt.Type != (short)AttachmentType.ForwardedMessages).ToList();
                }
                return(MessageConverter.GetMessagesVm(MessageConverter.GetMessagesDto(messages), requestorId));
            }
        }
Ejemplo n.º 2
0
        private async Task <bool> ValidateForwardedMessagesAttachment(AttachmentVm forwardedMessagesAttachment, long?userId, bool anotherNodeMessage)
        {
            ForwardedMessagesInfo fMessagesInfo = null;

            if (forwardedMessagesAttachment.Payload is string)
            {
                fMessagesInfo = ObjectSerializer.JsonToObject <ForwardedMessagesInfo>(forwardedMessagesAttachment.Payload.ToString());
            }
            else if (forwardedMessagesAttachment.Payload is List <MessageVm> messages)
            {
                if (!messages.Any())
                {
                    return(true);
                }
                var message = messages.FirstOrDefault();
                if (anotherNodeMessage)
                {
                    await _createMessagesService.SaveForwardedMessagesAsync(MessageConverter.GetMessagesDto(messages)).ConfigureAwait(false);

                    forwardedMessagesAttachment.Payload = new ForwardedMessagesInfo(
                        messages.Select(opt => opt.GlobalId.Value),
                        message.ConversationType == ConversationType.Dialog
                            ? (await _loadDialogsService.GetDialogsIdByUsersIdPairAsync(message.SenderId.Value, message.ReceiverId.Value).ConfigureAwait(false)).FirstOrDefault()
                            : message.ConversationId,
                        message.ConversationType);
                    return(true);
                }
                switch (message.ConversationType)
                {
                case ConversationType.Dialog:
                {
                    var dialogsId = await _loadDialogsService.GetDialogsIdByUsersIdPairAsync(message.SenderId.Value, message.ReceiverId.Value).ConfigureAwait(false);

                    if (dialogsId.Any())
                    {
                        fMessagesInfo = new ForwardedMessagesInfo(messages.Select(opt => opt.GlobalId.Value), dialogsId[0], ConversationType.Dialog);
                    }
                    else
                    {
                        fMessagesInfo = new ForwardedMessagesInfo(messages.Select(opt => opt.GlobalId.Value), null, ConversationType.Dialog);
                    }
                }
                break;

                case ConversationType.Chat:
                {
                    var chat = await _loadChatsService.GetChatByIdAsync(message.ConversationId.GetValueOrDefault()).ConfigureAwait(false);

                    if (chat.Type == ChatType.Private)
                    {
                        return(false);
                    }
                }
                break;

                case ConversationType.Channel:
                    fMessagesInfo = new ForwardedMessagesInfo(messages.Select(opt => opt.GlobalId.Value), message.ConversationId.GetValueOrDefault(), message.ConversationType);
                    break;
                }
            }
            else
            {
                fMessagesInfo = ObjectSerializer.JsonToObject <ForwardedMessagesInfo>(ObjectSerializer.ObjectToJson(forwardedMessagesAttachment.Payload));
            }
            if (fMessagesInfo == null)
            {
                return(false);
            }

            return(await _loadMessagesService.CanUserGetMessageAsync(fMessagesInfo.ConversationType, fMessagesInfo.ConversationId, userId).ConfigureAwait(false));
        }
Ejemplo n.º 3
0
        private static async Task <object> PayloadStringToObject(string attachmentPayload, AttachmentType attachmentType, long?requestorId)
        {
            if (attachmentPayload == null)
            {
                return(null);
            }

            switch (attachmentType)
            {
            case AttachmentType.Audio:
            case AttachmentType.File:
            case AttachmentType.Picture:
            case AttachmentType.Video:
            case AttachmentType.VoiceMessage:
            case AttachmentType.VideoMessage:
            {
                if (IsPayloadJsonObject(attachmentPayload, out FileInfoVm fileInfo))
                {
                    return(fileInfo);
                }
                return(await attachmentsService.LoadFileInfoAsync(attachmentPayload).ConfigureAwait(false));
            }

            case AttachmentType.EncryptedMessage:
            {
                return(ObjectSerializer.JsonToObject <EncryptedMessage>(attachmentPayload));
            }

            case AttachmentType.ForwardedMessages:
            {
                if (IsPayloadJsonObject(attachmentPayload, out ForwardedMessagesInfo messInfo))
                {
                    ForwardedMessagesInfo messagesInfo =
                        ObjectSerializer.JsonToObject <ForwardedMessagesInfo>(attachmentPayload);
                    return(await attachmentsService.LoadForwardedMessagesAsync(messagesInfo, requestorId).ConfigureAwait(false));
                }
                return(ObjectSerializer.JsonToObject <List <MessageVm> >(attachmentPayload));
            }

            case AttachmentType.KeyMessage:
            {
                return(ObjectSerializer.JsonToObject <KeyExchangeMessageVm>(attachmentPayload));
            }

            case AttachmentType.Poll:
            {
                try
                {
                    PollAttachmentInformation pollInfo =
                        ObjectSerializer.JsonToObject <PollAttachmentInformation>(attachmentPayload);
                    PollDto pollDto = await AppServiceProvider.Instance.PollsService.GetPollAsync(pollInfo.PollId, pollInfo.ConversationId, pollInfo.ConversationType).ConfigureAwait(false);

                    if (requestorId != null)
                    {
                        return(PollConverter.GetPollVm(pollDto, requestorId.Value));
                    }
                    else
                    {
                        return(PollConverter.GetPollVm(pollDto, pollDto.CreatorId));
                    }
                }
                catch (Exception ex)
                {
                    Logger.WriteLog(ex);
                    return(null);
                }
            }

            case AttachmentType.SystemMessage:
            {
                try
                {
                    return(ObjectSerializer.JsonToObject <SystemMessageInfo>(attachmentPayload));
                }
                catch (Exception ex)
                {
                    Logger.WriteLog(ex);
                    return(null);
                }
            }

            default:
                return(attachmentPayload);
            }
        }