@atproto/ozone 0.2.9 → 0.2.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (313) hide show
  1. package/CHANGELOG.md +40 -0
  2. package/dist/lexicon/lexicons.d.ts +8 -0
  3. package/dist/lexicon/lexicons.d.ts.map +1 -1
  4. package/dist/lexicon/lexicons.js +4 -0
  5. package/dist/lexicon/lexicons.js.map +1 -1
  6. package/dist/lexicon/types/app/bsky/actor/defs.d.ts +2 -0
  7. package/dist/lexicon/types/app/bsky/actor/defs.d.ts.map +1 -1
  8. package/dist/lexicon/types/app/bsky/actor/defs.js.map +1 -1
  9. package/package.json +25 -21
  10. package/bin/migration-create.ts +0 -38
  11. package/jest.config.cjs +0 -22
  12. package/src/api/chat/getActorMetadata.ts +0 -23
  13. package/src/api/chat/getConvo.ts +0 -23
  14. package/src/api/chat/getConvoMembers.ts +0 -23
  15. package/src/api/chat/getConvos.ts +0 -23
  16. package/src/api/chat/getMessageContext.ts +0 -42
  17. package/src/api/chat/index.ts +0 -16
  18. package/src/api/communication/createTemplate.ts +0 -51
  19. package/src/api/communication/deleteTemplate.ts +0 -23
  20. package/src/api/communication/listTemplates.ts +0 -31
  21. package/src/api/communication/updateTemplate.ts +0 -51
  22. package/src/api/health.ts +0 -27
  23. package/src/api/index.ts +0 -146
  24. package/src/api/label/fetchLabels.ts +0 -32
  25. package/src/api/label/queryLabels.ts +0 -57
  26. package/src/api/label/subscribeLabels.ts +0 -25
  27. package/src/api/moderation/cancelScheduledActions.ts +0 -72
  28. package/src/api/moderation/emitEvent.ts +0 -475
  29. package/src/api/moderation/getAccountTimeline.ts +0 -160
  30. package/src/api/moderation/getEvent.ts +0 -19
  31. package/src/api/moderation/getRecord.ts +0 -40
  32. package/src/api/moderation/getRecords.ts +0 -50
  33. package/src/api/moderation/getRepo.ts +0 -34
  34. package/src/api/moderation/getReporterStats.ts +0 -18
  35. package/src/api/moderation/getRepos.ts +0 -41
  36. package/src/api/moderation/getSubjects.ts +0 -101
  37. package/src/api/moderation/listScheduledActions.ts +0 -45
  38. package/src/api/moderation/queryEvents.ts +0 -72
  39. package/src/api/moderation/queryStatuses.ts +0 -23
  40. package/src/api/moderation/scheduleAction.ts +0 -129
  41. package/src/api/moderation/searchRepos.ts +0 -46
  42. package/src/api/moderation/util.ts +0 -96
  43. package/src/api/proxied.ts +0 -327
  44. package/src/api/queue/assignModerator.ts +0 -31
  45. package/src/api/queue/createQueue.ts +0 -62
  46. package/src/api/queue/deleteQueue.ts +0 -56
  47. package/src/api/queue/getAssignments.ts +0 -19
  48. package/src/api/queue/listQueues.ts +0 -39
  49. package/src/api/queue/routeReports.ts +0 -44
  50. package/src/api/queue/unassignModerator.ts +0 -26
  51. package/src/api/queue/updateQueue.ts +0 -54
  52. package/src/api/report/assignModerator.ts +0 -36
  53. package/src/api/report/createActivity.ts +0 -57
  54. package/src/api/report/createReport.ts +0 -93
  55. package/src/api/report/getAssignments.ts +0 -20
  56. package/src/api/report/getHistoricalStats.ts +0 -41
  57. package/src/api/report/getLatestReport.ts +0 -44
  58. package/src/api/report/getLiveStats.ts +0 -26
  59. package/src/api/report/getReport.ts +0 -55
  60. package/src/api/report/listActivities.ts +0 -37
  61. package/src/api/report/queryActivities.ts +0 -64
  62. package/src/api/report/queryReports.ts +0 -44
  63. package/src/api/report/reassignQueue.ts +0 -68
  64. package/src/api/report/refreshStats.ts +0 -27
  65. package/src/api/report/unassignModerator.ts +0 -21
  66. package/src/api/safelink/addRule.ts +0 -48
  67. package/src/api/safelink/queryEvents.ts +0 -32
  68. package/src/api/safelink/queryRules.ts +0 -58
  69. package/src/api/safelink/removeRule.ts +0 -42
  70. package/src/api/safelink/updateRule.ts +0 -48
  71. package/src/api/server/getConfig.ts +0 -35
  72. package/src/api/set/addValues.ts +0 -28
  73. package/src/api/set/deleteSet.ts +0 -34
  74. package/src/api/set/deleteValues.ts +0 -31
  75. package/src/api/set/getValues.ts +0 -42
  76. package/src/api/set/querySets.ts +0 -36
  77. package/src/api/set/upsertSet.ts +0 -38
  78. package/src/api/setting/listOptions.ts +0 -44
  79. package/src/api/setting/removeOptions.ts +0 -64
  80. package/src/api/setting/upsertOption.ts +0 -156
  81. package/src/api/team/addMember.ts +0 -51
  82. package/src/api/team/deleteMember.ts +0 -29
  83. package/src/api/team/listMembers.ts +0 -20
  84. package/src/api/team/updateMember.ts +0 -47
  85. package/src/api/util.ts +0 -265
  86. package/src/api/verification/grantVerifications.ts +0 -90
  87. package/src/api/verification/listVerifications.ts +0 -44
  88. package/src/api/verification/revokeVerifications.ts +0 -43
  89. package/src/api/well-known.ts +0 -46
  90. package/src/assignment/index.ts +0 -728
  91. package/src/auth-verifier.ts +0 -227
  92. package/src/background.ts +0 -183
  93. package/src/communication-service/template.ts +0 -110
  94. package/src/communication-service/util.ts +0 -8
  95. package/src/config/config.ts +0 -211
  96. package/src/config/env.ts +0 -95
  97. package/src/config/index.ts +0 -3
  98. package/src/config/secrets.ts +0 -17
  99. package/src/context.ts +0 -399
  100. package/src/daemon/blob-diverter.ts +0 -186
  101. package/src/daemon/context.ts +0 -247
  102. package/src/daemon/event-pusher.ts +0 -363
  103. package/src/daemon/event-reverser.ts +0 -128
  104. package/src/daemon/index.ts +0 -33
  105. package/src/daemon/job-cursor.ts +0 -33
  106. package/src/daemon/materialized-view-refresher.ts +0 -33
  107. package/src/daemon/queue-router.ts +0 -101
  108. package/src/daemon/scheduled-action-processor.ts +0 -304
  109. package/src/daemon/stats-computer.ts +0 -101
  110. package/src/daemon/strike-expiry-processor.ts +0 -95
  111. package/src/daemon/team-profile-synchronizer.ts +0 -15
  112. package/src/daemon/verification-listener.ts +0 -169
  113. package/src/db/index.ts +0 -203
  114. package/src/db/migrations/20231219T205730722Z-init.ts +0 -170
  115. package/src/db/migrations/20240116T085607200Z-communication-template.ts +0 -23
  116. package/src/db/migrations/20240201T051104136Z-mod-event-blobs.ts +0 -15
  117. package/src/db/migrations/20240208T213404429Z-add-tags-column-to-moderation-subject.ts +0 -31
  118. package/src/db/migrations/20240228T003647759Z-add-label-sigs.ts +0 -25
  119. package/src/db/migrations/20240408T192432676Z-mute-reporting.ts +0 -15
  120. package/src/db/migrations/20240506T225055595Z-message-subject.ts +0 -21
  121. package/src/db/migrations/20240521T211332580Z-member.ts +0 -17
  122. package/src/db/migrations/20240814T003647759Z-event-created-at-index.ts +0 -13
  123. package/src/db/migrations/20240903T205730722Z-add-template-lang.ts +0 -12
  124. package/src/db/migrations/20240904T205730722Z-add-subject-did-index.ts +0 -13
  125. package/src/db/migrations/20241001T205730722Z-subject-status-review-state-index.ts +0 -15
  126. package/src/db/migrations/20241008T205730722Z-sets.ts +0 -53
  127. package/src/db/migrations/20241018T205730722Z-setting.ts +0 -27
  128. package/src/db/migrations/20241026T205730722Z-add-hosting-status-to-subject-status.ts +0 -57
  129. package/src/db/migrations/20241220T144630860Z-stats-materialized-views.ts +0 -215
  130. package/src/db/migrations/20250204T003647759Z-add-subject-priority-score.ts +0 -22
  131. package/src/db/migrations/20250211T003647759Z-add-reporter-stats-index.ts +0 -38
  132. package/src/db/migrations/20250211T132135150Z-moderation-event-message-partial-idx.ts +0 -26
  133. package/src/db/migrations/20250221T132135150Z-member-details.ts +0 -14
  134. package/src/db/migrations/20250404T201720309Z-subject-status-sort-idxs.ts +0 -18
  135. package/src/db/migrations/20250415T201720309Z-verification.ts +0 -34
  136. package/src/db/migrations/20250417T201720309Z-firehose-cursor.ts +0 -16
  137. package/src/db/migrations/20250609T110704000Z-safelink.ts +0 -53
  138. package/src/db/migrations/20250618T180246000Z-add-mod-tool-to-moderation-event.ts +0 -18
  139. package/src/db/migrations/20250701T000000000Z-add-age-assurance-state.ts +0 -25
  140. package/src/db/migrations/20250715T000000000Z-add-mod-event-external-id.ts +0 -15
  141. package/src/db/migrations/20250718T150931000Z-update-appeal-reason-stats.ts +0 -310
  142. package/src/db/migrations/20250813T000000000Z-mod-tool-batch-id-index.ts +0 -14
  143. package/src/db/migrations/20250923T000000000Z-scheduled-actions.ts +0 -56
  144. package/src/db/migrations/20251008T120000000Z-add-strike-system.ts +0 -87
  145. package/src/db/migrations/20260210T154806448Z-mod-event-created-by-indexes.ts +0 -22
  146. package/src/db/migrations/20260219T164523000Z-create-report-table.ts +0 -155
  147. package/src/db/migrations/20260219T165302248Z-moderator-assignment.ts +0 -42
  148. package/src/db/migrations/20260225T000000000Z-add-report-queue-table.ts +0 -41
  149. package/src/db/migrations/20260313T000000000Z-add-report-activity-table.ts +0 -48
  150. package/src/db/migrations/20260318T152058935Z-add-report-stat.ts +0 -35
  151. package/src/db/migrations/20260428T000000000Z-add-expiring-tag-table.ts +0 -32
  152. package/src/db/migrations/20260513T202941104Z-add-subject-convo-id.ts +0 -114
  153. package/src/db/migrations/20260602T120000000Z-add-report-activity-created-index.ts +0 -17
  154. package/src/db/migrations/index.ts +0 -44
  155. package/src/db/migrations/provider.ts +0 -26
  156. package/src/db/pagination.ts +0 -335
  157. package/src/db/schema/account_events_stats.ts +0 -16
  158. package/src/db/schema/account_record_events_stats.ts +0 -15
  159. package/src/db/schema/account_record_status_stats.ts +0 -15
  160. package/src/db/schema/account_strike.ts +0 -13
  161. package/src/db/schema/blob_push_event.ts +0 -21
  162. package/src/db/schema/communication_template.ts +0 -19
  163. package/src/db/schema/expiring_tag.ts +0 -18
  164. package/src/db/schema/firehose_cursor.ts +0 -13
  165. package/src/db/schema/index.ts +0 -60
  166. package/src/db/schema/job_cursor.ts +0 -13
  167. package/src/db/schema/label.ts +0 -22
  168. package/src/db/schema/member.ts +0 -22
  169. package/src/db/schema/moderation_event.ts +0 -61
  170. package/src/db/schema/moderation_subject_status.ts +0 -52
  171. package/src/db/schema/moderator_assignment.ts +0 -16
  172. package/src/db/schema/ozone_set.ts +0 -24
  173. package/src/db/schema/record_events_stats.ts +0 -15
  174. package/src/db/schema/record_push_event.ts +0 -21
  175. package/src/db/schema/repo_push_event.ts +0 -19
  176. package/src/db/schema/report.ts +0 -28
  177. package/src/db/schema/report_activity.ts +0 -22
  178. package/src/db/schema/report_queue.ts +0 -21
  179. package/src/db/schema/report_stat.ts +0 -27
  180. package/src/db/schema/safelink.ts +0 -39
  181. package/src/db/schema/scheduled-action.ts +0 -25
  182. package/src/db/schema/setting.ts +0 -24
  183. package/src/db/schema/signing_key.ts +0 -10
  184. package/src/db/schema/verification.ts +0 -21
  185. package/src/db/types.ts +0 -24
  186. package/src/error.ts +0 -12
  187. package/src/image-invalidator.ts +0 -7
  188. package/src/index.ts +0 -154
  189. package/src/jetstream/service.ts +0 -107
  190. package/src/logger.ts +0 -29
  191. package/src/mod-service/expiring-tags.ts +0 -104
  192. package/src/mod-service/index.ts +0 -1842
  193. package/src/mod-service/profile.ts +0 -139
  194. package/src/mod-service/report.ts +0 -429
  195. package/src/mod-service/status.ts +0 -549
  196. package/src/mod-service/strike.ts +0 -96
  197. package/src/mod-service/subject.ts +0 -311
  198. package/src/mod-service/types.ts +0 -96
  199. package/src/mod-service/util.ts +0 -99
  200. package/src/mod-service/views.ts +0 -912
  201. package/src/queue/service.ts +0 -603
  202. package/src/report/activity.ts +0 -281
  203. package/src/report/handle-report-update.ts +0 -209
  204. package/src/report/reassign.ts +0 -109
  205. package/src/report/stats.ts +0 -852
  206. package/src/report/views.ts +0 -239
  207. package/src/safelink/service.ts +0 -304
  208. package/src/scheduled-action/service.ts +0 -281
  209. package/src/scheduled-action/types.ts +0 -17
  210. package/src/sequencer/index.ts +0 -2
  211. package/src/sequencer/outbox.ts +0 -123
  212. package/src/sequencer/sequencer.ts +0 -147
  213. package/src/set/service.ts +0 -230
  214. package/src/setting/constants.ts +0 -3
  215. package/src/setting/service.ts +0 -148
  216. package/src/setting/types.ts +0 -3
  217. package/src/setting/validators.ts +0 -333
  218. package/src/tag-service/content-tagger.ts +0 -30
  219. package/src/tag-service/embed-tagger.ts +0 -70
  220. package/src/tag-service/index.ts +0 -70
  221. package/src/tag-service/language-data.ts +0 -561
  222. package/src/tag-service/language-tagger.ts +0 -101
  223. package/src/tag-service/util.ts +0 -13
  224. package/src/team/index.ts +0 -296
  225. package/src/util.ts +0 -230
  226. package/src/verification/issuer.ts +0 -146
  227. package/src/verification/service.ts +0 -208
  228. package/src/verification/util.ts +0 -53
  229. package/test.env +0 -2
  230. package/tests/3p-labeler.test.ts +0 -288
  231. package/tests/__snapshots__/account-strikes.test.ts.snap +0 -159
  232. package/tests/__snapshots__/age-assurance.test.ts.snap +0 -66
  233. package/tests/__snapshots__/blob-divert.test.ts.snap +0 -219
  234. package/tests/__snapshots__/get-account-timeline.test.ts.snap +0 -36
  235. package/tests/__snapshots__/get-record.test.ts.snap +0 -271
  236. package/tests/__snapshots__/get-records.test.ts.snap +0 -175
  237. package/tests/__snapshots__/get-repo.test.ts.snap +0 -91
  238. package/tests/__snapshots__/get-repos.test.ts.snap +0 -127
  239. package/tests/__snapshots__/get-starter-pack.test.ts.snap +0 -535
  240. package/tests/__snapshots__/get-subjects.test.ts.snap +0 -529
  241. package/tests/__snapshots__/moderation-events.test.ts.snap +0 -347
  242. package/tests/__snapshots__/moderation-statuses.test.ts.snap +0 -276
  243. package/tests/__snapshots__/moderation.test.ts.snap +0 -85
  244. package/tests/__snapshots__/report-reason.test.ts.snap +0 -14
  245. package/tests/__snapshots__/safelink.test.ts.snap +0 -179
  246. package/tests/__snapshots__/scheduled-action.test.ts.snap +0 -61
  247. package/tests/__snapshots__/sets.test.ts.snap +0 -46
  248. package/tests/__snapshots__/settings.test.ts.snap +0 -52
  249. package/tests/__snapshots__/team.test.ts.snap +0 -374
  250. package/tests/__snapshots__/verification-listener.test.ts.snap +0 -152
  251. package/tests/__snapshots__/verification.test.ts.snap +0 -302
  252. package/tests/_util.ts +0 -242
  253. package/tests/account-strikes.test.ts +0 -184
  254. package/tests/ack-all-subjects-of-account.test.ts +0 -177
  255. package/tests/age-assurance.test.ts +0 -372
  256. package/tests/blob-divert.test.ts +0 -106
  257. package/tests/communication-templates.test.ts +0 -149
  258. package/tests/content-tagger.test.ts +0 -170
  259. package/tests/db.test.ts +0 -184
  260. package/tests/expiring-label.test.ts +0 -72
  261. package/tests/expiring-tags.test.ts +0 -232
  262. package/tests/get-account-timeline.test.ts +0 -85
  263. package/tests/get-config.test.ts +0 -55
  264. package/tests/get-lists.test.ts +0 -111
  265. package/tests/get-profiles.test.ts +0 -70
  266. package/tests/get-record.test.ts +0 -130
  267. package/tests/get-records.test.ts +0 -91
  268. package/tests/get-repo.test.ts +0 -171
  269. package/tests/get-report.test.ts +0 -136
  270. package/tests/get-reporter-stats.test.ts +0 -132
  271. package/tests/get-repos.test.ts +0 -91
  272. package/tests/get-starter-pack.test.ts +0 -115
  273. package/tests/get-subjects.test.ts +0 -81
  274. package/tests/mod-tool.test.ts +0 -268
  275. package/tests/moderation-appeals.test.ts +0 -260
  276. package/tests/moderation-events.test.ts +0 -756
  277. package/tests/moderation-status-tags.test.ts +0 -140
  278. package/tests/moderation-statuses.test.ts +0 -495
  279. package/tests/moderation.test.ts +0 -992
  280. package/tests/protected-tags.test.ts +0 -218
  281. package/tests/query-labels.test.ts +0 -238
  282. package/tests/query-reports.test.ts +0 -608
  283. package/tests/queue-assignment.test.ts +0 -428
  284. package/tests/queue-router.test.ts +0 -306
  285. package/tests/queues.test.ts +0 -690
  286. package/tests/record-and-account-events.test.ts +0 -197
  287. package/tests/repo-search.test.ts +0 -136
  288. package/tests/report-action.test.ts +0 -308
  289. package/tests/report-activity.test.ts +0 -711
  290. package/tests/report-assignment.test.ts +0 -517
  291. package/tests/report-muting.test.ts +0 -100
  292. package/tests/report-reason.test.ts +0 -154
  293. package/tests/report-reassign-queue.test.ts +0 -340
  294. package/tests/report-routing.test.ts +0 -245
  295. package/tests/report-stats.test.ts +0 -545
  296. package/tests/revoke-account-credentials.test.ts +0 -54
  297. package/tests/safelink.test.ts +0 -534
  298. package/tests/scheduled-action-processor.test.ts +0 -488
  299. package/tests/scheduled-action.test.ts +0 -334
  300. package/tests/sequencer.test.ts +0 -227
  301. package/tests/server.test.ts +0 -62
  302. package/tests/sets.test.ts +0 -246
  303. package/tests/settings.test.ts +0 -308
  304. package/tests/strike-expiry-processor.test.ts +0 -299
  305. package/tests/subject-priority-score.test.ts +0 -96
  306. package/tests/takedown.test.ts +0 -105
  307. package/tests/team.test.ts +0 -216
  308. package/tests/verification-listener.test.ts +0 -129
  309. package/tests/verification.test.ts +0 -186
  310. package/tsconfig.build.json +0 -9
  311. package/tsconfig.build.tsbuildinfo +0 -1
  312. package/tsconfig.json +0 -7
  313. package/tsconfig.tests.json +0 -8
