Repository navigation
Expand file tree
/
Copy pathadmin.ts
More file actions
581 lines (529 loc) · 18.6 KB
/
Copy pathadmin.ts
File metadata and controls
581 lines (529 loc) · 18.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
import { TRPCError } from "@trpc/server";
import { BanUserSchema, UnbanUserSchema } from "../../../schema/admin";
import z from "zod";
import * as Sentry from "@sentry/nextjs";
import { createTRPCRouter, adminOnlyProcedure } from "../trpc";
import { countOpenSystemReports } from "./utils";
import {
banned_users,
session,
user,
posts,
comments,
content_report,
reports,
feed_sources,
notification,
} from "@/server/db/schema";
import { and, count, desc, eq, isNotNull, sql } from "drizzle-orm";
import { alias } from "drizzle-orm/pg-core";
import { runPostGoLiveSideEffects } from "@/server/lib/post-go-live";
import { POST_APPROVED } from "@/utils/notifications";
import {
buildCommentHref,
buildContentHref,
buildSlug,
} from "@/server/lib/content-url";
import { groupReportedPosts, toLegacyReason } from "@/server/lib/reportedPosts";
import { mintUrlId } from "@/server/lib/url-id";
export const adminRouter = createTRPCRouter({
// Get dashboard stats
getStats: adminOnlyProcedure.query(async ({ ctx }) => {
const [usersCount] = await ctx.db.select({ count: count() }).from(user);
const [postsCount] = await ctx.db
.select({ count: count() })
.from(posts)
.where(eq(posts.status, "published"));
const [pendingReports] = await ctx.db
.select({ count: count() })
.from(content_report)
.where(eq(content_report.status, "PENDING"));
// System (auto-flagged) reports live in `reports`, not `content_report`.
const pendingSystemReports = await countOpenSystemReports(ctx.db);
const [bannedUsersCount] = await ctx.db
.select({ count: count() })
.from(banned_users);
const [activeSourcesCount] = await ctx.db
.select({ count: count() })
.from(feed_sources)
.where(eq(feed_sources.status, "active"));
return {
totalUsers: usersCount.count,
publishedPosts: postsCount.count,
pendingReports: pendingReports.count + pendingSystemReports,
bannedUsers: bannedUsersCount.count,
activeFeedSources: activeSourcesCount.count,
};
}),
// Get users with search/filter
getUsers: adminOnlyProcedure
.input(
z.object({
search: z.string().optional(),
limit: z.number().min(1).max(100).default(20),
cursor: z.number().optional(),
}),
)
.query(async ({ ctx, input }) => {
const { search, limit, cursor } = input;
const conditions = [];
if (search) {
conditions.push(
sql`(${user.username} ILIKE ${`%${search}%`} OR ${user.name} ILIKE ${`%${search}%`} OR ${user.email} ILIKE ${`%${search}%`})`,
);
}
const whereClause =
conditions.length > 0 ? and(...conditions) : undefined;
// Offset-based pagination. The cursor is the row offset; keyed pagination
// on `id` would be wrong here because the list is ordered by `createdAt`.
const offset = cursor ?? 0;
const users = await ctx.db.query.user.findMany({
where: whereClause,
columns: {
id: true,
username: true,
name: true,
email: true,
image: true,
role: true,
createdAt: true,
},
with: {
bannedUsers: {
columns: {
id: true,
createdAt: true,
note: true,
},
},
},
orderBy: [desc(user.createdAt)],
limit: limit + 1,
offset,
});
let nextCursor: number | undefined;
if (users.length > limit) {
users.pop();
nextCursor = offset + limit;
}
return {
users: users.map((u) => ({
...u,
isBanned: !!u.bannedUsers,
})),
nextCursor,
};
}),
// Get banned users list
getBannedUsers: adminOnlyProcedure.query(async ({ ctx }) => {
const banned = await ctx.db.query.banned_users.findMany({
with: {
user: {
columns: {
id: true,
username: true,
name: true,
email: true,
image: true,
},
},
bannedBy: {
columns: {
id: true,
username: true,
name: true,
},
},
},
orderBy: [desc(banned_users.createdAt)],
});
return banned;
}),
ban: adminOnlyProcedure
.input(BanUserSchema)
.mutation(async ({ input, ctx }) => {
const { userId, note } = input;
const currentUserId = ctx.session.user.id;
const user = await ctx.db.query.user.findFirst({
where: (user, { eq }) => eq(user.id, userId),
});
if (!user) throw new Error("User not found");
if (user.role === "ADMIN") {
throw new TRPCError({
code: "FORBIDDEN",
});
}
await ctx.db.insert(banned_users).values({
bannedById: currentUserId,
userId: userId,
note: note,
createdAt: new Date().toISOString(),
});
await ctx.db.delete(session).where(eq(session.userId, userId));
// Hide all published posts by the banned user
await ctx.db
.update(posts)
.set({ status: "draft" })
.where(and(eq(posts.authorId, userId), eq(posts.status, "published")));
return { banned: true };
}),
unban: adminOnlyProcedure
.input(UnbanUserSchema)
.mutation(async ({ input, ctx }) => {
const { userId } = input;
await ctx.db.delete(banned_users).where(eq(banned_users.userId, userId));
// Restore posts that were previously published (have publishedAt set)
await ctx.db
.update(posts)
.set({ status: "published" })
.where(
and(
eq(posts.authorId, userId),
eq(posts.status, "draft"),
isNotNull(posts.publishedAt),
),
);
return { unbanned: true };
}),
// Auto-moderation queue: posts awaiting human review (status `in_review`).
// `moderationNote` surfaces WHY a post was flagged (auto-mod reason, etc.).
// `excerpt` gives a moderator a first impression in the queue itself; the
// full read happens on the preview page, which loads the post by id.
listInReview: adminOnlyProcedure.query(async ({ ctx }) => {
const rows = await ctx.db
.select({
id: posts.id,
title: posts.title,
slug: posts.slug,
excerpt: posts.excerpt,
authorId: posts.authorId,
authorUsername: user.username,
authorName: user.name,
moderationNote: posts.moderationNote,
createdAt: posts.createdAt,
})
.from(posts)
.leftJoin(user, eq(posts.authorId, user.id))
.where(eq(posts.status, "in_review"))
.orderBy(desc(posts.createdAt))
.limit(50);
return rows;
}),
// Live posts (status `published`) that have at least one open report, from a
// member (`content_report`) or the nightly review cron (`reports`). These are
// flagged-but-still-public; an admin can Hide (→ in_review) or Dismiss the
// report(s). The two tables' ids are returned in separate fields
// (`reportIds` / `systemReportIds`) because their sequences overlap.
listReportedPosts: adminOnlyProcedure.query(async ({ ctx }) => {
// Pending member reports joined to their (live) post + author.
const pendingReports = await ctx.db
.select({
reportId: content_report.id,
reason: content_report.reason,
details: content_report.details,
reportCreatedAt: content_report.createdAt,
postId: posts.id,
title: posts.title,
slug: posts.slug,
authorId: posts.authorId,
authorUsername: user.username,
authorName: user.name,
})
.from(content_report)
.innerJoin(posts, eq(content_report.postId, posts.id))
.leftJoin(user, eq(posts.authorId, user.id))
.where(
and(
eq(content_report.status, "PENDING"),
eq(posts.status, "published"),
isNotNull(content_report.postId),
),
)
.orderBy(desc(content_report.createdAt))
.limit(200);
// Pending system reports, joined the same way.
const pendingSystemReports = await ctx.db
.select({
reportId: reports.id,
reason: reports.reason,
details: reports.details,
reportCreatedAt: reports.createdAt,
postId: posts.id,
title: posts.title,
slug: posts.slug,
authorId: posts.authorId,
authorUsername: user.username,
authorName: user.name,
})
.from(reports)
.innerJoin(posts, eq(reports.postId, posts.id))
.leftJoin(user, eq(posts.authorId, user.id))
.where(
and(
eq(reports.status, "pending"),
eq(reports.source, "system"),
eq(posts.status, "published"),
),
)
.orderBy(desc(reports.createdAt))
.limit(200);
return groupReportedPosts(pendingReports, pendingSystemReports);
}),
// Comments the nightly review cron has flagged (pending system reports in
// `reports`). Soft-deleted comments stay listed so their report can still be
// dismissed. `reportId` is a `reports` id — see report.dismissSystem.
listFlaggedComments: adminOnlyProcedure.query(async ({ ctx }) => {
// The comment's author and its parent post's author are both users; the
// latter drives member hrefs.
const postAuthor = alias(user, "post_author");
const rows = await ctx.db
.select({
reportId: reports.id,
reason: reports.reason,
details: reports.details,
reportCreatedAt: reports.createdAt,
commentId: comments.id,
body: comments.body,
deletedAt: comments.deletedAt,
authorUsername: user.username,
parentTitle: posts.title,
parentType: posts.type,
parentSlug: posts.slug,
sourceSlug: feed_sources.slug,
postAuthorUsername: postAuthor.username,
})
.from(reports)
.innerJoin(comments, eq(reports.commentId, comments.id))
.innerJoin(posts, eq(comments.postId, posts.id))
.leftJoin(user, eq(comments.authorId, user.id))
.leftJoin(feed_sources, eq(posts.sourceId, feed_sources.id))
.leftJoin(postAuthor, eq(posts.authorId, postAuthor.id))
.where(and(eq(reports.status, "pending"), eq(reports.source, "system")))
.orderBy(desc(reports.createdAt))
.limit(200);
return rows.map((row) => {
const parent = {
sourceSlug: row.sourceSlug,
authorUsername: row.postAuthorUsername,
};
// No public URL for the parent (a member post whose author has no
// username) means no link, rather than a broken one.
const hasPublicUrl =
buildContentHref({
type: row.parentType,
slug: row.parentSlug,
...parent,
}) !== null;
return {
reportId: row.reportId,
reason: toLegacyReason(row.reason),
details: row.details,
reportedAt: row.reportCreatedAt,
commentId: row.commentId,
body: row.body,
isDeleted: row.deletedAt !== null,
authorUsername: row.authorUsername,
parentTitle: row.parentTitle,
href: hasPublicUrl
? buildCommentHref({
commentId: row.commentId,
parentType: row.parentType,
parentSlug: row.parentSlug,
...parent,
})
: null,
};
});
}),
// Moderate a post from the queue.
// - approve : in_review → published (+ points + author notification)
// - reject : in_review → rejected (+ stores moderationNote so the author
// later sees "Hidden by moderator" + reason)
// - hide : published → in_review (a live, reported post leaves the public
// feed and re-enters the review queue for a human decision)
// Nothing is ever deleted. When hiding/rejecting a post that has open
// reports (member or system), those reports are resolved (set to actioned)
// so they leave the flagged queue.
moderatePost: adminOnlyProcedure
.input(
z.object({
id: z.string(),
decision: z.enum(["approve", "reject", "hide"]),
note: z.string().max(1000).optional(),
// Optional future release time when approving: a future value schedules
// the post (the promote-scheduled cron goes it live); missing/past
// publishes immediately.
publishAt: z.string().datetime().optional(),
}),
)
.mutation(async ({ ctx, input }) => {
const [existing] = await ctx.db
.select({
id: posts.id,
authorId: posts.authorId,
title: posts.title,
slug: posts.slug,
urlId: posts.urlId,
status: posts.status,
type: posts.type,
sourceId: posts.sourceId,
canonicalUrl: posts.canonicalUrl,
authorUsername: user.username,
})
.from(posts)
.leftJoin(user, eq(posts.authorId, user.id))
.where(eq(posts.id, input.id))
.limit(1);
if (!existing) {
throw new TRPCError({ code: "NOT_FOUND", message: "Post not found" });
}
// Guards: approve/reject only act on posts awaiting review; hide only acts
// on currently-live (published) posts. This keeps actions scoped to the
// correct lifecycle state and prevents acting on arbitrary statuses.
if (input.decision === "hide") {
if (existing.status !== "published") {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Only a published post can be hidden",
});
}
} else if (existing.status !== "in_review") {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Post is not in review",
});
}
// Resolve any open reports for a post (set to actioned) so it leaves the
// flagged queue once a moderator hides or rejects it. Member reports and
// system reports live in separate tables with their own status enums.
const resolveOpenReports = async () => {
const resolution = {
actionTaken: "Resolved by moderator",
reviewedById: ctx.session.user.id,
reviewedAt: new Date().toISOString(),
};
await ctx.db
.update(content_report)
.set({ status: "ACTIONED", ...resolution })
.where(
and(
eq(content_report.postId, input.id),
eq(content_report.status, "PENDING"),
),
);
await ctx.db
.update(reports)
.set({ status: "actioned", ...resolution })
.where(
and(eq(reports.postId, input.id), eq(reports.status, "pending")),
);
};
if (input.decision === "hide") {
// Move the live post into review and resolve its open reports.
const [hidden] = await ctx.db
.update(posts)
.set({
status: "in_review",
// Carry the moderator's note (if any) into the review queue so the
// reason a live post was pulled is visible to the next reviewer.
...(input.note !== undefined ? { moderationNote: input.note } : {}),
})
.where(eq(posts.id, input.id))
.returning();
await resolveOpenReports();
return hidden;
}
if (input.decision === "reject") {
const [rejected] = await ctx.db
.update(posts)
.set({
status: "rejected",
// Store the moderator's reason so the author can later see
// "Hidden by moderator" + this note.
moderationNote: input.note ?? null,
})
.where(eq(posts.id, input.id))
.returning();
// A rejected post may also have open reports (if it was hidden first);
// resolve them so they don't linger in the flagged queue.
await resolveOpenReports();
return rejected;
}
// Approving is a moderator ruling that the post is fine, so any system
// flag still open against it is dismissed rather than resurfacing once
// the post is live again.
await ctx.db
.update(reports)
.set({
status: "dismissed",
actionTaken: "Post approved by moderator",
reviewedById: ctx.session.user.id,
reviewedAt: new Date().toISOString(),
})
.where(
and(
eq(reports.postId, input.id),
eq(reports.source, "system"),
eq(reports.status, "pending"),
),
);
// Ensure the post has a stable slug before approving (now) or scheduling.
// The slug's trailing token must be the post's urlId (parseUrlId
// contract), so mint one if the row predates the urlId column.
const slugUrlId = existing.urlId ?? mintUrlId();
const urlIdPatch = existing.urlId ? {} : { urlId: slugUrlId };
const slug =
existing.slug ||
(existing.title ? buildSlug(existing.title, slugUrlId) : existing.slug);
// Approve & schedule: a FUTURE publishAt parks the post as `scheduled`;
// go-live side-effects run from the cron at the real go-live, not now.
const publishAt = input.publishAt ? new Date(input.publishAt) : null;
if (publishAt && publishAt.getTime() > Date.now()) {
const [scheduled] = await ctx.db
.update(posts)
.set({
status: "scheduled",
publishedAt: publishAt.toISOString(),
slug,
...urlIdPatch,
})
.where(eq(posts.id, input.id))
.returning();
// Courtesy "approved + scheduled" notification (POST_APPROVED, notifier =
// author); the go-live notification fires from the cron later.
try {
await ctx.db.insert(notification).values({
type: POST_APPROVED,
userId: existing.authorId,
notifierId: existing.authorId,
postId: existing.id,
});
} catch (error) {
Sentry.captureException(error);
}
return scheduled;
}
// Approve → publish now, mirroring the normal publish path.
const [approved] = await ctx.db
.update(posts)
.set({
status: "published",
publishedAt: new Date().toISOString(),
slug,
...urlIdPatch,
})
.where(eq(posts.id, input.id))
.returning();
// Same go-live side-effects the cron runs (points + IndexNow + notification).
await runPostGoLiveSideEffects(ctx.db, {
id: existing.id,
authorId: existing.authorId,
type: existing.type,
slug: approved?.slug ?? slug,
authorUsername: existing.authorUsername,
sourceId: existing.sourceId,
canonicalUrl: existing.canonicalUrl,
});
return approved;
}),
});