[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:
parent
764ef7b9cf
commit
0e6864fe38
3 changed files with 49 additions and 3 deletions
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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")]
|
||||||
|
|
|
@ -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)
|
||||||
|
|
Loading…
Add table
Reference in a new issue