public async Task <Response> CreateResponseAsync() { if (!await conversationsService.IsUserInConversationAsync(request.ConversationType, request.ConversationId, clientConnection.UserId.Value)) { return(new ResultResponse(request.RequestId, "The user is not in conversation.", ErrorCode.PermissionDenied)); } var nodesIds = await conversationsService.GetConversationNodesIdsAsync(request.ConversationType, request.ConversationId).ConfigureAwait(false); if (nodesIds.Count > 1) { long?dialogUserId = null; if (request.ConversationType == ConversationType.Dialog) { var users = await loadDialogsService.GetDialogUsersAsync(request.ConversationId).ConfigureAwait(false); dialogUserId = users.FirstOrDefault(user => user.Id != clientConnection.UserId).Id; } nodeNoticeService.SendConverationActionNodeNoticeAsync(clientConnection.UserId.Value, dialogUserId, request.ConversationId, request.ConversationType, request.Action, nodesIds); } if (request.Action != ConversationAction.Screenshot) { conversationsNoticeService.SendConversationActionNoticeAsync(clientConnection.UserId.Value, request.ConversationType, request.ConversationId, request.Action); } if (request.ConversationType != ConversationType.Channel && request.Action == ConversationAction.Screenshot) { var systemMessageInfo = SystemMessageInfoFactory.CreateScreenshotMessageInfo(clientConnection.UserId.Value); var message = await systemMessagesService.CreateMessageAsync(request.ConversationType, request.ConversationId, systemMessageInfo).ConfigureAwait(false); conversationsNoticeService.SendSystemMessageNoticeAsync(message); } return(new ResultResponse(request.RequestId)); }
public async Task HandleAsync() { if (!await conversationsService.IsUserInConversationAsync(notice.ConversationType, notice.ConversationId, notice.UserId) && notice.DialogUserId == null) { return; } if (notice.ConversationType == ConversationType.Dialog) { var dialogs = await loadDialogsService.GetUsersDialogsAsync(notice.DialogUserId.Value, notice.UserId).ConfigureAwait(false); var senderDialog = dialogs.FirstOrDefault(opt => opt.FirstUserId == notice.UserId); if (notice.Action != ConversationAction.Screenshot) { conversationsNoticeService.SendConversationActionNoticeAsync(notice.UserId, ConversationType.Dialog, senderDialog.Id, notice.Action); } } else { if (notice.Action != ConversationAction.Screenshot) { conversationsNoticeService.SendConversationActionNoticeAsync(notice.UserId, notice.ConversationType, notice.ConversationId, notice.Action); } } if (notice.ConversationType != ConversationType.Channel && notice.Action == ConversationAction.Screenshot) { var systemMessageInfo = SystemMessageInfoFactory.CreateScreenshotMessageInfo(notice.UserId); var message = await systemMessagesService.CreateMessageAsync(notice.ConversationType, notice.ConversationId, systemMessageInfo).ConfigureAwait(false); conversationsNoticeService.SendSystemMessageNoticeAsync(message); } }
public async Task IsUserInConversation() { var chat = fillTestDbHelper.Chats.FirstOrDefault(); Assert.True(await conversationsService.IsUserInConversationAsync(ConversationType.Chat, chat.Id, chat.ChatUsers.FirstOrDefault().UserId)); Assert.False(await conversationsService.IsUserInConversationAsync(ConversationType.Chat, chat.Id, long.MaxValue)); var channel = fillTestDbHelper.Channels.FirstOrDefault(); Assert.True(await conversationsService.IsUserInConversationAsync(ConversationType.Channel, channel.ChannelId, channel.ChannelUsers.FirstOrDefault().UserId)); Assert.False(await conversationsService.IsUserInConversationAsync(ConversationType.Channel, channel.ChannelId, long.MaxValue)); var dialog = fillTestDbHelper.Dialogs.FirstOrDefault(); Assert.True(await conversationsService.IsUserInConversationAsync(ConversationType.Dialog, dialog.Id, dialog.FirstUID)); Assert.False(await conversationsService.IsUserInConversationAsync(ConversationType.Dialog, dialog.Id, long.MaxValue)); }
public async Task <PollDto> VotePollAsync(Guid pollId, long conversationId, ConversationType conversationType, List <PollVoteVm> options, long votingUserId) { if (!await conversationsService.IsUserInConversationAsync(conversationType, conversationId, votingUserId).ConfigureAwait(false)) { throw new PermissionDeniedException("User is not in conversation."); } using (MessengerDbContext context = contextFactory.Create()) { var poll = await context.Polls .Include(opt => opt.Options) .ThenInclude(opt => opt.PollOptionVotes) .FirstOrDefaultAsync(opt => opt.PollId == pollId && opt.ConvertsationId == conversationId && opt.ConversationType == conversationType) .ConfigureAwait(false); if (poll == null) { throw new ObjectDoesNotExistsException($"Poll not found (Poll Id: {pollId}, {conversationType.ToString()}Id: {conversationId})."); } if (options == null || !options.Any()) { foreach (var option in poll.Options) { option.PollOptionVotes = option.PollOptionVotes.Where(vote => vote.UserId != votingUserId).ToList(); } } List <PollOptionVote> newVotes = new List <PollOptionVote>(); List <PollOptionVote> removedVotes = new List <PollOptionVote>(); foreach (var voteOption in options) { var option = poll.Options.FirstOrDefault(opt => opt.OptionId == voteOption.OptionId); if (option != null && voteOption.Sign != null && voteOption.Sign.Data != null) { if (!option.PollOptionVotes.Any(vote => vote.UserId == votingUserId)) { if (poll.SignRequired) { if (voteOption.Sign == null || voteOption.Sign.Data == null || !voteOption.Sign.Data.Any()) { throw new InvalidSignException($"Vote №{voteOption.OptionId} is not signed."); } var signKey = await context.Keys .FirstOrDefaultAsync(key => key.KeyId == voteOption.Sign.KeyId && key.Type == KeyType.SignAsymKey) .ConfigureAwait(false); if (signKey == null) { throw new ObjectDoesNotExistsException($"Key not found (Key Id: {voteOption.Sign.KeyId}."); } } newVotes.Add(new PollOptionVote { ConversationId = conversationId, ConversationType = conversationType, OptionId = voteOption.OptionId, UserId = votingUserId, PollId = pollId, Sign = voteOption.Sign?.Data, SignKeyId = voteOption.Sign?.KeyId }); } else { removedVotes.Add(option.PollOptionVotes .FirstOrDefault(opt => opt.UserId == votingUserId && opt.OptionId == voteOption.OptionId)); } } else { if (!option.PollOptionVotes.Any(vote => vote.UserId == votingUserId)) { newVotes.Add(new PollOptionVote { ConversationId = conversationId, ConversationType = conversationType, OptionId = voteOption.OptionId, UserId = votingUserId, PollId = pollId }); } else { removedVotes.Add(option.PollOptionVotes .FirstOrDefault(opt => opt.UserId == votingUserId && opt.OptionId == voteOption.OptionId)); } } foreach (var vote in newVotes) { option.PollOptionVotes.Add(vote); } foreach (var vote in removedVotes) { option.PollOptionVotes.Remove(vote); } } if (!poll.MultipleSelection && poll.Options.Count(opt => opt.PollOptionVotes.Any(vote => vote.UserId == votingUserId)) > 1) { throw new InvalidOperationException("Multiple voting is not allowed in current poll."); } context.Polls.Update(poll); await context.SaveChangesAsync().ConfigureAwait(false); return(PollConverter.GetPollDto(poll)); } }
public async Task <List <MessageDto> > SearchMessagesAsync( string query, ConversationType?conversationType, long?conversationId, ConversationType?navConversationType, long?navConversationId, Guid?navMessageId, long?userId, int limit = 30) { using (MessengerDbContext context = contextFactory.Create()) { var messagesCondition = PredicateBuilder.New <Message>(); var messagesQuery = context.Messages.AsNoTracking(); ExpressionsHelper expressionsHelper = new ExpressionsHelper(); var messageExpression = expressionsHelper.GetMessageExpression(query); if (conversationType != null && conversationId != null) { if (userId != null && !await conversationsService.IsUserInConversationAsync(conversationType.Value, conversationId.Value, userId.Value).ConfigureAwait(false)) { return(new List <MessageDto>()); } switch (conversationType.Value) { case ConversationType.Dialog: messagesQuery = messagesQuery.Where(message => message.DialogId == conversationId); break; case ConversationType.Chat: messagesQuery = messagesQuery.Where(message => message.ChatId == conversationId); break; case ConversationType.Channel: messagesQuery = messagesQuery.Where(message => message.ChannelId == conversationId); break; } } else { if (userId != null) { var dialogsIds = await loadDialogsService.GetUserDialogsIdAsync(userId.Value).ConfigureAwait(false); var chatsIds = await loadChatsService.GetUserChatsIdAsync(userId.Value).ConfigureAwait(false); var channelsIds = await loadChannelsService.GetUserChannelsIdAsync(userId.Value).ConfigureAwait(false); messagesCondition = dialogsIds.Aggregate(messagesCondition, (current, value) => current.Or(option => option.DialogId == value).Expand()); messagesCondition = chatsIds.Aggregate(messagesCondition, (current, value) => current.Or(option => option.ChatId == value).Expand()); messagesCondition = channelsIds.Aggregate(messagesCondition, (current, value) => current.Or(option => option.ChannelId == value).Expand()); messagesQuery = messagesQuery.Where(messagesCondition); } } if (navConversationId != null && navConversationType != null && navMessageId != null) { var navMessage = (await GetMessagesByIdAsync( new List <Guid> { navMessageId.Value }, navConversationType.Value, navConversationId.Value, userId).ConfigureAwait(false)).FirstOrDefault(); if (navMessage != null) { messagesQuery = messagesQuery .Where(message => message.SendingTime <= navMessage.SendingTime && message.GlobalId != navMessage.GlobalId); } } var messages = await messagesQuery .Where(messageExpression) .Where(message => !message.Deleted && (message.ExpiredAt == null || message.ExpiredAt > DateTime.UtcNow.ToUnixTime())) .OrderByDescending(message => message.SendingTime) .Take(limit) .Include(message => message.Attachments) .ToListAsync() .ConfigureAwait(false); return(MessageConverter.GetMessagesDto(messages)); } }