@atproto/bsky 0.0.15 → 0.0.17
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.
- package/CHANGELOG.md +18 -0
- package/dist/api/com/atproto/moderation/util.d.ts +4 -3
- package/dist/cache/read-through.d.ts +30 -0
- package/dist/config.d.ts +18 -0
- package/dist/context.d.ts +21 -6
- package/dist/daemon/config.d.ts +15 -0
- package/dist/daemon/context.d.ts +15 -0
- package/dist/daemon/index.d.ts +23 -0
- package/dist/daemon/logger.d.ts +3 -0
- package/dist/daemon/notifications.d.ts +18 -0
- package/dist/daemon/services.d.ts +11 -0
- package/dist/db/database-schema.d.ts +1 -2
- package/dist/db/index.js +41 -1
- package/dist/db/index.js.map +3 -3
- package/dist/db/migrations/20231003T202833377Z-create-moderation-subject-status.d.ts +3 -0
- package/dist/db/migrations/20231205T000257238Z-remove-did-cache.d.ts +3 -0
- package/dist/db/migrations/index.d.ts +2 -0
- package/dist/db/pagination.d.ts +2 -1
- package/dist/db/{periodic-moderation-action-reversal.d.ts → periodic-moderation-event-reversal.d.ts} +3 -5
- package/dist/db/tables/moderation.d.ts +24 -34
- package/dist/did-cache.d.ts +10 -7
- package/dist/feed-gen/types.d.ts +1 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.js +4370 -2758
- package/dist/index.js.map +3 -3
- package/dist/indexer/context.d.ts +2 -0
- package/dist/indexer/index.d.ts +1 -0
- package/dist/lexicon/index.d.ts +23 -18
- package/dist/lexicon/lexicons.d.ts +561 -412
- package/dist/lexicon/types/app/bsky/feed/defs.d.ts +1 -7
- package/dist/lexicon/types/app/bsky/graph/defs.d.ts +1 -0
- package/dist/lexicon/types/com/atproto/admin/defs.d.ts +114 -48
- package/dist/lexicon/types/com/atproto/admin/{resolveModerationReports.d.ts → deleteAccount.d.ts} +2 -13
- package/dist/lexicon/types/com/atproto/admin/{takeModerationAction.d.ts → emitModerationEvent.d.ts} +5 -6
- package/dist/lexicon/types/com/atproto/admin/{getModerationAction.d.ts → getModerationEvent.d.ts} +1 -1
- package/dist/lexicon/types/com/atproto/admin/{getModerationActions.d.ts → queryModerationEvents.d.ts} +5 -1
- package/dist/lexicon/types/com/atproto/admin/{getModerationReports.d.ts → queryModerationStatuses.d.ts} +12 -6
- package/dist/lexicon/types/com/atproto/admin/sendEmail.d.ts +1 -0
- package/dist/lexicon/types/com/atproto/{admin/getModerationReport.d.ts → temp/importRepo.d.ts} +10 -7
- package/dist/lexicon/types/com/atproto/temp/pushBlob.d.ts +25 -0
- package/dist/lexicon/types/com/atproto/{admin/reverseModerationAction.d.ts → temp/transferAccount.d.ts} +11 -5
- package/dist/logger.d.ts +1 -0
- package/dist/migrate-moderation-data.d.ts +1 -0
- package/dist/redis.d.ts +10 -1
- package/dist/services/actor/index.d.ts +18 -4
- package/dist/services/actor/views.d.ts +6 -8
- package/dist/services/feed/index.d.ts +7 -4
- package/dist/services/feed/util.d.ts +9 -1
- package/dist/services/feed/views.d.ts +11 -21
- package/dist/services/graph/index.d.ts +5 -29
- package/dist/services/graph/types.d.ts +1 -0
- package/dist/services/index.d.ts +3 -7
- package/dist/services/label/index.d.ts +10 -4
- package/dist/services/moderation/index.d.ts +134 -72
- package/dist/services/moderation/pagination.d.ts +36 -0
- package/dist/services/moderation/status.d.ts +13 -0
- package/dist/services/moderation/types.d.ts +35 -0
- package/dist/services/moderation/views.d.ts +18 -14
- package/dist/services/types.d.ts +3 -0
- package/dist/services/util/notification.d.ts +5 -0
- package/dist/services/util/post.d.ts +6 -6
- package/dist/util/debug.d.ts +1 -1
- package/dist/util/retry.d.ts +1 -6
- package/package.json +11 -11
- package/src/api/app/bsky/feed/getActorFeeds.ts +2 -1
- package/src/api/app/bsky/feed/getActorLikes.ts +1 -3
- package/src/api/app/bsky/feed/getAuthorFeed.ts +1 -3
- package/src/api/app/bsky/feed/getFeed.ts +9 -9
- package/src/api/app/bsky/feed/getFeedGenerator.ts +3 -0
- package/src/api/app/bsky/feed/getFeedGenerators.ts +2 -1
- package/src/api/app/bsky/feed/getListFeed.ts +1 -3
- package/src/api/app/bsky/feed/getPostThread.ts +15 -54
- package/src/api/app/bsky/feed/getPosts.ts +21 -18
- package/src/api/app/bsky/feed/getSuggestedFeeds.ts +2 -1
- package/src/api/app/bsky/feed/getTimeline.ts +1 -3
- package/src/api/app/bsky/feed/searchPosts.ts +20 -17
- package/src/api/app/bsky/graph/getList.ts +6 -3
- package/src/api/app/bsky/graph/getListBlocks.ts +3 -2
- package/src/api/app/bsky/graph/getListMutes.ts +2 -1
- package/src/api/app/bsky/graph/getLists.ts +2 -1
- package/src/api/app/bsky/unspecced/getPopularFeedGenerators.ts +3 -1
- package/src/api/blob-resolver.ts +6 -11
- package/src/api/com/atproto/admin/emitModerationEvent.ts +220 -0
- package/src/api/com/atproto/admin/{getModerationActions.ts → getModerationEvent.ts} +5 -11
- package/src/api/com/atproto/admin/getRecord.ts +1 -0
- package/src/api/com/atproto/admin/{getModerationReports.ts → queryModerationEvents.ts} +13 -16
- package/src/api/com/atproto/admin/queryModerationStatuses.ts +55 -0
- package/src/api/com/atproto/moderation/createReport.ts +9 -7
- package/src/api/com/atproto/moderation/util.ts +38 -20
- package/src/api/index.ts +8 -14
- package/src/auth.ts +29 -21
- package/src/auto-moderator/index.ts +26 -19
- package/src/cache/read-through.ts +151 -0
- package/src/config.ts +90 -1
- package/src/context.ts +11 -7
- package/src/daemon/config.ts +60 -0
- package/src/daemon/context.ts +27 -0
- package/src/daemon/index.ts +78 -0
- package/src/daemon/logger.ts +6 -0
- package/src/daemon/notifications.ts +54 -0
- package/src/daemon/services.ts +22 -0
- package/src/db/database-schema.ts +0 -2
- package/src/db/migrations/20231003T202833377Z-create-moderation-subject-status.ts +123 -0
- package/src/db/migrations/20231205T000257238Z-remove-did-cache.ts +14 -0
- package/src/db/migrations/index.ts +2 -0
- package/src/db/pagination.ts +26 -3
- package/src/db/{periodic-moderation-action-reversal.ts → periodic-moderation-event-reversal.ts} +50 -46
- package/src/db/tables/moderation.ts +35 -52
- package/src/did-cache.ts +33 -56
- package/src/feed-gen/bsky-team.ts +1 -1
- package/src/feed-gen/hot-classic.ts +1 -1
- package/src/feed-gen/index.ts +0 -4
- package/src/feed-gen/mutuals.ts +6 -2
- package/src/feed-gen/types.ts +1 -1
- package/src/index.ts +57 -17
- package/src/indexer/context.ts +5 -0
- package/src/indexer/index.ts +10 -7
- package/src/lexicon/index.ts +80 -67
- package/src/lexicon/lexicons.ts +698 -507
- package/src/lexicon/types/app/bsky/feed/defs.ts +1 -18
- package/src/lexicon/types/app/bsky/graph/defs.ts +1 -0
- package/src/lexicon/types/com/atproto/admin/defs.ts +276 -84
- package/src/lexicon/types/com/atproto/admin/{resolveModerationReports.ts → deleteAccount.ts} +2 -13
- package/src/lexicon/types/com/atproto/admin/{takeModerationAction.ts → emitModerationEvent.ts} +13 -11
- package/src/lexicon/types/com/atproto/admin/{getModerationReport.ts → getModerationEvent.ts} +1 -1
- package/src/lexicon/types/com/atproto/admin/{getModerationActions.ts → queryModerationEvents.ts} +8 -1
- package/src/lexicon/types/com/atproto/admin/{getModerationReports.ts → queryModerationStatuses.ts} +21 -14
- package/src/lexicon/types/com/atproto/admin/sendEmail.ts +1 -0
- package/src/lexicon/types/com/atproto/{admin/getModerationAction.ts → temp/importRepo.ts} +11 -7
- package/src/lexicon/types/com/atproto/temp/pushBlob.ts +39 -0
- package/src/lexicon/types/com/atproto/{admin/reverseModerationAction.ts → temp/transferAccount.ts} +18 -5
- package/src/logger.ts +2 -0
- package/src/migrate-moderation-data.ts +414 -0
- package/src/redis.ts +43 -3
- package/src/services/actor/index.ts +55 -7
- package/src/services/actor/views.ts +18 -21
- package/src/services/feed/index.ts +52 -19
- package/src/services/feed/util.ts +47 -19
- package/src/services/feed/views.ts +87 -13
- package/src/services/graph/index.ts +21 -3
- package/src/services/graph/types.ts +1 -0
- package/src/services/index.ts +14 -14
- package/src/services/indexing/index.ts +7 -10
- package/src/services/indexing/plugins/block.ts +2 -3
- package/src/services/indexing/plugins/feed-generator.ts +2 -3
- package/src/services/indexing/plugins/follow.ts +2 -3
- package/src/services/indexing/plugins/like.ts +2 -3
- package/src/services/indexing/plugins/list-block.ts +2 -3
- package/src/services/indexing/plugins/list-item.ts +2 -3
- package/src/services/indexing/plugins/list.ts +2 -3
- package/src/services/indexing/plugins/post.ts +16 -4
- package/src/services/indexing/plugins/repost.ts +2 -3
- package/src/services/indexing/plugins/thread-gate.ts +2 -3
- package/src/services/label/index.ts +68 -25
- package/src/services/moderation/index.ts +380 -395
- package/src/services/moderation/pagination.ts +96 -0
- package/src/services/moderation/status.ts +241 -0
- package/src/services/moderation/types.ts +49 -0
- package/src/services/moderation/views.ts +278 -329
- package/src/services/types.ts +4 -0
- package/src/services/util/notification.ts +70 -0
- package/src/util/debug.ts +2 -2
- package/src/util/retry.ts +1 -44
- package/tests/__snapshots__/feed-generation.test.ts.snap +322 -6
- package/tests/__snapshots__/indexing.test.ts.snap +0 -6
- package/tests/admin/__snapshots__/get-record.test.ts.snap +30 -132
- package/tests/admin/__snapshots__/get-repo.test.ts.snap +14 -60
- package/tests/admin/__snapshots__/moderation-events.test.ts.snap +146 -0
- package/tests/admin/__snapshots__/moderation-statuses.test.ts.snap +64 -0
- package/tests/admin/__snapshots__/moderation.test.ts.snap +0 -125
- package/tests/admin/get-record.test.ts +5 -9
- package/tests/admin/get-repo.test.ts +10 -12
- package/tests/admin/moderation-events.test.ts +221 -0
- package/tests/admin/moderation-statuses.test.ts +145 -0
- package/tests/admin/moderation.test.ts +512 -860
- package/tests/admin/repo-search.test.ts +3 -3
- package/tests/algos/hot-classic.test.ts +1 -2
- package/tests/auth.test.ts +1 -1
- package/tests/auto-moderator/fuzzy-matcher.test.ts +2 -1
- package/tests/auto-moderator/labeler.test.ts +19 -20
- package/tests/auto-moderator/takedowns.test.ts +61 -28
- package/tests/blob-resolver.test.ts +4 -2
- package/tests/daemon.test.ts +191 -0
- package/tests/did-cache.test.ts +20 -5
- package/tests/feed-generation.test.ts +57 -9
- package/tests/handle-invalidation.test.ts +1 -5
- package/tests/indexing.test.ts +20 -13
- package/tests/redis-cache.test.ts +231 -0
- package/tests/seeds/basic.ts +3 -0
- package/tests/subscription/repo.test.ts +4 -7
- package/tests/views/__snapshots__/block-lists.test.ts.snap +3 -9
- package/tests/views/__snapshots__/blocks.test.ts.snap +0 -9
- package/tests/views/__snapshots__/mute-lists.test.ts.snap +5 -5
- package/tests/views/__snapshots__/mutes.test.ts.snap +0 -3
- package/tests/views/__snapshots__/thread.test.ts.snap +0 -30
- package/tests/views/actor-search.test.ts +2 -3
- package/tests/views/author-feed.test.ts +42 -36
- package/tests/views/follows.test.ts +40 -35
- package/tests/views/list-feed.test.ts +17 -9
- package/tests/views/notifications.test.ts +13 -9
- package/tests/views/profile.test.ts +20 -19
- package/tests/views/thread.test.ts +117 -94
- package/tests/views/threadgating.test.ts +89 -19
- package/tests/views/timeline.test.ts +21 -13
- package/dist/api/com/atproto/admin/resolveModerationReports.d.ts +0 -3
- package/dist/api/com/atproto/admin/reverseModerationAction.d.ts +0 -3
- package/dist/api/com/atproto/admin/takeModerationAction.d.ts +0 -3
- package/dist/db/tables/did-cache.d.ts +0 -10
- package/dist/feed-gen/best-of-follows.d.ts +0 -29
- package/dist/feed-gen/whats-hot.d.ts +0 -29
- package/dist/feed-gen/with-friends.d.ts +0 -3
- package/dist/label-cache.d.ts +0 -19
- package/src/api/com/atproto/admin/getModerationAction.ts +0 -44
- package/src/api/com/atproto/admin/getModerationReport.ts +0 -43
- package/src/api/com/atproto/admin/resolveModerationReports.ts +0 -24
- package/src/api/com/atproto/admin/reverseModerationAction.ts +0 -115
- package/src/api/com/atproto/admin/takeModerationAction.ts +0 -156
- package/src/db/tables/did-cache.ts +0 -13
- package/src/feed-gen/best-of-follows.ts +0 -74
- package/src/feed-gen/whats-hot.ts +0 -101
- package/src/feed-gen/with-friends.ts +0 -39
- package/src/label-cache.ts +0 -90
- package/tests/admin/__snapshots__/get-moderation-action.test.ts.snap +0 -172
- package/tests/admin/__snapshots__/get-moderation-actions.test.ts.snap +0 -178
- package/tests/admin/__snapshots__/get-moderation-report.test.ts.snap +0 -177
- package/tests/admin/__snapshots__/get-moderation-reports.test.ts.snap +0 -307
- package/tests/admin/get-moderation-action.test.ts +0 -100
- package/tests/admin/get-moderation-actions.test.ts +0 -164
- package/tests/admin/get-moderation-report.test.ts +0 -100
- package/tests/admin/get-moderation-reports.test.ts +0 -332
- package/tests/algos/whats-hot.test.ts +0 -118
- package/tests/algos/with-friends.test.ts +0 -145
- /package/dist/api/com/atproto/admin/{getModerationAction.d.ts → emitModerationEvent.d.ts} +0 -0
- /package/dist/api/com/atproto/admin/{getModerationActions.d.ts → getModerationEvent.d.ts} +0 -0
- /package/dist/api/com/atproto/admin/{getModerationReport.d.ts → queryModerationEvents.d.ts} +0 -0
- /package/dist/api/com/atproto/admin/{getModerationReports.d.ts → queryModerationStatuses.d.ts} +0 -0
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { InvalidRequestError } from '@atproto/xrpc-server'
|
|
2
|
+
import { DynamicModule, sql } from 'kysely'
|
|
3
|
+
|
|
4
|
+
import { Cursor, GenericKeyset } from '../../db/pagination'
|
|
5
|
+
|
|
6
|
+
type StatusKeysetParam = {
|
|
7
|
+
lastReviewedAt: string | null
|
|
8
|
+
lastReportedAt: string | null
|
|
9
|
+
id: number
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export class StatusKeyset extends GenericKeyset<StatusKeysetParam, Cursor> {
|
|
13
|
+
labelResult(result: StatusKeysetParam): Cursor
|
|
14
|
+
labelResult(result: StatusKeysetParam) {
|
|
15
|
+
const primaryField = (
|
|
16
|
+
this.primary as ReturnType<DynamicModule['ref']>
|
|
17
|
+
).dynamicReference.includes('lastReviewedAt')
|
|
18
|
+
? 'lastReviewedAt'
|
|
19
|
+
: 'lastReportedAt'
|
|
20
|
+
|
|
21
|
+
return {
|
|
22
|
+
primary: result[primaryField]
|
|
23
|
+
? new Date(`${result[primaryField]}`).getTime().toString()
|
|
24
|
+
: '',
|
|
25
|
+
secondary: result.id.toString(),
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
labeledResultToCursor(labeled: Cursor) {
|
|
29
|
+
return {
|
|
30
|
+
primary: labeled.primary,
|
|
31
|
+
secondary: labeled.secondary,
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
cursorToLabeledResult(cursor: Cursor) {
|
|
35
|
+
return {
|
|
36
|
+
primary: cursor.primary
|
|
37
|
+
? new Date(parseInt(cursor.primary, 10)).toISOString()
|
|
38
|
+
: '',
|
|
39
|
+
secondary: cursor.secondary,
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
unpackCursor(cursorStr?: string): Cursor | undefined {
|
|
43
|
+
if (!cursorStr) return
|
|
44
|
+
const result = cursorStr.split('::')
|
|
45
|
+
const [primary, secondary, ...others] = result
|
|
46
|
+
if (!secondary || others.length > 0) {
|
|
47
|
+
throw new InvalidRequestError('Malformed cursor')
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
primary,
|
|
51
|
+
secondary,
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
// This is specifically built to handle nullable columns as primary sorting column
|
|
55
|
+
getSql(labeled?: Cursor, direction?: 'asc' | 'desc') {
|
|
56
|
+
if (labeled === undefined) return
|
|
57
|
+
if (direction === 'asc') {
|
|
58
|
+
return !labeled.primary
|
|
59
|
+
? sql`(${this.primary} IS NULL AND ${this.secondary} > ${labeled.secondary})`
|
|
60
|
+
: sql`((${this.primary}, ${this.secondary}) > (${labeled.primary}, ${labeled.secondary}) OR (${this.primary} is null))`
|
|
61
|
+
} else {
|
|
62
|
+
return !labeled.primary
|
|
63
|
+
? sql`(${this.primary} IS NULL AND ${this.secondary} < ${labeled.secondary})`
|
|
64
|
+
: sql`((${this.primary}, ${this.secondary}) < (${labeled.primary}, ${labeled.secondary}) OR (${this.primary} is null))`
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
type TimeIdKeysetParam = {
|
|
70
|
+
id: number
|
|
71
|
+
createdAt: string
|
|
72
|
+
}
|
|
73
|
+
type TimeIdResult = TimeIdKeysetParam
|
|
74
|
+
|
|
75
|
+
export class TimeIdKeyset extends GenericKeyset<TimeIdKeysetParam, Cursor> {
|
|
76
|
+
labelResult(result: TimeIdResult): Cursor
|
|
77
|
+
labelResult(result: TimeIdResult) {
|
|
78
|
+
return { primary: result.createdAt, secondary: result.id.toString() }
|
|
79
|
+
}
|
|
80
|
+
labeledResultToCursor(labeled: Cursor) {
|
|
81
|
+
return {
|
|
82
|
+
primary: new Date(labeled.primary).getTime().toString(),
|
|
83
|
+
secondary: labeled.secondary,
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
cursorToLabeledResult(cursor: Cursor) {
|
|
87
|
+
const primaryDate = new Date(parseInt(cursor.primary, 10))
|
|
88
|
+
if (isNaN(primaryDate.getTime())) {
|
|
89
|
+
throw new InvalidRequestError('Malformed cursor')
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
primary: primaryDate.toISOString(),
|
|
93
|
+
secondary: cursor.secondary,
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
// This may require better organization but for now, just dumping functions here containing DB queries for moderation status
|
|
2
|
+
|
|
3
|
+
import { AtUri } from '@atproto/syntax'
|
|
4
|
+
import { PrimaryDatabase } from '../../db'
|
|
5
|
+
import { ModerationSubjectStatus } from '../../db/tables/moderation'
|
|
6
|
+
import {
|
|
7
|
+
REVIEWOPEN,
|
|
8
|
+
REVIEWCLOSED,
|
|
9
|
+
REVIEWESCALATED,
|
|
10
|
+
} from '../../lexicon/types/com/atproto/admin/defs'
|
|
11
|
+
import { ModerationEventRow, ModerationSubjectStatusRow } from './types'
|
|
12
|
+
import { HOUR } from '@atproto/common'
|
|
13
|
+
import { CID } from 'multiformats/cid'
|
|
14
|
+
import { sql } from 'kysely'
|
|
15
|
+
|
|
16
|
+
const getSubjectStatusForModerationEvent = ({
|
|
17
|
+
action,
|
|
18
|
+
createdBy,
|
|
19
|
+
createdAt,
|
|
20
|
+
durationInHours,
|
|
21
|
+
}: {
|
|
22
|
+
action: string
|
|
23
|
+
createdBy: string
|
|
24
|
+
createdAt: string
|
|
25
|
+
durationInHours: number | null
|
|
26
|
+
}): Partial<ModerationSubjectStatusRow> | null => {
|
|
27
|
+
switch (action) {
|
|
28
|
+
case 'com.atproto.admin.defs#modEventAcknowledge':
|
|
29
|
+
return {
|
|
30
|
+
lastReviewedBy: createdBy,
|
|
31
|
+
reviewState: REVIEWCLOSED,
|
|
32
|
+
lastReviewedAt: createdAt,
|
|
33
|
+
}
|
|
34
|
+
case 'com.atproto.admin.defs#modEventReport':
|
|
35
|
+
return {
|
|
36
|
+
reviewState: REVIEWOPEN,
|
|
37
|
+
lastReportedAt: createdAt,
|
|
38
|
+
}
|
|
39
|
+
case 'com.atproto.admin.defs#modEventEscalate':
|
|
40
|
+
return {
|
|
41
|
+
lastReviewedBy: createdBy,
|
|
42
|
+
reviewState: REVIEWESCALATED,
|
|
43
|
+
lastReviewedAt: createdAt,
|
|
44
|
+
}
|
|
45
|
+
case 'com.atproto.admin.defs#modEventReverseTakedown':
|
|
46
|
+
return {
|
|
47
|
+
lastReviewedBy: createdBy,
|
|
48
|
+
reviewState: REVIEWCLOSED,
|
|
49
|
+
takendown: false,
|
|
50
|
+
suspendUntil: null,
|
|
51
|
+
lastReviewedAt: createdAt,
|
|
52
|
+
}
|
|
53
|
+
case 'com.atproto.admin.defs#modEventUnmute':
|
|
54
|
+
return {
|
|
55
|
+
lastReviewedBy: createdBy,
|
|
56
|
+
muteUntil: null,
|
|
57
|
+
reviewState: REVIEWOPEN,
|
|
58
|
+
lastReviewedAt: createdAt,
|
|
59
|
+
}
|
|
60
|
+
case 'com.atproto.admin.defs#modEventTakedown':
|
|
61
|
+
return {
|
|
62
|
+
takendown: true,
|
|
63
|
+
lastReviewedBy: createdBy,
|
|
64
|
+
reviewState: REVIEWCLOSED,
|
|
65
|
+
lastReviewedAt: createdAt,
|
|
66
|
+
suspendUntil: durationInHours
|
|
67
|
+
? new Date(Date.now() + durationInHours * HOUR).toISOString()
|
|
68
|
+
: null,
|
|
69
|
+
}
|
|
70
|
+
case 'com.atproto.admin.defs#modEventMute':
|
|
71
|
+
return {
|
|
72
|
+
lastReviewedBy: createdBy,
|
|
73
|
+
reviewState: REVIEWOPEN,
|
|
74
|
+
lastReviewedAt: createdAt,
|
|
75
|
+
// By default, mute for 24hrs
|
|
76
|
+
muteUntil: new Date(
|
|
77
|
+
Date.now() + (durationInHours || 24) * HOUR,
|
|
78
|
+
).toISOString(),
|
|
79
|
+
}
|
|
80
|
+
case 'com.atproto.admin.defs#modEventComment':
|
|
81
|
+
return {
|
|
82
|
+
lastReviewedBy: createdBy,
|
|
83
|
+
lastReviewedAt: createdAt,
|
|
84
|
+
}
|
|
85
|
+
default:
|
|
86
|
+
return null
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Based on a given moderation action event, this function will update the moderation status of the subject
|
|
91
|
+
// If there's no existing status, it will create one
|
|
92
|
+
// If the action event does not affect the status, it will do nothing
|
|
93
|
+
export const adjustModerationSubjectStatus = async (
|
|
94
|
+
db: PrimaryDatabase,
|
|
95
|
+
moderationEvent: ModerationEventRow,
|
|
96
|
+
blobCids?: CID[],
|
|
97
|
+
) => {
|
|
98
|
+
const {
|
|
99
|
+
action,
|
|
100
|
+
subjectDid,
|
|
101
|
+
subjectUri,
|
|
102
|
+
subjectCid,
|
|
103
|
+
createdBy,
|
|
104
|
+
meta,
|
|
105
|
+
comment,
|
|
106
|
+
createdAt,
|
|
107
|
+
} = moderationEvent
|
|
108
|
+
|
|
109
|
+
const subjectStatus = getSubjectStatusForModerationEvent({
|
|
110
|
+
action,
|
|
111
|
+
createdBy,
|
|
112
|
+
createdAt,
|
|
113
|
+
durationInHours: moderationEvent.durationInHours,
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
// If there are no subjectStatus that means there are no side-effect of the incoming event
|
|
117
|
+
if (!subjectStatus) {
|
|
118
|
+
return null
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const now = new Date().toISOString()
|
|
122
|
+
// If subjectUri exists, it's not a repoRef so pass along the uri to get identifier back
|
|
123
|
+
const identifier = getStatusIdentifierFromSubject(subjectUri || subjectDid)
|
|
124
|
+
|
|
125
|
+
db.assertTransaction()
|
|
126
|
+
|
|
127
|
+
const currentStatus = await db.db
|
|
128
|
+
.selectFrom('moderation_subject_status')
|
|
129
|
+
.where('did', '=', identifier.did)
|
|
130
|
+
.where('recordPath', '=', identifier.recordPath)
|
|
131
|
+
.selectAll()
|
|
132
|
+
.executeTakeFirst()
|
|
133
|
+
|
|
134
|
+
if (
|
|
135
|
+
currentStatus?.reviewState === REVIEWESCALATED &&
|
|
136
|
+
subjectStatus.reviewState === REVIEWOPEN
|
|
137
|
+
) {
|
|
138
|
+
// If the current status is escalated and the incoming event is to open the review
|
|
139
|
+
// We want to keep the status as escalated
|
|
140
|
+
subjectStatus.reviewState = REVIEWESCALATED
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Set these because we don't want to override them if they're already set
|
|
144
|
+
const defaultData = {
|
|
145
|
+
comment: null,
|
|
146
|
+
// Defaulting reviewState to open for any event may not be the desired behavior.
|
|
147
|
+
// For instance, if a subject never had any event and we just want to leave a comment to keep an eye on it
|
|
148
|
+
// that shouldn't mean we want to review the subject
|
|
149
|
+
reviewState: REVIEWOPEN,
|
|
150
|
+
recordCid: subjectCid || null,
|
|
151
|
+
}
|
|
152
|
+
const newStatus = {
|
|
153
|
+
...defaultData,
|
|
154
|
+
...subjectStatus,
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (
|
|
158
|
+
action === 'com.atproto.admin.defs#modEventReverseTakedown' &&
|
|
159
|
+
!subjectStatus.takendown
|
|
160
|
+
) {
|
|
161
|
+
newStatus.takendown = false
|
|
162
|
+
subjectStatus.takendown = false
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (action === 'com.atproto.admin.defs#modEventComment' && meta?.sticky) {
|
|
166
|
+
newStatus.comment = comment
|
|
167
|
+
subjectStatus.comment = comment
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (blobCids?.length) {
|
|
171
|
+
const newBlobCids = sql<string[]>`${JSON.stringify(
|
|
172
|
+
blobCids.map((c) => c.toString()),
|
|
173
|
+
)}` as unknown as ModerationSubjectStatusRow['blobCids']
|
|
174
|
+
newStatus.blobCids = newBlobCids
|
|
175
|
+
subjectStatus.blobCids = newBlobCids
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const insertQuery = db.db
|
|
179
|
+
.insertInto('moderation_subject_status')
|
|
180
|
+
.values({
|
|
181
|
+
...identifier,
|
|
182
|
+
...newStatus,
|
|
183
|
+
createdAt: now,
|
|
184
|
+
updatedAt: now,
|
|
185
|
+
// TODO: Need to get the types right here.
|
|
186
|
+
} as ModerationSubjectStatusRow)
|
|
187
|
+
.onConflict((oc) =>
|
|
188
|
+
oc.constraint('moderation_status_unique_idx').doUpdateSet({
|
|
189
|
+
...subjectStatus,
|
|
190
|
+
updatedAt: now,
|
|
191
|
+
}),
|
|
192
|
+
)
|
|
193
|
+
|
|
194
|
+
const status = await insertQuery.executeTakeFirst()
|
|
195
|
+
return status
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
type ModerationSubjectStatusFilter =
|
|
199
|
+
| Pick<ModerationSubjectStatus, 'did'>
|
|
200
|
+
| Pick<ModerationSubjectStatus, 'did' | 'recordPath'>
|
|
201
|
+
| Pick<ModerationSubjectStatus, 'did' | 'recordPath' | 'recordCid'>
|
|
202
|
+
export const getModerationSubjectStatus = async (
|
|
203
|
+
db: PrimaryDatabase,
|
|
204
|
+
filters: ModerationSubjectStatusFilter,
|
|
205
|
+
) => {
|
|
206
|
+
let builder = db.db
|
|
207
|
+
.selectFrom('moderation_subject_status')
|
|
208
|
+
// DID will always be passed at the very least
|
|
209
|
+
.where('did', '=', filters.did)
|
|
210
|
+
.where('recordPath', '=', 'recordPath' in filters ? filters.recordPath : '')
|
|
211
|
+
|
|
212
|
+
if ('recordCid' in filters) {
|
|
213
|
+
builder = builder.where('recordCid', '=', filters.recordCid)
|
|
214
|
+
} else {
|
|
215
|
+
builder = builder.where('recordCid', 'is', null)
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return builder.executeTakeFirst()
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export const getStatusIdentifierFromSubject = (
|
|
222
|
+
subject: string | AtUri,
|
|
223
|
+
): { did: string; recordPath: string } => {
|
|
224
|
+
const isSubjectString = typeof subject === 'string'
|
|
225
|
+
if (isSubjectString && subject.startsWith('did:')) {
|
|
226
|
+
return {
|
|
227
|
+
did: subject,
|
|
228
|
+
recordPath: '',
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
if (isSubjectString && !subject.startsWith('at://')) {
|
|
233
|
+
throw new Error('Subject is neither a did nor an at-uri')
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const uri = isSubjectString ? new AtUri(subject) : subject
|
|
237
|
+
return {
|
|
238
|
+
did: uri.host,
|
|
239
|
+
recordPath: `${uri.collection}/${uri.rkey}`,
|
|
240
|
+
}
|
|
241
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { Selectable } from 'kysely'
|
|
2
|
+
import {
|
|
3
|
+
ModerationEvent,
|
|
4
|
+
ModerationSubjectStatus,
|
|
5
|
+
} from '../../db/tables/moderation'
|
|
6
|
+
import { AtUri } from '@atproto/syntax'
|
|
7
|
+
import { CID } from 'multiformats/cid'
|
|
8
|
+
import { ComAtprotoAdminDefs } from '@atproto/api'
|
|
9
|
+
|
|
10
|
+
export type SubjectInfo =
|
|
11
|
+
| {
|
|
12
|
+
subjectType: 'com.atproto.admin.defs#repoRef'
|
|
13
|
+
subjectDid: string
|
|
14
|
+
subjectUri: null
|
|
15
|
+
subjectCid: null
|
|
16
|
+
}
|
|
17
|
+
| {
|
|
18
|
+
subjectType: 'com.atproto.repo.strongRef'
|
|
19
|
+
subjectDid: string
|
|
20
|
+
subjectUri: string
|
|
21
|
+
subjectCid: string
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export type ModerationEventRow = Selectable<ModerationEvent>
|
|
25
|
+
export type ReversibleModerationEvent = Pick<
|
|
26
|
+
ModerationEventRow,
|
|
27
|
+
'createdBy' | 'comment' | 'action'
|
|
28
|
+
> & {
|
|
29
|
+
createdAt?: Date
|
|
30
|
+
subject: { did: string } | { uri: AtUri; cid: CID }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type ModerationEventRowWithHandle = ModerationEventRow & {
|
|
34
|
+
subjectHandle?: string | null
|
|
35
|
+
creatorHandle?: string | null
|
|
36
|
+
}
|
|
37
|
+
export type ModerationSubjectStatusRow = Selectable<ModerationSubjectStatus>
|
|
38
|
+
export type ModerationSubjectStatusRowWithHandle =
|
|
39
|
+
ModerationSubjectStatusRow & { handle: string | null }
|
|
40
|
+
|
|
41
|
+
export type ModEventType =
|
|
42
|
+
| ComAtprotoAdminDefs.ModEventTakedown
|
|
43
|
+
| ComAtprotoAdminDefs.ModEventAcknowledge
|
|
44
|
+
| ComAtprotoAdminDefs.ModEventEscalate
|
|
45
|
+
| ComAtprotoAdminDefs.ModEventComment
|
|
46
|
+
| ComAtprotoAdminDefs.ModEventLabel
|
|
47
|
+
| ComAtprotoAdminDefs.ModEventReport
|
|
48
|
+
| ComAtprotoAdminDefs.ModEventMute
|
|
49
|
+
| ComAtprotoAdminDefs.ModEventReverseTakedown
|