Write chat declaration record in response to different events (#10216)
Co-authored-by: Eric Bailey <git@esb.lol>
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
import {type AppBskyActorDefs} from '@atproto/api'
|
||||
import type AtpAgent from '@atproto/api'
|
||||
import {
|
||||
type AppBskyActorDefs,
|
||||
type ChatBskyActorDeclaration,
|
||||
} from '@atproto/api'
|
||||
import {useMutation, useQueryClient} from '@tanstack/react-query'
|
||||
|
||||
import {logger} from '#/logger'
|
||||
@@ -78,3 +82,21 @@ export function useDeleteActorDeclaration() {
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export async function fetchActorDeclarationRecord({
|
||||
agent,
|
||||
did,
|
||||
}: {
|
||||
agent: AtpAgent
|
||||
did?: string
|
||||
}) {
|
||||
if (!did) return
|
||||
const res = await agent.com.atproto.repo
|
||||
.getRecord({
|
||||
repo: did,
|
||||
collection: 'chat.bsky.actor.declaration',
|
||||
rkey: 'self',
|
||||
})
|
||||
.catch(_e => undefined)
|
||||
return res?.data.value as ChatBskyActorDeclaration.Main
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
import type AtpAgent from '@atproto/api'
|
||||
import {type ChatBskyActorDeclaration} from '@atproto/api'
|
||||
|
||||
import {networkRetry} from '#/lib/async/retry'
|
||||
import {logger} from '#/logger'
|
||||
import {setOtherRequiredDataActorDeclarationCache} from '#/ageAssurance/data'
|
||||
|
||||
/**
|
||||
* Helper to update the chat settings record.
|
||||
*/
|
||||
export async function restrictChatSettings({
|
||||
agent,
|
||||
did,
|
||||
}: {
|
||||
agent: AtpAgent
|
||||
did: string
|
||||
}): Promise<void> {
|
||||
try {
|
||||
const record: ChatBskyActorDeclaration.Main = {
|
||||
$type: 'chat.bsky.actor.declaration',
|
||||
allowIncoming: 'none',
|
||||
}
|
||||
await networkRetry(3, () =>
|
||||
agent.com.atproto.repo.putRecord({
|
||||
repo: did,
|
||||
collection: 'chat.bsky.actor.declaration',
|
||||
rkey: 'self',
|
||||
record,
|
||||
}),
|
||||
)
|
||||
// important, update local cache to avoid running this again
|
||||
setOtherRequiredDataActorDeclarationCache({
|
||||
did,
|
||||
actorDeclaration: record,
|
||||
})
|
||||
} catch {
|
||||
logger.error(`restrictChatSettings: failed to set chat declaration`)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user