PluralKit/PluralKit.Bot/Services/WebhookCacheService.cs

90 lines
3.7 KiB
C#
Raw Normal View History

using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Discord;
using Discord.Webhook;
using Serilog;
namespace PluralKit.Bot
{
2019-08-12 03:47:55 +00:00
public class WebhookCacheService
{
public static readonly string WebhookName = "PluralKit Proxy Webhook";
private IDiscordClient _client;
2019-08-12 03:47:55 +00:00
private ConcurrentDictionary<ulong, Lazy<Task<IWebhook>>> _webhooks;
private ILogger _logger;
public WebhookCacheService(IDiscordClient client, ILogger logger)
{
_client = client;
_logger = logger.ForContext<WebhookCacheService>();
2019-08-12 03:47:55 +00:00
_webhooks = new ConcurrentDictionary<ulong, Lazy<Task<IWebhook>>>();
}
2019-08-12 03:47:55 +00:00
public async Task<IWebhook> GetWebhook(ulong channelId)
{
var channel = await _client.GetChannelAsync(channelId) as ITextChannel;
if (channel == null) return null;
return await GetWebhook(channel);
}
2019-08-12 03:47:55 +00:00
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.
var lazyWebhookValue =
2019-08-12 03:47:55 +00:00
_webhooks.GetOrAdd(channel.Id, new Lazy<Task<IWebhook>>(() => GetOrCreateWebhook(channel)));
// 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 03:47:55 +00:00
if (webhook.ChannelId != channel.Id) return await InvalidateAndRefreshWebhook(webhook);
return webhook;
}
2019-08-12 03:47:55 +00:00
public async Task<IWebhook> InvalidateAndRefreshWebhook(IWebhook webhook)
{
2019-08-12 03:47:55 +00:00
_logger.Information("Refreshing webhook for channel {Channel}", webhook.ChannelId);
2019-08-12 03:47:55 +00:00
_webhooks.TryRemove(webhook.ChannelId, out _);
return await GetWebhook(webhook.Channel);
}
private async Task<IWebhook> GetOrCreateWebhook(ITextChannel channel)
{
_logger.Debug("Webhook for channel {Channel} not found in cache, trying to fetch", channel.Id);
return await FindExistingWebhook(channel) ?? await DoCreateWebhook(channel);
}
2019-08-12 03:47:55 +00:00
private async Task<IWebhook> FindExistingWebhook(ITextChannel channel)
{
_logger.Debug("Finding webhook for channel {Channel}", channel.Id);
try
{
return (await channel.GetWebhooksAsync()).FirstOrDefault(IsWebhookMine);
}
catch (HttpRequestException e)
{
_logger.Warning(e, "Error occurred while fetching webhook list");
// This happens sometimes when Discord returns a malformed request for the webhook list
// Nothing we can do than just assume that none exist and return null.
return null;
}
}
private Task<IWebhook> DoCreateWebhook(ITextChannel channel)
{
_logger.Information("Creating new webhook for channel {Channel}", channel.Id);
return channel.CreateWebhookAsync(WebhookName);
}
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;
}
}