Ejemplo n.º 1
0
        public async Task CanUserGetDialogMessage()
        {
            var dialog  = fillTestDbHelper.Dialogs.FirstOrDefault();
            var user    = fillTestDbHelper.Users.FirstOrDefault(opt => opt.Id == dialog.FirstUID);
            var badUser = fillTestDbHelper.Users.FirstOrDefault(opt => opt.Id != dialog.FirstUID && opt.Id != dialog.SecondUID);

            Assert.True(await loadMessagesService.CanUserGetMessageAsync(ConversationType.Dialog, dialog.Id, user.Id));
            Assert.False(await loadMessagesService.CanUserGetMessageAsync(ConversationType.Dialog, dialog.Id, badUser.Id));
        }
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));
        }