public async Task <ChannelDto> HandleAsync(GetGroupChannelQuery query) { var group = await _context.Groups.AsNoTracking() .Where(g => g.Id == GroupId.From(query.GroupId)) .Where(g => g.Members.Select(m => m.UserId).Any(id => id == UserId.From(query.UserId))) .FirstOrDefaultAsync(); if (group is null) { return(null); } var channels = await _context.Channels.AsNoTracking() .Where( x => _context.Groups.Where(g => g.Id == query.GroupId).SelectMany(g => g.Members).Select(m => m.UserId) .Any(u => u == UserId.From(query.UserId)) ) .Where(x => x.GroupId == GroupId.From(query.GroupId)) .Where(x => x.Id == ChannelId.From(query.ChannelId)) .ToListAsync(); return(channels.Where(c => group.CanAccessChannel(UserId.From(query.UserId), c)) .Select( x => new ChannelDto { ChannelId = x.Id.Value, GroupId = x.GroupId.Value, ChannelName = x.Name.Value } ).FirstOrDefault()); }
public async Task <IEnumerable <GroupMessageView> > HandleAsync(GetMessagesQuery query) { var group = await _groupsContext.Groups.AsNoTracking() .Where(g => g.Members.Select(m => m.UserId).Any(id => id == UserId.From(query.UserId))) .FirstOrDefaultAsync(g => g.Id == GroupId.From(query.GroupId)); var channel = await _groupsContext.Channels.AsNoTracking().FirstOrDefaultAsync( c => c.GroupId == GroupId.From(query.GroupId) && c.Id == ChannelId.From(query.ChannelId) ); if (group is null || channel is null) { return(new List <GroupMessageView>()); } if (!group.CanAccessMessages(UserId.From(query.UserId), channel)) { return(new List <GroupMessageView>()); } var groupMessageDtos = await _groupsContext.GroupMessages .Where(m => m.GroupId == query.GroupId && m.ChannelId == query.ChannelId) .ToListAsync(); return(groupMessageDtos); }
public async Task HandleAsync(DenyPermissionForRoleCommand command) { var group = await _groupRepository.GetAsync(GroupId.From(command.GroupId)) ?? throw new GroupNotFoundException(GroupId.From(command.GroupId)); var channel = await _channelRepository.GetAsync(group.Id, ChannelId.From(command.ChannelId)) ?? throw new ChannelNotFoundException(ChannelId.From(command.ChannelId)); group.DenyPermission(UserId.From(command.UserId), channel, RoleId.From(command.RoleId), Permission.FromName(command.Permission)); }
public async Task HandleAsync(RemoveOverrideForMemberCommand command) { var group = await _groupRepository.GetAsync(GroupId.From(command.GroupId)) ?? throw new GroupNotFoundException(GroupId.From(command.GroupId)); var channel = await _channelRepository.GetAsync(group.Id, ChannelId.From(command.ChannelId)); group.RemoveOverride(UserId.From(command.UserId), channel, UserId.From(command.MemberUserId), Permission.FromName(command.Permission)); }
public async Task HandleAsync(UpdateRolePermissionOverrideCommand command) { var group = await _groupRepository.GetAsync(GroupId.From(command.GroupId)) ?? throw new GroupNotFoundException(GroupId.From(command.GroupId)); var channel = await _channelRepository.GetAsync(group.Id, ChannelId.From(command.ChannelId)) ?? throw new ChannelNotFoundException(ChannelId.From(command.ChannelId)); group.UpdateOverrides(UserId.From(command.UserId), channel, RoleId.From(command.RoleId), command.Overrides.Select(o => (Permission.FromName(o.Permission), (PermissionOverrideType)o.Type))); }
public async Task HandleAsync(RenameChannelCommand command) { var group = await _groupRepository.GetAsync(GroupId.From(command.GroupId)) ?? throw new GroupNotFoundException(GroupId.From(command.GroupId)); var channel = await _channelRepository.GetAsync(group.Id, ChannelId.From(command.ChannelId)) ?? throw new ChannelNotFoundException(ChannelId.From(command.ChannelId)); var name = ChannelName.Create(command.Name); group.RenameChannel(UserId.From(command.UserId), channel, name); }
public async Task HandleAsync(CreateChannelCommand command) { var groupId = GroupId.From(command.GroupId); var group = await _groupRepository.GetAsync(groupId) ?? throw new GroupNotFoundException(groupId); if (await _channelRepository.ExistsAsync(group.Id, ChannelId.From(command.ChannelId))) { throw new ChannelAlreadyExistsException(command.ChannelId); } var channel = group.CreateChannel(UserId.From(command.UserId), ChannelId.From(command.ChannelId), ChannelName.Create(command.ChannelName)); await _channelRepository.AddAsync(channel); }
public async Task HandleAsync(SendMessageCommand command) { var group = await _groupRepository.GetAsync(GroupId.From(command.GroupId)) ?? throw new GroupNotFoundException(GroupId.From(command.GroupId)); var channel = await _channelRepository.GetAsync(group.Id, ChannelId.From(command.ChannelId)) ?? throw new ChannelNotFoundException(ChannelId.From(command.ChannelId)); if (await _messageRepository.ExistsAsync(ChannelId.From(command.ChannelId), MessageId.From(command.MessageId))) { throw new MessageAlreadyExistsException(command.MessageId); } var message = group.SendMessage(UserId.From(command.UserId), channel, MessageId.From(command.MessageId), MessageContent.Create(command.Content), _clock); await _messageRepository.AddAsync(message); }
public async Task <IEnumerable <PermissionOverrideDto> > HandleAsync(GetChannelRolePermissionOverridesQuery query) { return(await _context.Channels .AsNoTracking() .Where(x => x.GroupId == GroupId.From(query.GroupId)) .Where(x => x.Id == ChannelId.From(query.ChannelId)) .SelectMany(x => x.RolePermissionOverrides) .Where(x => x.RoleId == RoleId.From(query.RoleId)) .Select( x => new PermissionOverrideDto { Permission = x.Permission.Name, Type = x.Type == OverrideType.Allow ? OverrideTypeDto.Allow : OverrideTypeDto.Deny } ).ToListAsync()); }
public async Task HandleAsync(ChannelCreatedEvent @event) { var group = await _groupRepository.GetAsync(GroupId.From(@event.GroupId)); var channel = await _channelRepository.GetAsync(GroupId.From(@event.GroupId), ChannelId.From(@event.ChannelId)); var members = group.Members; var allowedMembers = members.Where(m => group.CanAccessChannel(m.UserId, channel)).Select(m => m.UserId.Value.ToString("N")).ToList(); await _hubContext.Clients.Users(allowedMembers) .OnChannelCreated(new ChannelDto(@event.GroupId, @event.ChannelId, @event.ChannelName)); }
public async Task HandleAsync(MessageCreatedEvent @event) { var group = await _context.Groups.AsNoTracking() .Where(g => g.Id == GroupId.From(@event.GroupId)) .FirstOrDefaultAsync(); var channel = await _context.Channels.AsNoTracking() .Where(c => c.GroupId == GroupId.From(@event.GroupId) && c.Id == ChannelId.From(@event.ChannelId)) .FirstOrDefaultAsync(); var message = await _context.GroupMessages .AsNoTracking() .Where(x => x.GroupId == GroupId.From(@event.GroupId)) .Where(x => x.ChannelId == @event.ChannelId) .Where(x => x.MessageId == MessageId.From(@event.MessageId)) .FirstOrDefaultAsync(); if (message is null || group is null || channel is null) { return; } var allowedMembers = group.Members .Where(m => group.CanAccessMessages(m.UserId, channel)) .Select(m => m.UserId.Value.ToString("N")) .ToList(); await _hubContext.Clients.Users(allowedMembers).OnMessageCreated(message); }