2019-05-16 23:23:09 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Discord;
|
2019-07-21 02:27:04 +00:00
|
|
|
using Serilog;
|
2019-05-16 23:23:09 +00:00
|
|
|
|
|
|
|
namespace PluralKit.Bot
|
|
|
|
{
|
|
|
|
public class WebhookCacheService
|
|
|
|
{
|
|
|
|
public static readonly string WebhookName = "PluralKit Proxy Webhook";
|
|
|
|
|
|
|
|
private IDiscordClient _client;
|
|
|
|
private ConcurrentDictionary<ulong, Lazy<Task<IWebhook>>> _webhooks;
|
|
|
|
|
2019-07-21 02:27:04 +00:00
|
|
|
private ILogger _logger;
|
|
|
|
|
|
|
|
public WebhookCacheService(IDiscordClient client, ILogger logger)
|
2019-05-16 23:23:09 +00:00
|
|
|
{
|
2019-07-21 02:27:04 +00:00
|
|
|
_client = client;
|
|
|
|
_logger = logger.ForContext<WebhookCacheService>();
|
2019-05-16 23:23:09 +00:00
|
|
|
_webhooks = new ConcurrentDictionary<ulong, Lazy<Task<IWebhook>>>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<IWebhook> GetWebhook(ulong channelId)
|
|
|
|
{
|
|
|
|
var channel = await _client.GetChannelAsync(channelId) as ITextChannel;
|
|
|
|
if (channel == null) return null;
|
|
|
|
return await GetWebhook(channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<IWebhook> GetWebhook(ITextChannel channel)
|
|
|
|
{
|
|
|
|
// We cache the webhook through a Lazy<Task<T>>, this way we make sure to only create one webhook per channel
|
|
|
|
// If the webhook is requested twice before it's actually been found, the Lazy<T> wrapper will stop the
|
|
|
|
// webhook from being created twice.
|
2019-07-10 21:16:17 +00:00
|
|
|
var lazyWebhookValue =
|
2019-05-16 23:23:09 +00:00
|
|
|
_webhooks.GetOrAdd(channel.Id, new Lazy<Task<IWebhook>>(() => GetOrCreateWebhook(channel)));
|
2019-07-10 21:16:17 +00:00
|
|
|
|
|
|
|
// It's possible to "move" a webhook to a different channel after creation
|
|
|
|
// Here, we ensure it's actually still pointing towards the proper channel, and if not, wipe and refetch one.
|
|
|
|
var webhook = await lazyWebhookValue.Value;
|
|
|
|
if (webhook.Channel.Id != channel.Id) return await InvalidateAndRefreshWebhook(webhook);
|
|
|
|
return webhook;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<IWebhook> InvalidateAndRefreshWebhook(IWebhook webhook)
|
|
|
|
{
|
2019-07-21 02:27:04 +00:00
|
|
|
_logger.Information("Refreshing webhook for channel {Channel}", webhook.ChannelId);
|
|
|
|
|
|
|
|
_webhooks.TryRemove(webhook.ChannelId, out _);
|
|
|
|
return await GetWebhook(webhook.ChannelId);
|
2019-05-16 23:23:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<IWebhook> GetOrCreateWebhook(ITextChannel channel) =>
|
2019-07-10 21:16:17 +00:00
|
|
|
await FindExistingWebhook(channel) ?? await DoCreateWebhook(channel);
|
2019-05-16 23:23:09 +00:00
|
|
|
|
2019-07-21 02:27:04 +00:00
|
|
|
private async Task<IWebhook> FindExistingWebhook(ITextChannel channel)
|
|
|
|
{
|
|
|
|
_logger.Debug("Finding webhook for channel {Channel}", channel.Id);
|
|
|
|
return (await channel.GetWebhooksAsync()).FirstOrDefault(IsWebhookMine);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<IWebhook> DoCreateWebhook(ITextChannel channel)
|
|
|
|
{
|
|
|
|
_logger.Information("Creating new webhook for channel {Channel}", channel.Id);
|
|
|
|
return await channel.CreateWebhookAsync(WebhookName);
|
|
|
|
}
|
|
|
|
|
2019-05-16 23:23:09 +00:00
|
|
|
private bool IsWebhookMine(IWebhook arg) => arg.Creator.Id == _client.CurrentUser.Id && arg.Name == WebhookName;
|
2019-07-21 02:15:47 +00:00
|
|
|
|
|
|
|
public int CacheSize => _webhooks.Count;
|
2019-05-16 23:23:09 +00:00
|
|
|
}
|
|
|
|
}
|