feat(dashboard): add redirects page
This commit is contained in:
parent
32ddb9fae2
commit
cb425fe3cd
7 changed files with 400 additions and 62 deletions
92
Catalogger.Backend/Api/GuildsController.Redirects.cs
Normal file
92
Catalogger.Backend/Api/GuildsController.Redirects.cs
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
// 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/>.
|
||||
|
||||
using System.Net;
|
||||
using Catalogger.Backend.Api.Middleware;
|
||||
using Catalogger.Backend.Database.Queries;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Remora.Discord.API.Abstractions.Objects;
|
||||
|
||||
namespace Catalogger.Backend.Api;
|
||||
|
||||
public partial class GuildsController
|
||||
{
|
||||
[HttpPost("redirects")]
|
||||
public async Task<IActionResult> CreateRedirectAsync(
|
||||
string id,
|
||||
[FromBody] CreateRedirectRequest req
|
||||
)
|
||||
{
|
||||
var (guildId, _) = await ParseGuildAsync(id);
|
||||
var guildChannels = channelCache.GuildChannels(guildId).ToList();
|
||||
var guildConfig = await db.GetGuildAsync(guildId.Value);
|
||||
|
||||
Console.WriteLine($"Source: {req.Source}, target: {req.Target}");
|
||||
|
||||
var source = guildChannels.FirstOrDefault(c =>
|
||||
c.ID.Value == req.Source
|
||||
&& c.Type
|
||||
is ChannelType.GuildText
|
||||
or ChannelType.GuildCategory
|
||||
or ChannelType.GuildAnnouncement
|
||||
or ChannelType.GuildForum
|
||||
or ChannelType.GuildMedia
|
||||
or ChannelType.GuildVoice
|
||||
);
|
||||
if (source == null)
|
||||
throw new ApiError(
|
||||
HttpStatusCode.BadRequest,
|
||||
ErrorCode.BadRequest,
|
||||
"Unknown source channel ID or it's not a valid source"
|
||||
);
|
||||
var target = guildChannels.FirstOrDefault(c =>
|
||||
c.ID.Value == req.Target && c.Type is ChannelType.GuildText
|
||||
);
|
||||
if (target == null)
|
||||
throw new ApiError(
|
||||
HttpStatusCode.BadRequest,
|
||||
ErrorCode.BadRequest,
|
||||
"Unknown target channel ID or it's not a valid target"
|
||||
);
|
||||
|
||||
guildConfig.Channels.Redirects[source.ID.Value] = target.ID.Value;
|
||||
db.Update(guildConfig);
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpDelete("redirects/{channelId}")]
|
||||
public async Task<IActionResult> DeleteRedirectAsync(string id, ulong channelId)
|
||||
{
|
||||
var (guildId, _) = await ParseGuildAsync(id);
|
||||
var guildConfig = await db.GetGuildAsync(guildId.Value);
|
||||
|
||||
if (!guildConfig.Channels.Redirects.ContainsKey(channelId))
|
||||
throw new ApiError(
|
||||
HttpStatusCode.BadRequest,
|
||||
ErrorCode.BadRequest,
|
||||
"That channel is already not being redirected"
|
||||
);
|
||||
|
||||
guildConfig.Channels.Redirects.Remove(channelId, out _);
|
||||
db.Update(guildConfig);
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
public record CreateRedirectRequest(ulong Source, ulong Target);
|
||||
}
|
||||
|
|
@ -26,7 +26,7 @@ using Remora.Rest.Core;
|
|||
namespace Catalogger.Backend.Api;
|
||||
|
||||
[Route("/api/guilds/{id}")]
|
||||
public class GuildsController(
|
||||
public partial class GuildsController(
|
||||
Config config,
|
||||
DatabaseContext db,
|
||||
ChannelCache channelCache,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue