2020-08-29 11:46:27 +00:00
|
|
|
using Dapper;
|
|
|
|
|
2021-09-30 01:51:38 +00:00
|
|
|
using SqlKata;
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
namespace PluralKit.Core;
|
|
|
|
|
|
|
|
public partial class ModelRepository
|
2020-08-29 11:46:27 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
public Task AddMessage(PKMessage msg)
|
2020-08-29 11:46:27 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
var query = new Query("messages").AsInsert(new
|
2021-08-27 15:03:47 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
mid = msg.Mid,
|
|
|
|
guild = msg.Guild,
|
|
|
|
channel = msg.Channel,
|
|
|
|
member = msg.Member,
|
|
|
|
sender = msg.Sender,
|
|
|
|
original_mid = msg.OriginalMid
|
|
|
|
});
|
|
|
|
_logger.Debug("Stored message {@StoredMessage} in channel {Channel}", msg, msg.Channel);
|
2021-09-30 01:51:38 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
// "on conflict do nothing" in the (pretty rare) case of duplicate events coming in from Discord, which would lead to a DB error before
|
2022-11-23 09:17:19 +00:00
|
|
|
return _db.ExecuteQuery(query, "on conflict do nothing", messages: true);
|
2021-11-27 02:10:56 +00:00
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2022-12-03 12:04:12 +00:00
|
|
|
public Task<PKMessage?> GetMessage(ulong id)
|
|
|
|
=> _db.QueryFirst<PKMessage?>(new Query("messages").Where("mid", id), messages: true);
|
2022-11-23 09:17:19 +00:00
|
|
|
|
|
|
|
public async Task<FullMessage?> GetFullMessage(ulong id)
|
|
|
|
{
|
|
|
|
var rawMessage = await GetMessage(id);
|
|
|
|
if (rawMessage == null) return null;
|
2020-08-29 11:46:27 +00:00
|
|
|
|
2022-11-23 09:17:19 +00:00
|
|
|
var member = rawMessage.Member == null ? null : await GetMember(rawMessage.Member.Value);
|
|
|
|
var system = member == null ? null : await GetSystem(member.System);
|
2022-01-11 14:43:55 +00:00
|
|
|
|
2022-11-23 09:17:19 +00:00
|
|
|
return new FullMessage
|
|
|
|
{
|
|
|
|
Message = rawMessage,
|
|
|
|
Member = member,
|
|
|
|
System = system,
|
|
|
|
};
|
2021-11-27 02:10:56 +00:00
|
|
|
}
|
2020-08-29 11:46:27 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public async Task DeleteMessage(ulong id)
|
|
|
|
{
|
|
|
|
var query = new Query("messages").AsDelete().Where("mid", id);
|
2022-11-23 09:17:19 +00:00
|
|
|
var rowCount = await _db.ExecuteQuery(query, messages: true);
|
2021-11-27 02:10:56 +00:00
|
|
|
if (rowCount > 0)
|
|
|
|
_logger.Information("Deleted message {MessageId} from database", id);
|
|
|
|
}
|
2020-08-29 11:46:27 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public async Task DeleteMessagesBulk(IReadOnlyCollection<ulong> ids)
|
|
|
|
{
|
|
|
|
// Npgsql doesn't support ulongs in general - we hacked around it for plain ulongs but tbh not worth it for collections of ulong
|
|
|
|
// Hence we map them to single longs, which *are* supported (this is ok since they're Technically (tm) stored as signed longs in the db anyway)
|
|
|
|
var query = new Query("messages").AsDelete().WhereIn("mid", ids.Select(id => (long)id).ToArray());
|
2022-11-23 09:17:19 +00:00
|
|
|
var rowCount = await _db.ExecuteQuery(query, messages: true);
|
2021-11-27 02:10:56 +00:00
|
|
|
if (rowCount > 0)
|
|
|
|
_logger.Information("Bulk deleted messages ({FoundCount} found) from database: {MessageIds}", rowCount,
|
|
|
|
ids);
|
|
|
|
}
|
2020-08-29 11:46:27 +00:00
|
|
|
}
|