|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Discord; |
| 7 | +using Microsoft.EntityFrameworkCore; |
| 8 | +using Modix.Data; |
| 9 | +using Modix.Data.Models.Core; |
| 10 | +using Modix.Services.Core; |
| 11 | + |
| 12 | +namespace Modix.Services; |
| 13 | + |
| 14 | +public class DesignatedChannelService( |
| 15 | + ModixContext db, |
| 16 | + IAuthorizationService authorizationService) |
| 17 | +{ |
| 18 | + public async Task<long> AddDesignatedChannel(IGuild guild, IMessageChannel logChannel, |
| 19 | + DesignatedChannelType type) |
| 20 | + { |
| 21 | + authorizationService.RequireAuthenticatedUser(); |
| 22 | + authorizationService.RequireClaims(AuthorizationClaim.DesignatedChannelMappingCreate); |
| 23 | + |
| 24 | + var designationId = await db |
| 25 | + .Set<DesignatedChannelMappingEntity>() |
| 26 | + .Where(x => x.GuildId == guild.Id) |
| 27 | + .Where(x => x.ChannelId == logChannel.Id) |
| 28 | + .Where(x => x.Type == type) |
| 29 | + .Where(x => x.DeleteActionId == null) |
| 30 | + .Select(x => (long?)x.Id) |
| 31 | + .SingleOrDefaultAsync(); |
| 32 | + |
| 33 | + if (designationId is not null) |
| 34 | + { |
| 35 | + return designationId.Value; |
| 36 | + } |
| 37 | + |
| 38 | + var newEntity = new DesignatedChannelMappingEntity |
| 39 | + { |
| 40 | + GuildId = guild.Id, |
| 41 | + ChannelId = logChannel.Id, |
| 42 | + Type = type, |
| 43 | + CreateAction = new ConfigurationActionEntity |
| 44 | + { |
| 45 | + GuildId = guild.Id, |
| 46 | + Type = ConfigurationActionType.DesignatedChannelMappingCreated, |
| 47 | + Created = DateTimeOffset.UtcNow, |
| 48 | + CreatedById = authorizationService.CurrentUserId!.Value, |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + db.Add(newEntity); |
| 53 | + await db.SaveChangesAsync(); |
| 54 | + |
| 55 | + return newEntity.Id; |
| 56 | + } |
| 57 | + |
| 58 | + public async Task RemoveDesignatedChannel(IGuild guild, IMessageChannel logChannel, DesignatedChannelType type) |
| 59 | + { |
| 60 | + var designationId = await db |
| 61 | + .Set<DesignatedChannelMappingEntity>() |
| 62 | + .Where(x => x.GuildId == guild.Id) |
| 63 | + .Where(x => x.ChannelId == logChannel.Id) |
| 64 | + .Where(x => x.Type == type) |
| 65 | + .Where(x => x.DeleteActionId == null) |
| 66 | + .Select(x => (long?)x.Id) |
| 67 | + .SingleOrDefaultAsync(); |
| 68 | + |
| 69 | + if (designationId is null) |
| 70 | + { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + await RemoveDesignatedChannelById(designationId.Value); |
| 75 | + } |
| 76 | + |
| 77 | + public async Task RemoveDesignatedChannelById(long designationId) |
| 78 | + { |
| 79 | + authorizationService.RequireAuthenticatedUser(); |
| 80 | + authorizationService.RequireClaims(AuthorizationClaim.DesignatedChannelMappingDelete); |
| 81 | + |
| 82 | + var designation = await db |
| 83 | + .Set<DesignatedChannelMappingEntity>() |
| 84 | + .Where(x => x.Id == designationId) |
| 85 | + .SingleAsync(); |
| 86 | + |
| 87 | + var deleteAction = new ConfigurationActionEntity |
| 88 | + { |
| 89 | + Type = ConfigurationActionType.DesignatedChannelMappingDeleted, |
| 90 | + Created = DateTimeOffset.UtcNow, |
| 91 | + CreatedById = authorizationService.CurrentUserId!.Value, |
| 92 | + DesignatedChannelMappingId = designation.Id, |
| 93 | + GuildId = designation.GuildId, |
| 94 | + }; |
| 95 | + |
| 96 | + db.Add(deleteAction); |
| 97 | + await db.SaveChangesAsync(); |
| 98 | + } |
| 99 | + |
| 100 | + public async Task<bool> HasDesignatedChannelForType(ulong guildId, DesignatedChannelType type) |
| 101 | + { |
| 102 | + return await db |
| 103 | + .Set<DesignatedChannelMappingEntity>() |
| 104 | + .Where(x => x.GuildId == guildId) |
| 105 | + .Where(x => x.Type == type) |
| 106 | + .Where(x => x.DeleteActionId == null) |
| 107 | + .AnyAsync(); |
| 108 | + } |
| 109 | + |
| 110 | + public async Task<IReadOnlyCollection<ulong>> GetDesignatedChannelIds(ulong guildId, DesignatedChannelType type) |
| 111 | + { |
| 112 | + return await db |
| 113 | + .Set<DesignatedChannelMappingEntity>() |
| 114 | + .Where(x => x.GuildId == guildId) |
| 115 | + .Where(x => x.Type == type) |
| 116 | + .Select(x => x.ChannelId) |
| 117 | + .ToListAsync(); |
| 118 | + } |
| 119 | + |
| 120 | + public async Task<IReadOnlyCollection<IMessageChannel>> GetDesignatedChannels(IGuild guild, |
| 121 | + DesignatedChannelType type) |
| 122 | + { |
| 123 | + var channelIds = await GetDesignatedChannelIds(guild.Id, type); |
| 124 | + |
| 125 | + if (!channelIds.Any()) |
| 126 | + { |
| 127 | + return []; |
| 128 | + } |
| 129 | + |
| 130 | + var channels = await Task.WhenAll(channelIds.Select(d => guild.GetChannelAsync(d))); |
| 131 | + |
| 132 | + return channels |
| 133 | + .OfType<IMessageChannel>() |
| 134 | + .ToArray(); |
| 135 | + } |
| 136 | + |
| 137 | + public async Task<IReadOnlyCollection<DesignatedChannelMappingBrief>> GetDesignatedChannels(ulong guildId) |
| 138 | + { |
| 139 | + authorizationService.RequireClaims(AuthorizationClaim.DesignatedChannelMappingRead); |
| 140 | + |
| 141 | + return await db |
| 142 | + .Set<DesignatedChannelMappingEntity>() |
| 143 | + .Where(x => x.GuildId == guildId) |
| 144 | + .Where(x => x.DeleteActionId == null) |
| 145 | + .Select(x => new DesignatedChannelMappingBrief |
| 146 | + { |
| 147 | + Id = x.Id, |
| 148 | + Channel = new GuildChannelBrief |
| 149 | + { |
| 150 | + Id = x.ChannelId, |
| 151 | + Name = x.Channel.Name, |
| 152 | + ParentChannelId = x.Channel.ParentChannelId, |
| 153 | + }, |
| 154 | + Type = x.Type, |
| 155 | + }).ToListAsync(); |
| 156 | + } |
| 157 | + |
| 158 | + public async Task<bool> ChannelHasDesignation( |
| 159 | + ulong guildId, |
| 160 | + ulong channelId, |
| 161 | + DesignatedChannelType type, |
| 162 | + CancellationToken cancellationToken) |
| 163 | + { |
| 164 | + return await db |
| 165 | + .Set<DesignatedChannelMappingEntity>() |
| 166 | + .Where(x => x.GuildId == guildId) |
| 167 | + .Where(x => x.ChannelId == channelId) |
| 168 | + .Where(x => x.Type == type) |
| 169 | + .Where(x => x.DeleteActionId == null) |
| 170 | + .AnyAsync(cancellationToken: cancellationToken); |
| 171 | + } |
| 172 | +} |
0 commit comments