feat: decrement message counts on bulk message deletions

This commit is contained in:
rladenson 2024-11-25 02:09:40 -07:00
parent 71dd59b57d
commit 21a63a43cc
3 changed files with 30 additions and 6 deletions

View file

@ -111,21 +111,21 @@ public partial class ModelRepository
await _db.ExecuteQuery(query);
}
public async Task UpdateMemberForDeletedMessage(MemberId id, int msgCount)
public async Task UpdateMemberForDeletedMessage(MemberId id, int msgCount, int numberToDecrement = 1)
{
if (msgCount <= 0) return;
var query = new Query("members")
.When(msgCount > 1,
// when there are plenty of messages
.When(msgCount > numberToDecrement,
// when message count is higher than number we're removing
q => q.AsUpdate(new
{
message_count = new UnsafeLiteral("message_count - 1")
message_count = new UnsafeLiteral($"message_count - {numberToDecrement}")
}),
// when this is the only message (if the db for some reason thinks there are no messages we already returned)
// when this is the only/last message(s) (if the db for some reason thinks there are no messages we already returned)
q => q.AsUpdate(new
{
message_count = new UnsafeLiteral("message_count - 1"),
message_count = 0,
last_message_timestamp = new UnsafeLiteral("null")
})
)

View file

@ -26,6 +26,9 @@ public partial class ModelRepository
public Task<PKMessage?> GetMessage(ulong id)
=> _db.QueryFirst<PKMessage?>(new Query("messages").Where("mid", id), messages: true);
public Task<IEnumerable<PKMessage>> GetMessagesBulk(IReadOnlyCollection<ulong> ids)
=> _db.Query<PKMessage>(new Query("messages").WhereIn("mid", ids.Select(id => (long)id).ToArray()));
public async Task<FullMessage?> GetFullMessage(ulong id)
{
var rawMessage = await GetMessage(id);