Skip to content

Commit

Permalink
Add restore images endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
JustMaier committed Apr 21, 2024
1 parent 15df17b commit 035f977
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
62 changes: 62 additions & 0 deletions src/pages/api/mod/restore-images.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { NextApiRequest, NextApiResponse } from 'next';
import { z } from 'zod';
import { Tracker } from '~/server/clickhouse/client';
import { dbRead } from '~/server/db/client';
import { moderateImages } from '~/server/services/image.service';
import { WebhookEndpoint, handleEndpointError } from '~/server/utils/endpoint-helpers';
import { getNsfwLevelDeprecatedReverseMapping } from '~/shared/constants/browsingLevel.constants';

const schema = z.object({
imageIds: z.array(z.number()),
userId: z.number().optional(),
});

export default WebhookEndpoint(async (req: NextApiRequest, res: NextApiResponse) => {
try {
if (req.method !== 'POST') return res.status(405).json({ error: 'Method Not Allowed' });
const { imageIds } = schema.parse(req.body);

const images = await dbRead.image.findMany({
where: { id: { in: imageIds } },
select: {
nsfwLevel: true,
userId: true,
id: true,
tags: {
select: {
tag: {
select: {
name: true,
},
},
},
},
},
});

await moderateImages({
ids: imageIds,
needsReview: null,
reviewAction: undefined,
reviewType: 'blocked',
});

const tracker = new Tracker(req, res);
for (const image of images) {
const tags = image.tags.map((x) => x.tag.name);
tracker.image({
type: 'Restore',
imageId: image.id,
ownerId: image.userId,
nsfw: getNsfwLevelDeprecatedReverseMapping(image.nsfwLevel),
tags,
});
}

return res.status(200).json({
images: imageIds.length,
});
} catch (e) {
return handleEndpointError(res, e);
}
});
8 changes: 7 additions & 1 deletion src/server/clickhouse/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,13 @@ export type CommentType =
| 'BountyEntry';
export type CommentActivity = 'Create' | 'Delete' | 'Update' | 'Hide' | 'Unhide';
export type PostActivityType = 'Create' | 'Publish' | 'Tags';
export type ImageActivityType = 'Create' | 'Delete' | 'DeleteTOS' | 'Tags' | 'Resources';
export type ImageActivityType =
| 'Create'
| 'Delete'
| 'DeleteTOS'
| 'Tags'
| 'Resources'
| 'Restore';
export type QuestionType = 'Create' | 'Delete';
export type AnswerType = 'Create' | 'Delete';
export type PartnerActivity = 'Run' | 'Update';
Expand Down

0 comments on commit 035f977

Please sign in to comment.