[backend/core] Improve resolution of notes with pending reply/renote targets on note create

This fixes missing metadata (reply/renote user id/host), as well as stale reply/renote counts for replies/renotes processed out of order.
This commit is contained in:
Laura Hausmann 2024-05-30 12:30:45 +02:00
parent 3d15ed6807
commit 6fdc8b73f7
No known key found for this signature in database
GPG key ID: D044E84C5BE01605

View file

@ -234,24 +234,57 @@ public class NoteService(
_ = followupTaskSvc.ExecuteTask("ResolvePendingReplyRenoteTargets", async provider => _ = followupTaskSvc.ExecuteTask("ResolvePendingReplyRenoteTargets", async provider =>
{ {
var bgDb = provider.GetRequiredService<DatabaseContext>(); var bgDb = provider.GetRequiredService<DatabaseContext>();
var count = 0;
if (uri != null) if (uri != null)
{ {
count +=
await bgDb.Notes.Where(p => p.ReplyUri == uri) await bgDb.Notes.Where(p => p.ReplyUri == uri)
.ExecuteUpdateAsync(p => p.SetProperty(i => i.ReplyId, _ => note.Id) .ExecuteUpdateAsync(p => p.SetProperty(i => i.ReplyUri, _ => null)
.SetProperty(i => i.ReplyUri, _ => null)); .SetProperty(i => i.ReplyId, _ => note.Id)
.SetProperty(i => i.ReplyUserId, _ => note.UserId)
.SetProperty(i => i.ReplyUserHost, _ => note.UserHost)
.SetProperty(i => i.MastoReplyUserId,
i => i.UserId != user.Id
? i.UserId
: mastoReplyUserId));
count +=
await bgDb.Notes.Where(p => p.RenoteUri == uri) await bgDb.Notes.Where(p => p.RenoteUri == uri)
.ExecuteUpdateAsync(p => p.SetProperty(i => i.RenoteId, _ => note.Id) .ExecuteUpdateAsync(p => p.SetProperty(i => i.RenoteUri, _ => null)
.SetProperty(i => i.RenoteUri, _ => null)); .SetProperty(i => i.RenoteId, _ => note.Id)
.SetProperty(i => i.RenoteUserId, _ => note.UserId)
.SetProperty(i => i.RenoteUserHost, _ => note.UserHost));
} }
if (url != null) if (url != null)
{ {
count +=
await bgDb.Notes.Where(p => p.ReplyUri == url) await bgDb.Notes.Where(p => p.ReplyUri == url)
.ExecuteUpdateAsync(p => p.SetProperty(i => i.ReplyId, _ => note.Id) .ExecuteUpdateAsync(p => p.SetProperty(i => i.ReplyUri, _ => null)
.SetProperty(i => i.ReplyUri, _ => null)); .SetProperty(i => i.ReplyId, _ => note.Id)
.SetProperty(i => i.ReplyUserId, _ => note.UserId)
.SetProperty(i => i.ReplyUserHost, _ => note.UserHost)
.SetProperty(i => i.MastoReplyUserId,
i => i.UserId != user.Id
? i.UserId
: mastoReplyUserId));
count +=
await bgDb.Notes.Where(p => p.RenoteUri == url) await bgDb.Notes.Where(p => p.RenoteUri == url)
.ExecuteUpdateAsync(p => p.SetProperty(i => i.RenoteId, _ => note.Id) .ExecuteUpdateAsync(p => p.SetProperty(i => i.RenoteUri, _ => null)
.SetProperty(i => i.RenoteUri, _ => null)); .SetProperty(i => i.RenoteId, _ => note.Id)
.SetProperty(i => i.RenoteUserId, _ => note.UserId)
.SetProperty(i => i.RenoteUserHost, _ => note.UserHost));
}
if (count > 0)
{
await bgDb.Notes.Where(p => p.Id == note.Id)
.ExecuteUpdateAsync(p => p.SetProperty(i => i.RepliesCount,
i => db.Notes.Count(n => n.ReplyId == i.Id))
.SetProperty(i => i.RenoteCount,
i => db.Notes.Count(n => n.RenoteId == i.Id &&
n.IsPureRenote)));
} }
}); });
} }