about summary refs log tree commit diff
path: root/src/components/moderation/ReportDialog/action.ts
blob: fde2c78988ae55895d9798964fc21f861b595f70 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import {
  $Typed,
  ChatBskyConvoDefs,
  ComAtprotoModerationCreateReport,
} from '@atproto/api'
import {msg} from '@lingui/macro'
import {useLingui} from '@lingui/react'
import {useMutation} from '@tanstack/react-query'

import {logger} from '#/logger'
import {useAgent} from '#/state/session'
import {ReportState} from './state'
import {ParsedReportSubject} from './types'

export function useSubmitReportMutation() {
  const {_} = useLingui()
  const agent = useAgent()

  return useMutation({
    async mutationFn({
      subject,
      state,
    }: {
      subject: ParsedReportSubject
      state: ReportState
    }) {
      if (!state.selectedOption) {
        throw new Error(_(msg`Please select a reason for this report`))
      }
      if (!state.selectedLabeler) {
        throw new Error(_(msg`Please select a moderation service`))
      }

      let report:
        | ComAtprotoModerationCreateReport.InputSchema
        | (Omit<ComAtprotoModerationCreateReport.InputSchema, 'subject'> & {
            subject: $Typed<ChatBskyConvoDefs.MessageRef>
          })

      switch (subject.type) {
        case 'account': {
          report = {
            reasonType: state.selectedOption.reason,
            reason: state.details,
            subject: {
              $type: 'com.atproto.admin.defs#repoRef',
              did: subject.did,
            },
          }
          break
        }
        case 'post':
        case 'list':
        case 'feed':
        case 'starterPack': {
          report = {
            reasonType: state.selectedOption.reason,
            reason: state.details,
            subject: {
              $type: 'com.atproto.repo.strongRef',
              uri: subject.uri,
              cid: subject.cid,
            },
          }
          break
        }
        case 'chatMessage': {
          report = {
            reasonType: state.selectedOption.reason,
            reason: state.details,
            subject: {
              $type: 'chat.bsky.convo.defs#messageRef',
              messageId: subject.message.id,
              convoId: subject.convoId,
              did: subject.message.sender.did,
            },
          }
          break
        }
      }

      if (__DEV__) {
        logger.info('Submitting report', {
          labeler: {
            handle: state.selectedLabeler.creator.handle,
          },
          report,
        })
      } else {
        await agent.createModerationReport(report, {
          encoding: 'application/json',
          headers: {
            'atproto-proxy': `${state.selectedLabeler.creator.did}#atproto_labeler`,
          },
        })
      }
    },
  })
}