Use single restrictChatSettings util, use new flags
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import {createContext, useCallback, useContext, useMemo} from 'react'
|
||||
|
||||
import {useGetAndRegisterPushToken} from '#/lib/notifications/notifications'
|
||||
import {restrictChatSettings} from '#/state/queries/messages/restrictChatSettings'
|
||||
import {useAgent} from '#/state/session'
|
||||
import {Provider as RedirectOverlayProvider} from '#/ageAssurance/components/RedirectOverlay'
|
||||
import {
|
||||
@@ -20,7 +21,6 @@ import {
|
||||
} from '#/ageAssurance/types'
|
||||
import {
|
||||
computeAgeAssuranceFlags,
|
||||
maybeRestrictChatSettings,
|
||||
useAgeAssuranceRegionConfigWithFallback,
|
||||
} from '#/ageAssurance/util'
|
||||
|
||||
@@ -48,8 +48,10 @@ const AgeAssuranceStateContext = createContext<{
|
||||
access: AgeAssuranceAccess.Full,
|
||||
},
|
||||
flags: {
|
||||
isAgeRestricted: false,
|
||||
adultContentDisabled: false,
|
||||
chatDisabled: false,
|
||||
groupChatDisabled: false,
|
||||
isDeclaredUnderAdultAge: false,
|
||||
isOverRegionMinAccessAge: false,
|
||||
isOverAppMinAccessAge: false,
|
||||
@@ -84,13 +86,25 @@ function InnerProvider({children}: {children: React.ReactNode}) {
|
||||
|
||||
const handleAccessUpdate = useCallback(
|
||||
(s: AgeAssuranceState) => {
|
||||
const isAgeRestricted = s.access !== AgeAssuranceAccess.Full
|
||||
if (isAgeRestricted) {
|
||||
void getAndRegisterPushToken({isAgeRestricted})
|
||||
void maybeRestrictChatSettings({agent})
|
||||
const flags = computeAgeAssuranceFlags({
|
||||
state: s,
|
||||
regionConfig,
|
||||
metadata,
|
||||
})
|
||||
if (flags.isAgeRestricted) {
|
||||
void getAndRegisterPushToken({
|
||||
isAgeRestricted: true,
|
||||
})
|
||||
}
|
||||
if (flags.chatDisabled || flags.groupChatDisabled) {
|
||||
void restrictChatSettings({
|
||||
agent,
|
||||
restrictIncoming: flags.chatDisabled,
|
||||
restrictGroupInvites: flags.groupChatDisabled,
|
||||
})
|
||||
}
|
||||
},
|
||||
[agent, getAndRegisterPushToken],
|
||||
[agent, getAndRegisterPushToken, regionConfig, metadata],
|
||||
)
|
||||
useOnAgeAssuranceAccessUpdate(handleAccessUpdate)
|
||||
|
||||
|
||||
@@ -30,8 +30,10 @@ export type AgeAssuranceState = {
|
||||
}
|
||||
|
||||
export type AgeAssuranceFlags = {
|
||||
isAgeRestricted: boolean
|
||||
adultContentDisabled: boolean
|
||||
chatDisabled: boolean
|
||||
groupChatDisabled: boolean
|
||||
isDeclaredUnderAdultAge: boolean
|
||||
isOverRegionMinAccessAge: boolean
|
||||
isOverAppMinAccessAge: boolean
|
||||
|
||||
@@ -2,19 +2,13 @@ import {useMemo} from 'react'
|
||||
import {
|
||||
ageAssuranceRuleIDs as ids,
|
||||
type AppBskyAgeassuranceDefs,
|
||||
type AtpAgent,
|
||||
getAgeAssuranceRegionConfig,
|
||||
type ModerationPrefs,
|
||||
} from '@atproto/api'
|
||||
|
||||
import {getAge} from '#/lib/strings/time'
|
||||
import {restrictChatSettings} from '#/state/queries/messages/restrictChatSettings'
|
||||
import {DEFAULT_LOGGED_OUT_LABEL_PREFERENCES} from '#/state/queries/preferences/moderation'
|
||||
import {
|
||||
getDidFromAgentSession,
|
||||
getOtherRequiredDataFromCache,
|
||||
useAgeAssuranceServerDataContext,
|
||||
} from '#/ageAssurance/data'
|
||||
import {useAgeAssuranceServerDataContext} from '#/ageAssurance/data'
|
||||
import {
|
||||
AgeAssuranceAccess,
|
||||
type AgeAssuranceFlags,
|
||||
@@ -121,19 +115,6 @@ export const makeAgeRestrictedModerationPrefs = (
|
||||
labels: DEFAULT_LOGGED_OUT_LABEL_PREFERENCES,
|
||||
})
|
||||
|
||||
/**
|
||||
* Checks our cache of the actor's chat declaration record, and if it's not
|
||||
* already restricted, restricts it.
|
||||
*/
|
||||
export function maybeRestrictChatSettings({agent}: {agent: AtpAgent}) {
|
||||
const did = getDidFromAgentSession(agent)
|
||||
if (!did) return
|
||||
const data = getOtherRequiredDataFromCache({did})
|
||||
// ...update the chat setting record if allowIncoming is not already 'none'.
|
||||
if (data?.actorDeclaration?.allowIncoming === 'none') return
|
||||
restrictChatSettings({agent, did})
|
||||
}
|
||||
|
||||
export function computeAgeAssuranceFlags({
|
||||
state,
|
||||
regionConfig,
|
||||
@@ -143,10 +124,12 @@ export function computeAgeAssuranceFlags({
|
||||
regionConfig: AppBskyAgeassuranceDefs.ConfigRegion
|
||||
metadata?: AgeAssuranceMetadata
|
||||
}): AgeAssuranceFlags {
|
||||
const chatDisabled = state.access !== AgeAssuranceAccess.Full
|
||||
const isAgeRestricted = state.access !== AgeAssuranceAccess.Full
|
||||
const chatDisabled = isAgeRestricted
|
||||
const isDeclaredUnderAdultAge = metadata?.declaredAge
|
||||
? metadata.declaredAge < 18
|
||||
: true
|
||||
const groupChatDisabled = chatDisabled || isDeclaredUnderAdultAge
|
||||
const isOverRegionMinAccessAge = metadata?.declaredAge
|
||||
? metadata.declaredAge >= regionConfig.minAccessAge
|
||||
: false
|
||||
@@ -157,8 +140,10 @@ export function computeAgeAssuranceFlags({
|
||||
state.access !== AgeAssuranceAccess.Full || isDeclaredUnderAdultAge
|
||||
|
||||
return {
|
||||
isAgeRestricted,
|
||||
adultContentDisabled,
|
||||
chatDisabled,
|
||||
groupChatDisabled,
|
||||
isDeclaredUnderAdultAge,
|
||||
isOverRegionMinAccessAge,
|
||||
isOverAppMinAccessAge,
|
||||
|
||||
@@ -335,7 +335,7 @@ export function InitiateChatFlow({
|
||||
if (
|
||||
chatState === ChatState.NEW_CHAT &&
|
||||
searchText === '' &&
|
||||
!aa.flags.isDeclaredUnderAdultAge
|
||||
!aa.flags.groupChatDisabled
|
||||
) {
|
||||
_items.unshift({type: 'newGroupChat', key: 'newGroupChat'})
|
||||
}
|
||||
@@ -350,7 +350,7 @@ export function InitiateChatFlow({
|
||||
results,
|
||||
currentAccount?.did,
|
||||
follows,
|
||||
aa.flags.isDeclaredUnderAdultAge,
|
||||
aa.flags.groupChatDisabled,
|
||||
])
|
||||
|
||||
if (searchText && !isFetching && !items.length && !isError) {
|
||||
|
||||
@@ -231,7 +231,7 @@ export function ChatList({
|
||||
|
||||
const {refetch: refetchInbox} = useListConvosQuery({
|
||||
status: 'request',
|
||||
kind: aa.flags.isDeclaredUnderAdultAge ? 'direct' : 'all',
|
||||
kind: aa.flags.groupChatDisabled ? 'direct' : 'all',
|
||||
})
|
||||
|
||||
useRefreshOnFocus(refetch)
|
||||
@@ -465,7 +465,7 @@ export function Header({
|
||||
useListConvosQuery({
|
||||
status: 'request',
|
||||
readState: 'unread',
|
||||
kind: aa.flags.isDeclaredUnderAdultAge ? 'direct' : 'all',
|
||||
kind: aa.flags.groupChatDisabled ? 'direct' : 'all',
|
||||
})
|
||||
|
||||
const inboxAllConvos =
|
||||
|
||||
@@ -21,6 +21,7 @@ import {ChevronRight_Stroke2_Corner0_Rounded as ChevronRightIcon} from '#/compon
|
||||
import * as Layout from '#/components/Layout'
|
||||
import * as Toast from '#/components/Toast'
|
||||
import {Text} from '#/components/Typography'
|
||||
import {useAgeAssurance} from '#/ageAssurance'
|
||||
import {useAnalytics} from '#/analytics'
|
||||
import {IS_NATIVE} from '#/env'
|
||||
import {useBackgroundNotificationPreferences} from '../../../modules/expo-background-notification-handler/src/BackgroundNotificationHandlerProvider'
|
||||
@@ -46,6 +47,7 @@ export function MessagesSettingsScreenInner({}: Props) {
|
||||
const t = useTheme()
|
||||
const {t: l} = useLingui()
|
||||
const ax = useAnalytics()
|
||||
const aa = useAgeAssurance()
|
||||
const {currentAccount} = useSession()
|
||||
const {data: profile} = useProfileQuery({
|
||||
did: currentAccount!.did,
|
||||
@@ -55,6 +57,7 @@ export function MessagesSettingsScreenInner({}: Props) {
|
||||
const exportCarControl = Dialog.useDialogControl()
|
||||
|
||||
const isGroupChatEnabled = ax.features.enabled(ax.features.GroupChatsEnable)
|
||||
const groupInvitesLocked = aa.flags.groupChatDisabled
|
||||
|
||||
const allowMessagesFromOptions: {name: AllowIncoming; label: string}[] = [
|
||||
{
|
||||
@@ -192,15 +195,26 @@ export function MessagesSettingsScreenInner({}: Props) {
|
||||
a.leading_snug,
|
||||
t.atoms.text_contrast_high,
|
||||
]}>
|
||||
<Trans>
|
||||
You can continue ongoing conversations regardless of which
|
||||
setting you choose.
|
||||
</Trans>
|
||||
{groupInvitesLocked ? (
|
||||
<Trans>
|
||||
Group chats are only available to users 18 and over.
|
||||
</Trans>
|
||||
) : (
|
||||
<Trans>
|
||||
You can continue ongoing conversations regardless of which
|
||||
setting you choose.
|
||||
</Trans>
|
||||
)}
|
||||
</Text>
|
||||
<Toggle.Group
|
||||
disabled={groupInvitesLocked}
|
||||
label={l`Allow group chat invites from`}
|
||||
type="radio"
|
||||
values={[resolveAllowGroupInvites(profile?.associated?.chat)]}
|
||||
values={[
|
||||
groupInvitesLocked
|
||||
? 'none'
|
||||
: resolveAllowGroupInvites(profile?.associated?.chat),
|
||||
]}
|
||||
onChange={onSelectGroupInvitesFrom}>
|
||||
<View>
|
||||
{allowGroupInvitesFromOptions.map(option => (
|
||||
|
||||
@@ -67,15 +67,11 @@ export function useBirthdateMutation() {
|
||||
})
|
||||
|
||||
if (isUnderAge(birthDate.toISOString(), 18)) {
|
||||
const did = agent.sessionManager.did
|
||||
if (did) {
|
||||
await restrictChatSettings({
|
||||
agent,
|
||||
did,
|
||||
restrictIncoming: true,
|
||||
restrictGroupInvites: true,
|
||||
})
|
||||
}
|
||||
await restrictChatSettings({
|
||||
agent,
|
||||
restrictIncoming: true,
|
||||
restrictGroupInvites: true,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -128,7 +128,7 @@ export function ListConvosProviderInner({
|
||||
readState: 'unread',
|
||||
limit: UNREAD_LIMIT,
|
||||
lockStatus: 'unlocked',
|
||||
kind: aa.flags.isDeclaredUnderAdultAge ? 'direct' : 'all',
|
||||
kind: aa.flags.groupChatDisabled ? 'direct' : 'all',
|
||||
})
|
||||
const messagesBus = useMessagesEventBus()
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
@@ -4,6 +4,7 @@ import {type ChatBskyActorDeclaration} from '@atproto/api'
|
||||
import {networkRetry} from '#/lib/async/retry'
|
||||
import {logger} from '#/logger'
|
||||
import {
|
||||
getDidFromAgentSession,
|
||||
getOtherRequiredDataFromCache,
|
||||
setOtherRequiredDataActorDeclarationCache,
|
||||
} from '#/ageAssurance/data'
|
||||
@@ -24,15 +25,16 @@ import {
|
||||
*/
|
||||
export async function restrictChatSettings({
|
||||
agent,
|
||||
did,
|
||||
restrictIncoming = false,
|
||||
restrictGroupInvites = false,
|
||||
}: {
|
||||
agent: AtpAgent
|
||||
did: string
|
||||
restrictIncoming?: boolean
|
||||
restrictGroupInvites?: boolean
|
||||
}): Promise<void> {
|
||||
const did = getDidFromAgentSession(agent)
|
||||
if (!did) return
|
||||
|
||||
const cached = getOtherRequiredDataFromCache({did})?.actorDeclaration
|
||||
|
||||
// When the cache is empty we fall back to defaults for any dimension we're
|
||||
|
||||
@@ -29,7 +29,6 @@ import {
|
||||
setCreatedAtForDid,
|
||||
} from '#/ageAssurance/data'
|
||||
import {unsafeGetAndComputeAgeAssurance} from '#/ageAssurance/state'
|
||||
import {AgeAssuranceAccess} from '#/ageAssurance/types'
|
||||
import {features} from '#/analytics'
|
||||
import {emitNetworkConfirmed, emitNetworkLost} from '../events'
|
||||
import {addSessionErrorLog} from './logging'
|
||||
@@ -218,10 +217,14 @@ export async function createAgentAndCreateAccount(
|
||||
throw e
|
||||
}),
|
||||
// wait for AA data to load first, then check state
|
||||
aa.then(async () => {
|
||||
const {state} = unsafeGetAndComputeAgeAssurance({did: account.did})
|
||||
if (state.access !== AgeAssuranceAccess.Full) {
|
||||
restrictChatSettings({agent, did: account.did})
|
||||
aa.then(() => {
|
||||
const {flags} = unsafeGetAndComputeAgeAssurance({did: account.did})
|
||||
if (flags?.chatDisabled || flags?.groupChatDisabled) {
|
||||
void restrictChatSettings({
|
||||
agent,
|
||||
restrictIncoming: flags.chatDisabled,
|
||||
restrictGroupInvites: flags.groupChatDisabled,
|
||||
})
|
||||
}
|
||||
}),
|
||||
]).then(promises => {
|
||||
|
||||
Reference in New Issue
Block a user