2020-11-18 14:01:32 +00:00
|
|
|
using System;
|
2021-08-03 17:44:22 +00:00
|
|
|
using System.Linq;
|
2020-05-01 23:52:52 +00:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
2020-12-22 12:15:26 +00:00
|
|
|
using Myriad.Gateway;
|
2020-05-01 23:52:52 +00:00
|
|
|
|
2020-06-11 21:20:46 +00:00
|
|
|
using PluralKit.Core;
|
|
|
|
|
|
|
|
using Serilog;
|
2020-05-01 23:52:52 +00:00
|
|
|
|
|
|
|
namespace PluralKit.Bot
|
|
|
|
{
|
|
|
|
// Double duty :)
|
2020-12-22 12:15:26 +00:00
|
|
|
public class MessageDeleted: IEventHandler<MessageDeleteEvent>, IEventHandler<MessageDeleteBulkEvent>
|
2020-05-01 23:52:52 +00:00
|
|
|
{
|
2020-11-18 14:01:32 +00:00
|
|
|
private static readonly TimeSpan MessageDeleteDelay = TimeSpan.FromSeconds(15);
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2020-08-29 11:46:27 +00:00
|
|
|
private readonly IDatabase _db;
|
|
|
|
private readonly ModelRepository _repo;
|
2020-06-11 21:20:46 +00:00
|
|
|
private readonly ILogger _logger;
|
2021-08-03 17:44:22 +00:00
|
|
|
private readonly LastMessageCacheService _lastMessage;
|
2020-05-01 23:52:52 +00:00
|
|
|
|
2021-08-03 17:44:22 +00:00
|
|
|
public MessageDeleted(ILogger logger, IDatabase db, ModelRepository repo, LastMessageCacheService lastMessage)
|
2020-05-01 23:52:52 +00:00
|
|
|
{
|
2020-08-29 11:46:27 +00:00
|
|
|
_db = db;
|
|
|
|
_repo = repo;
|
2021-08-03 17:44:22 +00:00
|
|
|
_lastMessage = lastMessage;
|
2020-06-11 21:20:46 +00:00
|
|
|
_logger = logger.ForContext<MessageDeleted>();
|
2020-05-01 23:52:52 +00:00
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2020-12-22 12:15:26 +00:00
|
|
|
public Task Handle(Shard shard, MessageDeleteEvent evt)
|
2020-05-01 23:52:52 +00:00
|
|
|
{
|
2020-06-11 21:20:46 +00:00
|
|
|
// Delete deleted webhook messages from the data store
|
2020-08-29 11:46:27 +00:00
|
|
|
// Most of the data in the given message is wrong/missing, so always delete just to be sure.
|
2020-11-18 14:01:32 +00:00
|
|
|
|
|
|
|
async Task Inner()
|
|
|
|
{
|
|
|
|
await Task.Delay(MessageDeleteDelay);
|
2020-12-23 01:19:02 +00:00
|
|
|
await _db.Execute(c => _repo.DeleteMessage(c, evt.Id));
|
2020-11-18 14:01:32 +00:00
|
|
|
}
|
|
|
|
|
2021-08-03 17:44:22 +00:00
|
|
|
_lastMessage.HandleMessageDeletion(evt.ChannelId, evt.Id);
|
|
|
|
|
2020-11-18 14:01:32 +00:00
|
|
|
// Fork a task to delete the message after a short delay
|
|
|
|
// to allow for lookups to happen for a little while after deletion
|
|
|
|
_ = Inner();
|
|
|
|
return Task.CompletedTask;
|
2020-05-01 23:52:52 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 12:15:26 +00:00
|
|
|
public Task Handle(Shard shard, MessageDeleteBulkEvent evt)
|
2020-05-01 23:52:52 +00:00
|
|
|
{
|
2020-06-11 21:20:46 +00:00
|
|
|
// Same as above, but bulk
|
2020-11-18 14:01:32 +00:00
|
|
|
async Task Inner()
|
|
|
|
{
|
|
|
|
await Task.Delay(MessageDeleteDelay);
|
2020-12-23 01:19:02 +00:00
|
|
|
|
2021-08-27 15:03:47 +00:00
|
|
|
_logger.Information("Bulk deleting {Count} messages in channel {Channel}",
|
2020-12-23 01:19:02 +00:00
|
|
|
evt.Ids.Length, evt.ChannelId);
|
|
|
|
await _db.Execute(c => _repo.DeleteMessagesBulk(c, evt.Ids));
|
2020-11-18 14:01:32 +00:00
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-08-03 17:44:22 +00:00
|
|
|
_lastMessage.HandleMessageDeletion(evt.ChannelId, evt.Ids.ToList());
|
2020-11-18 14:01:32 +00:00
|
|
|
_ = Inner();
|
|
|
|
return Task.CompletedTask;
|
2020-05-01 23:52:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|