mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import {
2 type $Typed,
3 type ChatBskyConvoDefs,
4 type ComAtprotoModerationCreateReport,
5} from '@atproto/api'
6import {msg} from '@lingui/macro'
7import {useLingui} from '@lingui/react'
8import {useMutation} from '@tanstack/react-query'
9
10import {logger} from '#/logger'
11import {useAgent} from '#/state/session'
12import {type ReportState} from './state'
13import {type ParsedReportSubject} from './types'
14
15export function useSubmitReportMutation() {
16 const {_} = useLingui()
17 const agent = useAgent()
18
19 return useMutation({
20 async mutationFn({
21 subject,
22 state,
23 }: {
24 subject: ParsedReportSubject
25 state: ReportState
26 }) {
27 if (!state.selectedOption) {
28 throw new Error(_(msg`Please select a reason for this report`))
29 }
30 if (!state.selectedLabeler) {
31 throw new Error(_(msg`Please select a moderation service`))
32 }
33
34 let report:
35 | ComAtprotoModerationCreateReport.InputSchema
36 | (Omit<ComAtprotoModerationCreateReport.InputSchema, 'subject'> & {
37 subject: $Typed<ChatBskyConvoDefs.MessageRef>
38 })
39
40 switch (subject.type) {
41 case 'account': {
42 report = {
43 reasonType: state.selectedOption.reason,
44 reason: state.details,
45 subject: {
46 $type: 'com.atproto.admin.defs#repoRef',
47 did: subject.did,
48 },
49 }
50 break
51 }
52 case 'post':
53 case 'list':
54 case 'feed':
55 case 'starterPack': {
56 report = {
57 reasonType: state.selectedOption.reason,
58 reason: state.details,
59 subject: {
60 $type: 'com.atproto.repo.strongRef',
61 uri: subject.uri,
62 cid: subject.cid,
63 },
64 }
65 break
66 }
67 case 'chatMessage': {
68 report = {
69 reasonType: state.selectedOption.reason,
70 reason: state.details,
71 subject: {
72 $type: 'chat.bsky.convo.defs#messageRef',
73 messageId: subject.message.id,
74 convoId: subject.convoId,
75 did: subject.message.sender.did,
76 },
77 }
78 break
79 }
80 }
81
82 if (__DEV__) {
83 logger.info('Submitting report', {
84 labeler: {
85 handle: state.selectedLabeler.creator.handle,
86 },
87 report,
88 })
89 } else {
90 await agent.createModerationReport(report, {
91 encoding: 'application/json',
92 headers: {
93 'atproto-proxy': `${state.selectedLabeler.creator.did}#atproto_labeler`,
94 },
95 })
96 }
97 },
98 })
99}