Add queries
This commit is contained in:
@@ -4,7 +4,7 @@ import {
|
|||||||
AtUri,
|
AtUri,
|
||||||
BskyAgent,
|
BskyAgent,
|
||||||
} from '@atproto/api'
|
} from '@atproto/api'
|
||||||
import {useMutation, useQueryClient} from '@tanstack/react-query'
|
import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query'
|
||||||
|
|
||||||
import {networkRetry, retry} from '#/lib/async/retry'
|
import {networkRetry, retry} from '#/lib/async/retry'
|
||||||
import {logger} from '#/logger'
|
import {logger} from '#/logger'
|
||||||
@@ -85,17 +85,13 @@ export async function writePostgateRecord({
|
|||||||
postgate: AppBskyFeedPostgate.Record
|
postgate: AppBskyFeedPostgate.Record
|
||||||
}) {
|
}) {
|
||||||
const postUrip = new AtUri(postUri)
|
const postUrip = new AtUri(postUri)
|
||||||
const record = createPostgateRecord({
|
|
||||||
post: postUri,
|
|
||||||
detachedQuotes: postgate.detachedQuotes,
|
|
||||||
})
|
|
||||||
|
|
||||||
await networkRetry(2, () =>
|
await networkRetry(2, () =>
|
||||||
agent.api.com.atproto.repo.putRecord({
|
agent.api.com.atproto.repo.putRecord({
|
||||||
repo: agent.session!.did,
|
repo: agent.session!.did,
|
||||||
collection: POSTGATE_COLLECTION,
|
collection: POSTGATE_COLLECTION,
|
||||||
rkey: postUrip.rkey,
|
rkey: postUrip.rkey,
|
||||||
record,
|
record: postgate,
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -125,6 +121,20 @@ export async function upsertPostgate(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const createPostgateQueryKey = (postUri: string) => [
|
||||||
|
'postgate-record',
|
||||||
|
postUri,
|
||||||
|
]
|
||||||
|
export function usePostgateQuery({postUri}: {postUri: string}) {
|
||||||
|
const agent = useAgent()
|
||||||
|
return useQuery({
|
||||||
|
queryKey: createPostgateQueryKey(postUri),
|
||||||
|
queryFn() {
|
||||||
|
return getPostgateRecord({agent, postUri})
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
export function useToggleQuoteDetachmentMutation() {
|
export function useToggleQuoteDetachmentMutation() {
|
||||||
const agent = useAgent()
|
const agent = useAgent()
|
||||||
const queryClient = useQueryClient()
|
const queryClient = useQueryClient()
|
||||||
@@ -194,3 +204,40 @@ export function useToggleQuoteDetachmentMutation() {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function useToggleQuotepostEnabledMutation() {
|
||||||
|
const agent = useAgent()
|
||||||
|
|
||||||
|
return useMutation({
|
||||||
|
mutationFn: async ({
|
||||||
|
postUri,
|
||||||
|
action,
|
||||||
|
}: {
|
||||||
|
postUri: string
|
||||||
|
action: 'enable' | 'disable'
|
||||||
|
}) => {
|
||||||
|
await upsertPostgate({agent, postUri: postUri}, async prev => {
|
||||||
|
console.log({action})
|
||||||
|
if (prev) {
|
||||||
|
if (action === 'disable') {
|
||||||
|
return mergePostgateRecords(prev, {
|
||||||
|
quotepostRules: [{$type: 'app.bsky.feed.postgate#disableRule'}],
|
||||||
|
})
|
||||||
|
} else if (action === 'enable') {
|
||||||
|
return {
|
||||||
|
...prev,
|
||||||
|
quotepostRules: [],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (action === 'disable') {
|
||||||
|
return createPostgateRecord({
|
||||||
|
post: postUri,
|
||||||
|
quotepostRules: [{$type: 'app.bsky.feed.postgate#disableRule'}],
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ export function createPostgateRecord(
|
|||||||
createdAt: new Date().toISOString(),
|
createdAt: new Date().toISOString(),
|
||||||
post: postgate.post,
|
post: postgate.post,
|
||||||
detachedQuotes: postgate.detachedQuotes || [],
|
detachedQuotes: postgate.detachedQuotes || [],
|
||||||
|
quotepostRules: postgate.quotepostRules || [],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,9 +32,16 @@ export function mergePostgateRecords(
|
|||||||
const detachedQuotes = Array.from(
|
const detachedQuotes = Array.from(
|
||||||
new Set([...(prev.detachedQuotes || []), ...(next.detachedQuotes || [])]),
|
new Set([...(prev.detachedQuotes || []), ...(next.detachedQuotes || [])]),
|
||||||
)
|
)
|
||||||
|
const quotepostRules = [
|
||||||
|
...(prev.quotepostRules || []),
|
||||||
|
...(next.quotepostRules || []),
|
||||||
|
].filter(
|
||||||
|
(rule, i, all) => all.findIndex(_rule => _rule.$type === rule.$type) === i,
|
||||||
|
)
|
||||||
return createPostgateRecord({
|
return createPostgateRecord({
|
||||||
post: prev.post,
|
post: prev.post,
|
||||||
detachedQuotes,
|
detachedQuotes,
|
||||||
|
quotepostRules,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user