2019-05-16 23:23:09 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Discord;
|
2019-08-12 02:32:01 +00:00
|
|
|
using Discord.Webhook;
|
2019-07-21 02:27:04 +00:00
|
|
|
using Serilog;
|
2019-05-16 23:23:09 +00:00
|
|
|
|
|
|
|
namespace PluralKit.Bot
|
|
|
|
{
|
2019-08-12 02:32:01 +00:00
|
|
|
public class WebhookCacheService: IDisposable
|
2019-05-16 23:23:09 +00:00
|
|
|
{
|
2019-08-12 02:32:01 +00:00
|
|
|
public class WebhookCacheEntry
|
|
|
|
{
|
|
|
|
internal DiscordWebhookClient Client;
|
|
|
|
internal IWebhook Webhook;
|
|
|
|
}
|
|
|
|
|
2019-05-16 23:23:09 +00:00
|
|
|
public static readonly string WebhookName = "PluralKit Proxy Webhook";
|
|
|
|
|
|
|
|
private IDiscordClient _client;
|
2019-08-12 02:32:01 +00:00
|
|
|
private ConcurrentDictionary<ulong, Lazy<Task<WebhookCacheEntry>>> _webhooks;
|
2019-05-16 23:23:09 +00:00
|
|
|
|
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-08-12 02:32:01 +00:00
|
|
|
_webhooks = new ConcurrentDictionary<ulong, Lazy<Task<WebhookCacheEntry>>>();
|
2019-05-16 23:23:09 +00:00
|
|
|
}
|
|
|
|
|
2019-08-12 02:32:01 +00:00
|
|
|
public async Task<WebhookCacheEntry> GetWebhook(ulong channelId)
|
2019-05-16 23:23:09 +00:00
|
|
|
{
|
|
|
|
var channel = await _client.GetChannelAsync(channelId) as ITextChannel;
|
|
|
|
if (channel == null) return null;
|
|
|
|
return await GetWebhook(channel);
|
|
|
|
}
|
|
|
|
|
2019-08-12 02:32:01 +00:00
|
|
|
public async Task<WebhookCacheEntry> GetWebhook(ITextChannel channel)
|
2019-05-16 23:23:09 +00:00
|
|
|
{
|
|
|
|
// 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-08-12 02:32:01 +00:00
|
|
|
_webhooks.GetOrAdd(channel.Id, new Lazy<Task<WebhookCacheEntry>>(() => 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;
|
2019-08-12 02:32:01 +00:00
|
|
|
if (webhook.Webhook.ChannelId != channel.Id) return await InvalidateAndRefreshWebhook(webhook);
|
2019-07-10 21:16:17 +00:00
|
|
|
return webhook;
|
|
|
|
}
|
|
|
|
|
2019-08-12 02:32:01 +00:00
|
|
|
public async Task<WebhookCacheEntry> InvalidateAndRefreshWebhook(WebhookCacheEntry webhook)
|
2019-07-10 21:16:17 +00:00
|
|
|
{
|
2019-08-12 02:32:01 +00:00
|
|
|
_logger.Information("Refreshing webhook for channel {Channel}", webhook.Webhook.ChannelId);
|
2019-07-21 02:27:04 +00:00
|
|
|
|
2019-08-12 02:32:01 +00:00
|
|
|
_webhooks.TryRemove(webhook.Webhook.ChannelId, out _);
|
|
|
|
return await GetWebhook(webhook.Webhook.Channel);
|
2019-05-16 23:23:09 +00:00
|
|
|
}
|
|
|
|
|
2019-08-12 02:32:01 +00:00
|
|
|
private async Task<WebhookCacheEntry> GetOrCreateWebhook(ITextChannel channel)
|
|
|
|
{
|
|
|
|
var webhook = await FindExistingWebhook(channel) ?? await DoCreateWebhook(channel);
|
|
|
|
return await DoCreateWebhookClient(webhook);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-08-12 02:32:01 +00:00
|
|
|
private Task<IWebhook> DoCreateWebhook(ITextChannel channel)
|
2019-07-21 02:27:04 +00:00
|
|
|
{
|
|
|
|
_logger.Information("Creating new webhook for channel {Channel}", channel.Id);
|
2019-08-12 02:32:01 +00:00
|
|
|
return channel.CreateWebhookAsync(WebhookName);
|
2019-07-21 02:27:04 +00:00
|
|
|
}
|
|
|
|
|
2019-08-12 02:32:01 +00:00
|
|
|
private Task<WebhookCacheEntry> DoCreateWebhookClient(IWebhook webhook)
|
|
|
|
{
|
|
|
|
// DiscordWebhookClient's ctor is synchronous despite doing web calls, so we wrap it in a Task
|
|
|
|
return Task.Run(() =>
|
|
|
|
{
|
|
|
|
return new WebhookCacheEntry
|
|
|
|
{
|
|
|
|
Client = new DiscordWebhookClient(webhook),
|
|
|
|
Webhook = webhook
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-12 02:42:16 +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-08-12 02:32:01 +00:00
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
foreach (var entry in _webhooks.Values)
|
|
|
|
if (entry.IsValueCreated)
|
|
|
|
entry.Value.Result.Client.Dispose();
|
|
|
|
}
|
2019-05-16 23:23:09 +00:00
|
|
|
}
|
|
|
|
}
|