Re-org
This commit is contained in:
@@ -0,0 +1,71 @@
|
|||||||
|
import {AppBskyFeedThreadgate, AtUri, BskyAgent} from '@atproto/api'
|
||||||
|
import {useQuery} from '@tanstack/react-query'
|
||||||
|
|
||||||
|
import {useAgent} from '#/state/session'
|
||||||
|
|
||||||
|
export * from '#/state/queries/threadgate/types'
|
||||||
|
export * from '#/state/queries/threadgate/util'
|
||||||
|
|
||||||
|
export const threadgateRecordQueryKeyRoot = 'threadgate-record'
|
||||||
|
export const createThreadgateRecordQueryKey = (uri: string) => [
|
||||||
|
threadgateRecordQueryKeyRoot,
|
||||||
|
uri,
|
||||||
|
]
|
||||||
|
|
||||||
|
export function useThreadgateRecordQuery({
|
||||||
|
postUri,
|
||||||
|
initialData,
|
||||||
|
}: {
|
||||||
|
postUri?: string
|
||||||
|
initialData?: AppBskyFeedThreadgate.Record
|
||||||
|
} = {}) {
|
||||||
|
const agent = useAgent()
|
||||||
|
|
||||||
|
return useQuery({
|
||||||
|
enabled: !!postUri,
|
||||||
|
queryKey: createThreadgateRecordQueryKey(postUri || ''),
|
||||||
|
placeholderData: initialData,
|
||||||
|
async queryFn() {
|
||||||
|
const urip = new AtUri(postUri!)
|
||||||
|
|
||||||
|
if (!urip.host.startsWith('did:')) {
|
||||||
|
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
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createThreadgate({
|
||||||
|
agent,
|
||||||
|
postUri,
|
||||||
|
threadgate,
|
||||||
|
}: {
|
||||||
|
agent: BskyAgent
|
||||||
|
postUri: string
|
||||||
|
threadgate: Partial<AppBskyFeedThreadgate.Record>
|
||||||
|
}) {
|
||||||
|
const urip = new AtUri(postUri)
|
||||||
|
const record = {
|
||||||
|
...threadgate,
|
||||||
|
post: postUri,
|
||||||
|
createdAt: new Date().toISOString(),
|
||||||
|
}
|
||||||
|
|
||||||
|
return agent.api.app.bsky.feed.threadgate.create(
|
||||||
|
{
|
||||||
|
repo: urip.host,
|
||||||
|
rkey: urip.rkey,
|
||||||
|
},
|
||||||
|
record,
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
export type ThreadgateAllowUISetting =
|
||||||
|
| {type: 'nobody'}
|
||||||
|
| {type: 'mention'}
|
||||||
|
| {type: 'following'}
|
||||||
|
| {type: 'list'; list: unknown}
|
||||||
@@ -1,18 +1,6 @@
|
|||||||
import {
|
import {AppBskyFeedDefs, AppBskyFeedThreadgate} from '@atproto/api'
|
||||||
AppBskyFeedDefs,
|
|
||||||
AppBskyFeedThreadgate,
|
|
||||||
AtUri,
|
|
||||||
BskyAgent,
|
|
||||||
} from '@atproto/api'
|
|
||||||
import {useQuery} from '@tanstack/react-query'
|
|
||||||
|
|
||||||
import {useAgent} from '#/state/session'
|
import {ThreadgateAllowUISetting} from '#/state/queries/threadgate/types'
|
||||||
|
|
||||||
export type ThreadgateAllowUISetting =
|
|
||||||
| {type: 'nobody'}
|
|
||||||
| {type: 'mention'}
|
|
||||||
| {type: 'following'}
|
|
||||||
| {type: 'list'; list: unknown}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts a full {@link AppBskyFeedThreadgate.Record} to a list of
|
* Converts a full {@link AppBskyFeedThreadgate.Record} to a list of
|
||||||
@@ -80,66 +68,26 @@ export function threadgateAllowUISettingToAllowType(
|
|||||||
return allow
|
return allow
|
||||||
}
|
}
|
||||||
|
|
||||||
export const threadgateRecordQueryKeyRoot = 'threadgate-record'
|
/**
|
||||||
export const createThreadgateRecordQueryKey = (uri: string) => [
|
* Merges two {@link AppBskyFeedThreadgate.Record} objects, combining their
|
||||||
threadgateRecordQueryKeyRoot,
|
* `allow` and `hiddenReplies` arrays and de-deduplicating them.
|
||||||
uri,
|
*/
|
||||||
]
|
export function mergeThreadgateRecords(
|
||||||
|
prev: AppBskyFeedThreadgate.Record,
|
||||||
export function useThreadgateRecordQuery({
|
next: Partial<AppBskyFeedThreadgate.Record>,
|
||||||
postUri,
|
): AppBskyFeedThreadgate.Record {
|
||||||
initialData,
|
const allow = [...(prev.allow || []), ...(next.allow || [])].filter(
|
||||||
}: {
|
(v, i, a) => a.findIndex(t => t.$type === v.$type) === i,
|
||||||
postUri?: string
|
|
||||||
initialData?: AppBskyFeedThreadgate.Record
|
|
||||||
} = {}) {
|
|
||||||
const agent = useAgent()
|
|
||||||
|
|
||||||
return useQuery({
|
|
||||||
enabled: !!postUri,
|
|
||||||
queryKey: createThreadgateRecordQueryKey(postUri || ''),
|
|
||||||
placeholderData: initialData,
|
|
||||||
async queryFn() {
|
|
||||||
const urip = new AtUri(postUri!)
|
|
||||||
|
|
||||||
if (!urip.host.startsWith('did:')) {
|
|
||||||
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
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
export function createThreadgate({
|
|
||||||
agent,
|
|
||||||
postUri,
|
|
||||||
threadgate,
|
|
||||||
}: {
|
|
||||||
agent: BskyAgent
|
|
||||||
postUri: string
|
|
||||||
threadgate: Partial<AppBskyFeedThreadgate.Record>
|
|
||||||
}) {
|
|
||||||
const urip = new AtUri(postUri)
|
|
||||||
const record = {
|
|
||||||
...threadgate,
|
|
||||||
post: postUri,
|
|
||||||
createdAt: new Date().toISOString(),
|
|
||||||
}
|
|
||||||
|
|
||||||
return agent.api.app.bsky.feed.threadgate.create(
|
|
||||||
{
|
|
||||||
repo: urip.host,
|
|
||||||
rkey: urip.rkey,
|
|
||||||
},
|
|
||||||
record,
|
|
||||||
)
|
)
|
||||||
|
const hiddenReplies = Array.from(
|
||||||
|
new Set([...(prev.hiddenReplies || []), ...(next.hiddenReplies || [])]),
|
||||||
|
)
|
||||||
|
|
||||||
|
return {
|
||||||
|
$type: 'app.bsky.feed.threadgate',
|
||||||
|
post: prev.post,
|
||||||
|
allow,
|
||||||
|
createdAt: new Date().toISOString(),
|
||||||
|
hiddenReplies,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user