Work around historical handling of allow field
This commit is contained in:
@@ -11,13 +11,13 @@ import {msg, Trans} from '@lingui/macro'
|
|||||||
import {useLingui} from '@lingui/react'
|
import {useLingui} from '@lingui/react'
|
||||||
import {useQueryClient} from '@tanstack/react-query'
|
import {useQueryClient} from '@tanstack/react-query'
|
||||||
|
|
||||||
import {createThreadgate} from '#/lib/api'
|
|
||||||
import {until} from '#/lib/async/until'
|
import {until} from '#/lib/async/until'
|
||||||
import {HITSLOP_10} from '#/lib/constants'
|
import {HITSLOP_10} from '#/lib/constants'
|
||||||
import {makeListLink, makeProfileLink} from '#/lib/routes/links'
|
import {makeListLink, makeProfileLink} from '#/lib/routes/links'
|
||||||
import {logger} from '#/logger'
|
import {logger} from '#/logger'
|
||||||
import {isNative} from '#/platform/detection'
|
import {isNative} from '#/platform/detection'
|
||||||
import {RQKEY_ROOT as POST_THREAD_RQKEY_ROOT} from '#/state/queries/post-thread'
|
import {RQKEY_ROOT as POST_THREAD_RQKEY_ROOT} from '#/state/queries/post-thread'
|
||||||
|
import {updateThreadgateAllow} from '#/state/queries/threadgate'
|
||||||
import {threadgateRecordQueryKeyRoot} from '#/state/queries/threadgate'
|
import {threadgateRecordQueryKeyRoot} from '#/state/queries/threadgate'
|
||||||
import {
|
import {
|
||||||
ThreadgateAllowUISetting,
|
ThreadgateAllowUISetting,
|
||||||
@@ -88,15 +88,13 @@ export function WhoCanReply({post, isThreadAuthor, style}: WhoCanReplyProps) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
if (newSettings.length) {
|
await updateThreadgateAllow({
|
||||||
await createThreadgate(agent, post.uri, newSettings)
|
agent,
|
||||||
} else {
|
postUri: post.uri,
|
||||||
await agent.api.com.atproto.repo.deleteRecord({
|
allow: newSettings,
|
||||||
repo: agent.session!.did,
|
})
|
||||||
collection: 'app.bsky.feed.threadgate',
|
|
||||||
rkey: new AtUri(post.uri).rkey,
|
// TODO
|
||||||
})
|
|
||||||
}
|
|
||||||
await whenAppViewReady(agent, post.uri, res => {
|
await whenAppViewReady(agent, post.uri, res => {
|
||||||
const thread = res.data.thread
|
const thread = res.data.thread
|
||||||
if (AppBskyFeedDefs.isThreadViewPost(thread)) {
|
if (AppBskyFeedDefs.isThreadViewPost(thread)) {
|
||||||
|
|||||||
+11
-12
@@ -12,9 +12,10 @@ import {AtUri} from '@atproto/api'
|
|||||||
|
|
||||||
import {logger} from '#/logger'
|
import {logger} from '#/logger'
|
||||||
import {
|
import {
|
||||||
createThreadgate as upsertThreadgate,
|
createThreadgateRecord,
|
||||||
ThreadgateAllowUISetting,
|
ThreadgateAllowUISetting,
|
||||||
threadgateAllowUISettingToAllowType,
|
threadgateAllowUISettingToAllowRecordValue,
|
||||||
|
writeThreadgateRecord,
|
||||||
} from '#/state/queries/threadgate'
|
} from '#/state/queries/threadgate'
|
||||||
import {isNetworkError} from 'lib/strings/errors'
|
import {isNetworkError} from 'lib/strings/errors'
|
||||||
import {shortenLinks, stripInvalidMentions} from 'lib/strings/rich-text-manip'
|
import {shortenLinks, stripInvalidMentions} from 'lib/strings/rich-text-manip'
|
||||||
@@ -265,7 +266,14 @@ export async function post(agent: BskyAgent, opts: PostOpts) {
|
|||||||
try {
|
try {
|
||||||
// TODO: this needs to be batch-created with the post!
|
// TODO: this needs to be batch-created with the post!
|
||||||
if (opts.threadgate?.length) {
|
if (opts.threadgate?.length) {
|
||||||
await createThreadgate(agent, res.uri, opts.threadgate)
|
await writeThreadgateRecord({
|
||||||
|
agent,
|
||||||
|
postUri: res.uri,
|
||||||
|
threadgate: createThreadgateRecord({
|
||||||
|
post: res.uri,
|
||||||
|
allow: threadgateAllowUISettingToAllowRecordValue(opts.threadgate),
|
||||||
|
}),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
console.error(`Failed to create threadgate: ${e.toString()}`)
|
console.error(`Failed to create threadgate: ${e.toString()}`)
|
||||||
@@ -277,15 +285,6 @@ export async function post(agent: BskyAgent, opts: PostOpts) {
|
|||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function createThreadgate(
|
|
||||||
agent: BskyAgent,
|
|
||||||
postUri: string,
|
|
||||||
threadgate: ThreadgateAllowUISetting[],
|
|
||||||
) {
|
|
||||||
const allow = threadgateAllowUISettingToAllowType(threadgate)
|
|
||||||
return upsertThreadgate({agent, postUri, threadgate: {allow}})
|
|
||||||
}
|
|
||||||
|
|
||||||
// helpers
|
// helpers
|
||||||
// =
|
// =
|
||||||
|
|
||||||
|
|||||||
@@ -2,9 +2,11 @@ import {AppBskyFeedThreadgate, AtUri, BskyAgent} from '@atproto/api'
|
|||||||
import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query'
|
import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query'
|
||||||
|
|
||||||
import {networkRetry} from '#/lib/async/retry'
|
import {networkRetry} from '#/lib/async/retry'
|
||||||
|
import {ThreadgateAllowUISetting} from '#/state/queries/threadgate/types'
|
||||||
import {
|
import {
|
||||||
createThreadgateRecord,
|
createThreadgateRecord,
|
||||||
mergeThreadgateRecords,
|
mergeThreadgateRecords,
|
||||||
|
threadgateAllowUISettingToAllowRecordValue,
|
||||||
} from '#/state/queries/threadgate/util'
|
} from '#/state/queries/threadgate/util'
|
||||||
import {useAgent} from '#/state/session'
|
import {useAgent} from '#/state/session'
|
||||||
|
|
||||||
@@ -31,21 +33,10 @@ export function useThreadgateRecordQuery({
|
|||||||
queryKey: createThreadgateRecordQueryKey(postUri || ''),
|
queryKey: createThreadgateRecordQueryKey(postUri || ''),
|
||||||
placeholderData: initialData,
|
placeholderData: initialData,
|
||||||
async queryFn() {
|
async queryFn() {
|
||||||
const urip = new AtUri(postUri!)
|
return getThreadgateRecord({
|
||||||
|
agent,
|
||||||
if (!urip.host.startsWith('did:')) {
|
postUri: postUri!,
|
||||||
const res = await agent.resolveHandle({
|
|
||||||
handle: urip.host,
|
|
||||||
})
|
|
||||||
urip.host = res.data.did
|
|
||||||
}
|
|
||||||
|
|
||||||
const {value} = await agent.api.app.bsky.feed.threadgate.get({
|
|
||||||
repo: urip.host,
|
|
||||||
rkey: urip.rkey,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return value
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -57,19 +48,28 @@ export async function getThreadgateRecord({
|
|||||||
agent: BskyAgent
|
agent: BskyAgent
|
||||||
postUri: string
|
postUri: string
|
||||||
}): Promise<AppBskyFeedThreadgate.Record | undefined> {
|
}): Promise<AppBskyFeedThreadgate.Record | undefined> {
|
||||||
const postUrip = new AtUri(postUri)
|
const urip = new AtUri(postUri)
|
||||||
|
|
||||||
|
if (!urip.host.startsWith('did:')) {
|
||||||
|
const res = await agent.resolveHandle({
|
||||||
|
handle: urip.host,
|
||||||
|
})
|
||||||
|
urip.host = res.data.did
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const {data} = await networkRetry(2, () =>
|
const {data} = await networkRetry(2, () =>
|
||||||
agent.api.com.atproto.repo.getRecord({
|
agent.api.com.atproto.repo.getRecord({
|
||||||
repo: agent.session!.did,
|
repo: urip.host,
|
||||||
collection: 'app.bsky.feed.threadgate',
|
collection: 'app.bsky.feed.threadgate',
|
||||||
rkey: postUrip.rkey,
|
rkey: urip.rkey,
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
if (data.value && AppBskyFeedThreadgate.isRecord(data.value)) {
|
if (data.value && AppBskyFeedThreadgate.isRecord(data.value)) {
|
||||||
return data.value
|
return data.value
|
||||||
|
} else {
|
||||||
|
return undefined
|
||||||
}
|
}
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
if (e.message.includes(`Could not locate record:`)) {
|
if (e.message.includes(`Could not locate record:`)) {
|
||||||
@@ -80,60 +80,7 @@ export async function getThreadgateRecord({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function createThreadgate({
|
export async function writeThreadgateRecord({
|
||||||
agent,
|
|
||||||
postUri,
|
|
||||||
threadgate,
|
|
||||||
}: {
|
|
||||||
agent: BskyAgent
|
|
||||||
postUri: string
|
|
||||||
threadgate: Partial<AppBskyFeedThreadgate.Record>
|
|
||||||
}) {
|
|
||||||
const postUrip = new AtUri(postUri)
|
|
||||||
|
|
||||||
const {data} = await networkRetry(2, () =>
|
|
||||||
agent.api.com.atproto.repo.getRecord({
|
|
||||||
repo: agent.session!.did,
|
|
||||||
collection: 'app.bsky.feed.threadgate',
|
|
||||||
rkey: postUrip.rkey,
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
|
|
||||||
if (data.value && AppBskyFeedThreadgate.isRecord(data.value)) {
|
|
||||||
// has existing, merge
|
|
||||||
const prev = data.value
|
|
||||||
const merged = mergeThreadgateRecords(prev, threadgate)
|
|
||||||
|
|
||||||
await networkRetry(2, () =>
|
|
||||||
agent.api.com.atproto.repo.putRecord({
|
|
||||||
repo: agent.session!.did,
|
|
||||||
collection: 'app.bsky.feed.threadgate',
|
|
||||||
rkey: postUrip.rkey,
|
|
||||||
record: merged,
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
// no existing, create new
|
|
||||||
const record: AppBskyFeedThreadgate.Record = {
|
|
||||||
$type: 'app.bsky.feed.threadgate',
|
|
||||||
post: postUri,
|
|
||||||
allow: threadgate.allow || [],
|
|
||||||
hiddenReplies: threadgate.hiddenReplies || [],
|
|
||||||
createdAt: new Date().toISOString(),
|
|
||||||
}
|
|
||||||
|
|
||||||
await networkRetry(2, () =>
|
|
||||||
agent.api.com.atproto.repo.putRecord({
|
|
||||||
repo: agent.session!.did,
|
|
||||||
collection: 'app.bsky.feed.threadgate',
|
|
||||||
rkey: postUrip.rkey,
|
|
||||||
record,
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function overwriteThreadgateRecord({
|
|
||||||
agent,
|
agent,
|
||||||
postUri,
|
postUri,
|
||||||
threadgate,
|
threadgate,
|
||||||
@@ -143,13 +90,11 @@ export async function overwriteThreadgateRecord({
|
|||||||
threadgate: AppBskyFeedThreadgate.Record
|
threadgate: AppBskyFeedThreadgate.Record
|
||||||
}) {
|
}) {
|
||||||
const postUrip = new AtUri(postUri)
|
const postUrip = new AtUri(postUri)
|
||||||
const record: AppBskyFeedThreadgate.Record = {
|
const record = createThreadgateRecord({
|
||||||
$type: 'app.bsky.feed.threadgate',
|
|
||||||
post: postUri,
|
post: postUri,
|
||||||
allow: threadgate.allow || [],
|
allow: threadgate.allow, // can/should be undefined!
|
||||||
hiddenReplies: threadgate.hiddenReplies || [],
|
hiddenReplies: threadgate.hiddenReplies || [],
|
||||||
createdAt: new Date().toISOString(),
|
})
|
||||||
}
|
|
||||||
|
|
||||||
await networkRetry(2, () =>
|
await networkRetry(2, () =>
|
||||||
agent.api.com.atproto.repo.putRecord({
|
agent.api.com.atproto.repo.putRecord({
|
||||||
@@ -161,29 +106,57 @@ export async function overwriteThreadgateRecord({
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useCreateThreadgateMutation() {
|
export async function upsertThreadgate(
|
||||||
const agent = useAgent()
|
{
|
||||||
const queryClient = useQueryClient()
|
agent,
|
||||||
|
postUri,
|
||||||
|
}: {
|
||||||
|
agent: BskyAgent
|
||||||
|
postUri: string
|
||||||
|
},
|
||||||
|
callback: (
|
||||||
|
threadgate: AppBskyFeedThreadgate.Record | undefined,
|
||||||
|
) => Promise<AppBskyFeedThreadgate.Record | undefined>,
|
||||||
|
) {
|
||||||
|
const prev = await getThreadgateRecord({
|
||||||
|
agent,
|
||||||
|
postUri,
|
||||||
|
})
|
||||||
|
const next = await callback(prev)
|
||||||
|
if (!next) return
|
||||||
|
await writeThreadgateRecord({
|
||||||
|
agent,
|
||||||
|
postUri,
|
||||||
|
threadgate: next,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
return useMutation({
|
/**
|
||||||
mutationFn: async ({
|
* Update the allow list for a threadgate record.
|
||||||
postUri,
|
*
|
||||||
threadgate,
|
* Note: to allow everyone to reply, pass `allow: []`.
|
||||||
}: {
|
*/
|
||||||
postUri: string
|
export async function updateThreadgateAllow({
|
||||||
threadgate: Partial<AppBskyFeedThreadgate.Record>
|
agent,
|
||||||
}) => {
|
postUri,
|
||||||
return createThreadgate({
|
allow,
|
||||||
agent,
|
}: {
|
||||||
postUri,
|
agent: BskyAgent
|
||||||
threadgate,
|
postUri: string
|
||||||
|
allow: ThreadgateAllowUISetting[]
|
||||||
|
}) {
|
||||||
|
return upsertThreadgate({agent, postUri}, async prev => {
|
||||||
|
if (prev) {
|
||||||
|
return {
|
||||||
|
...prev,
|
||||||
|
allow: threadgateAllowUISettingToAllowRecordValue(allow),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return createThreadgateRecord({
|
||||||
|
post: postUri,
|
||||||
|
allow: threadgateAllowUISettingToAllowRecordValue(allow),
|
||||||
})
|
})
|
||||||
},
|
}
|
||||||
onSuccess() {
|
|
||||||
queryClient.invalidateQueries({
|
|
||||||
queryKey: [threadgateRecordQueryKeyRoot],
|
|
||||||
})
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -201,44 +174,28 @@ export function useToggleReplyVisibilityMutation() {
|
|||||||
replyUri: string
|
replyUri: string
|
||||||
action: 'hide' | 'show'
|
action: 'hide' | 'show'
|
||||||
}) => {
|
}) => {
|
||||||
const prev = await getThreadgateRecord({
|
await upsertThreadgate({agent, postUri}, async prev => {
|
||||||
agent,
|
if (prev) {
|
||||||
postUri,
|
if (action === 'hide') {
|
||||||
})
|
return mergeThreadgateRecords(prev, {
|
||||||
|
hiddenReplies: [replyUri],
|
||||||
if (prev) {
|
})
|
||||||
let threadgate = prev
|
} else if (action === 'show') {
|
||||||
|
return {
|
||||||
if (action === 'hide') {
|
...prev,
|
||||||
threadgate = mergeThreadgateRecords(prev, {
|
hiddenReplies:
|
||||||
hiddenReplies: [replyUri],
|
prev.hiddenReplies?.filter(uri => uri !== replyUri) || [],
|
||||||
})
|
}
|
||||||
} else if (action === 'show') {
|
}
|
||||||
threadgate = {
|
} else {
|
||||||
...prev,
|
if (action === 'hide') {
|
||||||
hiddenReplies:
|
return createThreadgateRecord({
|
||||||
prev.hiddenReplies?.filter(uri => uri !== replyUri) || [],
|
post: postUri,
|
||||||
|
hiddenReplies: [replyUri],
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
})
|
||||||
await overwriteThreadgateRecord({
|
|
||||||
agent,
|
|
||||||
postUri,
|
|
||||||
threadgate,
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
if (action === 'hide') {
|
|
||||||
const threadgate = createThreadgateRecord({
|
|
||||||
post: postUri,
|
|
||||||
hiddenReplies: [replyUri],
|
|
||||||
})
|
|
||||||
await overwriteThreadgateRecord({
|
|
||||||
agent,
|
|
||||||
postUri,
|
|
||||||
threadgate,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
onSuccess() {
|
onSuccess() {
|
||||||
queryClient.invalidateQueries({
|
queryClient.invalidateQueries({
|
||||||
|
|||||||
@@ -15,12 +15,21 @@ export function threadgateViewToAllowUISetting(
|
|||||||
AppBskyFeedThreadgate.validateRecord(threadgate.record).success
|
AppBskyFeedThreadgate.validateRecord(threadgate.record).success
|
||||||
? threadgate.record
|
? threadgate.record
|
||||||
: null
|
: null
|
||||||
if (!record) {
|
/*
|
||||||
|
* If record doesn't exist (default), or if `record.allow === undefined`, it means
|
||||||
|
* anyone can reply.
|
||||||
|
*
|
||||||
|
* If `record.allow === []` it means no one can reply, and we translate to UI code
|
||||||
|
* here. This was a historical choice, and we have no lexicon representation
|
||||||
|
* for 'replies disabled' other than an empty array.
|
||||||
|
*/
|
||||||
|
if (!record || record.allow === undefined) {
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
if (!record.allow?.length) {
|
if (record.allow.length === 0) {
|
||||||
return [{type: 'nobody'}]
|
return [{type: 'nobody'}]
|
||||||
}
|
}
|
||||||
|
|
||||||
const settings: ThreadgateAllowUISetting[] = record.allow
|
const settings: ThreadgateAllowUISetting[] = record.allow
|
||||||
.map(allow => {
|
.map(allow => {
|
||||||
let setting: ThreadgateAllowUISetting | undefined
|
let setting: ThreadgateAllowUISetting | undefined
|
||||||
@@ -38,12 +47,18 @@ export function threadgateViewToAllowUISetting(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts a list of {@link ThreadgateAllowUISetting} to the `allow` prop on
|
* Converts an array of {@link ThreadgateAllowUISetting} to the `allow` prop on
|
||||||
* {@link AppBskyFeedThreadgate.Record},
|
* {@link AppBskyFeedThreadgate.Record}.
|
||||||
|
*
|
||||||
|
* If the array passed is empty, we infer that to mean anyone can reply, and
|
||||||
|
* return undefined. An undefined value in the record is interpretted as anyone
|
||||||
|
* can reply, whereas an empty array means no on can reply.
|
||||||
*/
|
*/
|
||||||
export function threadgateAllowUISettingToAllowType(
|
export function threadgateAllowUISettingToAllowRecordValue(
|
||||||
threadgate: ThreadgateAllowUISetting[],
|
threadgate: ThreadgateAllowUISetting[],
|
||||||
) {
|
): AppBskyFeedThreadgate.Record['allow'] {
|
||||||
|
if (threadgate.length === 0) return undefined
|
||||||
|
|
||||||
let allow: (
|
let allow: (
|
||||||
| AppBskyFeedThreadgate.MentionRule
|
| AppBskyFeedThreadgate.MentionRule
|
||||||
| AppBskyFeedThreadgate.FollowingRule
|
| AppBskyFeedThreadgate.FollowingRule
|
||||||
@@ -90,6 +105,14 @@ export function mergeThreadgateRecords(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new {@link AppBskyFeedThreadgate.Record} object with the given
|
||||||
|
* properties.
|
||||||
|
*
|
||||||
|
* Note: setting `allow` to `undefined` resets and allows everyone to reply. An
|
||||||
|
* empty array means that no one can reply. This is a bit of a hack bc of how
|
||||||
|
* these were designed, but should be fine as long as we're careful.
|
||||||
|
*/
|
||||||
export function createThreadgateRecord(
|
export function createThreadgateRecord(
|
||||||
threadgate: Partial<AppBskyFeedThreadgate.Record>,
|
threadgate: Partial<AppBskyFeedThreadgate.Record>,
|
||||||
): AppBskyFeedThreadgate.Record {
|
): AppBskyFeedThreadgate.Record {
|
||||||
@@ -101,7 +124,7 @@ export function createThreadgateRecord(
|
|||||||
$type: 'app.bsky.feed.threadgate',
|
$type: 'app.bsky.feed.threadgate',
|
||||||
post: threadgate.post,
|
post: threadgate.post,
|
||||||
createdAt: new Date().toISOString(),
|
createdAt: new Date().toISOString(),
|
||||||
allow: threadgate.allow || [],
|
allow: threadgate.allow, // can be undefined!
|
||||||
hiddenReplies: threadgate.hiddenReplies || [],
|
hiddenReplies: threadgate.hiddenReplies || [],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user