summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/gallery/posts/update.ts
blob: d9176ea4073782456ea6dd8f9eb42cbfc4fd99cd (plain)
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
import $ from 'cafy';
import * as ms from 'ms';
import define from '../../../define';
import { ID } from '../../../../../misc/cafy-id';
import { DriveFiles, GalleryPosts } from '../../../../../models';
import { GalleryPost } from '../../../../../models/entities/gallery-post';
import { ApiError } from '../../../error';
import { DriveFile } from '@/models/entities/drive-file';

export const meta = {
	tags: ['gallery'],

	requireCredential: true as const,

	kind: 'write:gallery',

	limit: {
		duration: ms('1hour'),
		max: 300
	},

	params: {
		postId: {
			validator: $.type(ID),
		},

		title: {
			validator: $.str.min(1),
		},

		description: {
			validator: $.optional.nullable.str,
		},

		fileIds: {
			validator: $.arr($.type(ID)).unique().range(1, 32),
		},

		isSensitive: {
			validator: $.optional.bool,
			default: false,
		},
	},

	res: {
		type: 'object' as const,
		optional: false as const, nullable: false as const,
		ref: 'GalleryPost',
	},

	errors: {

	}
};

export default define(meta, async (ps, user) => {
	const files = (await Promise.all(ps.fileIds.map(fileId =>
		DriveFiles.findOne({
			id: fileId,
			userId: user.id
		})
	))).filter((file): file is DriveFile => file != null);

	if (files.length === 0) {
		throw new Error();
	}

	await GalleryPosts.update({
		id: ps.postId,
		userId: user.id,
	}, {
		updatedAt: new Date(),
		title: ps.title,
		description: ps.description,
		isSensitive: ps.isSensitive,
		fileIds: files.map(file => file.id)
	});

	const post = await GalleryPosts.findOneOrFail(ps.postId);

	return await GalleryPosts.pack(post, user);
});