Files
bsky-social-app/src/state/queries/messages/restrictChatSettings.ts
T
Samuel Newman 2199997c3e gate group chats for under-18 users
Per the group chats spec, users under 18 cannot be added to or participate in
group chats. This wires up the client-side half:

- expose isUnder18 flag from the AA context (conservative default when
  birthdate is unknown)
- new restrictGroupChatSettings helper that writes
  allowGroupInvites: 'none' to the chat actor declaration, called on signup
  and on birthdate-update when the user is under 18
- hide the 'New group chat' entry in InitiateChatFlow for under-18 users
- lock the 'Allow group chat invites from' toggle to 'none' for under-18 in
  chat settings, with explanatory copy
- pass kind: 'direct' to listConvos for under-18 users so groups don't show
  in the requests inbox or count toward the unread/requests badges
2026-06-04 16:30:51 +03:00

84 lines
2.2 KiB
TypeScript

import type AtpAgent from '@atproto/api'
import {type ChatBskyActorDeclaration} from '@atproto/api'
import {networkRetry} from '#/lib/async/retry'
import {logger} from '#/logger'
import {
getOtherRequiredDataFromCache,
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`)
}
}
/**
* Locks the user out of being added to group chats by setting
* `allowGroupInvites: 'none'` on the chat actor declaration. Used for under-18
* users, who per spec cannot participate in group chats.
*
* Preserves the existing `allowIncoming` value if one is cached, otherwise
* defaults to 'following' (the lexicon default) since the field is required.
*/
export async function restrictGroupChatSettings({
agent,
did,
}: {
agent: AtpAgent
did: string
}): Promise<void> {
const cached = getOtherRequiredDataFromCache({did})?.actorDeclaration
if (cached?.allowGroupInvites === 'none') return
try {
const record: ChatBskyActorDeclaration.Main = {
$type: 'chat.bsky.actor.declaration',
allowIncoming: cached?.allowIncoming ?? 'following',
allowGroupInvites: 'none',
}
await networkRetry(3, () =>
agent.com.atproto.repo.putRecord({
repo: did,
collection: 'chat.bsky.actor.declaration',
rkey: 'self',
record,
}),
)
setOtherRequiredDataActorDeclarationCache({
did,
actorDeclaration: record,
})
} catch {
logger.error(`restrictGroupChatSettings: failed to set chat declaration`)
}
}