@atproto/ozone 0.4.3 → 0.5.0
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 +24 -0
- package/dist/api/inbox/appealActionedSubject.d.ts +4 -0
- package/dist/api/inbox/appealActionedSubject.d.ts.map +1 -0
- package/dist/api/inbox/appealActionedSubject.js +60 -0
- package/dist/api/inbox/appealActionedSubject.js.map +1 -0
- package/dist/api/index.d.ts.map +1 -1
- package/dist/api/index.js +2 -0
- package/dist/api/index.js.map +1 -1
- package/dist/api/queue/createQueue.d.ts.map +1 -1
- package/dist/api/queue/createQueue.js +22 -17
- package/dist/api/queue/createQueue.js.map +1 -1
- package/dist/api/queue/updateQueue.d.ts.map +1 -1
- package/dist/api/queue/updateQueue.js +33 -18
- package/dist/api/queue/updateQueue.js.map +1 -1
- package/dist/assignment/index.d.ts +1 -0
- package/dist/assignment/index.d.ts.map +1 -1
- package/dist/assignment/index.js +5 -0
- package/dist/assignment/index.js.map +1 -1
- package/dist/config/config.d.ts +11 -0
- package/dist/config/config.d.ts.map +1 -1
- package/dist/config/config.js +4 -0
- package/dist/config/config.js.map +1 -1
- package/dist/config/env.d.ts +1 -0
- package/dist/config/env.d.ts.map +1 -1
- package/dist/config/env.js +1 -0
- package/dist/config/env.js.map +1 -1
- package/dist/daemon/stats-computer.d.ts +2 -8
- package/dist/daemon/stats-computer.d.ts.map +1 -1
- package/dist/daemon/stats-computer.js +21 -9
- package/dist/daemon/stats-computer.js.map +1 -1
- package/dist/db/migrations/20260219T164523000Z-create-report-table.js +2 -2
- package/dist/db/migrations/20260219T164523000Z-create-report-table.js.map +1 -1
- package/dist/db/migrations/20260810T000000000Z-add-report-stats-components.d.ts +4 -0
- package/dist/db/migrations/20260810T000000000Z-add-report-stats-components.d.ts.map +1 -0
- package/dist/db/migrations/20260810T000000000Z-add-report-stats-components.js +31 -0
- package/dist/db/migrations/20260810T000000000Z-add-report-stats-components.js.map +1 -0
- package/dist/db/migrations/20260922T153505234Z-add-report-closed-at-index.d.ts +4 -0
- package/dist/db/migrations/20260922T153505234Z-add-report-closed-at-index.d.ts.map +1 -0
- package/dist/db/migrations/20260922T153505234Z-add-report-closed-at-index.js +14 -0
- package/dist/db/migrations/20260922T153505234Z-add-report-closed-at-index.js.map +1 -0
- package/dist/db/migrations/20260928T000000000Z-inbox-appeal.d.ts +4 -0
- package/dist/db/migrations/20260928T000000000Z-inbox-appeal.d.ts.map +1 -0
- package/dist/db/migrations/20260928T000000000Z-inbox-appeal.js +25 -0
- package/dist/db/migrations/20260928T000000000Z-inbox-appeal.js.map +1 -0
- package/dist/db/migrations/index.d.ts +3 -0
- package/dist/db/migrations/index.d.ts.map +1 -1
- package/dist/db/migrations/index.js +3 -0
- package/dist/db/migrations/index.js.map +1 -1
- package/dist/db/schema/report.js.map +1 -1
- package/dist/db/schema/report_queue.d.ts +1 -0
- package/dist/db/schema/report_queue.d.ts.map +1 -1
- package/dist/db/schema/report_queue.js.map +1 -1
- package/dist/db/schema/report_stat.d.ts +11 -1
- package/dist/db/schema/report_stat.d.ts.map +1 -1
- package/dist/db/schema/report_stat.js.map +1 -1
- package/dist/inbox/appeal.d.ts +187 -0
- package/dist/inbox/appeal.d.ts.map +1 -0
- package/dist/inbox/appeal.js +340 -0
- package/dist/inbox/appeal.js.map +1 -0
- package/dist/inbox/views.d.ts +92 -0
- package/dist/inbox/views.d.ts.map +1 -0
- package/dist/inbox/views.js +313 -0
- package/dist/inbox/views.js.map +1 -0
- package/dist/lexicons/app/bsky/authFullApp.defs.d.ts.map +1 -1
- package/dist/lexicons/app/bsky/authFullApp.defs.js +10 -0
- package/dist/lexicons/app/bsky/authFullApp.defs.js.map +1 -1
- package/dist/lexicons/app/bsky/feed/defs.defs.d.ts +8 -0
- package/dist/lexicons/app/bsky/feed/defs.defs.d.ts.map +1 -1
- package/dist/lexicons/app/bsky/feed/defs.defs.js +2 -0
- package/dist/lexicons/app/bsky/feed/defs.defs.js.map +1 -1
- package/dist/lexicons/app/bsky/feed/getQuotes.defs.d.ts +6 -0
- package/dist/lexicons/app/bsky/feed/getQuotes.defs.d.ts.map +1 -1
- package/dist/lexicons/app/bsky/feed/getQuotes.defs.js +2 -0
- package/dist/lexicons/app/bsky/feed/getQuotes.defs.js.map +1 -1
- package/dist/lexicons/tools/ozone/inbox/appealActionedSubject.d.ts +3 -0
- package/dist/lexicons/tools/ozone/inbox/appealActionedSubject.d.ts.map +1 -0
- package/dist/lexicons/tools/ozone/inbox/appealActionedSubject.defs.d.ts +65 -0
- package/dist/lexicons/tools/ozone/inbox/appealActionedSubject.defs.d.ts.map +1 -0
- package/dist/lexicons/tools/ozone/inbox/appealActionedSubject.defs.js +49 -0
- package/dist/lexicons/tools/ozone/inbox/appealActionedSubject.defs.js.map +1 -0
- package/dist/lexicons/tools/ozone/inbox/appealActionedSubject.js +6 -0
- package/dist/lexicons/tools/ozone/inbox/appealActionedSubject.js.map +1 -0
- package/dist/lexicons/tools/ozone/inbox/defs.d.ts +2 -0
- package/dist/lexicons/tools/ozone/inbox/defs.d.ts.map +1 -0
- package/dist/lexicons/tools/ozone/inbox/defs.defs.d.ts +89 -0
- package/dist/lexicons/tools/ozone/inbox/defs.defs.d.ts.map +1 -0
- package/dist/lexicons/tools/ozone/inbox/defs.defs.js +73 -0
- package/dist/lexicons/tools/ozone/inbox/defs.defs.js.map +1 -0
- package/dist/lexicons/tools/ozone/inbox/defs.js +5 -0
- package/dist/lexicons/tools/ozone/inbox/defs.js.map +1 -0
- package/dist/lexicons/tools/ozone/inbox.d.ts +3 -0
- package/dist/lexicons/tools/ozone/inbox.d.ts.map +1 -0
- package/dist/lexicons/tools/ozone/inbox.js +6 -0
- package/dist/lexicons/tools/ozone/inbox.js.map +1 -0
- package/dist/lexicons/tools/ozone/queue/createQueue.defs.d.ts +2 -0
- package/dist/lexicons/tools/ozone/queue/createQueue.defs.d.ts.map +1 -1
- package/dist/lexicons/tools/ozone/queue/createQueue.defs.js +2 -0
- package/dist/lexicons/tools/ozone/queue/createQueue.defs.js.map +1 -1
- package/dist/lexicons/tools/ozone/queue/defs.defs.d.ts +4 -0
- package/dist/lexicons/tools/ozone/queue/defs.defs.d.ts.map +1 -1
- package/dist/lexicons/tools/ozone/queue/defs.defs.js +2 -0
- package/dist/lexicons/tools/ozone/queue/defs.defs.js.map +1 -1
- package/dist/lexicons/tools/ozone/queue/updateQueue.defs.d.ts +3 -1
- package/dist/lexicons/tools/ozone/queue/updateQueue.defs.d.ts.map +1 -1
- package/dist/lexicons/tools/ozone/queue/updateQueue.defs.js +3 -0
- package/dist/lexicons/tools/ozone/queue/updateQueue.defs.js.map +1 -1
- package/dist/lexicons/tools/ozone/report/defs.defs.d.ts +89 -9
- package/dist/lexicons/tools/ozone/report/defs.defs.d.ts.map +1 -1
- package/dist/lexicons/tools/ozone/report/defs.defs.js +20 -0
- package/dist/lexicons/tools/ozone/report/defs.defs.js.map +1 -1
- package/dist/lexicons/tools/ozone/report/getLiveStats.defs.d.ts +1 -1
- package/dist/lexicons/tools/ozone/report/getLiveStats.defs.d.ts.map +1 -1
- package/dist/lexicons/tools/ozone/report/getLiveStats.defs.js +1 -1
- package/dist/lexicons/tools/ozone/report/getLiveStats.defs.js.map +1 -1
- package/dist/lexicons/tools/ozone/report/refreshStats.defs.d.ts +1 -1
- package/dist/lexicons/tools/ozone/report/refreshStats.defs.d.ts.map +1 -1
- package/dist/lexicons/tools/ozone/report/refreshStats.defs.js +1 -1
- package/dist/lexicons/tools/ozone/report/refreshStats.defs.js.map +1 -1
- package/dist/lexicons/tools/ozone.d.ts +1 -0
- package/dist/lexicons/tools/ozone.d.ts.map +1 -1
- package/dist/lexicons/tools/ozone.js +1 -0
- package/dist/lexicons/tools/ozone.js.map +1 -1
- package/dist/mod-service/index.d.ts +2 -0
- package/dist/mod-service/index.d.ts.map +1 -1
- package/dist/mod-service/index.js +6 -3
- package/dist/mod-service/index.js.map +1 -1
- package/dist/queue/service.d.ts +16 -3
- package/dist/queue/service.d.ts.map +1 -1
- package/dist/queue/service.js +81 -27
- package/dist/queue/service.js.map +1 -1
- package/dist/report/stats.d.ts +25 -31
- package/dist/report/stats.d.ts.map +1 -1
- package/dist/report/stats.js +269 -175
- package/dist/report/stats.js.map +1 -1
- package/dist/report/views.d.ts.map +1 -1
- package/dist/report/views.js +20 -0
- package/dist/report/views.js.map +1 -1
- package/package.json +12 -12
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
import { sql } from 'kysely';
|
|
2
|
+
import { currentDatetimeString, toDatetimeString, } from '@atproto/lex';
|
|
3
|
+
import { AtUri } from '@atproto/syntax';
|
|
4
|
+
import { ForbiddenError, InvalidRequestError } from '@atproto/xrpc-server';
|
|
5
|
+
import { jsonb } from '../db/types.js';
|
|
6
|
+
import { tools } from '../lexicons/index.js';
|
|
7
|
+
import { findMatchingQueue } from '../queue/service.js';
|
|
8
|
+
export const APPEAL_REASON_TYPE = 'tools.ozone.report.defs#reasonAppeal';
|
|
9
|
+
export const TAKEDOWN = 'tools.ozone.moderation.defs#modEventTakedown';
|
|
10
|
+
export const REVERSE_TAKEDOWN = 'tools.ozone.moderation.defs#modEventReverseTakedown';
|
|
11
|
+
export const LABEL = 'tools.ozone.moderation.defs#modEventLabel';
|
|
12
|
+
export const EMAIL = 'tools.ozone.moderation.defs#modEventEmail';
|
|
13
|
+
export const MUTE_REPORTER = 'tools.ozone.moderation.defs#modEventMuteReporter';
|
|
14
|
+
export const REVOKE_CREDENTIALS = 'tools.ozone.moderation.defs#revokeAccountCredentialsEvent';
|
|
15
|
+
export const PUBLIC_EVENT_ACTIONS = [
|
|
16
|
+
TAKEDOWN,
|
|
17
|
+
LABEL,
|
|
18
|
+
EMAIL,
|
|
19
|
+
MUTE_REPORTER,
|
|
20
|
+
REVOKE_CREDENTIALS,
|
|
21
|
+
REVERSE_TAKEDOWN,
|
|
22
|
+
];
|
|
23
|
+
export const APPEALABLE_EVENT_ACTIONS = [TAKEDOWN, LABEL];
|
|
24
|
+
export const isAppealableEvent = (action) => APPEALABLE_EVENT_ACTIONS.includes(action);
|
|
25
|
+
export const appealWindowEnd = (actionCreatedAt, windowMonths) => {
|
|
26
|
+
const end = new Date(actionCreatedAt);
|
|
27
|
+
const day = end.getUTCDate();
|
|
28
|
+
end.setUTCMonth(end.getUTCMonth() + windowMonths);
|
|
29
|
+
// Clamp a rollover: 31 Aug + 6 months is 28/29 Feb, not 2/3 Mar.
|
|
30
|
+
if (end.getUTCDate() !== day)
|
|
31
|
+
end.setUTCDate(0);
|
|
32
|
+
return toDatetimeString(end);
|
|
33
|
+
};
|
|
34
|
+
export const isAppealWindowOpen = (actionCreatedAt, windowMonths, now = new Date()) => new Date(appealWindowEnd(actionCreatedAt, windowMonths)) > now;
|
|
35
|
+
/**
|
|
36
|
+
* Appeal state, derived entirely from data Ozone already records.
|
|
37
|
+
*/
|
|
38
|
+
export const toAppealState = ({ subject, status, report, publicNote, latestAppealableAt, windowMonths, }) => {
|
|
39
|
+
const appealableUntil = latestAppealableAt
|
|
40
|
+
? appealWindowEnd(latestAppealableAt, windowMonths)
|
|
41
|
+
: null;
|
|
42
|
+
const windowOpen = !!appealableUntil && new Date(appealableUntil) > new Date();
|
|
43
|
+
let state;
|
|
44
|
+
if (status?.appealed) {
|
|
45
|
+
state = 'pending';
|
|
46
|
+
}
|
|
47
|
+
else if (report) {
|
|
48
|
+
// Cleared without the appeal being worked - a takedown or an automatic
|
|
49
|
+
// resolution reset the flag - rather than actually reviewed.
|
|
50
|
+
state = report.closedAt ? 'resolved' : 'superseded';
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
state = appealableUntil && !windowOpen ? 'expired' : 'none';
|
|
54
|
+
}
|
|
55
|
+
const view = { state };
|
|
56
|
+
if (report) {
|
|
57
|
+
view.appealedAt = status?.lastAppealedAt ?? report.createdAt;
|
|
58
|
+
if (report.closedAt)
|
|
59
|
+
view.resolvedAt = report.closedAt;
|
|
60
|
+
}
|
|
61
|
+
if (state === 'resolved' && publicNote)
|
|
62
|
+
view.note = publicNote;
|
|
63
|
+
if (appealableUntil)
|
|
64
|
+
view.appealableUntil = appealableUntil;
|
|
65
|
+
const availableActions = windowOpen && !appealsExhausted(subject, report) ? ['appeal'] : [];
|
|
66
|
+
return { view, availableActions };
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* Stable identity of a moderated subject, independent of which action touched
|
|
70
|
+
* it. Two appeals against the same record - or the same message, or the same
|
|
71
|
+
* conversation - share a key even when they name different action IDs.
|
|
72
|
+
*/
|
|
73
|
+
export const subjectKey = (subject) => {
|
|
74
|
+
const { subjectDid, subjectMessageId, subjectConvoId } = subject.info();
|
|
75
|
+
if (subject.isMessage())
|
|
76
|
+
return `message:${subjectDid}:${subjectConvoId}:${subjectMessageId}`;
|
|
77
|
+
if (subject.isConvo())
|
|
78
|
+
return `convo:${subjectDid}:${subjectConvoId}`;
|
|
79
|
+
if (subject.isRecord())
|
|
80
|
+
return `record:${subjectDid}:${subject.recordPath}`;
|
|
81
|
+
return `account:${subjectDid}`;
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* Match report rows by that identity. Mirrors the subject normalization in
|
|
85
|
+
* `mod-service/report.ts`: `recordPath` is '' for accounts, messages, and
|
|
86
|
+
* conversations alike, so an account match has to exclude the chat columns.
|
|
87
|
+
*/
|
|
88
|
+
export const reportSubjectFilter = (eb, subject) => {
|
|
89
|
+
const { subjectDid, subjectMessageId, subjectConvoId } = subject.info();
|
|
90
|
+
if (subject.isMessage()) {
|
|
91
|
+
return eb.and([
|
|
92
|
+
eb('did', '=', subjectDid),
|
|
93
|
+
eb('subjectMessageId', '=', subjectMessageId),
|
|
94
|
+
eb('subjectConvoId', '=', subjectConvoId),
|
|
95
|
+
]);
|
|
96
|
+
}
|
|
97
|
+
if (subject.isConvo()) {
|
|
98
|
+
return eb.and([
|
|
99
|
+
eb('did', '=', subjectDid),
|
|
100
|
+
eb('subjectConvoId', '=', subjectConvoId),
|
|
101
|
+
eb('subjectMessageId', 'is', null),
|
|
102
|
+
]);
|
|
103
|
+
}
|
|
104
|
+
if (subject.isRecord()) {
|
|
105
|
+
return eb.and([
|
|
106
|
+
eb('did', '=', subjectDid),
|
|
107
|
+
eb('recordPath', '=', subject.recordPath),
|
|
108
|
+
]);
|
|
109
|
+
}
|
|
110
|
+
return eb.and([
|
|
111
|
+
eb('did', '=', subjectDid),
|
|
112
|
+
eb('recordPath', '=', ''),
|
|
113
|
+
eb('subjectMessageId', 'is', null),
|
|
114
|
+
eb('subjectConvoId', 'is', null),
|
|
115
|
+
]);
|
|
116
|
+
};
|
|
117
|
+
export const eventSubjectFilter = (eb, subject) => {
|
|
118
|
+
const { subjectType, subjectDid, subjectUri, subjectMessageId, subjectConvoId, } = subject.info();
|
|
119
|
+
if (subject.isMessage()) {
|
|
120
|
+
return eb.and([
|
|
121
|
+
eb('subjectDid', '=', subjectDid),
|
|
122
|
+
eb('subjectMessageId', '=', subjectMessageId),
|
|
123
|
+
eb('subjectConvoId', '=', subjectConvoId),
|
|
124
|
+
]);
|
|
125
|
+
}
|
|
126
|
+
if (subject.isConvo()) {
|
|
127
|
+
return eb.and([
|
|
128
|
+
eb('subjectDid', '=', subjectDid),
|
|
129
|
+
eb('subjectConvoId', '=', subjectConvoId),
|
|
130
|
+
eb('subjectMessageId', 'is', null),
|
|
131
|
+
]);
|
|
132
|
+
}
|
|
133
|
+
if (subject.isRecord()) {
|
|
134
|
+
return eb.and([
|
|
135
|
+
eb('subjectDid', '=', subjectDid),
|
|
136
|
+
eb('subjectUri', '=', subjectUri),
|
|
137
|
+
]);
|
|
138
|
+
}
|
|
139
|
+
return eb.and([
|
|
140
|
+
eb('subjectDid', '=', subjectDid),
|
|
141
|
+
eb('subjectType', '=', subjectType),
|
|
142
|
+
]);
|
|
143
|
+
};
|
|
144
|
+
export const findAppealedEvent = async (ctx, subject, action) => {
|
|
145
|
+
let query = ctx.db.db
|
|
146
|
+
.selectFrom('moderation_event')
|
|
147
|
+
.where((eb) => eventSubjectFilter(eb, subject))
|
|
148
|
+
.where('action', '=', action.type === 'label' ? LABEL : TAKEDOWN);
|
|
149
|
+
if (action.type === 'label') {
|
|
150
|
+
query = query.where(sql `${action.val} = ANY(string_to_array("createLabelVals", ' '))`);
|
|
151
|
+
}
|
|
152
|
+
const matches = await query
|
|
153
|
+
.orderBy('id', 'desc')
|
|
154
|
+
.limit(2)
|
|
155
|
+
.selectAll()
|
|
156
|
+
.execute();
|
|
157
|
+
return matches.length === 1 ? matches[0] : undefined;
|
|
158
|
+
};
|
|
159
|
+
export const resolveAppealAction = async (ctx, subject, action) => {
|
|
160
|
+
if (!action)
|
|
161
|
+
return undefined;
|
|
162
|
+
const reference = tools.ozone.inbox.appealActionedSubject;
|
|
163
|
+
if (reference.actionRef.$isTypeOf(action)) {
|
|
164
|
+
return ctx.modService(ctx.db).getEvent(action.id);
|
|
165
|
+
}
|
|
166
|
+
if (reference.labelRef.$isTypeOf(action)) {
|
|
167
|
+
return findAppealedEvent(ctx, subject, { type: 'label', val: action.val });
|
|
168
|
+
}
|
|
169
|
+
if (reference.takedownRef.$isTypeOf(action)) {
|
|
170
|
+
return findAppealedEvent(ctx, subject, { type: 'takedown' });
|
|
171
|
+
}
|
|
172
|
+
throw new InvalidRequestError('Unknown appeal action reference', 'InvalidAppealSubject');
|
|
173
|
+
};
|
|
174
|
+
/**
|
|
175
|
+
* Label rows are keyed by a single string: the record URI, or the DID for an
|
|
176
|
+
* account.
|
|
177
|
+
*/
|
|
178
|
+
export const subjectLabelUri = (subject) => (subject.info().subjectUri ?? subject.did);
|
|
179
|
+
/**
|
|
180
|
+
* Whether the subject has used up its appeals.
|
|
181
|
+
*
|
|
182
|
+
* Non-account subjects get one ever, closed or not. An account gets one at a
|
|
183
|
+
* time - and an appeal whose report is still open counts even when the
|
|
184
|
+
* `appealed` flag was cleared out from under it, which is what happens when a
|
|
185
|
+
* takedown supersedes an appeal nobody ever worked.
|
|
186
|
+
*
|
|
187
|
+
* The read path calls this to decide whether to offer `appeal`, and the write
|
|
188
|
+
* path calls it to decide whether to accept one. They have to be the same
|
|
189
|
+
* question: anything else advertises an appeal that submission will refuse.
|
|
190
|
+
*/
|
|
191
|
+
export const appealsExhausted = (subject, report) => {
|
|
192
|
+
if (!report)
|
|
193
|
+
return false;
|
|
194
|
+
return subject.isRepo() ? report.status !== 'closed' : true;
|
|
195
|
+
};
|
|
196
|
+
export const findLatestAppealReport = async (db, subject) => {
|
|
197
|
+
const report = await db.db
|
|
198
|
+
.selectFrom('report')
|
|
199
|
+
.where('reportType', '=', APPEAL_REASON_TYPE)
|
|
200
|
+
.where((eb) => reportSubjectFilter(eb, subject))
|
|
201
|
+
.orderBy('id', 'desc')
|
|
202
|
+
.select(['id', 'status', 'createdAt', 'closedAt'])
|
|
203
|
+
.executeTakeFirst();
|
|
204
|
+
return report ?? null;
|
|
205
|
+
};
|
|
206
|
+
/**
|
|
207
|
+
* Reject an appeal the subject is no longer entitled to.
|
|
208
|
+
*
|
|
209
|
+
* Answers the same question as the `availableActions` the read path
|
|
210
|
+
* advertises, through {@link appealsExhausted}, so the two cannot drift.
|
|
211
|
+
*/
|
|
212
|
+
export const assertAppealAllowed = async (dbTxn, subject) => {
|
|
213
|
+
dbTxn.assertTransaction();
|
|
214
|
+
await sql `select pg_advisory_xact_lock(
|
|
215
|
+
hashtextextended(${subjectKey(subject)}, 0)
|
|
216
|
+
)`.execute(dbTxn.db);
|
|
217
|
+
const existing = await findLatestAppealReport(dbTxn, subject);
|
|
218
|
+
if (appealsExhausted(subject, existing)) {
|
|
219
|
+
throw new ForbiddenError(subject.isRepo()
|
|
220
|
+
? 'Awaiting decision on previous appeal'
|
|
221
|
+
: 'This has already been appealed', 'AlreadyAppealed');
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
const buildAppealEventMeta = (action) => {
|
|
225
|
+
if (!action)
|
|
226
|
+
return undefined;
|
|
227
|
+
const meta = {
|
|
228
|
+
appealActionType: action.$type,
|
|
229
|
+
};
|
|
230
|
+
const reference = tools.ozone.inbox.appealActionedSubject;
|
|
231
|
+
if (reference.actionRef.$isTypeOf(action)) {
|
|
232
|
+
meta.appealActionId = action.id;
|
|
233
|
+
}
|
|
234
|
+
else if (reference.labelRef.$isTypeOf(action)) {
|
|
235
|
+
meta.appealLabel = action.val;
|
|
236
|
+
}
|
|
237
|
+
return meta;
|
|
238
|
+
};
|
|
239
|
+
const findSourceQueueIds = async (db, subject, actionId) => {
|
|
240
|
+
// @NOTE Closed reports retain the source action link. Query each status
|
|
241
|
+
// separately so the existing active-subject and closed-DID indexes apply.
|
|
242
|
+
const query = db.db
|
|
243
|
+
.selectFrom('report')
|
|
244
|
+
.where((eb) => reportSubjectFilter(eb, subject))
|
|
245
|
+
.where('reportType', '!=', APPEAL_REASON_TYPE)
|
|
246
|
+
.where('actionEventIds', '@>', jsonb([actionId]));
|
|
247
|
+
const [active, closed] = await Promise.all([
|
|
248
|
+
query
|
|
249
|
+
.where(sql `status != 'closed'`)
|
|
250
|
+
.select('queueId')
|
|
251
|
+
.execute(),
|
|
252
|
+
query
|
|
253
|
+
.where(sql `status = 'closed'`)
|
|
254
|
+
.select('queueId')
|
|
255
|
+
.execute(),
|
|
256
|
+
]);
|
|
257
|
+
return [...active, ...closed].map((report) => report.queueId);
|
|
258
|
+
};
|
|
259
|
+
/**
|
|
260
|
+
* Pick the queue an appeal should land in.
|
|
261
|
+
*
|
|
262
|
+
* A linked appeal inherits the queue of the report the appealed action
|
|
263
|
+
* resolved, but only when every candidate agrees - bulk and collateral actions
|
|
264
|
+
* can link one event to reports sitting in different queues, and guessing
|
|
265
|
+
* between them is worse than using normal appeal routing. An unlinked or
|
|
266
|
+
* ambiguously linked appeal falls back to whatever queue accepts appeals.
|
|
267
|
+
*
|
|
268
|
+
* This is a read of best-effort routing data, so it deliberately runs outside
|
|
269
|
+
* the appeal transaction: holding the subject lock across a queue listing and
|
|
270
|
+
* a report scan would serialize unrelated appeals for no consistency gain, and
|
|
271
|
+
* if the routing data moves underneath us the router reassigns anyway.
|
|
272
|
+
*/
|
|
273
|
+
const selectQueue = async (ctx, subject, resolvedActionId, action) => {
|
|
274
|
+
const sourceQueueIds = resolvedActionId === undefined
|
|
275
|
+
? []
|
|
276
|
+
: await findSourceQueueIds(ctx.db, subject, resolvedActionId);
|
|
277
|
+
const allSourcesAssigned = sourceQueueIds.length > 0 &&
|
|
278
|
+
sourceQueueIds.every((queueId) => queueId !== null && queueId > 0);
|
|
279
|
+
const sourceQueues = new Set(sourceQueueIds);
|
|
280
|
+
let queueId = allSourcesAssigned && sourceQueues.size === 1
|
|
281
|
+
? sourceQueueIds[0]
|
|
282
|
+
: null;
|
|
283
|
+
const queueService = ctx.queueService(ctx.db);
|
|
284
|
+
if (queueId === null &&
|
|
285
|
+
action &&
|
|
286
|
+
tools.ozone.inbox.appealActionedSubject.labelRef.$isTypeOf(action)) {
|
|
287
|
+
queueId = (await queueService.getByRecommendedLabel(action.val))?.id ?? null;
|
|
288
|
+
}
|
|
289
|
+
if (queueId === null) {
|
|
290
|
+
const { queues } = await queueService.list({ limit: 1000, enabled: true });
|
|
291
|
+
const subjectUri = subject.info().subjectUri;
|
|
292
|
+
const collection = subjectUri ? new AtUri(subjectUri).collection : null;
|
|
293
|
+
queueId =
|
|
294
|
+
findMatchingQueue(queues, subject.isRecord() ? 'record' : 'account', collection, APPEAL_REASON_TYPE)?.id ?? -1;
|
|
295
|
+
}
|
|
296
|
+
// An unrouted report carries no queue timestamp: `queuedAt` records when a
|
|
297
|
+
// report entered a queue, and matches the `queueId: -1` / `status: 'open'`
|
|
298
|
+
// shape the queue service uses for everything it cannot route.
|
|
299
|
+
return {
|
|
300
|
+
queueId,
|
|
301
|
+
queuedAt: queueId > 0 ? currentDatetimeString() : null,
|
|
302
|
+
};
|
|
303
|
+
};
|
|
304
|
+
/**
|
|
305
|
+
* File an appeal: validate the subject, route it, and record it.
|
|
306
|
+
*
|
|
307
|
+
* An appeal is an ordinary `reasonAppeal` report, so it inherits the report
|
|
308
|
+
* lifecycle, queue routing, and activity machinery rather than introducing a
|
|
309
|
+
* parallel one. What makes it a *linked* appeal is `actionEventIds`, whose
|
|
310
|
+
* first entry is the action being challenged.
|
|
311
|
+
*
|
|
312
|
+
* Everything that must be atomic - the eligibility guard, the report event,
|
|
313
|
+
* and the report row - shares one transaction. Everything that needs
|
|
314
|
+
* not be, is already done by the time it opens.
|
|
315
|
+
*/
|
|
316
|
+
export const fileAppeal = async (ctx, { requester, subject, resolvedActionId, action, reason, modTool, }) => {
|
|
317
|
+
const { queueId, queuedAt } = await selectQueue(ctx, subject, resolvedActionId, action);
|
|
318
|
+
const reportId = await ctx.db.transaction(async (dbTxn) => {
|
|
319
|
+
await assertAppealAllowed(dbTxn, subject);
|
|
320
|
+
// create event and report row
|
|
321
|
+
const moderationTxn = ctx.modService(dbTxn);
|
|
322
|
+
const { event: reportEvent } = await moderationTxn.report({
|
|
323
|
+
reason,
|
|
324
|
+
subject,
|
|
325
|
+
reasonType: APPEAL_REASON_TYPE,
|
|
326
|
+
reportedBy: requester,
|
|
327
|
+
modTool,
|
|
328
|
+
eventMeta: buildAppealEventMeta(action),
|
|
329
|
+
});
|
|
330
|
+
return ctx.queueService(dbTxn).insertReportFromEvent({
|
|
331
|
+
event: reportEvent,
|
|
332
|
+
reportType: APPEAL_REASON_TYPE,
|
|
333
|
+
queueId,
|
|
334
|
+
queuedAt,
|
|
335
|
+
actionEventIds: resolvedActionId === undefined ? null : [resolvedActionId],
|
|
336
|
+
});
|
|
337
|
+
});
|
|
338
|
+
return { reportId };
|
|
339
|
+
};
|
|
340
|
+
//# sourceMappingURL=appeal.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"appeal.js","sourceRoot":"","sources":["../../src/inbox/appeal.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,GAAG,EAAE,MAAM,QAAQ,CAAA;AAC5B,OAAO,EAIL,qBAAqB,EACrB,gBAAgB,GACjB,MAAM,cAAc,CAAA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EAAE,CAAC,CAAC,CAAC,IAAI;KACvD,CAAA;AACH,CAAC,CAAA;AAED;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,KAAK,EAC7B,GAAe,EACf,EACE,SAAS,EACT,OAAO,EACP,gBAAgB,EAChB,MAAM,EACN,MAAM,EACN,OAAO,GACS,EACa,EAAE;IACjC,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,GAAG,MAAM,WAAW,CAC7C,GAAG,EACH,OAAO,EACP,gBAAgB,EAChB,MAAM,CACP,CAAA;IAED,MAAM,QAAQ,GAAG,MAAM,GAAG,CAAC,EAAE,CAAC,WAAW,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE;QACxD,MAAM,mBAAmB,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;QAEzC,8BAA8B;QAC9B,MAAM,aAAa,GAAG,GAAG,CAAC,UAAU,CAAC,KAAK,CAAC,CAAA;QAC3C,MAAM,EAAE,KAAK,EAAE,WAAW,EAAE,GAAG,MAAM,aAAa,CAAC,MAAM,CAAC;YACxD,MAAM;YACN,OAAO;YACP,UAAU,EAAE,kBAAkB;YAC9B,UAAU,EAAE,SAAS;YACrB,OAAO;YACP,SAAS,EAAE,oBAAoB,CAAC,MAAM,CAAC;SACxC,CAAC,CAAA;QACF,OAAO,GAAG,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC,qBAAqB,CAAC;YACnD,KAAK,EAAE,WAAW;YAClB,UAAU,EAAE,kBAAkB;YAC9B,OAAO;YACP,QAAQ;YACR,cAAc,EACZ,gBAAgB,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,gBAAgB,CAAC;SAC7D,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;IAEF,OAAO,EAAE,QAAQ,EAAE,CAAA;AACrB,CAAC,CAAA","sourcesContent":["import type { Expression, ExpressionBuilder, SqlBool } from 'kysely'\nimport { sql } from 'kysely'\nimport {\n type DatetimeString,\n type DidString,\n type UriString,\n currentDatetimeString,\n toDatetimeString,\n} from '@atproto/lex'\nimport { AtUri } from '@atproto/syntax'\nimport { ForbiddenError, InvalidRequestError } from '@atproto/xrpc-server'\nimport type { AppContext } from '../context.js'\nimport type { Database } from '../db/index.js'\nimport type { DatabaseSchemaType } from '../db/schema/index.js'\nimport { jsonb } from '../db/types.js'\nimport { tools } from '../lexicons/index.js'\nimport type { AppealView } from '../lexicons/tools/ozone/inbox/defs.js'\nimport type { ModSubject } from '../mod-service/subject.js'\nimport type { ModerationSubjectStatusRow } from '../mod-service/types.js'\nimport { findMatchingQueue } from '../queue/service.js'\n\nexport const APPEAL_REASON_TYPE = 'tools.ozone.report.defs#reasonAppeal'\nexport const TAKEDOWN = 'tools.ozone.moderation.defs#modEventTakedown'\nexport const REVERSE_TAKEDOWN =\n 'tools.ozone.moderation.defs#modEventReverseTakedown'\nexport const LABEL = 'tools.ozone.moderation.defs#modEventLabel'\nexport const EMAIL = 'tools.ozone.moderation.defs#modEventEmail'\nexport const MUTE_REPORTER = 'tools.ozone.moderation.defs#modEventMuteReporter'\nexport const REVOKE_CREDENTIALS =\n 'tools.ozone.moderation.defs#revokeAccountCredentialsEvent'\n\n/**\n * The subject's most recent `reasonAppeal` report, reduced to the columns the\n * appeal state derives from. Deliberately excludes `actionNote` and everything\n * else a moderator wrote for other moderators.\n */\nexport type AppealReport = {\n id: number\n status: string\n createdAt: DatetimeString\n closedAt: DatetimeString | null\n}\n\nexport type AppealInput = {\n subject: ModSubject\n status: ModerationSubjectStatusRow | null\n report: AppealReport | null\n publicNote: string | null\n\n /** Calendar months an action stays appealable, from `InboxConfig`. */\n windowMonths: number\n latestAppealableAt: DatetimeString | null\n}\n\nexport type AppealState = {\n view: AppealView\n availableActions: string[]\n}\n\nexport const PUBLIC_EVENT_ACTIONS = [\n TAKEDOWN,\n LABEL,\n EMAIL,\n MUTE_REPORTER,\n REVOKE_CREDENTIALS,\n REVERSE_TAKEDOWN,\n] as const\n\nexport const APPEALABLE_EVENT_ACTIONS = [TAKEDOWN, LABEL] as const\n\nexport const isAppealableEvent = (action: string): boolean =>\n (APPEALABLE_EVENT_ACTIONS as readonly string[]).includes(action)\n\nexport const appealWindowEnd = (\n actionCreatedAt: DatetimeString,\n windowMonths: number,\n): DatetimeString => {\n const end = new Date(actionCreatedAt)\n const day = end.getUTCDate()\n end.setUTCMonth(end.getUTCMonth() + windowMonths)\n // Clamp a rollover: 31 Aug + 6 months is 28/29 Feb, not 2/3 Mar.\n if (end.getUTCDate() !== day) end.setUTCDate(0)\n return toDatetimeString(end)\n}\n\nexport const isAppealWindowOpen = (\n actionCreatedAt: DatetimeString,\n windowMonths: number,\n now = new Date(),\n): boolean => new Date(appealWindowEnd(actionCreatedAt, windowMonths)) > now\n\n/**\n * Appeal state, derived entirely from data Ozone already records.\n */\nexport const toAppealState = ({\n subject,\n status,\n report,\n publicNote,\n latestAppealableAt,\n windowMonths,\n}: AppealInput): AppealState => {\n const appealableUntil = latestAppealableAt\n ? appealWindowEnd(latestAppealableAt, windowMonths)\n : null\n const windowOpen = !!appealableUntil && new Date(appealableUntil) > new Date()\n\n let state: AppealView['state']\n if (status?.appealed) {\n state = 'pending'\n } else if (report) {\n // Cleared without the appeal being worked - a takedown or an automatic\n // resolution reset the flag - rather than actually reviewed.\n state = report.closedAt ? 'resolved' : 'superseded'\n } else {\n state = appealableUntil && !windowOpen ? 'expired' : 'none'\n }\n\n const view: AppealView = { state }\n if (report) {\n view.appealedAt = status?.lastAppealedAt ?? report.createdAt\n if (report.closedAt) view.resolvedAt = report.closedAt\n }\n if (state === 'resolved' && publicNote) view.note = publicNote\n if (appealableUntil) view.appealableUntil = appealableUntil\n\n const availableActions =\n windowOpen && !appealsExhausted(subject, report) ? ['appeal'] : []\n\n return { view, availableActions }\n}\n\n/**\n * Stable identity of a moderated subject, independent of which action touched\n * it. Two appeals against the same record - or the same message, or the same\n * conversation - share a key even when they name different action IDs.\n */\nexport const subjectKey = (subject: ModSubject): string => {\n const { subjectDid, subjectMessageId, subjectConvoId } = subject.info()\n if (subject.isMessage())\n return `message:${subjectDid}:${subjectConvoId}:${subjectMessageId}`\n if (subject.isConvo()) return `convo:${subjectDid}:${subjectConvoId}`\n if (subject.isRecord()) return `record:${subjectDid}:${subject.recordPath}`\n return `account:${subjectDid}`\n}\n\n/**\n * Match report rows by that identity. Mirrors the subject normalization in\n * `mod-service/report.ts`: `recordPath` is '' for accounts, messages, and\n * conversations alike, so an account match has to exclude the chat columns.\n */\nexport const reportSubjectFilter = (\n eb: ExpressionBuilder<DatabaseSchemaType, 'report'>,\n subject: ModSubject,\n): Expression<SqlBool> => {\n const { subjectDid, subjectMessageId, subjectConvoId } = subject.info()\n if (subject.isMessage()) {\n return eb.and([\n eb('did', '=', subjectDid),\n eb('subjectMessageId', '=', subjectMessageId),\n eb('subjectConvoId', '=', subjectConvoId),\n ])\n }\n if (subject.isConvo()) {\n return eb.and([\n eb('did', '=', subjectDid),\n eb('subjectConvoId', '=', subjectConvoId),\n eb('subjectMessageId', 'is', null),\n ])\n }\n if (subject.isRecord()) {\n return eb.and([\n eb('did', '=', subjectDid),\n eb('recordPath', '=', subject.recordPath),\n ])\n }\n return eb.and([\n eb('did', '=', subjectDid),\n eb('recordPath', '=', ''),\n eb('subjectMessageId', 'is', null),\n eb('subjectConvoId', 'is', null),\n ])\n}\n\nexport const eventSubjectFilter = (\n eb: ExpressionBuilder<DatabaseSchemaType, 'moderation_event'>,\n subject: ModSubject,\n): Expression<SqlBool> => {\n const {\n subjectType,\n subjectDid,\n subjectUri,\n subjectMessageId,\n subjectConvoId,\n } = subject.info()\n if (subject.isMessage()) {\n return eb.and([\n eb('subjectDid', '=', subjectDid),\n eb('subjectMessageId', '=', subjectMessageId),\n eb('subjectConvoId', '=', subjectConvoId),\n ])\n }\n if (subject.isConvo()) {\n return eb.and([\n eb('subjectDid', '=', subjectDid),\n eb('subjectConvoId', '=', subjectConvoId),\n eb('subjectMessageId', 'is', null),\n ])\n }\n if (subject.isRecord()) {\n return eb.and([\n eb('subjectDid', '=', subjectDid),\n eb('subjectUri', '=', subjectUri),\n ])\n }\n return eb.and([\n eb('subjectDid', '=', subjectDid),\n eb('subjectType', '=', subjectType),\n ])\n}\n\nexport const findAppealedEvent = async (\n ctx: AppContext,\n subject: ModSubject,\n action: { type: 'label'; val: string } | { type: 'takedown' },\n) => {\n let query = ctx.db.db\n .selectFrom('moderation_event')\n .where((eb) => eventSubjectFilter(eb, subject))\n .where('action', '=', action.type === 'label' ? LABEL : TAKEDOWN)\n\n if (action.type === 'label') {\n query = query.where(\n sql<boolean>`${action.val} = ANY(string_to_array(\"createLabelVals\", ' '))`,\n )\n }\n\n const matches = await query\n .orderBy('id', 'desc')\n .limit(2)\n .selectAll()\n .execute()\n return matches.length === 1 ? matches[0] : undefined\n}\n\nexport const resolveAppealAction = async (\n ctx: AppContext,\n subject: ModSubject,\n action: tools.ozone.inbox.appealActionedSubject.$InputBody['action'],\n) => {\n if (!action) return undefined\n const reference = tools.ozone.inbox.appealActionedSubject\n if (reference.actionRef.$isTypeOf(action)) {\n return ctx.modService(ctx.db).getEvent(action.id)\n }\n if (reference.labelRef.$isTypeOf(action)) {\n return findAppealedEvent(ctx, subject, { type: 'label', val: action.val })\n }\n if (reference.takedownRef.$isTypeOf(action)) {\n return findAppealedEvent(ctx, subject, { type: 'takedown' })\n }\n throw new InvalidRequestError(\n 'Unknown appeal action reference',\n 'InvalidAppealSubject',\n )\n}\n\n/**\n * Label rows are keyed by a single string: the record URI, or the DID for an\n * account.\n */\nexport const subjectLabelUri = (subject: ModSubject): UriString =>\n (subject.info().subjectUri ?? subject.did) as UriString\n\n/**\n * Whether the subject has used up its appeals.\n *\n * Non-account subjects get one ever, closed or not. An account gets one at a\n * time - and an appeal whose report is still open counts even when the\n * `appealed` flag was cleared out from under it, which is what happens when a\n * takedown supersedes an appeal nobody ever worked.\n *\n * The read path calls this to decide whether to offer `appeal`, and the write\n * path calls it to decide whether to accept one. They have to be the same\n * question: anything else advertises an appeal that submission will refuse.\n */\nexport const appealsExhausted = (\n subject: ModSubject,\n report: AppealReport | null,\n): boolean => {\n if (!report) return false\n return subject.isRepo() ? report.status !== 'closed' : true\n}\n\nexport const findLatestAppealReport = async (\n db: Database,\n subject: ModSubject,\n): Promise<AppealReport | null> => {\n const report = await db.db\n .selectFrom('report')\n .where('reportType', '=', APPEAL_REASON_TYPE)\n .where((eb) => reportSubjectFilter(eb, subject))\n .orderBy('id', 'desc')\n .select(['id', 'status', 'createdAt', 'closedAt'])\n .executeTakeFirst()\n return report ?? null\n}\n\n/**\n * Reject an appeal the subject is no longer entitled to.\n *\n * Answers the same question as the `availableActions` the read path\n * advertises, through {@link appealsExhausted}, so the two cannot drift.\n */\nexport const assertAppealAllowed = async (\n dbTxn: Database,\n subject: ModSubject,\n) => {\n dbTxn.assertTransaction()\n await sql`select pg_advisory_xact_lock(\n hashtextextended(${subjectKey(subject)}, 0)\n )`.execute(dbTxn.db)\n\n const existing = await findLatestAppealReport(dbTxn, subject)\n\n if (appealsExhausted(subject, existing)) {\n throw new ForbiddenError(\n subject.isRepo()\n ? 'Awaiting decision on previous appeal'\n : 'This has already been appealed',\n 'AlreadyAppealed',\n )\n }\n}\n\nexport type FileAppealInput = {\n /** DID of the authenticated account filing the appeal. */\n requester: DidString\n /** The subject being appealed, already resolved and authorized. */\n subject: ModSubject\n /** Resolved moderation event ID, when one could be found. */\n resolvedActionId?: number\n /** The action reference supplied by the caller. */\n action?: tools.ozone.inbox.appealActionedSubject.$InputBody['action']\n /** Optional: an appeal is a request for review, not an argued case. */\n reason?: string\n modTool?: { name: string; meta?: { [_ in string]: unknown } }\n}\n\nconst buildAppealEventMeta = (\n action: FileAppealInput['action'],\n): Record<string, string | number | boolean> | undefined => {\n if (!action) return undefined\n const meta: Record<string, string | number | boolean> = {\n appealActionType: action.$type,\n }\n const reference = tools.ozone.inbox.appealActionedSubject\n if (reference.actionRef.$isTypeOf(action)) {\n meta.appealActionId = action.id\n } else if (reference.labelRef.$isTypeOf(action)) {\n meta.appealLabel = action.val\n }\n return meta\n}\n\nconst findSourceQueueIds = async (\n db: Database,\n subject: ModSubject,\n actionId: number,\n): Promise<(number | null)[]> => {\n // @NOTE Closed reports retain the source action link. Query each status\n // separately so the existing active-subject and closed-DID indexes apply.\n const query = db.db\n .selectFrom('report')\n .where((eb) => reportSubjectFilter(eb, subject))\n .where('reportType', '!=', APPEAL_REASON_TYPE)\n .where('actionEventIds', '@>', jsonb([actionId]))\n\n const [active, closed] = await Promise.all([\n query\n .where(sql<boolean>`status != 'closed'`)\n .select('queueId')\n .execute(),\n query\n .where(sql<boolean>`status = 'closed'`)\n .select('queueId')\n .execute(),\n ])\n return [...active, ...closed].map((report) => report.queueId)\n}\n\n/**\n * Pick the queue an appeal should land in.\n *\n * A linked appeal inherits the queue of the report the appealed action\n * resolved, but only when every candidate agrees - bulk and collateral actions\n * can link one event to reports sitting in different queues, and guessing\n * between them is worse than using normal appeal routing. An unlinked or\n * ambiguously linked appeal falls back to whatever queue accepts appeals.\n *\n * This is a read of best-effort routing data, so it deliberately runs outside\n * the appeal transaction: holding the subject lock across a queue listing and\n * a report scan would serialize unrelated appeals for no consistency gain, and\n * if the routing data moves underneath us the router reassigns anyway.\n */\nconst selectQueue = async (\n ctx: AppContext,\n subject: ModSubject,\n resolvedActionId: number | undefined,\n action: FileAppealInput['action'],\n) => {\n const sourceQueueIds =\n resolvedActionId === undefined\n ? []\n : await findSourceQueueIds(ctx.db, subject, resolvedActionId)\n const allSourcesAssigned =\n sourceQueueIds.length > 0 &&\n sourceQueueIds.every(\n (queueId): queueId is number => queueId !== null && queueId > 0,\n )\n const sourceQueues = new Set(sourceQueueIds)\n let queueId: number | null =\n allSourcesAssigned && sourceQueues.size === 1\n ? (sourceQueueIds[0] as number)\n : null\n const queueService = ctx.queueService(ctx.db)\n if (\n queueId === null &&\n action &&\n tools.ozone.inbox.appealActionedSubject.labelRef.$isTypeOf(action)\n ) {\n queueId = (await queueService.getByRecommendedLabel(action.val))?.id ?? null\n }\n if (queueId === null) {\n const { queues } = await queueService.list({ limit: 1000, enabled: true })\n const subjectUri = subject.info().subjectUri\n const collection = subjectUri ? new AtUri(subjectUri).collection : null\n queueId =\n findMatchingQueue(\n queues,\n subject.isRecord() ? 'record' : 'account',\n collection,\n APPEAL_REASON_TYPE,\n )?.id ?? -1\n }\n\n // An unrouted report carries no queue timestamp: `queuedAt` records when a\n // report entered a queue, and matches the `queueId: -1` / `status: 'open'`\n // shape the queue service uses for everything it cannot route.\n return {\n queueId,\n queuedAt: queueId > 0 ? currentDatetimeString() : null,\n }\n}\n\n/**\n * File an appeal: validate the subject, route it, and record it.\n *\n * An appeal is an ordinary `reasonAppeal` report, so it inherits the report\n * lifecycle, queue routing, and activity machinery rather than introducing a\n * parallel one. What makes it a *linked* appeal is `actionEventIds`, whose\n * first entry is the action being challenged.\n *\n * Everything that must be atomic - the eligibility guard, the report event,\n * and the report row - shares one transaction. Everything that needs\n * not be, is already done by the time it opens.\n */\nexport const fileAppeal = async (\n ctx: AppContext,\n {\n requester,\n subject,\n resolvedActionId,\n action,\n reason,\n modTool,\n }: FileAppealInput,\n): Promise<{ reportId: number }> => {\n const { queueId, queuedAt } = await selectQueue(\n ctx,\n subject,\n resolvedActionId,\n action,\n )\n\n const reportId = await ctx.db.transaction(async (dbTxn) => {\n await assertAppealAllowed(dbTxn, subject)\n\n // create event and report row\n const moderationTxn = ctx.modService(dbTxn)\n const { event: reportEvent } = await moderationTxn.report({\n reason,\n subject,\n reasonType: APPEAL_REASON_TYPE,\n reportedBy: requester,\n modTool,\n eventMeta: buildAppealEventMeta(action),\n })\n return ctx.queueService(dbTxn).insertReportFromEvent({\n event: reportEvent,\n reportType: APPEAL_REASON_TYPE,\n queueId,\n queuedAt,\n actionEventIds:\n resolvedActionId === undefined ? null : [resolvedActionId],\n })\n })\n\n return { reportId }\n}\n"]}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { type DatetimeString, type DidString } from '@atproto/lex';
|
|
2
|
+
import type { InboxConfig } from '../config/config.js';
|
|
3
|
+
import type { Database } from '../db/index.js';
|
|
4
|
+
import type { ActionView, EnforcementView, SubjectView } from '../lexicons/tools/ozone/inbox/defs.js';
|
|
5
|
+
import type { ModSubject } from '../mod-service/subject.js';
|
|
6
|
+
import type { ModerationEventRow, ModerationSubjectStatusRow } from '../mod-service/types.js';
|
|
7
|
+
import type { AppealReport } from './appeal.js';
|
|
8
|
+
/** Everything one `subjectView` needs, as loaded from the database. */
|
|
9
|
+
export type SubjectSnapshot = {
|
|
10
|
+
status: ModerationSubjectStatusRow | null;
|
|
11
|
+
events: ModerationEventRow[];
|
|
12
|
+
/** Active, non-negated, unexpired label values on the subject. */
|
|
13
|
+
labels: string[];
|
|
14
|
+
/** Total public actions, exact even when `events` was capped. */
|
|
15
|
+
actionCount: number;
|
|
16
|
+
firstActionAt: DatetimeString | null;
|
|
17
|
+
/** Newest action the user is allowed to appeal, if any. */
|
|
18
|
+
latestAppealableAt: DatetimeString | null;
|
|
19
|
+
appealReport: AppealReport | null;
|
|
20
|
+
/** Latest nonempty `publicNote` from the appeal's close activities. */
|
|
21
|
+
appealPublicNote: string | null;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Load the public moderation state needed to hydrate one subject view.
|
|
25
|
+
*
|
|
26
|
+
* Only public columns are read. Event comments, `report.actionNote`, and
|
|
27
|
+
* `report_activity.internalNote` are moderator-facing and never leave the
|
|
28
|
+
* database on this path.
|
|
29
|
+
*/
|
|
30
|
+
export declare const loadSubject: (db: Database, subject: ModSubject) => Promise<SubjectSnapshot>;
|
|
31
|
+
export type SubjectViewInput = {
|
|
32
|
+
subject: ModSubject;
|
|
33
|
+
/** DID of the moderation service the view speaks for. */
|
|
34
|
+
serviceDid: string;
|
|
35
|
+
cfg: InboxConfig;
|
|
36
|
+
snapshot: SubjectSnapshot;
|
|
37
|
+
};
|
|
38
|
+
export type EnforcementViewInput = {
|
|
39
|
+
subject: ModSubject;
|
|
40
|
+
status: ModerationSubjectStatusRow | null;
|
|
41
|
+
/** Active, non-negated, unexpired label values on the subject. */
|
|
42
|
+
labels: string[];
|
|
43
|
+
/** Public action history, newest first, used only for the takedown's scope. */
|
|
44
|
+
actions: ActionView[];
|
|
45
|
+
};
|
|
46
|
+
/** Public action type for one event, or null when it is not an entry. */
|
|
47
|
+
export declare const publicActionType: (row: ModerationEventRow) => string | null;
|
|
48
|
+
/**
|
|
49
|
+
* Map raw moderation events to the public action history, newest first.
|
|
50
|
+
*
|
|
51
|
+
* Reversals do not appear as actions. A `modEventReverseTakedown` stamps
|
|
52
|
+
* `reversedAt` on the newest takedown it undoes, and a label negation does the
|
|
53
|
+
* same to the `labelApplied` that put those values there - so the user sees
|
|
54
|
+
* "removed, then restored" as one action with an end date rather than as two
|
|
55
|
+
* unrelated events.
|
|
56
|
+
*/
|
|
57
|
+
export declare const toActionViews: (rows: ModerationEventRow[]) => ActionView[];
|
|
58
|
+
/**
|
|
59
|
+
* Current enforcement, as a single state.
|
|
60
|
+
*
|
|
61
|
+
* A subject can be labeled and taken down at once, so precedence decides which
|
|
62
|
+
* one `state` reports: takendown > suspended > removed > labeled > none. Which
|
|
63
|
+
* of `takendown` / `suspended` / `removed` applies is a question of subject
|
|
64
|
+
* type and duration, not of three separate flags - a record is `removed`, an
|
|
65
|
+
* account is `takendown`, and either becomes a suspension when the takedown
|
|
66
|
+
* carried a duration.
|
|
67
|
+
*
|
|
68
|
+
* `labels[]` carries the label values whichever state won, minus `!takedown`
|
|
69
|
+
* and `!suspend`: those are written straight to the label table by the
|
|
70
|
+
* takedown paths and already say what `state` says.
|
|
71
|
+
*/
|
|
72
|
+
export declare const toEnforcementView: ({ subject, status, labels, actions, }: EnforcementViewInput) => EnforcementView;
|
|
73
|
+
/**
|
|
74
|
+
* Compose one `tools.ozone.inbox.defs#subjectView` from an already-loaded
|
|
75
|
+
* snapshot. Pure and synchronous: every query belongs to the caller, so the
|
|
76
|
+
* view rules can be read and tested without a database.
|
|
77
|
+
*
|
|
78
|
+
* Returns null when the subject has no moderation history at all - it has
|
|
79
|
+
* never been actioned and has no status row - which is not a subject the
|
|
80
|
+
* inbox has anything to say about.
|
|
81
|
+
*
|
|
82
|
+
* `isRead` is deliberately absent: it is a comparison against a per-section
|
|
83
|
+
* read watermark, and Ozone has nowhere to store one yet. Sending a value now
|
|
84
|
+
* would mean inventing the watermark.
|
|
85
|
+
*/
|
|
86
|
+
export declare const toSubjectView: ({ subject, serviceDid, snapshot, cfg, }: SubjectViewInput) => SubjectView | null;
|
|
87
|
+
/**
|
|
88
|
+
* Load a subject and compose its view: the read path in one call, for callers
|
|
89
|
+
* that have a database and a subject and want the finished thing.
|
|
90
|
+
*/
|
|
91
|
+
export declare const hydrateSubjectView: (db: Database, subject: ModSubject, serviceDid: DidString, cfg: InboxConfig) => Promise<SubjectView | null>;
|
|
92
|
+
//# sourceMappingURL=views.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"views.d.ts","sourceRoot":"","sources":["../../src/inbox/views.ts"],"names":[],"mappings":"AACA,OAAO,EACL,KAAK,cAAc,EACnB,KAAK,SAAS,EAEf,MAAM,cAAc,CAAA;AACrB,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAA;AACtD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAA;AAC9C,OAAO,KAAK,EACV,UAAU,EACV,eAAe,EACf,WAAW,EACZ,MAAM,uCAAuC,CAAA;AAE9C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAA;AAC3D,OAAO,KAAK,EACV,kBAAkB,EAClB,0BAA0B,EAC3B,MAAM,yBAAyB,CAAA;AAChC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAwB/C,uEAAuE;AACvE,MAAM,MAAM,eAAe,GAAG;IAC5B,MAAM,EAAE,0BAA0B,GAAG,IAAI,CAAA;IACzC,MAAM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