@@ -1,1842 +0,0 @@
1
- import { Expression, Insertable, sql } from 'kysely'
2
- import { CID } from 'multiformats/cid'
3
- import { AtpAgent, ToolsOzoneModerationDefs } from '@atproto/api'
4
- import { addHoursToDate, chunkArray } from '@atproto/common'
5
- import { Keypair } from '@atproto/crypto'
6
- import { IdResolver } from '@atproto/identity'
7
- import { AtUri, INVALID_HANDLE } from '@atproto/syntax'
8
- import { InvalidRequestError } from '@atproto/xrpc-server'
9
- import { getReviewState } from '../api/util.js'
10
- import { BackgroundQueue } from '../background.js'
11
- import { OzoneConfig } from '../config/index.js'
12
- import { EventPusher } from '../daemon/index.js'
13
- import { Database } from '../db/index.js'
14
- import { StatusKeyset, TimeIdKeyset, paginate } from '../db/pagination.js'
15
- import { BlobPushEvent } from '../db/schema/blob_push_event.js'
16
- import { LabelChannel } from '../db/schema/label.js'
17
- import { ModerationEvent } from '../db/schema/moderation_event.js'
18
- import { jsonb } from '../db/types.js'
19
- import { ImageInvalidator } from '../image-invalidator.js'
20
- import { ids } from '../lexicon/lexicons.js'
21
- import {
22
- RepoBlobRef,
23
- RepoRef,
24
- } from '../lexicon/types/com/atproto/admin/defs.js'
25
- import { Label } from '../lexicon/types/com/atproto/label/defs.js'
26
- import { ReasonType } from '../lexicon/types/com/atproto/moderation/defs.js'
27
- import { Main as StrongRef } from '../lexicon/types/com/atproto/repo/strongRef.js'
28
- import {
29
- REVIEWESCALATED,
30
- REVIEWOPEN,
31
- isAccountEvent,
32
- isAgeAssuranceEvent,
33
- isAgeAssuranceOverrideEvent,
34
- isAgeAssurancePurgeEvent,
35
- isIdentityEvent,
36
- isModEventAcknowledge,
37
- isModEventComment,
38
- isModEventEmail,
39
- isModEventLabel,
40
- isModEventMute,
41
- isModEventPriorityScore,
42
- isModEventReport,
43
- isModEventReverseTakedown,
44
- isModEventTag,
45
- isModEventTakedown,
46
- isRecordEvent,
47
- isScheduleTakedownEvent,
48
- } from '../lexicon/types/tools/ozone/moderation/defs.js'
49
- import { QueryParams as QueryStatusParams } from '../lexicon/types/tools/ozone/moderation/queryStatuses.js'
50
- import { httpLogger as log } from '../logger.js'
51
- import { LABELER_HEADER_NAME, ParsedLabelers } from '../util.js'
52
- import { insertExpiringTags, removeExpiringTags } from './expiring-tags.js'
53
- import {
54
- adjustModerationSubjectStatus,
55
- getStatusIdentifierFromSubject,
56
- moderationSubjectStatusQueryBuilder,
57
- } from './status.js'
58
- import { StrikeService, StrikeServiceCreator } from './strike.js'
59
- import {
60
- ModSubject,
61
- RecordSubject,
62
- RepoSubject,
63
- subjectFromStatusRow,
64
- } from './subject.js'
65
- import {
66
- ModEventType,
67
- ModerationEventRow,
68
- ModerationEventRowWithHandle,
69
- ModerationSubjectStatusRow,
70
- ModerationSubjectStatusRowWithHandle,
71
- ReporterStats,
72
- ReporterStatsResult,
73
- ReversibleModerationEvent,
74
- } from './types.js'
75
- import {
76
- dateFromDbDatetime,
77
- formatLabel,
78
- formatLabelRow,
79
- getPdsAgentForRepo,
80
- signLabel,
81
- } from './util.js'
82
- import { AuthHeaders, ModerationViews } from './views.js'
83
-
84
- export type ModerationServiceCreator = (db: Database) => ModerationService
85
-
86
- export class ModerationService {
87
- constructor(
88
- public db: Database,
89
- public signingKey: Keypair,
90
- public signingKeyId: number,
91
- public cfg: OzoneConfig,
92
- public backgroundQueue: BackgroundQueue,
93
- public idResolver: IdResolver,
94
- public eventPusher: EventPusher,
95
- public appviewAgent: AtpAgent,
96
- private createAuthHeaders: (
97
- aud: string,
98
- method: string,
99
- ) => Promise<AuthHeaders>,
100
- public strikeService: StrikeService,
101
- public imgInvalidator?: ImageInvalidator,
102
- ) {}
103
-
104
- static creator(
105
- signingKey: Keypair,
106
- signingKeyId: number,
107
- cfg: OzoneConfig,
108
- backgroundQueue: BackgroundQueue,
109
- idResolver: IdResolver,
110
- eventPusher: EventPusher,
111
- appviewAgent: AtpAgent,
112
- createAuthHeaders: (aud: string, method: string) => Promise<AuthHeaders>,
113
- strikeServiceCreator: StrikeServiceCreator,
114
- imgInvalidator?: ImageInvalidator,
115
- ) {
116
- return (db: Database) => {
117
- const strikeService = strikeServiceCreator(db)
118
- return new ModerationService(
119
- db,
120
- signingKey,
121
- signingKeyId,
122
- cfg,
123
- backgroundQueue,
124
- idResolver,
125
- eventPusher,
126
- appviewAgent,
127
- createAuthHeaders,
128
- strikeService,
129
- imgInvalidator,
130
- )
131
- }
132
- }
133
-
134
- views = new ModerationViews(
135
- this.db,
136
- this.signingKey,
137
- this.signingKeyId,
138
- this.appviewAgent,
139
- async (method: string, labelers?: ParsedLabelers) => {
140
- const authHeaders = await this.createAuthHeaders(
141
- this.cfg.appview.did,
142
- method,
143
- )
144
- if (labelers?.dids?.length) {
145
- authHeaders.headers[LABELER_HEADER_NAME] = labelers.dids.join(', ')
146
- }
147
- return authHeaders
148
- },
149
- this.idResolver,
150
- this.cfg.service.devMode,
151
- )
152
-
153
- async getEvent(id: number): Promise<ModerationEventRow | undefined> {
154
- return await this.db.db
155
- .selectFrom('moderation_event')
156
- .selectAll()
157
- .where('id', '=', id)
158
- .executeTakeFirst()
159
- }
160
-
161
- async getEventOrThrow(id: number): Promise<ModerationEventRow> {
162
- const event = await this.getEvent(id)
163
- if (!event) throw new InvalidRequestError('Moderation event not found')
164
- return event
165
- }
166
-
167
- async getEventByExternalId(
168
- eventType: ModerationEvent['action'],
169
- externalId: string,
170
- subject: ModSubject,
171
- ): Promise<boolean> {
172
- const result = await this.db.db
173
- .selectFrom('moderation_event')
174
- .where('action', '=', eventType)
175
- .where('externalId', '=', externalId)
176
- .where('subjectDid', '=', subject.did)
177
- .select(sql`1`.as('exists'))
178
- .limit(1)
179
- .executeTakeFirst()
180
- return !!result
181
- }
182
-
183
- async getEvents(opts: {
184
- subject?: string
185
- createdBy?: string
186
- limit: number
187
- cursor?: string
188
- includeAllUserRecords: boolean
189
- types: ModerationEvent['action'][]
190
- sortDirection?: 'asc' | 'desc'
191
- hasComment?: boolean
192
- comment?: string
193
- createdAfter?: string
194
- createdBefore?: string
195
- addedLabels: string[]
196
- removedLabels: string[]
197
- addedTags: string[]
198
- removedTags: string[]
199
- reportTypes?: string[]
200
- collections: string[]
201
- subjectType?: string
202
- policies?: string[]
203
- modTool?: string[]
204
- ageAssuranceState?: string
205
- batchId?: string
206
- withStrike?: boolean
207
- }): Promise<{ cursor?: string; events: ModerationEventRow[] }> {
208
- const {
209
- subject,
210
- createdBy,
211
- limit,
212
- cursor,
213
- includeAllUserRecords,
214
- sortDirection = 'desc',
215
- types,
216
- hasComment,
217
- comment,
218
- createdAfter,
219
- createdBefore,
220
- addedLabels,
221
- removedLabels,
222
- addedTags,
223
- removedTags,
224
- reportTypes,
225
- collections,
226
- subjectType,
227
- policies,
228
- modTool,
229
- ageAssuranceState,
230
- batchId,
231
- withStrike,
232
- } = opts
233
- const { ref } = this.db.db.dynamic
234
- let builder = this.db.db.selectFrom('moderation_event').selectAll()
235
-
236
- if (subject) {
237
- const isSubjectAtUri = subject.startsWith('at://')
238
- const subjectAtUri = isSubjectAtUri ? new AtUri(subject) : null
239
- const subjectDid = isSubjectAtUri ? new AtUri(subject).hostname : subject
240
- const subjectUri = isSubjectAtUri ? subject : null
241
- // regardless of subjectUri check, we always want to query against subjectDid column since that's indexed
242
- builder = builder.where('subjectDid', '=', subjectDid)
243
-
244
- // subjectUri or subjectConvoId
245
- if (!includeAllUserRecords) {
246
- if (subjectAtUri?.collection === 'chat.bsky.convo') {
247
- builder = builder.where('subjectConvoId', '=', subjectAtUri.rkey)
248
- } else if (subjectUri) {
249
- builder = builder.where('subjectUri', '=', subjectUri)
250
- } else {
251
- // Account-level: subjectUri IS NULL also matches conversation events,
252
- // so explicitly exclude them.
253
- builder = builder
254
- .where('subjectUri', 'is', null)
255
- .where('subjectConvoId', 'is', null)
256
- }
257
- }
258
- } else if (subjectType === 'account') {
259
- builder = builder
260
- .where('subjectUri', 'is', null)
261
- .where('subjectConvoId', 'is', null)
262
- } else if (subjectType === 'record') {
263
- builder = builder.where('subjectUri', 'is not', null)
264
- } else if (subjectType === 'conversation') {
265
- builder = builder.where('subjectConvoId', 'is not', null)
266
- }
267
-
268
- // If subjectType is set to 'account' let that take priority and ignore collections filter
269
- if (collections.length && subjectType !== 'account') {
270
- builder = builder
271
- .where('subjectUri', 'is not', null)
272
- .where((eb) =>
273
- eb.or(
274
- collections.map((collection) =>
275
- eb('subjectUri', 'like', `%/${collection}/%`),
276
- ),
277
- ),
278
- )
279
- }
280
-
281
- if (types.length) {
282
- builder = builder.where((eb) => {
283
- if (types.length === 1) {
284
- return eb('action', '=', types[0])
285
- }
286
-
287
- return eb('action', 'in', types)
288
- })
289
- }
290
- if (createdBy) {
291
- builder = builder.where('createdBy', '=', createdBy)
292
- }
293
- if (createdAfter) {
294
- builder = builder.where('createdAt', '>=', createdAfter)
295
- }
296
- if (createdBefore) {
297
- builder = builder.where('createdAt', '<=', createdBefore)
298
- }
299
-
300
- if (comment) {
301
- // the input may end in || in which case, there may be item in the array which is just '' and we want to ignore those
302
- const keywords = comment.split('||').filter((keyword) => !!keyword.trim())
303
- if (keywords.length > 1) {
304
- builder = builder.where((eb) =>
305
- eb.or(
306
- keywords.map((keyword) => eb('comment', 'ilike', `%${keyword}%`)),
307
- ),
308
- )
309
- } else if (keywords.length === 1) {
310
- builder = builder.where('comment', 'ilike', `%${keywords[0]}%`)
311
- }
312
- }
313
- if (hasComment) {
314
- builder = builder.where('comment', 'is not', null)
315
- }
316
-
317
- // If multiple labels are passed, then only retrieve events where all those labels exist
318
- if (addedLabels.length) {
319
- addedLabels.forEach((label) => {
320
- builder = builder.where('createLabelVals', 'ilike', `%${label}%`)
321
- })
322
- }
323
- if (removedLabels.length) {
324
- removedLabels.forEach((label) => {
325
- builder = builder.where('negateLabelVals', 'ilike', `%${label}%`)
326
- })
327
- }
328
- if (addedTags.length) {
329
- builder = builder.where(
330
- sql<boolean>`${ref('addedTags')} @> ${jsonb(addedTags)}`,
331
- )
332
- }
333
- if (removedTags.length) {
334
- builder = builder.where(
335
- sql<boolean>`${ref('removedTags')} @> ${jsonb(removedTags)}`,
336
- )
337
- }
338
- if (reportTypes?.length) {
339
- builder = builder.where(sql`meta->>'reportType'`, 'in', reportTypes)
340
- }
341
- if (policies?.length) {
342
- builder = builder.where((eb) =>
343
- eb.or(
344
- policies.map((policy) =>
345
- eb(sql`meta->>'policies'`, 'ilike', `%${policy}%`),
346
- ),
347
- ),
348
- )
349
- }
350
- if (modTool?.length) {
351
- builder = builder
352
- .where('modTool', 'is not', null)
353
- .where(sql`("modTool" ->> 'name')`, 'in', modTool)
354
- }
355
- if (batchId) {
356
- builder = builder
357
- .where('modTool', 'is not', null)
358
- .where(sql`("modTool" -> 'meta' ->> 'batchId')`, '=', batchId)
359
- }
360
- if (ageAssuranceState) {
361
- builder = builder
362
- .where('action', 'in', [
363
- 'tools.ozone.moderation.defs#ageAssuranceEvent',
364
- 'tools.ozone.moderation.defs#ageAssuranceOverrideEvent',
365
- ])
366
- .where(sql`meta->>'status'`, '=', ageAssuranceState)
367
- }
368
-
369
- if (withStrike !== undefined) {
370
- builder = builder.where('strikeCount', 'is not', null)
371
- }
372
-
373
- const keyset = new TimeIdKeyset(
374
- ref(`moderation_event.createdAt`),
375
- ref('moderation_event.id'),
376
- )
377
- const paginatedBuilder = paginate(builder, {
378
- limit,
379
- cursor,
380
- keyset,
381
- direction: sortDirection,
382
- tryIndex: true,
383
- })
384
-
385
- const result = await paginatedBuilder.execute()
386
-
387
- const infos = await this.views.getAccoutInfosByDid([
388
- ...result.map((row) => row.subjectDid),
389
- ...result.map((row) => row.createdBy),
390
- ])
391
-
392
- const resultWithHandles = result.map((r) => ({
393
- ...r,
394
- creatorHandle: infos.get(r.createdBy)?.handle,
395
- subjectHandle: infos.get(r.subjectDid)?.handle,
396
- }))
397
-
398
- return { cursor: keyset.packFromResult(result), events: resultWithHandles }
399
- }
400
-
401
- async getEventsByIds(ids: number[]): Promise<ModerationEventRowWithHandle[]> {
402
- if (!ids.length) return []
403
-
404
- const result = await this.db.db
405
- .selectFrom('moderation_event')
406
- .selectAll()
407
- .where('id', 'in', ids)
408
- .execute()
409
-
410
- if (!result.length) return []
411
-
412
- const infos = await this.views.getAccoutInfosByDid([
413
- ...result.map((row) => row.subjectDid),
414
- ...result.map((row) => row.createdBy),
415
- ])
416
-
417
- return result.map((r) => ({
418
- ...r,
419
- creatorHandle: infos.get(r.createdBy)?.handle,
420
- subjectHandle: infos.get(r.subjectDid)?.handle,
421
- }))
422
- }
423
-
424
- async getReport(id: number): Promise<ModerationEventRow | undefined> {
425
- return await this.db.db
426
- .selectFrom('moderation_event')
427
- .where('action', '=', 'tools.ozone.moderation.defs#modEventReport')
428
- .selectAll()
429
- .where('id', '=', id)
430
- .executeTakeFirst()
431
- }
432
-
433
- async getCurrentStatus(
434
- subject: { did: string } | { uri: AtUri } | { cids: CID[] },
435
- ) {
436
- let builder = this.db.db.selectFrom('moderation_subject_status').selectAll()
437
- if ('did' in subject) {
438
- builder = builder.where('did', '=', subject.did)
439
- } else if ('uri' in subject) {
440
- builder = builder.where('recordPath', '=', subject.uri.toString())
441
- }
442
- // TODO: Handle the cid status
443
- return await builder.execute()
444
- }
445
-
446
- async resolveSubjectsForAccount(
447
- did: string,
448
- createdBy: string,
449
- accountEvent: ModerationEventRow,
450
- ) {
451
- const subjectsToBeResolved = await this.db.db
452
- .selectFrom('moderation_subject_status')
453
- .where('did', '=', did)
454
- .where((eb) =>
455
- eb.or([eb('recordPath', '!=', ''), eb('convoId', '!=', '')]),
456
- )
457
- .where('reviewState', 'in', [REVIEWESCALATED, REVIEWOPEN])
458
- .selectAll()
459
- .execute()
460
-
461
- if (subjectsToBeResolved.length === 0) {
462
- return
463
- }
464
-
465
- let accountEventInfo = `Account Event ID: ${accountEvent.id}`
466
- if (accountEvent.comment) {
467
- accountEventInfo += ` | Account Event Comment: ${accountEvent.comment}`
468
- }
469
- // Process subjects in chunks of 100 since each of these will trigger multiple db queries
470
- for (const subjects of chunkArray(subjectsToBeResolved, 100)) {
471
- await Promise.all(
472
- subjects.map(async (subject) => {
473
- const eventData = {
474
- createdBy,
475
- subject: subjectFromStatusRow(subject),
476
- }
477
-
478
- // For consistency's sake, when acknowledging appealed subjects, we should first resolve the appeal
479
- if (subject.appealed) {
480
- await this.logEvent({
481
- event: {
482
- $type: 'tools.ozone.moderation.defs#modEventResolveAppeal',
483
- comment: `[AUTO_RESOLVE_ON_ACCOUNT_ACTION]: Automatically resolving all appealed content due to account level action | ${accountEventInfo}`,
484
- },
485
- ...eventData,
486
- })
487
- }
488
-
489
- await this.logEvent({
490
- event: {
491
- $type: 'tools.ozone.moderation.defs#modEventAcknowledge',
492
- comment: `[AUTO_RESOLVE_ON_ACCOUNT_ACTION]: Automatically resolving all reported content due to account level action | ${accountEventInfo}`,
493
- },
494
- ...eventData,
495
- })
496
- }),
497
- )
498
- }
499
- }
500
-
501
- async logEvent(info: {
502
- event: ModEventType
503
- subject: ModSubject
504
- createdBy: string
505
- createdAt?: Date
506
- modTool?: ToolsOzoneModerationDefs.ModTool
507
- externalId?: string
508
- }): Promise<{
509
- event: ModerationEventRow
510
- subjectStatus: ModerationSubjectStatusRow | null
511
- }> {
512
- this.db.assertTransaction()
513
- const {
514
- event,
515
- subject,
516
- createdBy,
517
- externalId,
518
- createdAt = new Date(),
519
- modTool,
520
- } = info
521
-
522
- const createLabelVals =
523
- isModEventLabel(event) && event.createLabelVals.length > 0
524
- ? event.createLabelVals.join(' ')
525
- : undefined
526
- const negateLabelVals =
527
- isModEventLabel(event) && event.negateLabelVals.length > 0
528
- ? event.negateLabelVals.join(' ')
529
- : undefined
530
-
531
- const meta: Record<string, string | number | boolean> = {}
532
-
533
- const addedTags = isModEventTag(event) ? jsonb(event.add) : null
534
- const removedTags = isModEventTag(event) ? jsonb(event.remove) : null
535
-
536
- if (isModEventReport(event)) {
537
- meta.reportType = event.reportType
538
- }
539
-
540
- if (isModEventComment(event) && event.sticky) {
541
- meta.sticky = event.sticky
542
- }
543
-
544
- if (isModEventEmail(event)) {
545
- meta.subjectLine = event.subjectLine
546
- meta.isDelivered = !!event.isDelivered
547
- if (event.content) {
548
- meta.content = event.content
549
- }
550
- if (event.policies?.length) {
551
- meta.policies = event.policies.join(',')
552
- }
553
- }
554
-
555
- if (isAccountEvent(event)) {
556
- meta.active = event.active
557
- meta.timestamp = event.timestamp
558
- if (event.status) meta.status = event.status
559
- }
560
-
561
- if (isModEventPriorityScore(event)) {
562
- meta.priorityScore = event.score
563
- }
564
-
565
- if (isIdentityEvent(event)) {
566
- meta.timestamp = event.timestamp
567
- if (event.handle) meta.handle = event.handle
568
- if (event.pdsHost) meta.pdsHost = event.pdsHost
569
- if (event.tombstone) meta.tombstone = event.tombstone
570
- }
571
-
572
- if (isRecordEvent(event)) {
573
- meta.timestamp = event.timestamp
574
- meta.op = event.op
575
- if (event.cid) meta.cid = event.cid
576
- }
577
-
578
- if (isAgeAssuranceEvent(event)) {
579
- meta.status = event.status
580
- meta.createdAt = event.createdAt
581
- if (event.attemptId) {
582
- meta.attemptId = event.attemptId
583
- }
584
- if (event.access) {
585
- meta.access = event.access
586
- }
587
- if (event.initIp) {
588
- meta.initIp = event.initIp
589
- }
590
- if (event.initUa) {
591
- meta.initUa = event.initUa
592
- }
593
- if (event.completeIp) {
594
- meta.completeIp = event.completeIp
595
- }
596
- if (event.completeUa) {
597
- meta.completeUa = event.completeUa
598
- }
599
- }
600
-
601
- if (isAgeAssuranceOverrideEvent(event)) {
602
- meta.status = event.status
603
- if (event.access) {
604
- meta.access = event.access
605
- }
606
- }
607
-
608
- if (isScheduleTakedownEvent(event)) {
609
- if (event.executeAfter) {
610
- meta.executeAfter = event.executeAfter
611
- }
612
- if (event.executeAt) {
613
- meta.executeAt = event.executeAt
614
- }
615
- if (event.executeUntil) {
616
- meta.executeUntil = event.executeUntil
617
- }
618
- }
619
-
620
- if (
621
- (isModEventTakedown(event) || isModEventAcknowledge(event)) &&
622
- event.acknowledgeAccountSubjects
623
- ) {
624
- meta.acknowledgeAccountSubjects = true
625
- }
626
-
627
- if (isModEventTakedown(event) && event.policies?.length) {
628
- meta.policies = event.policies.join(',')
629
- }
630
-
631
- if (isModEventTakedown(event) && event.targetServices?.length) {
632
- meta.targetServices = event.targetServices.join(',')
633
- }
634
-
635
- // Keep trace of reports that came in while the reporter was in muted stated
636
- if (isModEventReport(event)) {
637
- const isReportingMuted = await this.isReportingMutedForSubject(createdBy)
638
- if (isReportingMuted) {
639
- meta.isReporterMuted = true
640
- }
641
- // Also capture whether the subject was muted at event-creation time, so
642
- // the queue-router daemon can populate report.isMuted later without
643
- // racing against subsequent mute/unmute changes.
644
- const isSubjectMuted = await this.isSubjectMuted(subject.did)
645
- if (isSubjectMuted) {
646
- meta.isSubjectMuted = true
647
- }
648
- }
649
-
650
- const subjectInfo = subject.info()
651
-
652
- // Store severityLevel, strikeCount, and strikeExpiresAt if provided
653
- // These values should be calculated by the client based on configuration
654
- // processNewEvent will update the account_strike table with the new strike count
655
- let severityLevel: string | null = null
656
- let strikeCount: number | null = null
657
- let strikeExpiresAt: string | null = null
658
-
659
- if (
660
- isModEventTakedown(event) ||
661
- isModEventEmail(event) ||
662
- isModEventReverseTakedown(event)
663
- ) {
664
- // Store severityLevel if provided (for display/tracking)
665
- if (event.severityLevel) {
666
- severityLevel = event.severityLevel
667
- }
668
- // Store explicit strikeCount if provided
669
- if (event.strikeCount !== undefined) {
670
- strikeCount = event.strikeCount
671
- }
672
- // Store strikeExpiresAt if provided by client
673
- if ('strikeExpiresAt' in event && event.strikeExpiresAt) {
674
- strikeExpiresAt = event.strikeExpiresAt
675
- }
676
- }
677
-
678
- const modEvent = await this.db.db
679
- .insertInto('moderation_event')
680
- .values({
681
- comment:
682
- ('comment' in event &&
683
- typeof event.comment === 'string' &&
684
- event.comment) ||
685
- null,
686
- action: event.$type as ModerationEvent['action'],
687
- createdAt: createdAt.toISOString(),
688
- createdBy,
689
- createLabelVals,
690
- negateLabelVals,
691
- addedTags,
692
- removedTags,
693
- durationInHours:
694
- 'durationInHours' in event && event.durationInHours
695
- ? Number(event.durationInHours)
696
- : null,
697
- meta: Object.assign(meta, subjectInfo.meta),
698
- expiresAt:
699
- (isModEventTakedown(event) || isModEventMute(event)) &&
700
- event.durationInHours
701
- ? addHoursToDate(event.durationInHours, createdAt).toISOString()
702
- : undefined,
703
- subjectType: subjectInfo.subjectType,
704
- subjectDid: subjectInfo.subjectDid,
705
- subjectUri: subjectInfo.subjectUri,
706
- subjectCid: subjectInfo.subjectCid,
707
- subjectBlobCids: jsonb(subjectInfo.subjectBlobCids),
708
- subjectMessageId: subjectInfo.subjectMessageId,
709
- subjectConvoId: subjectInfo.subjectConvoId,
710
- modTool: modTool ? jsonb(modTool) : null,
711
- externalId: externalId ?? null,
712
- severityLevel,
713
- strikeCount,
714
- strikeExpiresAt,
715
- })
716
- .returningAll()
717
- .executeTakeFirstOrThrow()
718
-
719
- const subjectStatus = await adjustModerationSubjectStatus(
720
- this.db,
721
- modEvent,
722
- subject.blobCids,
723
- )
724
-
725
- // Manage expiring tag rows for temporary tags
726
- if (isModEventTag(event)) {
727
- if (event.durationInHours && event.add.length > 0) {
728
- const expiresAt = addHoursToDate(
729
- event.durationInHours,
730
- createdAt,
731
- ).toISOString()
732
- await insertExpiringTags(this.db, {
733
- eventId: modEvent.id,
734
- did: subjectInfo.subjectDid,
735
- recordPath: subjectInfo.subjectUri ?? '',
736
- convoId: subjectInfo.subjectConvoId ?? '',
737
- tags: event.add,
738
- expiresAt,
739
- createdBy,
740
- })
741
- }
742
- if (event.remove.length > 0) {
743
- await removeExpiringTags(this.db, {
744
- did: subjectInfo.subjectDid,
745
- recordPath: subjectInfo.subjectUri ?? '',
746
- convoId: subjectInfo.subjectConvoId ?? '',
747
- tags: event.remove,
748
- })
749
- }
750
- }
751
-
752
- if (isAgeAssurancePurgeEvent(event)) {
753
- await this.purgeAgeAssuranceEvents(subjectInfo.subjectDid)
754
- }
755
-
756
- // Updates are only needed if strikeCount is numeric (in some cases even 0)
757
- if (modEvent.strikeCount !== null) {
758
- try {
759
- await this.strikeService.updateSubjectStrikeCount(modEvent.subjectDid)
760
- } catch (error) {
761
- // Log error but don't fail the entire operation to ensure that events are logged even if updating strike count fails
762
- log.error(
763
- { err: error, modEventId: modEvent.id },
764
- 'Error processing strikes for moderation event',
765
- )
766
- }
767
- }
768
-
769
- return { event: modEvent, subjectStatus }
770
- }
771
-
772
- async purgeAgeAssuranceEvents(subjectDid: string) {
773
- this.db.assertTransaction()
774
- await this.db.db
775
- .deleteFrom('moderation_event')
776
- .where('subjectDid', '=', subjectDid)
777
- .where('action', '=', 'tools.ozone.moderation.defs#ageAssuranceEvent')
778
- .execute()
779
- }
780
-
781
- async getLastReversibleEventForSubject(subject: ReversalSubject) {
782
- // If the subject is neither suspended nor muted don't bother finding the last reversible event
783
- // Ideally, this should never happen because the caller of this method should only call this
784
- // after ensuring that the suspended or muted subjects are being reversed
785
- if (!subject.reverseMute && !subject.reverseSuspend) {
786
- return null
787
- }
788
-
789
- let builder = this.db.db
790
- .selectFrom('moderation_event')
791
- .where('subjectDid', '=', subject.subject.did)
792
-
793
- if (subject.subject.recordPath) {
794
- builder = builder.where(
795
- 'subjectUri',
796
- 'like',
797
- `%${subject.subject.recordPath}%`,
798
- )
799
- }
800
-
801
- // Means the subject was suspended and needs to be unsuspended
802
- if (subject.reverseSuspend) {
803
- builder = builder
804
- .where('action', '=', 'tools.ozone.moderation.defs#modEventTakedown')
805
- .where('durationInHours', 'is not', null)
806
- }
807
- if (subject.reverseMute) {
808
- builder = builder
809
- .where('action', '=', 'tools.ozone.moderation.defs#modEventMute')
810
- .where('durationInHours', 'is not', null)
811
- }
812
-
813
- return await builder
814
- .orderBy('id', 'desc')
815
- .selectAll()
816
- .limit(1)
817
- .executeTakeFirst()
818
- }
819
-
820
- async getSubjectsDueForReversal(): Promise<ReversalSubject[]> {
821
- const now = new Date().toISOString()
822
- const subjects = await this.db.db
823
- .selectFrom('moderation_subject_status')
824
- .where((eb) =>
825
- eb.or([eb('suspendUntil', '<', now), eb('muteUntil', '<', now)]),
826
- )
827
- .selectAll()
828
- .execute()
829
-
830
- return subjects.map((row) => ({
831
- subject: subjectFromStatusRow(row),
832
- reverseSuspend: !!row.suspendUntil && row.suspendUntil < now,
833
- reverseMute: !!row.muteUntil && row.muteUntil < now,
834
- }))
835
- }
836
-
837
- async isSubjectSuspended(did: string): Promise<boolean> {
838
- const res = await this.db.db
839
- .selectFrom('moderation_subject_status')
840
- .where('did', '=', did)
841
- .where('recordPath', '=', '')
842
- .where('convoId', '=', '')
843
- .where('suspendUntil', '>', new Date().toISOString())
844
- .select('did')
845
- .limit(1)
846
- .executeTakeFirst()
847
- return !!res
848
- }
849
-
850
- async revertState({
851
- createdBy,
852
- createdAt,
853
- comment,
854
- action,
855
- subject,
856
- }: ReversibleModerationEvent): Promise<ModerationEventRow> {
857
- const isRevertingTakedown =
858
- action === 'tools.ozone.moderation.defs#modEventTakedown'
859
- this.db.assertTransaction()
860
- const { event } = await this.logEvent({
861
- event: {
862
- $type: isRevertingTakedown
863
- ? 'tools.ozone.moderation.defs#modEventReverseTakedown'
864
- : 'tools.ozone.moderation.defs#modEventUnmute',
865
- comment: comment ?? undefined,
866
- },
867
- createdAt,
868
- createdBy,
869
- subject,
870
- })
871
-
872
- if (isRevertingTakedown) {
873
- if (subject.isRepo()) {
874
- await this.reverseTakedownRepo(subject)
875
- } else if (subject.isRecord()) {
876
- await this.reverseTakedownRecord(subject)
877
- }
878
- }
879
-
880
- return event
881
- }
882
-
883
- async takedownRepo(
884
- subject: RepoSubject,
885
- takedownId: number,
886
- targetServices: Set<string>,
887
- isSuspend = false,
888
- ) {
889
- const takedownRef = `BSKY-${
890
- isSuspend ? 'SUSPEND' : 'TAKEDOWN'
891
- }-${takedownId}`
892
-
893
- const values = this.eventPusher
894
- .getTakedownServices(targetServices)
895
- .map((eventType) => ({
896
- eventType,
897
- subjectDid: subject.did,
898
- takedownRef,
899
- }))
900
-
901
- // The label is consumed by appview if we opt for appview only takedown, this is needed
902
- // if we opt for pds level takedown, adding the label doesn't hurt
903
- const takedownLabel = isSuspend ? SUSPEND_LABEL : TAKEDOWN_LABEL
904
- await this.formatAndCreateLabels(subject.did, null, {
905
- create: [takedownLabel],
906
- })
907
-
908
- // If we dont have to push any events, return early
909
- if (!values.length) {
910
- return
911
- }
912
-
913
- const repoEvts = await this.db.db
914
- .insertInto('repo_push_event')
915
- .values(values)
916
- .onConflict((oc) =>
917
- oc.columns(['subjectDid', 'eventType']).doUpdateSet({
918
- takedownRef,
919
- confirmedAt: null,
920
- attempts: 0,
921
- lastAttempted: null,
922
- }),
923
- )
924
- .returning('id')
925
- .execute()
926
-
927
- this.db.onCommit(() => {
928
- this.backgroundQueue.add(async () => {
929
- await Promise.all(
930
- repoEvts.map((evt) => this.eventPusher.attemptRepoEvent(evt.id)),
931
- )
932
- })
933
- })
934
- }
935
-
936
- async reverseTakedownRepo(subject: RepoSubject) {
937
- const repoEvts = await this.db.db
938
- .updateTable('repo_push_event')
939
- .where('eventType', 'in', TAKEDOWNS)
940
- .where('subjectDid', '=', subject.did)
941
- .set({
942
- takedownRef: null,
943
- confirmedAt: null,
944
- attempts: 0,
945
- lastAttempted: null,
946
- })
947
- .returning('id')
948
- .execute()
949
-
950
- const existingTakedownLabels = await this.db.db
951
- .selectFrom('label')
952
- .where('label.uri', '=', subject.did)
953
- .where('label.val', 'in', [TAKEDOWN_LABEL, SUSPEND_LABEL])
954
- .where('neg', '=', false)
955
- .selectAll()
956
- .execute()
957
-
958
- const takedownVals = existingTakedownLabels.map((row) => row.val)
959
- await this.formatAndCreateLabels(subject.did, null, {
960
- negate: takedownVals,
961
- })
962
-
963
- this.db.onCommit(() => {
964
- this.backgroundQueue.add(async () => {
965
- await Promise.all(
966
- repoEvts.map((evt) => this.eventPusher.attemptRepoEvent(evt.id)),
967
- )
968
- })
969
- })
970
- }
971
-
972
- async takedownRecord(
973
- subject: RecordSubject,
974
- takedownId: number,
975
- targetServices: Set<string>,
976
- ) {
977
- this.db.assertTransaction()
978
- await this.formatAndCreateLabels(subject.uri, subject.cid, {
979
- create: [TAKEDOWN_LABEL],
980
- })
981
-
982
- const takedownRef = `BSKY-TAKEDOWN-${takedownId}`
983
- const blobCids = subject.blobCids
984
- if (blobCids && blobCids.length > 0) {
985
- const blobValues: Insertable<BlobPushEvent>[] = []
986
- for (const eventType of this.eventPusher.getTakedownServices(
987
- targetServices,
988
- )) {
989
- for (const cid of blobCids) {
990
- blobValues.push({
991
- eventType,
992
- takedownRef,
993
- subjectDid: subject.did,
994
- subjectUri: subject.uri || null,
995
- subjectBlobCid: cid.toString(),
996
- })
997
- }
998
- }
999
- const blobEvts = await this.eventPusher.logBlobPushEvent(
1000
- blobValues,
1001
- takedownRef,
1002
- )
1003
-
1004
- this.db.onCommit(() => {
1005
- this.backgroundQueue.add(async () => {
1006
- await Promise.allSettled(
1007
- blobEvts.map((evt) =>
1008
- this.eventPusher
1009
- .attemptBlobEvent(evt.id)
1010
- .catch((err) =>
1011
- log.error({ ...evt, err }, 'failed to push blob event'),
1012
- ),
1013
- ),
1014
- )
1015
-
1016
- if (this.imgInvalidator) {
1017
- await Promise.allSettled(
1018
- (subject.blobCids ?? []).map((cid) => {
1019
- const paths = (this.cfg.cdn.paths ?? []).map((path) =>
1020
- path.replace('%s', subject.did).replace('%s', cid),
1021
- )
1022
- return this.imgInvalidator
1023
- ?.invalidate(cid, paths)
1024
- .catch((err) =>
1025
- log.error(
1026
- { err, paths, cid },
1027
- 'failed to invalidate blob on cdn',
1028
- ),
1029
- )
1030
- }),
1031
- )
1032
- }
1033
- })
1034
- })
1035
- }
1036
- }
1037
-
1038
- async reverseTakedownRecord(subject: RecordSubject) {
1039
- this.db.assertTransaction()
1040
- await this.formatAndCreateLabels(subject.uri, subject.cid, {
1041
- negate: [TAKEDOWN_LABEL],
1042
- })
1043
-
1044
- const blobCids = subject.blobCids
1045
- if (blobCids && blobCids.length > 0) {
1046
- const blobEvts = await this.db.db
1047
- .updateTable('blob_push_event')
1048
- .where('eventType', 'in', TAKEDOWNS)
1049
- .where('subjectDid', '=', subject.did)
1050
- .where(
1051
- 'subjectBlobCid',
1052
- 'in',
1053
- blobCids.map((c) => c.toString()),
1054
- )
1055
- .set({
1056
- takedownRef: null,
1057
- confirmedAt: null,
1058
- attempts: 0,
1059
- lastAttempted: null,
1060
- })
1061
- .returning('id')
1062
- .execute()
1063
-
1064
- this.db.onCommit(() => {
1065
- this.backgroundQueue.add(async () => {
1066
- await Promise.all(
1067
- blobEvts.map((evt) => this.eventPusher.attemptBlobEvent(evt.id)),
1068
- )
1069
- })
1070
- })
1071
- }
1072
- }
1073
-
1074
- async report(info: {
1075
- reasonType: ReasonType
1076
- reason?: string
1077
- subject: ModSubject
1078
- reportedBy: string
1079
- createdAt?: Date
1080
- modTool?: {
1081
- name: string
1082
- meta?: { [_ in string]: unknown }
1083
- }
1084
- }): Promise<{
1085
- event: ModerationEventRow
1086
- subjectStatus: ModerationSubjectStatusRow | null
1087
- }> {
1088
- const {
1089
- reasonType,
1090
- reason,
1091
- reportedBy,
1092
- createdAt = new Date(),
1093
- subject,
1094
- modTool,
1095
- } = info
1096
-
1097
- return await this.logEvent({
1098
- event: {
1099
- $type: 'tools.ozone.moderation.defs#modEventReport',
1100
- reportType: reasonType,
1101
- comment: reason,
1102
- },
1103
- createdBy: reportedBy,
1104
- subject,
1105
- createdAt,
1106
- modTool,
1107
- })
1108
- }
1109
-
1110
- async getSubjectStatuses({
1111
- queueCount,
1112
- queueIndex,
1113
- queueSeed = '',
1114
- includeAllUserRecords,
1115
- cursor,
1116
- limit = 50,
1117
- takendown,
1118
- appealed,
1119
- reviewState,
1120
- reviewedAfter,
1121
- reviewedBefore,
1122
- reportedAfter,
1123
- reportedBefore,
1124
- includeMuted = false,
1125
- hostingDeletedBefore,
1126
- hostingDeletedAfter,
1127
- hostingUpdatedBefore,
1128
- hostingUpdatedAfter,
1129
- hostingStatuses,
1130
- onlyMuted = false,
1131
- ignoreSubjects,
1132
- sortDirection = 'desc',
1133
- lastReviewedBy,
1134
- sortField = 'lastReportedAt',
1135
- subject,
1136
- tags,
1137
- excludeTags,
1138
- collections,
1139
- subjectType,
1140
- minAccountSuspendCount,
1141
- minReportedRecordsCount,
1142
- minTakendownRecordsCount,
1143
- minPriorityScore,
1144
- minStrikeCount,
1145
- ageAssuranceState,
1146
- }: QueryStatusParams): Promise<{
1147
- statuses: ModerationSubjectStatusRowWithHandle[]
1148
- cursor?: string
1149
- }> {
1150
- let builder = moderationSubjectStatusQueryBuilder(this.db.db)
1151
-
1152
- const { ref } = this.db.db.dynamic
1153
-
1154
- if (subject) {
1155
- const subjectInfo = getStatusIdentifierFromSubject(subject)
1156
- builder = builder.where(
1157
- 'moderation_subject_status.did',
1158
- '=',
1159
- subjectInfo.did,
1160
- )
1161
-
1162
- if (!includeAllUserRecords) {
1163
- if (subjectInfo.convoId) {
1164
- builder = builder.where(
1165
- 'moderation_subject_status.convoId',
1166
- '=',
1167
- subjectInfo.convoId,
1168
- )
1169
- } else if (subjectInfo.recordPath) {
1170
- builder = builder.where(
1171
- 'moderation_subject_status.recordPath',
1172
- '=',
1173
- subjectInfo.recordPath,
1174
- )
1175
- } else {
1176
- // Account-level: recordPath = '' also matches conversation statuses,
1177
- // so explicitly exclude them.
1178
- builder = builder
1179
- .where('moderation_subject_status.recordPath', '=', '')
1180
- .where('moderation_subject_status.convoId', '=', '')
1181
- }
1182
- }
1183
- } else if (subjectType === 'account') {
1184
- builder = builder
1185
- .where('moderation_subject_status.recordPath', '=', '')
1186
- .where('moderation_subject_status.convoId', '=', '')
1187
- } else if (subjectType === 'record') {
1188
- builder = builder.where('moderation_subject_status.recordPath', '!=', '')
1189
- } else if (subjectType === 'conversation') {
1190
- builder = builder.where('moderation_subject_status.convoId', '!=', '')
1191
- }
1192
-
1193
- // Only fetch items that belongs to the specified queue when specified
1194
- if (
1195
- !subject &&
1196
- queueCount &&
1197
- queueCount > 0 &&
1198
- queueIndex !== undefined &&
1199
- queueIndex >= 0 &&
1200
- queueIndex < queueCount
1201
- ) {
1202
- builder = builder.where(
1203
- queueSeed
1204
- ? sql`ABS(HASHTEXT(${queueSeed} || moderation_subject_status.did)) % ${queueCount}`
1205
- : sql`ABS(HASHTEXT(moderation_subject_status.did)) % ${queueCount}`,
1206
- '=',
1207
- queueIndex,
1208
- )
1209
- }
1210
-
1211
- // If subjectType is set to 'account' let that take priority and ignore collections filter
1212
- if (subjectType !== 'account' && collections?.length) {
1213
- builder = builder
1214
- .where('moderation_subject_status.recordPath', '!=', '')
1215
- .where((eb) =>
1216
- eb.or(
1217
- collections.map((collection) =>
1218
- eb(
1219
- 'moderation_subject_status.recordPath',
1220
- 'like',
1221
- `${collection}/%`,
1222
- ),
1223
- ),
1224
- ),
1225
- )
1226
- }
1227
-
1228
- if (ignoreSubjects?.length) {
1229
- builder = builder
1230
- .where('moderation_subject_status.did', 'not in', ignoreSubjects)
1231
- .where('moderation_subject_status.recordPath', 'not in', ignoreSubjects)
1232
- }
1233
-
1234
- const reviewStateNormalized = getReviewState(reviewState)
1235
- if (reviewStateNormalized) {
1236
- builder = builder.where(
1237
- 'moderation_subject_status.reviewState',
1238
- '=',
1239
- reviewStateNormalized,
1240
- )
1241
- }
1242
-
1243
- if (lastReviewedBy) {
1244
- builder = builder.where(
1245
- 'moderation_subject_status.lastReviewedBy',
1246
- '=',
1247
- lastReviewedBy,
1248
- )
1249
- }
1250
-
1251
- if (reviewedAfter) {
1252
- builder = builder.where(
1253
- 'moderation_subject_status.lastReviewedAt',
1254
- '>',
1255
- reviewedAfter,
1256
- )
1257
- }
1258
-
1259
- if (reviewedBefore) {
1260
- builder = builder.where(
1261
- 'moderation_subject_status.lastReviewedAt',
1262
- '<',
1263
- reviewedBefore,
1264
- )
1265
- }
1266
-
1267
- if (hostingUpdatedAfter) {
1268
- builder = builder.where(
1269
- 'moderation_subject_status.hostingUpdatedAt',
1270
- '>',
1271
- hostingUpdatedAfter,
1272
- )
1273
- }
1274
-
1275
- if (hostingUpdatedBefore) {
1276
- builder = builder.where(
1277
- 'moderation_subject_status.hostingUpdatedAt',
1278
- '<',
1279
- hostingUpdatedBefore,
1280
- )
1281
- }
1282
-
1283
- if (hostingDeletedAfter) {
1284
- builder = builder.where(
1285
- 'moderation_subject_status.hostingDeletedAt',
1286
- '>',
1287
- hostingDeletedAfter,
1288
- )
1289
- }
1290
-
1291
- if (hostingDeletedBefore) {
1292
- builder = builder.where(
1293
- 'moderation_subject_status.hostingDeletedAt',
1294
- '<',
1295
- hostingDeletedBefore,
1296
- )
1297
- }
1298
-
1299
- if (hostingStatuses?.length) {
1300
- builder = builder.where(
1301
- 'moderation_subject_status.hostingStatus',
1302
- 'in',
1303
- hostingStatuses,
1304
- )
1305
- }
1306
-
1307
- if (reportedAfter) {
1308
- builder = builder.where(
1309
- 'moderation_subject_status.lastReviewedAt',
1310
- '>',
1311
- reportedAfter,
1312
- )
1313
- }
1314
-
1315
- if (reportedBefore) {
1316
- builder = builder.where(
1317
- 'moderation_subject_status.lastReportedAt',
1318
- '<',
1319
- reportedBefore,
1320
- )
1321
- }
1322
-
1323
- if (takendown) {
1324
- builder = builder.where('moderation_subject_status.takendown', '=', true)
1325
- }
1326
-
1327
- if (appealed !== undefined) {
1328
- builder =
1329
- appealed === false
1330
- ? builder.where('moderation_subject_status.appealed', 'is', null)
1331
- : builder.where('moderation_subject_status.appealed', '=', appealed)
1332
- }
1333
-
1334
- if (!includeMuted) {
1335
- builder = builder.where((eb) =>
1336
- eb.or([
1337
- eb(
1338
- 'moderation_subject_status.muteUntil',
1339
- '<',
1340
- new Date().toISOString(),
1341
- ),
1342
- eb('moderation_subject_status.muteUntil', 'is', null),
1343
- ]),
1344
- )
1345
- }
1346
-
1347
- if (onlyMuted) {
1348
- builder = builder.where((eb) =>
1349
- eb.or([
1350
- eb(
1351
- 'moderation_subject_status.muteUntil',
1352
- '>',
1353
- new Date().toISOString(),
1354
- ),
1355
- eb(
1356
- 'moderation_subject_status.muteReportingUntil',
1357
- '>',
1358
- new Date().toISOString(),
1359
- ),
1360
- ]),
1361
- )
1362
- }
1363
-
1364
- // ["tag1", "tag2 && tag3", "tag4"] => [["tag1"], ["tag2", "tag3"], ["tag4"]]
1365
- const conditions = parseTags(tags)
1366
- if (conditions?.length) {
1367
- // [["tag1"], ["tag2", "tag3"], ["tag4"]] => (tags ? 'tag1') OR (tags ? 'tag2' AND tags ? 'tag3') OR (tags ? 'tag4')
1368
- builder = builder.where((eb) =>
1369
- // OR between every conditions items (subTags)
1370
- eb.or(
1371
- conditions.map((subTags) =>
1372
- // AND between every subTags items (subTag)
1373
- eb.and(
1374
- subTags.map(
1375
- (subTag) =>
1376
- sql<boolean>`${ref('moderation_subject_status.tags')} ? ${subTag}`,
1377
- ),
1378
- ),
1379
- ),
1380
- ),
1381
- )
1382
- }
1383
-
1384
- if (excludeTags?.length) {
1385
- builder = builder.where((eb) =>
1386
- eb.or([
1387
- sql<boolean>`NOT(${ref('moderation_subject_status.tags')} ?| array[${sql.join(excludeTags)}]::TEXT[])`,
1388
- eb('tags', 'is', null),
1389
- ]),
1390
- )
1391
- }
1392
-
1393
- if (minAccountSuspendCount != null && minAccountSuspendCount > 0) {
1394
- builder = builder.where(
1395
- 'account_events_stats.suspendCount',
1396
- '>=',
1397
- minAccountSuspendCount,
1398
- )
1399
- }
1400
-
1401
- if (minTakendownRecordsCount != null && minTakendownRecordsCount > 0) {
1402
- builder = builder.where(
1403
- 'account_record_status_stats.takendownCount',
1404
- '>=',
1405
- minTakendownRecordsCount,
1406
- )
1407
- }
1408
-
1409
- if (minReportedRecordsCount != null && minReportedRecordsCount > 0) {
1410
- builder = builder.where(
1411
- 'account_record_events_stats.reportedCount',
1412
- '>=',
1413
- minReportedRecordsCount,
1414
- )
1415
- }
1416
-
1417
- if (minPriorityScore != null && minPriorityScore >= 0) {
1418
- builder = builder.where(
1419
- 'moderation_subject_status.priorityScore',
1420
- '>=',
1421
- minPriorityScore,
1422
- )
1423
- }
1424
-
1425
- if (minStrikeCount != null && minStrikeCount >= 0) {
1426
- builder = builder.where(
1427
- 'account_strike.activeStrikeCount',
1428
- '>=',
1429
- minStrikeCount,
1430
- )
1431
- }
1432
-
1433
- if (ageAssuranceState) {
1434
- builder = builder.where(
1435
- 'moderation_subject_status.ageAssuranceState',
1436
- '=',
1437
- ageAssuranceState,
1438
- )
1439
- }
1440
-
1441
- const keyset = new StatusKeyset(
1442
- sortField === 'reportedRecordsCount'
1443
- ? ref(`account_record_events_stats.reportedCount`)
1444
- : sortField === 'takendownRecordsCount'
1445
- ? ref(`account_record_status_stats.takendownCount`)
1446
- : sortField === 'priorityScore'
1447
- ? ref(`moderation_subject_status.priorityScore`)
1448
- : ref(`moderation_subject_status.${sortField}`),
1449
- ref('moderation_subject_status.id'),
1450
- )
1451
- const paginatedBuilder = paginate(builder, {
1452
- limit,
1453
- cursor,
1454
- keyset,
1455
- direction: sortDirection,
1456
- tryIndex: true,
1457
- nullsLast: true,
1458
- })
1459
- const results = await paginatedBuilder.execute()
1460
-
1461
- const infos = await this.views.getAccoutInfosByDid(
1462
- results.map((r) => r.did),
1463
- )
1464
-
1465
- return {
1466
- statuses: results.map((r) => ({
1467
- ...r,
1468
- handle: infos.get(r.did)?.handle ?? INVALID_HANDLE,
1469
- })),
1470
- cursor: keyset.packFromResult(results),
1471
- }
1472
- }
1473
-
1474
- async getStatus(
1475
- subject: ModSubject,
1476
- ): Promise<ModerationSubjectStatusRow | null> {
1477
- const result = await this.db.db
1478
- .selectFrom('moderation_subject_status')
1479
- .where('did', '=', subject.did)
1480
- .where('recordPath', '=', subject.recordPath ?? '')
1481
- .where('convoId', '=', subject.convoId ?? '')
1482
- .selectAll()
1483
- .executeTakeFirst()
1484
- return result ?? null
1485
- }
1486
-
1487
- // This is used to check if the reporter of an incoming report is muted from reporting
1488
- // so we want to make sure this look up is as fast as possible
1489
- async isReportingMutedForSubject(did: string) {
1490
- const result = await this.db.db
1491
- .selectFrom('moderation_subject_status')
1492
- .where('did', '=', did)
1493
- .where('recordPath', '=', '')
1494
- .where('convoId', '=', '')
1495
- .where('muteReportingUntil', '>', new Date().toISOString())
1496
- .select(sql`true`.as('status'))
1497
- .executeTakeFirst()
1498
-
1499
- return !!result
1500
- }
1501
-
1502
- // Check if a subject (the account being reported) has an active mute
1503
- async isSubjectMuted(did: string) {
1504
- const result = await this.db.db
1505
- .selectFrom('moderation_subject_status')
1506
- .where('did', '=', did)
1507
- .where('recordPath', '=', '')
1508
- .where('convoId', '=', '')
1509
- .where('muteUntil', '>', new Date().toISOString())
1510
- .select(sql`true`.as('status'))
1511
- .executeTakeFirst()
1512
-
1513
- return !!result
1514
- }
1515
-
1516
- async formatAndCreateLabels(
1517
- uri: string,
1518
- cid: string | null,
1519
- labels: { create?: string[]; negate?: string[] },
1520
- durationInHours?: number,
1521
- ): Promise<Label[]> {
1522
- const exp =
1523
- durationInHours !== undefined
1524
- ? addHoursToDate(durationInHours).toISOString()
1525
- : undefined
1526
- const { create = [], negate = [] } = labels
1527
- const toCreate = create.map((val) => ({
1528
- src: this.cfg.service.did,
1529
- uri,
1530
- cid: cid ?? undefined,
1531
- val,
1532
- exp,
1533
- cts: new Date().toISOString(),
1534
- }))
1535
- const toNegate = negate.map((val) => ({
1536
- src: this.cfg.service.did,
1537
- uri,
1538
- cid: cid ?? undefined,
1539
- val,
1540
- neg: true,
1541
- cts: new Date().toISOString(),
1542
- }))
1543
- const formatted = [...toCreate, ...toNegate]
1544
- return this.createLabels(formatted)
1545
- }
1546
-
1547
- async createLabels(labels: Label[]): Promise<Label[]> {
1548
- if (labels.length < 1) return []
1549
- const signedLabels = await Promise.all(
1550
- labels.map((l) => signLabel(l, this.signingKey)),
1551
- )
1552
- const dbVals = signedLabels.map((l) => formatLabelRow(l, this.signingKeyId))
1553
- const { ref } = this.db.db.dynamic
1554
- const excluded = (col: string) => ref(`excluded.${col}`)
1555
- const res = await this.db.db
1556
- .insertInto('label')
1557
- .values(dbVals)
1558
- .onConflict((oc) =>
1559
- oc.columns(['src', 'uri', 'cid', 'val']).doUpdateSet({
1560
- id: sql`${excluded('id')}`,
1561
- neg: sql`${excluded('neg')}`,
1562
- cts: sql`${excluded('cts')}`,
1563
- exp: sql`${excluded('exp')}`,
1564
- sig: sql`${excluded('sig')}`,
1565
- signingKeyId: sql`${excluded('signingKeyId')}`,
1566
- }),
1567
- )
1568
- .returningAll()
1569
- .execute()
1570
- await sql`notify ${ref(LabelChannel)}`.execute(this.db.db)
1571
- return res.map((row) => formatLabel(row))
1572
- }
1573
-
1574
- async sendEmail(opts: {
1575
- content: string
1576
- recipientDid: string
1577
- subject: string
1578
- }) {
1579
- const { subject, content, recipientDid } = opts
1580
- const { agent: pdsAgent, url } = await getPdsAgentForRepo(
1581
- this.idResolver,
1582
- recipientDid,
1583
- this.cfg.service.devMode,
1584
- )
1585
- if (!pdsAgent) {
1586
- throw new InvalidRequestError('Invalid pds service in DID doc')
1587
- }
1588
- const { data: serverInfo } =
1589
- await pdsAgent.com.atproto.server.describeServer()
1590
- if (serverInfo.did !== `did:web:${url.hostname}`) {
1591
- // @TODO do bidirectional check once implemented. in the meantime,
1592
- // matching did to hostname we're talking to is pretty good.
1593
- throw new InvalidRequestError('Invalid pds service in DID doc')
1594
- }
1595
- const { data: delivery } = await pdsAgent.com.atproto.admin.sendEmail(
1596
- {
1597
- subject,
1598
- content,
1599
- recipientDid,
1600
- senderDid: this.cfg.service.did,
1601
- },
1602
- {
1603
- encoding: 'application/json',
1604
- ...(await this.createAuthHeaders(
1605
- serverInfo.did,
1606
- ids.ComAtprotoAdminSendEmail,
1607
- )),
1608
- },
1609
- )
1610
- if (!delivery.sent) {
1611
- throw new InvalidRequestError('Email was accepted but not sent')
1612
- }
1613
- }
1614
-
1615
- async buildModerationQuery(
1616
- subjectType: 'account' | 'record',
1617
- createdByDids: string[],
1618
- isActionQuery: boolean,
1619
- ): Promise<(Partial<ReporterStatsResult> & { did: string })[]> {
1620
- if (!createdByDids.length) return []
1621
-
1622
- const actionTypes = [
1623
- 'tools.ozone.moderation.defs#modEventTakedown',
1624
- 'tools.ozone.moderation.defs#modEventLabel',
1625
- ] as const
1626
-
1627
- const countAll = () => {
1628
- return sql<number>`COUNT(*)`
1629
- }
1630
- const countAllDistinctBy = (ref: Expression<unknown>) => {
1631
- return sql<number>`COUNT(DISTINCT ${ref})`
1632
- }
1633
- const countTakedownsDistinctBy = (ref: Expression<unknown>) => {
1634
- return sql<number>`COUNT(DISTINCT ${ref}) FILTER (
1635
- WHERE actions."action" = 'tools.ozone.moderation.defs#modEventTakedown'
1636
- )`
1637
- }
1638
- const countLabelsDistinctBy = (ref: Expression<unknown>) => {
1639
- return sql<number>`COUNT(DISTINCT ${ref}) FILTER (
1640
- WHERE actions."action" = 'tools.ozone.moderation.defs#modEventLabel'
1641
- )`
1642
- }
1643
-
1644
- const query = this.db.db
1645
- .selectFrom('moderation_event as reports')
1646
- .where(
1647
- 'reports.action',
1648
- '=',
1649
- 'tools.ozone.moderation.defs#modEventReport',
1650
- )
1651
- .where(
1652
- 'reports.subjectUri',
1653
- subjectType === 'account' ? 'is' : 'is not',
1654
- null,
1655
- )
1656
- .where('reports.createdBy', 'in', createdByDids)
1657
- .select(['reports.createdBy as did'])
1658
-
1659
- if (!isActionQuery) {
1660
- if (subjectType === 'account') {
1661
- return query
1662
- .select([
1663
- () => countAll().as('accountReportCount'),
1664
- (eb) =>
1665
- countAllDistinctBy(eb.ref('reports.subjectDid')).as(
1666
- 'reportedAccountCount',
1667
- ),
1668
- ])
1669
- .groupBy('reports.createdBy')
1670
- .execute()
1671
- } else {
1672
- return query
1673
- .select([
1674
- () => countAll().as('recordReportCount'),
1675
- (eb) =>
1676
- countAllDistinctBy(eb.ref('reports.subjectUri')).as(
1677
- 'reportedRecordCount',
1678
- ),
1679
- ])
1680
- .groupBy('reports.createdBy')
1681
- .execute()
1682
- }
1683
- }
1684
-
1685
- if (subjectType === 'account') {
1686
- return query
1687
- .leftJoin('moderation_event as actions', (join) =>
1688
- join
1689
- .onRef('actions.subjectDid', '=', 'reports.subjectDid')
1690
- .on('actions.subjectUri', 'is', null)
1691
- .onRef('actions.createdAt', '>', 'reports.createdAt')
1692
- .on('actions.action', 'in', actionTypes),
1693
- )
1694
- .select([
1695
- (eb) =>
1696
- countTakedownsDistinctBy(eb.ref('actions.subjectDid')).as(
1697
- 'takendownAccountCount',
1698
- ),
1699
- (eb) =>
1700
- countLabelsDistinctBy(eb.ref('actions.subjectDid')).as(
1701
- 'labeledAccountCount',
1702
- ),
1703
- ])
1704
- .groupBy('reports.createdBy')
1705
- .execute()
1706
- } else {
1707
- return query
1708
- .leftJoin('moderation_event as actions', (join) =>
1709
- join
1710
- .onRef('actions.subjectDid', '=', 'reports.subjectDid')
1711
- .onRef('actions.subjectUri', '=', 'reports.subjectUri')
1712
- .onRef('actions.createdAt', '>', 'reports.createdAt')
1713
- .on('actions.action', 'in', actionTypes),
1714
- )
1715
- .select([
1716
- (eb) =>
1717
- countTakedownsDistinctBy(eb.ref('actions.subjectUri')).as(
1718
- 'takendownRecordCount',
1719
- ),
1720
- (eb) =>
1721
- countLabelsDistinctBy(eb.ref('actions.subjectUri')).as(
1722
- 'labeledRecordCount',
1723
- ),
1724
- ])
1725
- .groupBy('reports.createdBy')
1726
- .execute()
1727
- }
1728
- }
1729
-
1730
- async getReporterStats(dids: string[]) {
1731
- const [accountReports, recordReports, accountActions, recordActions] =
1732
- await Promise.all([
1733
- this.buildModerationQuery('account', dids, false),
1734
- this.buildModerationQuery('record', dids, false),
1735
- this.buildModerationQuery('account', dids, true),
1736
- this.buildModerationQuery('record', dids, true),
1737
- ])
1738
-
1739
- // Create a map to hold the aggregated stats for each `did`
1740
- const statsMap = new Map<string, ReporterStats>()
1741
-
1742
- // Helper function to ensure a `did` entry exists in the map
1743
- const ensureDidEntry = (did: string) => {
1744
- if (!statsMap.has(did)) {
1745
- statsMap.set(did, {
1746
- did,
1747
- accountReportCount: 0,
1748
- recordReportCount: 0,
1749
- reportedAccountCount: 0,
1750
- reportedRecordCount: 0,
1751
- takendownAccountCount: 0,
1752
- takendownRecordCount: 0,
1753
- labeledAccountCount: 0,
1754
- labeledRecordCount: 0,
1755
- })
1756
- }
1757
- return statsMap.get(did)!
1758
- }
1759
-
1760
- // Merge accountReports
1761
- for (const report of accountReports) {
1762
- const entry = ensureDidEntry(report.did)
1763
- entry.accountReportCount = report.accountReportCount ?? 0
1764
- entry.reportedAccountCount = report.reportedAccountCount ?? 0
1765
- }
1766
-
1767
- // Merge recordReports
1768
- for (const report of recordReports) {
1769
- const entry = ensureDidEntry(report.did)
1770
- entry.recordReportCount = report.recordReportCount ?? 0
1771
- entry.reportedRecordCount = report.reportedRecordCount ?? 0
1772
- }
1773
-
1774
- // Merge accountActions
1775
- for (const action of accountActions) {
1776
- const entry = ensureDidEntry(action.did)
1777
- entry.takendownAccountCount = action.takendownAccountCount ?? 0
1778
- entry.labeledAccountCount = action.labeledAccountCount ?? 0
1779
- }
1780
-
1781
- // Merge recordActions
1782
- for (const action of recordActions) {
1783
- const entry = ensureDidEntry(action.did)
1784
- entry.takendownRecordCount = action.takendownRecordCount ?? 0
1785
- entry.labeledRecordCount = action.labeledRecordCount ?? 0
1786
- }
1787
-
1788
- // Convert map values to an array and return
1789
- return Array.from(statsMap.values())
1790
- }
1791
-
1792
- async getAccountTimeline(did: string) {
1793
- const { ref } = this.db.db.dynamic
1794
- // Without the subquery approach, pg tries to do the sort operation first which can be super expensive when a subjectDid has too many entries
1795
- const result = await this.db.db
1796
- .selectFrom(
1797
- this.db.db
1798
- .selectFrom('moderation_event')
1799
- .where('subjectDid', '=', did)
1800
- .select([
1801
- dateFromDbDatetime(ref('createdAt')).as('day'),
1802
- 'subjectUri',
1803
- 'action',
1804
- sql<number>`count(*)`.as('count'),
1805
- ])
1806
- .groupBy(['day', 'subjectUri', 'action'])
1807
- .as('results'),
1808
- )
1809
- .selectAll()
1810
- .orderBy('day', 'desc')
1811
- .execute()
1812
- return result
1813
- }
1814
- }
1815
-
1816
- const parseTags = (tags?: string[]) =>
1817
- tags
1818
- ?.map((tag) =>
1819
- tag
1820
- .split(/\s*&&\s*/g)
1821
- .map((subTag) => subTag.trim())
1822
- // Ignore invalid syntax ("", "tag1 &&", "&& tag2", "tag1 && && tag2", etc.)
1823
- .filter(Boolean),
1824
- )
1825
- // Ignore invalid items
1826
- .filter((subTags): subTags is [string, ...string[]] => subTags.length > 0)
1827
-
1828
- const TAKEDOWNS = ['pds_takedown' as const, 'appview_takedown' as const]
1829
-
1830
- export const TAKEDOWN_LABEL = '!takedown'
1831
- export const SUSPEND_LABEL = '!suspend'
1832
-
1833
- export type TakedownSubjects = {
1834
- did: string
1835
- subjects: (RepoRef | RepoBlobRef | StrongRef)[]
1836
- }
1837
-
1838
- export type ReversalSubject = {
1839
- subject: ModSubject
1840
- reverseSuspend: boolean
1841
- reverseMute: boolean
1842
- }