diff --git a/src/state/queries/postgate/index.ts b/src/state/queries/postgate/index.ts index c4d5c7a0ce..d3b847675d 100644 --- a/src/state/queries/postgate/index.ts +++ b/src/state/queries/postgate/index.ts @@ -4,7 +4,7 @@ import { AtUri, BskyAgent, } 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 {logger} from '#/logger' @@ -85,17 +85,13 @@ export async function writePostgateRecord({ postgate: AppBskyFeedPostgate.Record }) { const postUrip = new AtUri(postUri) - const record = createPostgateRecord({ - post: postUri, - detachedQuotes: postgate.detachedQuotes, - }) await networkRetry(2, () => agent.api.com.atproto.repo.putRecord({ repo: agent.session!.did, collection: POSTGATE_COLLECTION, 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() { const agent = useAgent() 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'}], + }) + } + } + }) + }, + }) +} diff --git a/src/state/queries/postgate/util.ts b/src/state/queries/postgate/util.ts index 40b3faab98..c351909695 100644 --- a/src/state/queries/postgate/util.ts +++ b/src/state/queries/postgate/util.ts @@ -21,6 +21,7 @@ export function createPostgateRecord( createdAt: new Date().toISOString(), post: postgate.post, detachedQuotes: postgate.detachedQuotes || [], + quotepostRules: postgate.quotepostRules || [], } } @@ -31,9 +32,16 @@ export function mergePostgateRecords( const detachedQuotes = Array.from( 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({ post: prev.post, detachedQuotes, + quotepostRules, }) }