2024-10-14 14:56:40 +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-08-19 16:12:28 +02:00
|
|
|
using Catalogger.Backend.Database.Redis;
|
|
|
|
|
using Remora.Discord.API;
|
|
|
|
|
using Remora.Discord.API.Abstractions.Objects;
|
|
|
|
|
using Remora.Discord.API.Objects;
|
|
|
|
|
using Remora.Rest.Core;
|
|
|
|
|
|
|
|
|
|
namespace Catalogger.Backend.Cache.RedisCache;
|
|
|
|
|
|
|
|
|
|
public class RedisInviteCache(RedisService redisService) : IInviteCache
|
|
|
|
|
{
|
2024-10-09 22:31:58 +02:00
|
|
|
public async Task<IEnumerable<IInviteWithMetadata>> TryGetAsync(Snowflake guildId)
|
2024-08-19 16:12:28 +02:00
|
|
|
{
|
2024-10-09 17:35:11 +02:00
|
|
|
var redisInvites =
|
|
|
|
|
await redisService.GetAsync<List<RedisInvite>>(InvitesKey(guildId)) ?? [];
|
2024-08-19 16:12:28 +02:00
|
|
|
return redisInvites.Select(r => r.ToRemoraInvite());
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-09 22:31:58 +02:00
|
|
|
public async Task SetAsync(Snowflake guildId, IEnumerable<IInviteWithMetadata> invites) =>
|
2024-08-19 16:12:28 +02:00
|
|
|
await redisService.SetAsync(InvitesKey(guildId), invites.Select(RedisInvite.FromIInvite));
|
|
|
|
|
|
|
|
|
|
private static string InvitesKey(Snowflake guildId) => $"guild-invites:{guildId}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal record RedisInvite(
|
|
|
|
|
string Code,
|
|
|
|
|
RedisPartialGuild? Guild,
|
|
|
|
|
RedisPartialChannel? Channel,
|
2024-10-09 22:31:58 +02:00
|
|
|
int Uses,
|
|
|
|
|
int MaxUses,
|
|
|
|
|
TimeSpan MaxAge,
|
|
|
|
|
bool IsTemporary,
|
|
|
|
|
DateTimeOffset CreatedAt,
|
2024-08-19 16:12:28 +02:00
|
|
|
RedisUser? Inviter,
|
2024-10-09 17:35:11 +02:00
|
|
|
DateTimeOffset? ExpiresAt
|
|
|
|
|
)
|
2024-08-19 16:12:28 +02:00
|
|
|
{
|
2024-10-09 22:31:58 +02:00
|
|
|
public static RedisInvite FromIInvite(IInviteWithMetadata invite) =>
|
2024-10-09 17:35:11 +02:00
|
|
|
new(
|
|
|
|
|
invite.Code,
|
|
|
|
|
invite.Guild.Map(RedisPartialGuild.FromIPartialGuild).OrDefault(),
|
|
|
|
|
invite.Channel != null ? RedisPartialChannel.FromIPartialChannel(invite.Channel) : null,
|
2024-10-09 22:31:58 +02:00
|
|
|
invite.Uses,
|
|
|
|
|
invite.MaxUses,
|
|
|
|
|
invite.MaxAge,
|
|
|
|
|
invite.IsTemporary,
|
|
|
|
|
invite.CreatedAt,
|
2024-10-09 17:35:11 +02:00
|
|
|
invite.Inviter.Map(RedisUser.FromIUser).OrDefault(),
|
|
|
|
|
invite.ExpiresAt.OrDefault()
|
|
|
|
|
);
|
2024-08-19 16:12:28 +02:00
|
|
|
|
2024-10-09 22:31:58 +02:00
|
|
|
public InviteWithMetadata ToRemoraInvite() =>
|
2024-10-09 17:35:11 +02:00
|
|
|
new(
|
|
|
|
|
Code,
|
|
|
|
|
Guild?.ToRemoraPartialGuild() ?? new Optional<IPartialGuild>(),
|
|
|
|
|
Channel?.ToRemoraPartialChannel(),
|
2024-10-09 22:31:58 +02:00
|
|
|
Uses,
|
|
|
|
|
MaxUses,
|
|
|
|
|
MaxAge,
|
|
|
|
|
IsTemporary,
|
|
|
|
|
CreatedAt,
|
2024-10-09 17:35:11 +02:00
|
|
|
Inviter?.ToRemoraUser() ?? new Optional<IUser>(),
|
|
|
|
|
ExpiresAt: ExpiresAt
|
|
|
|
|
);
|
2024-08-19 16:12:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal record RedisPartialGuild(ulong Id, string? Name)
|
|
|
|
|
{
|
|
|
|
|
public static RedisPartialGuild FromIPartialGuild(IPartialGuild guild) =>
|
|
|
|
|
new(guild.ID.Value.Value, guild.Name.OrDefault(null));
|
|
|
|
|
|
2024-10-09 17:35:11 +02:00
|
|
|
public PartialGuild ToRemoraPartialGuild() =>
|
|
|
|
|
new(DiscordSnowflake.New(Id), Name ?? new Optional<string>());
|
2024-08-19 16:12:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal record RedisPartialChannel(ulong Id, string? Name)
|
|
|
|
|
{
|
|
|
|
|
public static RedisPartialChannel FromIPartialChannel(IPartialChannel channel) =>
|
|
|
|
|
new(channel.ID.Value.Value, channel.Name.OrDefault(null));
|
|
|
|
|
|
|
|
|
|
public PartialChannel ToRemoraPartialChannel() => new(DiscordSnowflake.New(Id), Name: Name);
|
2024-10-09 17:35:11 +02:00
|
|
|
}
|