Working optimistic reply insertions, preference for OP
This commit is contained in:
@@ -6,11 +6,13 @@ import {findAllPostsInQueryData as findAllPostsInNotifsQueryData} from '#/state/
|
|||||||
import {findAllPostsInQueryData as findAllPostsInFeedQueryData} from '#/state/queries/post-feed'
|
import {findAllPostsInQueryData as findAllPostsInFeedQueryData} from '#/state/queries/post-feed'
|
||||||
import {findAllPostsInQueryData as findAllPostsInQuoteQueryData} from '#/state/queries/post-quotes'
|
import {findAllPostsInQueryData as findAllPostsInQuoteQueryData} from '#/state/queries/post-quotes'
|
||||||
import {findAllPostsInQueryData as findAllPostsInSearchQueryData} from '#/state/queries/search-posts'
|
import {findAllPostsInQueryData as findAllPostsInSearchQueryData} from '#/state/queries/search-posts'
|
||||||
|
import {getBranch} from '#/state/queries/usePostThread/traversal'
|
||||||
import {
|
import {
|
||||||
type createPostThreadQueryKey,
|
type createPostThreadQueryKey,
|
||||||
postThreadQueryKeyRoot,
|
|
||||||
type PostThreadParams,
|
type PostThreadParams,
|
||||||
|
postThreadQueryKeyRoot,
|
||||||
} from '#/state/queries/usePostThread/types'
|
} from '#/state/queries/usePostThread/types'
|
||||||
|
import {getRootPostAtUri} from '#/state/queries/usePostThread/utils'
|
||||||
import {
|
import {
|
||||||
embedViewToThreadPlaceholder,
|
embedViewToThreadPlaceholder,
|
||||||
postViewToThreadPlaceholder,
|
postViewToThreadPlaceholder,
|
||||||
@@ -58,31 +60,49 @@ export function createCacheMutator({
|
|||||||
replyCount: parent.value.post.replyCount,
|
replyCount: parent.value.post.replyCount,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 isEndOfReplyChain =
|
const isEndOfReplyChain =
|
||||||
!nextItem || nextItem.depth <= existingParent.depth
|
!nextItem || nextItem.depth <= existingParent.depth
|
||||||
const shouldInsertReplies =
|
const firstReply = replies.at(0)
|
||||||
|
const opIsReplier =
|
||||||
|
AppBskyUnspeccedGetPostThreadV2.isThreadItemPost(
|
||||||
|
firstReply?.value,
|
||||||
|
)
|
||||||
|
? opDid === firstReply.value.post.author.did
|
||||||
|
: false
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Always insert replies if the following conditions are met.
|
||||||
|
*/
|
||||||
|
const shouldAlwaysInsertReplies =
|
||||||
isReplyToRoot ||
|
isReplyToRoot ||
|
||||||
params.view === 'tree' ||
|
params.view === 'tree' ||
|
||||||
(params.view === 'linear' && isEndOfReplyChain)
|
(params.view === 'linear' && isEndOfReplyChain)
|
||||||
|
|
||||||
if (shouldInsertReplies) {
|
|
||||||
/*
|
/*
|
||||||
* Splice in new replies
|
* Maybe insert replies if the replier is the OP and certain conditions are met
|
||||||
*/
|
*/
|
||||||
for (let ri = 0; ri < replies.length; ri++) {
|
const shouldReplaceWithOPReplies =
|
||||||
const reply = replies[ri]
|
!isReplyToRoot && params.view === 'linear' && opIsReplier
|
||||||
if (
|
|
||||||
!AppBskyUnspeccedGetPostThreadV2.isThreadItemPost(reply.value)
|
if (shouldAlwaysInsertReplies || shouldReplaceWithOPReplies) {
|
||||||
|
const branch = getBranch(thread, i, existingParent.depth)
|
||||||
|
/*
|
||||||
|
* OP insertions replace other replies _in linear view_.
|
||||||
|
*/
|
||||||
|
const itemsToRemove = shouldReplaceWithOPReplies
|
||||||
|
? branch.length
|
||||||
|
: 0
|
||||||
|
|
||||||
|
thread.splice(
|
||||||
|
i + 1,
|
||||||
|
itemsToRemove,
|
||||||
|
...replies.map((r, ri) => {
|
||||||
|
r.depth = existingParent.depth + 1 + ri
|
||||||
|
return r
|
||||||
|
}),
|
||||||
)
|
)
|
||||||
continue
|
|
||||||
const insertIndex = i + 1 + ri
|
|
||||||
thread.splice(insertIndex, 0, {
|
|
||||||
...reply,
|
|
||||||
depth: existingParent.depth + 1 + ri,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
import {
|
import {
|
||||||
AtUri,
|
|
||||||
AppBskyUnspeccedGetPostThreadV2,
|
AppBskyUnspeccedGetPostThreadV2,
|
||||||
type ModerationDecision,
|
type ModerationDecision,
|
||||||
type ModerationOpts,
|
type ModerationOpts,
|
||||||
} from '@atproto/api'
|
} from '@atproto/api'
|
||||||
|
|
||||||
import {HiddenReplyKind, type PostThreadParams, type Slice} from '#/state/queries/usePostThread/types'
|
import {HiddenReplyKind, type Slice} from '#/state/queries/usePostThread/types'
|
||||||
import * as views from '#/state/queries/usePostThread/views'
|
import * as views from '#/state/queries/usePostThread/views'
|
||||||
|
|
||||||
export function flatten(
|
export function flatten(
|
||||||
@@ -76,11 +75,9 @@ export function flatten(
|
|||||||
export function sort(
|
export function sort(
|
||||||
thread: AppBskyUnspeccedGetPostThreadV2.OutputSchema['thread'],
|
thread: AppBskyUnspeccedGetPostThreadV2.OutputSchema['thread'],
|
||||||
{
|
{
|
||||||
view,
|
|
||||||
threadgateHiddenReplies,
|
threadgateHiddenReplies,
|
||||||
moderationOpts,
|
moderationOpts,
|
||||||
}: {
|
}: {
|
||||||
view: PostThreadParams['view']
|
|
||||||
threadgateHiddenReplies: Set<string>
|
threadgateHiddenReplies: Set<string>
|
||||||
moderationOpts: ModerationOpts
|
moderationOpts: ModerationOpts
|
||||||
},
|
},
|
||||||
@@ -197,73 +194,13 @@ export function sort(
|
|||||||
})
|
})
|
||||||
const postMod = getModerationState(post.moderation)
|
const postMod = getModerationState(post.moderation)
|
||||||
const postIsHidden = threadgateHiddenReplies.has(item.uri)
|
const postIsHidden = threadgateHiddenReplies.has(item.uri)
|
||||||
const postIsModerated =
|
const postIsModerated = postIsHidden || postMod.blurred || postMod.muted
|
||||||
postIsHidden || postMod.blurred || postMod.muted
|
|
||||||
|
|
||||||
if (!postIsModerated) {
|
if (!postIsModerated) {
|
||||||
/*
|
/*
|
||||||
* Not moderated, probably need to insert it
|
* Not moderated, probably need to insert it
|
||||||
*/
|
*/
|
||||||
items.push(post)
|
items.push(post)
|
||||||
// if (view === 'tree') {
|
|
||||||
// items.push(post)
|
|
||||||
// } else {
|
|
||||||
// if (post.depth > 1) {
|
|
||||||
// const maybeNextSiblingIndex = getBranch(thread, i, post.depth).end + 1
|
|
||||||
// const maybeNextSibling = thread.at(maybeNextSiblingIndex)
|
|
||||||
|
|
||||||
// /*
|
|
||||||
// * If we've got two replies at the same depth, we need to decide
|
|
||||||
// * which one to show.
|
|
||||||
// */
|
|
||||||
// if (post.depth === maybeNextSibling?.depth) {
|
|
||||||
// // continue with next sibling
|
|
||||||
// i = maybeNextSiblingIndex - 1
|
|
||||||
// debugger;
|
|
||||||
// continue traversal
|
|
||||||
// } else {
|
|
||||||
// const maybePrevSiblingIndex = getBranchUp(thread, i - 1, post.depth).end
|
|
||||||
// const maybePrevSibling = thread.at(maybePrevSiblingIndex)
|
|
||||||
|
|
||||||
// if (post.depth === maybePrevSibling?.depth) {
|
|
||||||
// const post = views.threadPost({
|
|
||||||
// uri: item.uri,
|
|
||||||
// depth: item.depth,
|
|
||||||
// value: item.value,
|
|
||||||
// oneUp: thread.at(maybePrevSiblingIndex - 1),
|
|
||||||
// oneDown,
|
|
||||||
// moderationOpts,
|
|
||||||
// })
|
|
||||||
// debugger;
|
|
||||||
// items.push(post)
|
|
||||||
// } else {
|
|
||||||
// items.push(post)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// /*
|
|
||||||
// if (post.depth === oneDown?.depth) {
|
|
||||||
// const opDid = new AtUri(post.value.post.record.reply?.root?.uri).host
|
|
||||||
// const postAuthorDid = new AtUri(post.uri).host
|
|
||||||
// debugger;
|
|
||||||
// if (postAuthorDid === opDid) {
|
|
||||||
// // prioritize OP optimistic reply
|
|
||||||
// items.push(post)
|
|
||||||
// // skip next reply
|
|
||||||
// i = getBranch(thread, i, oneDown.depth).end
|
|
||||||
// debugger;
|
|
||||||
// continue traversal
|
|
||||||
// } else {
|
|
||||||
// i = getBranch(thread, i, post.depth).end
|
|
||||||
// debugger;
|
|
||||||
// continue traversal
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// */
|
|
||||||
// } else {
|
|
||||||
// items.push(post)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
} else {
|
} else {
|
||||||
/*
|
/*
|
||||||
* Moderated in some way, we're going to walk children
|
* Moderated in some way, we're going to walk children
|
||||||
@@ -351,7 +288,7 @@ export function sort(
|
|||||||
*
|
*
|
||||||
* const { start: 1, end: 3 } = getBranch(items, 1, 1)
|
* const { start: 1, end: 3 } = getBranch(items, 1, 1)
|
||||||
*/
|
*/
|
||||||
function getBranch(
|
export function getBranch(
|
||||||
thread: AppBskyUnspeccedGetPostThreadV2.OutputSchema['thread'],
|
thread: AppBskyUnspeccedGetPostThreadV2.OutputSchema['thread'],
|
||||||
branchStartIndex: number,
|
branchStartIndex: number,
|
||||||
branchStartDepth: number,
|
branchStartDepth: number,
|
||||||
@@ -371,29 +308,7 @@ function getBranch(
|
|||||||
return {
|
return {
|
||||||
start: branchStartIndex,
|
start: branchStartIndex,
|
||||||
end,
|
end,
|
||||||
}
|
length: end - branchStartIndex,
|
||||||
}
|
|
||||||
|
|
||||||
function getBranchUp(
|
|
||||||
thread: AppBskyUnspeccedGetPostThreadV2.OutputSchema['thread'],
|
|
||||||
branchStartIndex: number,
|
|
||||||
branchEndDepth: number,
|
|
||||||
) {
|
|
||||||
let start = branchStartIndex
|
|
||||||
|
|
||||||
for (let ci = branchStartIndex; ci >= 0; ci--) {
|
|
||||||
const next = thread[ci]
|
|
||||||
if (next.depth > branchEndDepth) {
|
|
||||||
start = ci
|
|
||||||
} else {
|
|
||||||
start = ci
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
start,
|
|
||||||
end: branchStartIndex,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
import {
|
import {
|
||||||
APP_BSKY_UNSPECCED,
|
APP_BSKY_UNSPECCED,
|
||||||
|
type AppBskyFeedDefs,
|
||||||
|
AppBskyFeedPost,
|
||||||
AppBskyFeedThreadgate,
|
AppBskyFeedThreadgate,
|
||||||
type AppBskyUnspeccedGetPostThreadV2,
|
type AppBskyUnspeccedGetPostThreadV2,
|
||||||
|
AtUri,
|
||||||
} from '@atproto/api'
|
} from '@atproto/api'
|
||||||
|
|
||||||
import {type PostThreadParams} from '#/state/queries/usePostThread/types'
|
import {type PostThreadParams} from '#/state/queries/usePostThread/types'
|
||||||
@@ -32,3 +35,16 @@ export function getThreadgateRecord(
|
|||||||
? view?.record
|
? view?.record
|
||||||
: undefined
|
: undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getRootPostAtUri(post: AppBskyFeedDefs.PostView) {
|
||||||
|
if (
|
||||||
|
bsky.dangerousIsType<AppBskyFeedPost.Record>(
|
||||||
|
post.record,
|
||||||
|
AppBskyFeedPost.isRecord,
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
if (post.record.reply?.root?.uri) {
|
||||||
|
return new AtUri(post.record.reply.root.uri)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user