2019-10-27 12:09:07 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Net.Http;
|
|
|
|
using System.Threading.Tasks;
|
2020-04-17 21:10:01 +00:00
|
|
|
|
|
|
|
using DSharpPlus;
|
|
|
|
using DSharpPlus.Entities;
|
2019-10-27 12:09:07 +00:00
|
|
|
|
|
|
|
using Serilog;
|
|
|
|
|
|
|
|
namespace PluralKit.Bot
|
|
|
|
{
|
|
|
|
public class WebhookCacheService
|
|
|
|
{
|
|
|
|
public static readonly string WebhookName = "PluralKit Proxy Webhook";
|
|
|
|
|
|
|
|
private DiscordShardedClient _client;
|
2020-04-17 21:10:01 +00:00
|
|
|
private ConcurrentDictionary<ulong, Lazy<Task<DiscordWebhook>>> _webhooks;
|
2019-10-27 12:09:07 +00:00
|
|
|
|
|
|
|
private ILogger _logger;
|
|
|
|
|
2020-04-17 21:10:01 +00:00
|
|
|
public WebhookCacheService(DiscordShardedClient client, ILogger logger)
|
2019-10-27 12:09:07 +00:00
|
|
|
{
|
2020-04-17 21:10:01 +00:00
|
|
|
_client = client;
|
2019-10-27 12:09:07 +00:00
|
|
|
_logger = logger.ForContext<WebhookCacheService>();
|
2020-04-17 21:10:01 +00:00
|
|
|
_webhooks = new ConcurrentDictionary<ulong, Lazy<Task<DiscordWebhook>>>();
|
2019-10-27 12:09:07 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 21:10:01 +00:00
|
|
|
public async Task<DiscordWebhook> GetWebhook(DiscordClient client, ulong channelId)
|
2019-10-27 12:09:07 +00:00
|
|
|
{
|
2020-04-17 21:10:01 +00:00
|
|
|
var channel = await client.GetChannelAsync(channelId);
|
2019-10-27 12:09:07 +00:00
|
|
|
if (channel == null) return null;
|
2020-04-17 21:10:01 +00:00
|
|
|
if (channel.Type == ChannelType.Text) return null;
|
2019-10-27 12:09:07 +00:00
|
|
|
return await GetWebhook(channel);
|
|
|
|
}
|
|
|
|
|
2020-04-17 21:10:01 +00:00
|
|
|
public async Task<DiscordWebhook> GetWebhook(DiscordChannel channel)
|
2019-10-27 12:09:07 +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.
|
|
|
|
var lazyWebhookValue =
|
2020-04-17 21:10:01 +00:00
|
|
|
_webhooks.GetOrAdd(channel.Id, new Lazy<Task<DiscordWebhook>>(() => GetOrCreateWebhook(channel)));
|
2019-10-27 12:09:07 +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;
|
2020-04-17 21:10:01 +00:00
|
|
|
if (webhook.ChannelId != channel.Id) return await InvalidateAndRefreshWebhook(channel, webhook);
|
2019-10-27 12:09:07 +00:00
|
|
|
return webhook;
|
|
|
|
}
|
|
|
|
|
2020-04-17 21:10:01 +00:00
|
|
|
public async Task<DiscordWebhook> InvalidateAndRefreshWebhook(DiscordChannel channel, DiscordWebhook webhook)
|
2019-10-27 12:09:07 +00:00
|
|
|
{
|
|
|
|
_logger.Information("Refreshing webhook for channel {Channel}", webhook.ChannelId);
|
|
|
|
|
|
|
|
_webhooks.TryRemove(webhook.ChannelId, out _);
|
2020-04-17 21:10:01 +00:00
|
|
|
return await GetWebhook(channel);
|
2019-10-27 12:09:07 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 21:10:01 +00:00
|
|
|
private async Task<DiscordWebhook> GetOrCreateWebhook(DiscordChannel channel)
|
2019-10-27 12:09:07 +00:00
|
|
|
{
|
|
|
|
_logger.Debug("Webhook for channel {Channel} not found in cache, trying to fetch", channel.Id);
|
|
|
|
return await FindExistingWebhook(channel) ?? await DoCreateWebhook(channel);
|
|
|
|
}
|
|
|
|
|
2020-04-17 21:10:01 +00:00
|
|
|
private async Task<DiscordWebhook> FindExistingWebhook(DiscordChannel channel)
|
2019-10-27 12:09:07 +00:00
|
|
|
{
|
|
|
|
_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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 21:10:01 +00:00
|
|
|
private Task<DiscordWebhook> DoCreateWebhook(DiscordChannel channel)
|
2019-10-27 12:09:07 +00:00
|
|
|
{
|
|
|
|
_logger.Information("Creating new webhook for channel {Channel}", channel.Id);
|
|
|
|
return channel.CreateWebhookAsync(WebhookName);
|
|
|
|
}
|
|
|
|
|
2020-04-17 21:10:01 +00:00
|
|
|
private bool IsWebhookMine(DiscordWebhook arg) => arg.User.Id == _client.CurrentUser.Id && arg.Name == WebhookName;
|
2019-10-27 12:09:07 +00:00
|
|
|
|
|
|
|
public int CacheSize => _webhooks.Count;
|
|
|
|
}
|
2019-05-16 23:23:09 +00:00
|
|
|
}
|