Fix bug where optimistic replies were inserted above the root post

This commit is contained in:
Eric Bailey
2025-07-17 11:32:55 -05:00
parent 42bfee5d9d
commit e80120e379
2 changed files with 13 additions and 4 deletions
@@ -93,6 +93,7 @@ export function createCacheMutator({
const opDid = getRootPostAtUri(existingParent.value.post)?.host const opDid = getRootPostAtUri(existingParent.value.post)?.host
const nextItem = thread.at(i + 1) const nextItem = thread.at(i + 1)
const isReplyToRoot = existingParent.depth === 0 const isReplyToRoot = existingParent.depth === 0
const isReplyBelowRoot = existingParent.depth > 0
const isEndOfReplyChain = const isEndOfReplyChain =
!nextItem || nextItem.depth <= existingParent.depth !nextItem || nextItem.depth <= existingParent.depth
const firstReply = replies.at(0) const firstReply = replies.at(0)
@@ -103,17 +104,19 @@ export function createCacheMutator({
: false : false
/* /*
* Always insert replies if the following conditions are met. * Always insert replies if the following conditions are met. Max
* depth checks are handled below.
*/ */
const shouldAlwaysInsertReplies = const shouldAlwaysInsertReplies =
isReplyToRoot || isReplyToRoot ||
params.view === 'tree' || (params.view === 'tree' && isReplyBelowRoot) ||
(params.view === 'linear' && isEndOfReplyChain) (params.view === 'linear' && isEndOfReplyChain)
/* /*
* Maybe insert replies if the replier is the OP and certain conditions are met * Maybe insert replies if the replier is the OP and certain
* conditions are met
*/ */
const shouldReplaceWithOPReplies = const shouldReplaceWithOPReplies =
!isReplyToRoot && params.view === 'linear' && opIsReplier params.view === 'linear' && opIsReplier && isReplyBelowRoot
if (shouldAlwaysInsertReplies || shouldReplaceWithOPReplies) { if (shouldAlwaysInsertReplies || shouldReplaceWithOPReplies) {
const branch = getBranch(thread, i, existingParent.depth) const branch = getBranch(thread, i, existingParent.depth)
+6
View File
@@ -33,6 +33,12 @@ export function getRootPostAtUri(post: AppBskyFeedDefs.PostView) {
AppBskyFeedPost.isRecord, AppBskyFeedPost.isRecord,
) )
) { ) {
/**
* If the record has no `reply` field, it is a root post.
*/
if (!post.record.reply) {
return new AtUri(post.uri)
}
if (post.record.reply?.root?.uri) { if (post.record.reply?.root?.uri) {
return new AtUri(post.record.reply.root.uri) return new AtUri(post.record.reply.root.uri)
} }