2024-10-20 15:20:22 +02:00
|
|
|
// Copyright (C) 2021-present sam (starshines.gay)
|
|
|
|
|
//
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
|
// by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
2024-10-29 14:19:18 +01:00
|
|
|
using System.Net;
|
|
|
|
|
using Catalogger.Backend.Api.Middleware;
|
2024-10-20 15:20:22 +02:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Remora.Discord.API;
|
|
|
|
|
using Remora.Discord.API.Abstractions.Objects;
|
|
|
|
|
|
|
|
|
|
namespace Catalogger.Backend.Api;
|
|
|
|
|
|
|
|
|
|
public partial class GuildsController
|
|
|
|
|
{
|
|
|
|
|
[HttpPut("ignored-channels/{channelId}")]
|
|
|
|
|
public async Task<IActionResult> AddIgnoredChannelAsync(string id, ulong channelId)
|
|
|
|
|
{
|
|
|
|
|
var (guildId, _) = await ParseGuildAsync(id);
|
2024-10-28 14:04:55 +01:00
|
|
|
var guildConfig = await guildRepository.GetAsync(guildId);
|
2024-10-20 15:20:22 +02:00
|
|
|
|
2024-11-18 00:47:27 +01:00
|
|
|
if (guildConfig.Messages.IgnoredChannels.Contains(channelId))
|
2024-10-20 15:20:22 +02:00
|
|
|
return NoContent();
|
|
|
|
|
|
|
|
|
|
var channel = channelCache
|
|
|
|
|
.GuildChannels(guildId)
|
|
|
|
|
.FirstOrDefault(c =>
|
|
|
|
|
c.ID.Value == channelId
|
|
|
|
|
&& c.Type
|
|
|
|
|
is ChannelType.GuildText
|
|
|
|
|
or ChannelType.GuildCategory
|
|
|
|
|
or ChannelType.GuildAnnouncement
|
|
|
|
|
or ChannelType.GuildForum
|
|
|
|
|
or ChannelType.GuildMedia
|
|
|
|
|
or ChannelType.GuildVoice
|
|
|
|
|
);
|
|
|
|
|
if (channel == null)
|
|
|
|
|
return NoContent();
|
|
|
|
|
|
2024-11-18 00:47:27 +01:00
|
|
|
guildConfig.Messages.IgnoredChannels.Add(channelId);
|
|
|
|
|
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig);
|
2024-10-20 15:20:22 +02:00
|
|
|
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpDelete("ignored-channels/{channelId}")]
|
|
|
|
|
public async Task<IActionResult> RemoveIgnoredChannelAsync(string id, ulong channelId)
|
|
|
|
|
{
|
|
|
|
|
var (guildId, _) = await ParseGuildAsync(id);
|
2024-10-28 14:04:55 +01:00
|
|
|
var guildConfig = await guildRepository.GetAsync(guildId);
|
2024-10-20 15:20:22 +02:00
|
|
|
|
2024-11-18 00:47:27 +01:00
|
|
|
guildConfig.Messages.IgnoredChannels.Remove(channelId);
|
|
|
|
|
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig);
|
2024-10-20 15:20:22 +02:00
|
|
|
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-29 14:19:18 +01:00
|
|
|
[HttpPut("key-roles/{roleId}")]
|
|
|
|
|
public async Task<IActionResult> AddKeyRoleAsync(string id, ulong roleId)
|
|
|
|
|
{
|
|
|
|
|
var (guildId, _) = await ParseGuildAsync(id);
|
|
|
|
|
var guildConfig = await guildRepository.GetAsync(guildId);
|
|
|
|
|
|
|
|
|
|
if (roleCache.GuildRoles(guildId).All(r => r.ID.Value != roleId))
|
|
|
|
|
throw new ApiError(HttpStatusCode.BadRequest, ErrorCode.BadRequest, "Role not found");
|
|
|
|
|
|
|
|
|
|
if (guildConfig.KeyRoles.Contains(roleId))
|
|
|
|
|
throw new ApiError(
|
|
|
|
|
HttpStatusCode.BadRequest,
|
|
|
|
|
ErrorCode.BadRequest,
|
|
|
|
|
"Role is already a key role"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await guildRepository.AddKeyRoleAsync(guildId, DiscordSnowflake.New(roleId));
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpDelete("key-roles/{roleId}")]
|
|
|
|
|
public async Task<IActionResult> RemoveKeyRoleAsync(string id, ulong roleId)
|
|
|
|
|
{
|
|
|
|
|
var (guildId, _) = await ParseGuildAsync(id);
|
|
|
|
|
var guildConfig = await guildRepository.GetAsync(guildId);
|
|
|
|
|
|
|
|
|
|
if (!guildConfig.KeyRoles.Contains(roleId))
|
|
|
|
|
throw new ApiError(
|
|
|
|
|
HttpStatusCode.BadRequest,
|
|
|
|
|
ErrorCode.BadRequest,
|
|
|
|
|
"Role is already not a key role"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await guildRepository.RemoveKeyRoleAsync(guildId, DiscordSnowflake.New(roleId));
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
2024-10-20 15:20:22 +02:00
|
|
|
}
|