[backend/api-shared] Order note ancestors by tree path

Background: the database function might not return these in the correct order, as the ids will almost, but not always be in the right order.
This commit is contained in:
Laura Hausmann 2024-05-01 23:21:32 +02:00
parent 764ef7b9cf
commit 0e6864fe38
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
3 changed files with 49 additions and 3 deletions

View file

@ -97,7 +97,10 @@ public class StatusController(
.PrecomputeVisibilities(user) .PrecomputeVisibilities(user)
.RenderAllForMastodonAsync(noteRenderer, user, Filter.FilterContext.Threads); .RenderAllForMastodonAsync(noteRenderer, user, Filter.FilterContext.Threads);
var res = new StatusContext { Ancestors = ancestors, Descendants = descendants.OrderDescendants() }; var res = new StatusContext
{
Ancestors = ancestors.OrderAncestors(), Descendants = descendants.OrderDescendants()
};
return Ok(res); return Ok(res);
} }

View file

@ -68,9 +68,10 @@ public class NoteController(
.FilterHidden(user, db) .FilterHidden(user, db)
.PrecomputeNoteContextVisibilities(user) .PrecomputeNoteContextVisibilities(user)
.ToListAsync(); .ToListAsync();
var res = await noteRenderer.RenderMany(notes.EnforceRenoteReplyVisibility(), user,
Filter.FilterContext.Threads);
return Ok(await noteRenderer.RenderMany(notes.EnforceRenoteReplyVisibility(), user, return Ok(res.ToList().OrderAncestors());
Filter.FilterContext.Threads));
} }
[HttpGet("{id}/descendants")] [HttpGet("{id}/descendants")]

View file

@ -13,6 +13,48 @@ public static class NoteThreadHelpers
public TreeNode<T>? Parent; public TreeNode<T>? Parent;
} }
public static List<NoteResponse> OrderAncestors(this List<NoteResponse> notes)
{
var final = new List<NoteResponse>();
foreach (var note in notes)
{
if (note.ReplyId == null)
{
final.Insert(0, note);
continue;
}
var parent = final.Find(p => p.Id == note.ReplyId);
if (parent != null)
final.Insert(final.IndexOf(parent) + 1, note);
else
final.Add(note);
}
return final;
}
public static List<StatusEntity> OrderAncestors(this List<StatusEntity> notes)
{
var final = new List<StatusEntity>();
foreach (var note in notes)
{
if (note.ReplyId == null)
{
final.Insert(0, note);
continue;
}
var parent = final.Find(p => p.Id == note.ReplyId);
if (parent != null)
final.Insert(final.IndexOf(parent) + 1, note);
else
final.Add(note);
}
return final;
}
public static List<NoteResponse> OrderDescendants(this List<NoteResponse> notes) public static List<NoteResponse> OrderDescendants(this List<NoteResponse> notes)
{ {
foreach (var note in notes) foreach (var note in notes)