From 9faa33c17cea25c7df81aaa0d68a2a735aadfa04 Mon Sep 17 00:00:00 2001 From: DS Boyce <260543580+ds-boyce@users.noreply.github.com> Date: Thu, 15 Oct 2026 09:41:44 -0700 Subject: [PATCH] Wire up Moderation Inbox notices and appeals (#11970) --- src/lib/routes/types.ts | 2 +- src/screens/ModerationInbox/Notice.tsx | 397 +++++++++++++++--- .../AccountSubjectPreview.tsx | 29 +- .../ChatSubjectPreview.tsx | 5 +- .../FeedSubjectPreview.tsx | 5 +- .../ListSubjectPreview.tsx | 5 +- .../PostSubjectPreview.tsx | 81 ++-- .../ReportSubjectPreview/PreviewContent.tsx | 63 +-- .../StarterPackSubjectPreview.tsx | 5 +- .../StatusSubjectPreview.tsx | 5 +- .../components/ReportSubjectPreview/index.tsx | 14 +- .../ModerationInbox/components/Timeline.tsx | 35 -- .../components/TimelineItem.tsx | 72 ---- .../components/YourAccountRow.tsx | 16 +- .../hooks/useActionedSubjectLabels.ts | 7 +- .../components/util/noticeLabels.ts | 104 +++++ src/state/queries/moderation-inbox.ts | 113 +++++ 17 files changed, 700 insertions(+), 258 deletions(-) delete mode 100644 src/screens/ModerationInbox/components/Timeline.tsx delete mode 100644 src/screens/ModerationInbox/components/TimelineItem.tsx create mode 100644 src/screens/ModerationInbox/components/util/noticeLabels.ts diff --git a/src/lib/routes/types.ts b/src/lib/routes/types.ts index a115fa480..e1769b96e 100644 --- a/src/lib/routes/types.ts +++ b/src/lib/routes/types.ts @@ -28,7 +28,7 @@ export type CommonNavigatorParams = { ModerationInbox: {tab?: ModerationInboxTab} ModerationInboxSettings: undefined ModerationInboxReportDetails: {moderationService: string; id: string} - ModerationInboxNoticeDetails: undefined + ModerationInboxNoticeDetails: {moderationService: string; subject: string} ModerationModlists: undefined ModerationMutedAccounts: undefined ModerationBlockedAccounts: undefined diff --git a/src/screens/ModerationInbox/Notice.tsx b/src/screens/ModerationInbox/Notice.tsx index f6dd54d34..7ae3729ef 100644 --- a/src/screens/ModerationInbox/Notice.tsx +++ b/src/screens/ModerationInbox/Notice.tsx @@ -1,34 +1,96 @@ -import {useState} from 'react' +import {useRef, useState} from 'react' import {View} from 'react-native' -import {Trans, useLingui} from '@lingui/react/macro' +import {AtUri, isAtUriString, isValidDid} from '@atproto/syntax' +import {moderateProfile} from '@bsky/sdk/moderation' +import {Plural, Trans, useLingui} from '@lingui/react/macro' import {countGraphemes} from 'unicode-segmenter/grapheme' -import {MAX_REPORT_REASON_GRAPHEME_LENGTH} from '#/lib/constants' +import {MAX_REPORT_REASON_GRAPHEME_LENGTH, webLinks} from '#/lib/constants' +import {isAppLabeler} from '#/lib/moderation' +import {createSanitizedDisplayName} from '#/lib/moderation/create-sanitized-display-name' +import { + type CommonNavigatorParams, + type NativeStackScreenProps, +} from '#/lib/routes/types' +import {cleanError} from '#/lib/strings/errors' +import {matchXrpcError} from '#/lib/xrpc-error' +import {useModerationOpts} from '#/state/preferences/moderation-opts' +import {useLabelerInfoQuery} from '#/state/queries/labeler' +import { + useAppealModerationInboxSubjectMutation, + useModerationInboxCacheMutation, + useModerationInboxSubjectQuery, +} from '#/state/queries/moderation-inbox' +import {useSession} from '#/state/session' +import {ErrorMessage} from '#/view/com/util/error/ErrorMessage' import {NotFoundScreen} from '#/view/screens/NotFound' import {atoms as a, useTheme} from '#/alf' -import {Button, ButtonText} from '#/components/Button' +import {Admonition} from '#/components/Admonition' +import {Button, ButtonIcon, ButtonText} from '#/components/Button' import * as Dialog from '#/components/Dialog' import * as Layout from '#/components/Layout' import {InlineLinkText} from '#/components/Link' +import {Loader} from '#/components/Loader' +import * as Toast from '#/components/Toast' import {Text} from '#/components/Typography' import {useAnalytics} from '#/analytics' +import {tools} from '#/lexicons' import {ActionSummaryText} from './components/ActionSummaryText' import {ContentBlock} from './components/ContentBlock' -import {SubjectPreview} from './components/SubjectPreview' -import {Timeline} from './components/Timeline' +import {useActionedSubjectLabels} from './components/hooks/useActionedSubjectLabels' +import {ReportSubjectPreview} from './components/ReportSubjectPreview' +import {getNoticeLabels} from './components/util/noticeLabels' -const ANTI_HARASSMENT = - 'https://bsky.social/about/support/community-guidelines#:~:text=coordinated%20harassment%20campaigns.-,Anti%2DHarassment,-%3A%20We%20create%20space' - -export function ModerationInboxNoticeDetailsScreen() { +export function ModerationInboxNoticeDetailsScreen({ + route, +}: NativeStackScreenProps< + CommonNavigatorParams, + 'ModerationInboxNoticeDetails' +>) { const t = useTheme() const {t: l} = useLingui() const ax = useAnalytics() - const appealDialogControl = Dialog.useDialogControl() - + const {currentAccount, hasSession} = useSession() + const moderationOpts = useModerationOpts() const isEnabled = ax.features.enabled(ax.features.ModerationInboxEnable) + const moderationService = isValidDid(route.params?.moderationService) + ? route.params.moderationService + : undefined + const rawSubject = route.params?.subject + const subject = isValidDid(rawSubject) + ? rawSubject + : isAtUriString(rawSubject, {strict: false}) && + isValidDid(new AtUri(rawSubject).host) && + !!new AtUri(rawSubject).collection && + !!new AtUri(rawSubject).rkey + ? rawSubject + : undefined + const subjectQuery = useModerationInboxSubjectQuery({ + moderationService, + subject, + enabled: isEnabled, + }) + const {data: labeler} = useLabelerInfoQuery({ + did: moderationService, + enabled: isEnabled && !!subject && hasSession, + }) + const serviceName = + labeler && moderationOpts + ? createSanitizedDisplayName( + labeler.creator, + false, + moderateProfile(labeler.creator, moderationOpts).ui('displayName'), + ) + : moderationService && isAppLabeler(moderationService) + ? l`Bluesky Moderation Service` + : moderationService + const data = subjectQuery.data?.pages[0] + const isLoading = !data && !subjectQuery.isError + const notFound = + matchXrpcError(subjectQuery.error, tools.ozone.inbox.getActionedSubject) === + 'NotFound' - if (!isEnabled) { + if (!isEnabled || !hasSession || !moderationService || !subject || notFound) { return } @@ -41,52 +103,165 @@ export function ModerationInboxNoticeDetailsScreen() { Notice - Bluesky Moderation Service + + {serviceName} + + + {data ? ( + page.actions) ?? [] + } + serviceName={serviceName ?? moderationService} + /> + ) : isLoading ? ( + + + + ) : null} + {subjectQuery.isError ? ( + void subjectQuery.refetch()} + /> + ) : null} + {data && subjectQuery.hasNextPage ? ( + + + + ) : null} + + + ) +} - - - +function NoticeDetails({ + data, + actions, + serviceName, +}: { + data: tools.ozone.inbox.getActionedSubject.$OutputBody + actions: tools.ozone.inbox.defs.ActionView[] + serviceName: string +}) { + const t = useTheme() + const {i18n, t: l} = useLingui() + const appealDialogControl = Dialog.useDialogControl() + const labels = getNoticeLabels(data) + const actionLabels = useActionedSubjectLabels({ + src: data.src, + subject: data.subject, + enforcement: data.enforcement, + latestAction: data.actions[0], + }) + const policies = data.actions[0]?.policies ?? [] + const canAppeal = data.availableActions?.includes('appeal') ?? false + const isAccount = 'did' in data.subject + + return ( + <> + + + {labels.appealResolved ? ( + i18n._(labels.appealSummary) + ) : data.appeal?.state === 'pending' ? ( + + Your appeal has been submitted and is awaiting review. + + ) : data.actions[0]?.type === 'contentRemoved' && + !data.actions[0]?.reversedAt ? ( - Bluesky Moderation Service removed a post from Bluesky for - violating our{' '} + {serviceName} removed your content for violating our{' '} Community Guidelines . - - - - - + ) : undefined} + + + + {(data.actions[0]?.type === 'labelApplied' || + data.actions[0]?.type === 'labelRemoved') && + actionLabels?.summary ? ( + + {actionLabels.summary} + + ) : null} + {policies.length ? ( - Rule violated: - - + + + + {policies.map(policy => ( + - Community Guidelines: Harassment + {policy.displayName} - - + + ))} - + ) : null} + {canAppeal ? ( + {isAccount ? ( + + You can submit one account appeal at a time. A different + moderator will review the appeal. + + ) : ( + + You can appeal once per decision. A different moderator will + review the appeal. + + )}{' '} - You can appeal once per decision. A different moderator will - review the appeal. + For more information about our appeals and redress process, + please see our{' '} + + Terms of Service + + . @@ -103,27 +278,96 @@ export function ModerationInboxNoticeDetailsScreen() { - - - - - - - - + ) : data.appeal?.state === 'expired' ? ( + + The appeal window has closed. + + ) : null} + + + + ) } -function AppealDialog({control}: {control: Dialog.DialogControlProps}) { +function AppealDialog({ + control, + data, +}: { + control: Dialog.DialogControlProps + data: tools.ozone.inbox.getActionedSubject.$OutputBody +}) { const t = useTheme() const {i18n, t: l} = useLingui() const [details, setDetails] = useState('') + const submitting = useRef(false) + const appeal = useAppealModerationInboxSubjectMutation(data.src) + const invalidateInbox = useModerationInboxCacheMutation() + const appealedAction = data.actions.find( + action => + !action.reversedAt && + [ + 'contentRemoved', + 'accountSuspended', + 'accountTakedown', + 'labelApplied', + ].includes(action.type), + ) + const errorCode = matchXrpcError( + appeal.error, + tools.ozone.inbox.appealActionedSubject, + ) + let errorMessage: string + switch (errorCode) { + case 'AlreadyAppealed': + errorMessage = l`This subject has already been appealed.` + break + case 'AppealWindowExpired': + errorMessage = l`The appeal window has closed.` + break + case 'NotAppealable': + errorMessage = l`This subject can no longer be appealed.` + break + case 'InvalidAppealSubject': + errorMessage = l`This content could not be appealed. Please reload the notice and try again.` + break + default: + errorMessage = l`Failed to submit appeal. Please try again.` + break + } const detailsLength = countGraphemes(details) const isOverLimit = detailsLength > MAX_REPORT_REASON_GRAPHEME_LENGTH - const isSubmitDisabled = details.trim().length === 0 || isOverLimit + const isSubmitDisabled = + details.trim().length === 0 || + isOverLimit || + appeal.isPending || + !data.availableActions?.includes('appeal') || + !!errorCode + + const onSubmit = async () => { + if (submitting.current || isSubmitDisabled) return + submitting.current = true + try { + const updatedSubject = await appeal.mutateAsync({ + subject: data.subject, + reason: details.trim(), + action: appealedAction + ? { + $type: 'tools.ozone.inbox.appealActionedSubject#actionRef', + id: appealedAction.id, + } + : undefined, + }) + control.close(() => { + void invalidateInbox(updatedSubject) + Toast.show(l`Appeal submitted`) + }) + } catch { + // The mutation reports unexpected errors; the dialog keeps the reason. + submitting.current = false + } + } const counterAccessibilityLabel = l`${detailsLength} of ${MAX_REPORT_REASON_GRAPHEME_LENGTH} characters used` const cancelButton = () => ( @@ -131,8 +375,9 @@ function AppealDialog({control}: {control: Dialog.DialogControlProps}) { testID="moderationInboxAppealCancelBtn" label={l`Cancel appeal`} size="small" + color="secondary" variant="ghost" - color="primary" + disabled={appeal.isPending} onPress={() => control.close()}> Cancel @@ -147,7 +392,8 @@ function AppealDialog({control}: {control: Dialog.DialogControlProps}) { size="small" color="primary" disabled={isSubmitDisabled} - onPress={() => control.close()}> + onPress={() => void onSubmit()}> + {appeal.isPending ? : null} Submit @@ -155,7 +401,19 @@ function AppealDialog({control}: {control: Dialog.DialogControlProps}) { ) return ( - + { + appeal.reset() + setDetails('') + submitting.current = false + }} + nativeOptions={{preventDismiss: appeal.isPending}} + webOptions={{ + onBackgroundPress: () => { + if (!submitting.current) control.close() + }, + }}> Tell us why you believe this decision was a mistake. We will - review your appeal along with the original content. You can appeal - once per moderation decision. - - - - - For more information about our appeals and redress process, please - see our{' '} - - Terms of Service - - . + review your appeal along with the original content. + {appeal.isError ? ( + {errorMessage} + ) : null} + {errorCode ? ( + + ) : null} @@ -211,8 +473,9 @@ function AppealDialog({control}: {control: Dialog.DialogControlProps}) { - - {handle ? ( - - {handle} - - ) : null} - + avatar = ( + ) + content = handle ? ( + + {handle} + + ) : null if (profile.createdAt) { const createdAt = i18n.date(new Date(profile.createdAt), { dateStyle: 'medium', @@ -48,6 +46,7 @@ export function AccountSubjectPreview({did}: {did: string}) { }) { const {t: l} = useLingui() const title = useReportSubjectLabel(report.subject) diff --git a/src/screens/ModerationInbox/components/ReportSubjectPreview/FeedSubjectPreview.tsx b/src/screens/ModerationInbox/components/ReportSubjectPreview/FeedSubjectPreview.tsx index 2e6a74a21..d68b9e9cd 100644 --- a/src/screens/ModerationInbox/components/ReportSubjectPreview/FeedSubjectPreview.tsx +++ b/src/screens/ModerationInbox/components/ReportSubjectPreview/FeedSubjectPreview.tsx @@ -15,7 +15,10 @@ export function FeedSubjectPreview({ report, uri, }: { - report: tools.ozone.inbox.getReport.$OutputBody['report'] + report: Pick< + tools.ozone.inbox.getReport.$OutputBody['report'], + 'subject' | 'record' + > uri: AtUri }) { const {i18n, t: l} = useLingui() diff --git a/src/screens/ModerationInbox/components/ReportSubjectPreview/ListSubjectPreview.tsx b/src/screens/ModerationInbox/components/ReportSubjectPreview/ListSubjectPreview.tsx index 1d8361edf..411324d67 100644 --- a/src/screens/ModerationInbox/components/ReportSubjectPreview/ListSubjectPreview.tsx +++ b/src/screens/ModerationInbox/components/ReportSubjectPreview/ListSubjectPreview.tsx @@ -16,7 +16,10 @@ export function ListSubjectPreview({ report, uri, }: { - report: tools.ozone.inbox.getReport.$OutputBody['report'] + report: Pick< + tools.ozone.inbox.getReport.$OutputBody['report'], + 'subject' | 'record' + > uri: AtUri }) { const {i18n, t: l} = useLingui() diff --git a/src/screens/ModerationInbox/components/ReportSubjectPreview/PostSubjectPreview.tsx b/src/screens/ModerationInbox/components/ReportSubjectPreview/PostSubjectPreview.tsx index 5672de7cd..544a117ae 100644 --- a/src/screens/ModerationInbox/components/ReportSubjectPreview/PostSubjectPreview.tsx +++ b/src/screens/ModerationInbox/components/ReportSubjectPreview/PostSubjectPreview.tsx @@ -7,6 +7,7 @@ import {createSanitizedDisplayName} from '#/lib/moderation/create-sanitized-disp import {useModerationOpts} from '#/state/preferences/moderation-opts' import {usePostQuery} from '#/state/queries/post' import {useProfileQuery} from '#/state/queries/profile' +import {useSession} from '#/state/session' import {atoms as a, useTheme} from '#/alf' import {ButtonText} from '#/components/Button' import {Link} from '#/components/Link' @@ -17,20 +18,26 @@ import {Text} from '#/components/Typography' import {app, type tools} from '#/lexicons' import * as bsky from '#/types/bsky' -type Report = tools.ozone.inbox.getReport.$OutputBody['report'] +type Report = Pick< + tools.ozone.inbox.getReport.$OutputBody['report'], + 'subject' | 'record' +> export function PostSubjectPreview({ report, uri, subjectLabel, + allowOwnRecordSnapshot = false, }: { report: Report uri: AtUri subjectLabel: string + allowOwnRecordSnapshot?: boolean }) { const t = useTheme() const {i18n, t: l} = useLingui() const moderationOpts = useModerationOpts() + const {currentAccount, hasSession} = useSession() const postQuery = usePostQuery(uri.toString()) // Do not present an edited post as the content that was reported. const post = @@ -44,6 +51,15 @@ export function PostSubjectPreview({ : post && bsky.matches(app.bsky.feed.post, post.record) ? post.record : undefined + /* + * Ozone can return the owner's removed record even when appview cannot. + * Report previews still require a live, moderated post with the same CID. + */ + const canUseSnapshot = + allowOwnRecordSnapshot && + hasSession && + uri.host === currentAccount?.did && + bsky.matches(app.bsky.feed.post, report.record) const parentUri = record?.reply && isAtUriString(record.reply.parent.uri, {strict: false}) ? new AtUri(record.reply.parent.uri) @@ -66,6 +82,11 @@ export function PostSubjectPreview({ : l`Reply` : subjectLabel const isLoading = postQuery.isPending || (!!post && !moderationOpts) + const text = record?.text ? ( + + “{record.text}” + + ) : null return ( @@ -74,41 +95,43 @@ export function PostSubjectPreview({ {isLoading ? ( - ) : post && record && moderation ? ( + ) : record && ((post && moderation) || canUseSnapshot) ? ( <> - - - {record.text ? ( - - “{record.text}” - - ) : null} - + {post && moderation ? ( + + + {text} + + ) : ( + text + )} Posted{' '} {i18n.date(new Date(record.createdAt), {dateStyle: 'medium'})} - - - - Open post - - - + {post && moderation ? ( + + + + Open post + + + + ) : null} ) : ( <> diff --git a/src/screens/ModerationInbox/components/ReportSubjectPreview/PreviewContent.tsx b/src/screens/ModerationInbox/components/ReportSubjectPreview/PreviewContent.tsx index 8a789be94..780326fdc 100644 --- a/src/screens/ModerationInbox/components/ReportSubjectPreview/PreviewContent.tsx +++ b/src/screens/ModerationInbox/components/ReportSubjectPreview/PreviewContent.tsx @@ -11,6 +11,7 @@ import {Text} from '#/components/Typography' export function PreviewContent({ testID, title, + leading, children, metadataText = [], loading = false, @@ -21,6 +22,7 @@ export function PreviewContent({ }: { testID: string title: string + leading?: React.ReactNode children?: React.ReactNode metadataText?: string[] loading?: boolean @@ -33,35 +35,38 @@ export function PreviewContent({ return ( - - - {title} - - {loading ? : children} - {!loading && metadataText.length ? ( - - {metadataText.map((text, index) => ( - - {text} - - ))} - - ) : null} - {!loading && to && openLabel ? ( - - - {openLabel} - - - ) : null} + + {leading} + + + {title} + + {loading ? : children} + {!loading && metadataText.length ? ( + + {metadataText.map((text, index) => ( + + {text} + + ))} + + ) : null} + {!loading && to && openLabel ? ( + + + {openLabel} + + + ) : null} + ) diff --git a/src/screens/ModerationInbox/components/ReportSubjectPreview/StarterPackSubjectPreview.tsx b/src/screens/ModerationInbox/components/ReportSubjectPreview/StarterPackSubjectPreview.tsx index d0d506ae2..39a3ee208 100644 --- a/src/screens/ModerationInbox/components/ReportSubjectPreview/StarterPackSubjectPreview.tsx +++ b/src/screens/ModerationInbox/components/ReportSubjectPreview/StarterPackSubjectPreview.tsx @@ -16,7 +16,10 @@ export function StarterPackSubjectPreview({ report, uri, }: { - report: tools.ozone.inbox.getReport.$OutputBody['report'] + report: Pick< + tools.ozone.inbox.getReport.$OutputBody['report'], + 'subject' | 'record' + > uri: AtUri }) { const {i18n, t: l} = useLingui() diff --git a/src/screens/ModerationInbox/components/ReportSubjectPreview/StatusSubjectPreview.tsx b/src/screens/ModerationInbox/components/ReportSubjectPreview/StatusSubjectPreview.tsx index aaabdc8c6..1b4d95ab1 100644 --- a/src/screens/ModerationInbox/components/ReportSubjectPreview/StatusSubjectPreview.tsx +++ b/src/screens/ModerationInbox/components/ReportSubjectPreview/StatusSubjectPreview.tsx @@ -13,7 +13,10 @@ export function StatusSubjectPreview({ report, uri, }: { - report: tools.ozone.inbox.getReport.$OutputBody['report'] + report: Pick< + tools.ozone.inbox.getReport.$OutputBody['report'], + 'subject' | 'record' + > uri: AtUri }) { const {i18n, t: l} = useLingui() diff --git a/src/screens/ModerationInbox/components/ReportSubjectPreview/index.tsx b/src/screens/ModerationInbox/components/ReportSubjectPreview/index.tsx index 794150cea..86664623e 100644 --- a/src/screens/ModerationInbox/components/ReportSubjectPreview/index.tsx +++ b/src/screens/ModerationInbox/components/ReportSubjectPreview/index.tsx @@ -16,9 +16,18 @@ import {PostSubjectPreview} from './PostSubjectPreview' import {StarterPackSubjectPreview} from './StarterPackSubjectPreview' import {StatusSubjectPreview} from './StatusSubjectPreview' -type Report = tools.ozone.inbox.getReport.$OutputBody['report'] +type Report = Pick< + tools.ozone.inbox.getReport.$OutputBody['report'], + 'subject' | 'record' +> -export function ReportSubjectPreview({report}: {report: Report}) { +export function ReportSubjectPreview({ + report, + allowOwnRecordSnapshot = false, +}: { + report: Report + allowOwnRecordSnapshot?: boolean +}) { const t = useTheme() const {t: l} = useLingui() const subjectLabel = useReportSubjectLabel(report.subject) @@ -48,6 +57,7 @@ export function ReportSubjectPreview({report}: {report: Report}) { report={report} uri={uri} subjectLabel={subjectLabel} + allowOwnRecordSnapshot={allowOwnRecordSnapshot} /> ) break diff --git a/src/screens/ModerationInbox/components/Timeline.tsx b/src/screens/ModerationInbox/components/Timeline.tsx deleted file mode 100644 index cd643dfba..000000000 --- a/src/screens/ModerationInbox/components/Timeline.tsx +++ /dev/null @@ -1,35 +0,0 @@ -import {View} from 'react-native' -import {useLingui} from '@lingui/react/macro' - -import {formatDateTime} from '#/lib/strings/time' -import {ContentBlock} from './ContentBlock' -import {TimelineItem} from './TimelineItem' - -export function Timeline({items}: {items: {title: string; date?: Date}[]}) { - const {i18n, t: l} = useLingui() - - return ( - - - {items.map(({title, date}, index) => ( - - ))} - - - ) -} diff --git a/src/screens/ModerationInbox/components/TimelineItem.tsx b/src/screens/ModerationInbox/components/TimelineItem.tsx deleted file mode 100644 index 7c126ac03..000000000 --- a/src/screens/ModerationInbox/components/TimelineItem.tsx +++ /dev/null @@ -1,72 +0,0 @@ -import {View} from 'react-native' - -import {atoms as a, useTheme} from '#/alf' -import {Text} from '#/components/Typography' - -export function TimelineItem({ - title, - date, - last, -}: { - title: string - date?: string - last?: boolean -}) { - const t = useTheme() - - return ( - - - {last ? ( - - ) : ( - - )} - {last ? undefined : ( - - )} - - - {title} - {date ? ( - - {date} - - ) : undefined} - - - ) -} diff --git a/src/screens/ModerationInbox/components/YourAccountRow.tsx b/src/screens/ModerationInbox/components/YourAccountRow.tsx index bcdc1f1cf..89d2e8f9c 100644 --- a/src/screens/ModerationInbox/components/YourAccountRow.tsx +++ b/src/screens/ModerationInbox/components/YourAccountRow.tsx @@ -13,8 +13,14 @@ export function YourAccountRow({ }) { const labels = useActionedSubjectLabels(item) const action = item.latestAction + const subject = + 'did' in item.subject && typeof item.subject.did === 'string' + ? item.subject.did + : 'uri' in item.subject && typeof item.subject.uri === 'string' + ? item.subject.uri + : undefined - if (!action || !labels) return null + if (!action || !labels || !subject) return null const dateEnd = action.reversedAt ?? action.expiresAt @@ -24,7 +30,13 @@ export function YourAccountRow({ action={labels.summary} date={new Date(action.createdAt)} dateEnd={dateEnd ? new Date(dateEnd) : undefined} - to="/moderation/inbox/notice/details" + to={{ + screen: 'ModerationInboxNoticeDetails', + params: { + moderationService: item.src, + subject, + }, + }} unread={unread} /> ) diff --git a/src/screens/ModerationInbox/components/hooks/useActionedSubjectLabels.ts b/src/screens/ModerationInbox/components/hooks/useActionedSubjectLabels.ts index 6288fda61..7da6f9ed2 100644 --- a/src/screens/ModerationInbox/components/hooks/useActionedSubjectLabels.ts +++ b/src/screens/ModerationInbox/components/hooks/useActionedSubjectLabels.ts @@ -9,7 +9,12 @@ import {type tools} from '#/lexicons' export type ActionedSubject = tools.ozone.inbox.listActionedSubjects.$OutputBody['subjects'][number] -export function useActionedSubjectLabels(item: ActionedSubject) { +export function useActionedSubjectLabels( + item: Pick< + ActionedSubject, + 'src' | 'subject' | 'enforcement' | 'latestAction' + >, +) { const {i18n, t: l} = useLingui() const {labelDefs} = useLabelDefinitions() const globalLabelStrings = useGlobalLabelStrings() diff --git a/src/screens/ModerationInbox/components/util/noticeLabels.ts b/src/screens/ModerationInbox/components/util/noticeLabels.ts new file mode 100644 index 000000000..076c66b69 --- /dev/null +++ b/src/screens/ModerationInbox/components/util/noticeLabels.ts @@ -0,0 +1,104 @@ +import {AtUri, isAtUriString} from '@atproto/syntax' +import {msg, plural} from '@lingui/core/macro' + +import {type tools} from '#/lexicons' +import {getReportActionLabel} from './reportActionLabel' + +/** Labels reflect explicit appeal outcomes and the current moderation state. */ +export function getNoticeLabels( + data: tools.ozone.inbox.getActionedSubject.$OutputBody, +) { + const action = data.actions[0] + const isAccount = 'did' in data.subject + const isPost = + 'uri' in data.subject && + isAtUriString(data.subject.uri, {strict: false}) && + new AtUri(data.subject.uri).collection === 'app.bsky.feed.post' + const isRestoredPost = + isPost && + data.enforcement.state !== 'removed' && + data.actions.some( + action => action.type === 'contentRemoved' && action.reversedAt, + ) + const appealResolved = + data.appeal?.state === 'resolved' && + (!data.appeal.resolvedAt || + !action || + Date.parse(data.appeal.resolvedAt) >= Date.parse(action.createdAt)) + let header = + getReportActionLabel({ + status: '', + lastActionTaken: action?.type, + }) ?? msg`Moderation notice` + let appealSummary = msg`We reviewed your appeal. The current status of your content or account is shown below.` + + if (data.appeal?.state === 'pending') { + header = msg`Appeal under review` + } else if (appealResolved) { + switch (data.appeal?.outcome) { + case 'approved': + header = msg`Appeal approved` + appealSummary = isRestoredPost + ? msg`A moderator reviewed your appeal and reversed the original decision. Your post has been restored and is visible again.` + : msg`A moderator reviewed your appeal and reversed the original decision.` + break + case 'denied': + header = msg`Appeal denied` + appealSummary = msg`We reviewed your appeal and are keeping our original decision in place.` + break + default: + header = msg`Appeal resolved` + break + } + } else if (action?.reversedAt) { + header = isAccount + ? msg`The action against your account was reversed` + : msg`The action against your content was reversed` + } else if (action?.type === 'accountSuspended' && action.expiresAt) { + const hours = Math.max( + 0, + Math.round( + (Date.parse(action.expiresAt) - Date.parse(action.createdAt)) / + 3_600_000, + ), + ) + header = msg`Your account was temporarily suspended for ${plural(hours, { + one: '# hour', + other: '# hours', + })}` + } else if ( + action?.type === 'accountTakedown' || + action?.type === 'accountSuspended' + ) { + header = msg`Your account was suspended` + } else if (action?.type === 'contentRemoved') { + header = isPost ? msg`Your post was removed` : msg`Your content was removed` + } else if (action?.type === 'labelApplied') { + header = isAccount + ? msg`A label was added to your account` + : isPost + ? msg`A label was added to your post` + : msg`A label was added to your content` + } + + const blockHeader = isAccount + ? data.enforcement.state === 'suspended' || + data.enforcement.state === 'takendown' + ? msg`Limited profile` + : msg`Your account` + : isPost + ? data.enforcement.state === 'removed' + ? msg`Removed post` + : isRestoredPost + ? msg`Restored post` + : msg`Your post` + : msg`Your content` + const date = + data.appeal?.state === 'pending' + ? data.appeal.appealedAt + : appealResolved + ? data.appeal?.resolvedAt + : (action?.reversedAt ?? action?.createdAt) + + return {header, blockHeader, date, appealResolved, appealSummary} +} diff --git a/src/state/queries/moderation-inbox.ts b/src/state/queries/moderation-inbox.ts index e1cb92d0d..9bafead88 100644 --- a/src/state/queries/moderation-inbox.ts +++ b/src/state/queries/moderation-inbox.ts @@ -1,6 +1,7 @@ import {type DidString} from '@atproto/syntax' import {useLingui} from '@lingui/react/macro' import { + type InfiniteData, useInfiniteQuery, useMutation, useQuery, @@ -9,6 +10,7 @@ import { import {MOD_PROXY_SERVICE} from '#/lib/constants' import {isNetworkError} from '#/lib/strings/errors' +import {matchXrpcError} from '#/lib/xrpc-error' import {logger} from '#/logger' import {STALE} from '#/state/queries' import {createQueryKey} from '#/state/queries/util' @@ -130,6 +132,117 @@ export function useModerationInboxAccountStatusQuery() { }) } +export function useModerationInboxSubjectQuery({ + moderationService, + subject, + enabled = true, +}: { + moderationService?: DidString + subject?: tools.ozone.inbox.getActionedSubject.$Params['subject'] + enabled?: boolean +}) { + const client = useAppviewClient() + const {currentAccount, hasSession} = useSession() + const did = currentAccount?.did + + return useInfiniteQuery({ + queryKey: createQueryKey(QUERY_KEY_ROOT, { + did, + endpoint: 'subject', + moderationService, + subject, + }), + queryFn: async ({pageParam}) => { + if (!moderationService || !subject) { + throw new Error('Missing moderation service or subject') + } + return await client.call( + tools.ozone.inbox.getActionedSubject, + {subject, limit: 50, cursor: pageParam}, + {service: `${moderationService}#atproto_labeler`}, + ) + }, + initialPageParam: undefined as string | undefined, + getNextPageParam: page => page.cursor, + staleTime: STALE.MINUTES.ONE, + enabled: enabled && hasSession && !!did && !!moderationService && !!subject, + }) +} + +export function useAppealModerationInboxSubjectMutation( + moderationService: DidString, +) { + const client = useAppviewClient() + + return useMutation({ + mutationFn: async ( + input: tools.ozone.inbox.appealActionedSubject.$InputBody, + ) => + await client.call(tools.ozone.inbox.appealActionedSubject, input, { + service: `${moderationService}#atproto_labeler`, + }), + onError: error => { + if ( + !isNetworkError(error) && + !matchXrpcError(error, tools.ozone.inbox.appealActionedSubject) + ) { + logger.error('Could not submit moderation inbox appeal', { + safeMessage: error, + }) + } + }, + }) +} + +/** Refresh after closing the appeal dialog so cache updates cannot dismiss it. */ +export function useModerationInboxCacheMutation() { + const queryClient = useQueryClient() + const {currentAccount} = useSession() + const queryKey = createQueryKey(QUERY_KEY_ROOT, {did: currentAccount?.did}) + + return async ( + updatedSubject?: tools.ozone.inbox.appealActionedSubject.$OutputBody, + ) => { + if (updatedSubject) { + const subject = + 'did' in updatedSubject.subject && + typeof updatedSubject.subject.did === 'string' + ? updatedSubject.subject.did + : 'uri' in updatedSubject.subject && + typeof updatedSubject.subject.uri === 'string' + ? updatedSubject.subject.uri + : undefined + if (subject) { + const subjectQueryKey = createQueryKey(QUERY_KEY_ROOT, { + did: currentAccount?.did, + endpoint: 'subject', + moderationService: updatedSubject.src, + subject, + }) + await queryClient.cancelQueries({queryKey: subjectQueryKey}) + queryClient.setQueryData< + InfiniteData + >(subjectQueryKey, data => + data + ? { + ...data, + pages: data.pages.map(page => ({ + ...page, + enforcement: updatedSubject.enforcement, + appeal: updatedSubject.appeal, + availableActions: updatedSubject.availableActions, + isRead: updatedSubject.isRead, + updatedAt: updatedSubject.updatedAt, + })), + } + : data, + ) + } + } + await queryClient.invalidateQueries({queryKey}) + } +} + export function useModerationInboxUnreadCountQuery(isEnabled = true) { const client = useAppviewClient() const {currentAccount, hasSession} = useSession() -- knotmirror 0.0.1