Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 0.18.x comment ordering issue #1418

Merged
merged 6 commits into from
Nov 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions Mlem/App/Views/Shared/ExpandedPost/CommentTreeTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class CommentTreeTracker: Hashable {
if let first = newComments.first, first.api != root.wrappedValue.api {
resolveCommentTree(comments: newComments)
} else {
buildCommentTree(comments: newComments)
await buildCommentTree(comments: newComments)
}
loadingState = .done
errorDetails = nil
Expand Down Expand Up @@ -145,17 +145,40 @@ class CommentTreeTracker: Hashable {
}
}

private func buildCommentTree(comments newComments: [Comment2]) {
private func buildCommentTree(comments newComments: [Comment2]) async {
var output: [CommentWrapper] = []
var commentsKeyedById: [Int: CommentWrapper] = [:]
var commentsKeyedByActorId: [URL: CommentWrapper] = [:]

for comment in newComments {
// From 0.19.0 onwards, a comment's parent is guaranteed to precede it in the array.
//
// In 0.18.x versions, this isn't always the case - sometimes the parent can come after
// the child. As the tree-building logic relies on correct comment order, we need to sort
// the comments by depth before processing them.
//
// Also on 0.18.x, in super large comment threads where some comments are hidden under
// "More replies", comments may be included that don't have a parent *anywhere* in the
// list! There's nothing we can do in that circumstance, so those comments are ignored
// entirely. I'm not sure under what circumstances this happens. Going to the parent comment
// on lemmy-ui loads the comment just fine, but neither the "Show context" nor "Show replies"
// buttons work. This issue could be related to Lemmy 0.18, or maybe Beehaw's database is
// broken somehow. Comment example: https://beehaw.org/comment/4033679

var sortedComments: [Comment2]
if let version = try? await newComments.first?.api.version, version < .v19_0 {
sortedComments = newComments.sorted { $0.depth < $1.depth }
} else {
sortedComments = newComments
}

for comment in sortedComments {
let wrapper: CommentWrapper = .init(comment)
commentsKeyedById[comment.id] = wrapper
commentsKeyedByActorId[comment.actorId] = wrapper
if let parentId = comment.parentCommentIds.last, comment.depth > root.depth {
commentsKeyedById[parentId]?.addChild(wrapper)
if let parent = commentsKeyedById[parentId] {
parent.addChild(wrapper)
}
} else {
output.append(wrapper)
}
Expand Down
Loading