2021-05-03 08:29:22 +00:00
|
|
|
|
#nullable enable
|
|
|
|
|
using System.Collections.Concurrent;
|
2020-02-12 13:21:48 +00:00
|
|
|
|
using System.Collections.Generic;
|
2021-08-03 17:44:22 +00:00
|
|
|
|
using System.Linq;
|
2020-02-12 13:21:48 +00:00
|
|
|
|
|
2021-03-22 07:07:33 +00:00
|
|
|
|
using Myriad.Types;
|
|
|
|
|
|
2020-02-12 13:21:48 +00:00
|
|
|
|
namespace PluralKit.Bot
|
|
|
|
|
{
|
2021-03-25 18:02:44 +00:00
|
|
|
|
// TODO: Should this be moved to Myriad.Cache?
|
2020-02-12 13:21:48 +00:00
|
|
|
|
public class LastMessageCacheService
|
|
|
|
|
{
|
2021-03-22 07:07:33 +00:00
|
|
|
|
private readonly IDictionary<ulong, CachedMessage> _cache = new ConcurrentDictionary<ulong, CachedMessage>();
|
2020-02-12 13:21:48 +00:00
|
|
|
|
|
2021-03-22 07:07:33 +00:00
|
|
|
|
public void AddMessage(Message msg)
|
2020-02-12 13:21:48 +00:00
|
|
|
|
{
|
2021-08-03 17:44:22 +00:00
|
|
|
|
var previous = GetLastMessage(msg.ChannelId);
|
|
|
|
|
_cache[msg.ChannelId] = new CachedMessage(msg.Id, msg.ReferencedMessage.Value?.Id, previous?.Id);
|
2020-02-12 13:21:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-03 08:29:22 +00:00
|
|
|
|
public CachedMessage? GetLastMessage(ulong channel)
|
2020-02-12 13:21:48 +00:00
|
|
|
|
{
|
2021-05-03 08:29:22 +00:00
|
|
|
|
return _cache.TryGetValue(channel, out var message) ? message : null;
|
2020-02-12 13:21:48 +00:00
|
|
|
|
}
|
2021-08-03 17:44:22 +00:00
|
|
|
|
|
|
|
|
|
public void HandleMessageDeletion(ulong channel, ulong message)
|
|
|
|
|
{
|
|
|
|
|
var storedMessage = GetLastMessage(channel);
|
|
|
|
|
if (storedMessage == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (message == storedMessage.Id)
|
|
|
|
|
if (storedMessage.Previous != null)
|
|
|
|
|
_cache[channel] = new CachedMessage(storedMessage.Previous.Value, null, null);
|
|
|
|
|
else
|
|
|
|
|
_cache.Remove(channel);
|
|
|
|
|
else if (message == storedMessage.Previous)
|
|
|
|
|
_cache[channel] = new CachedMessage(storedMessage.Id, storedMessage.ReferencedMessage, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void HandleMessageDeletion(ulong channel, List<ulong> messages)
|
|
|
|
|
{
|
|
|
|
|
var storedMessage = GetLastMessage(channel);
|
|
|
|
|
if (storedMessage == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (!(messages.Contains(storedMessage.Id) || (storedMessage.Previous != null && messages.Contains(storedMessage.Previous.Value))))
|
|
|
|
|
// none of the deleted messages are relevant to the cache
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
ulong? newLastMessage = null;
|
|
|
|
|
|
|
|
|
|
if (messages.Contains(storedMessage.Id))
|
|
|
|
|
newLastMessage = storedMessage.Previous;
|
|
|
|
|
|
|
|
|
|
if (storedMessage.Previous != null && messages.Contains(storedMessage.Previous.Value))
|
|
|
|
|
if (newLastMessage == storedMessage.Previous)
|
|
|
|
|
newLastMessage = null;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_cache[channel] = new CachedMessage(storedMessage.Id, storedMessage.ReferencedMessage, null);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (newLastMessage == null)
|
|
|
|
|
_cache.Remove(channel);
|
|
|
|
|
}
|
2020-02-12 13:21:48 +00:00
|
|
|
|
}
|
2021-03-22 07:07:33 +00:00
|
|
|
|
|
2021-08-03 17:44:22 +00:00
|
|
|
|
public record CachedMessage(ulong Id, ulong? ReferencedMessage, ulong? Previous);
|
2021-03-25 18:02:44 +00:00
|
|
|
|
}
|