summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/admin/emoji/copy.ts
blob: 7df7830072c1bbed9c5b394155dfaeef03c23610 (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 define from '../../../define';
import { Emojis } from '../../../../../models';
import { genId } from '@/misc/gen-id';
import { getConnection } from 'typeorm';
import { ApiError } from '../../../error';
import { DriveFile } from '../../../../../models/entities/drive-file';
import { ID } from '@/misc/cafy-id';
import uploadFromUrl from '../../../../../services/drive/upload-from-url';

export const meta = {
	desc: {
		'ja-JP': '選択したカスタム絵文字をコピーします。',
		'en-US': 'Copies the selected custom emoji.'
	},

	tags: ['admin'],

	requireCredential: true as const,
	requireModerator: true,

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

	errors: {
		noSuchEmoji: {
			message: 'No such emoji.',
			code: 'NO_SUCH_EMOJI',
			id: 'e2785b66-dca3-4087-9cac-b93c541cc425'
		}
	},

	res: {
		type: 'object' as const,
		optional: false as const, nullable: false as const,
		properties: {
			id: {
				type: 'string' as const,
				optional: false as const, nullable: false as const,
				format: 'id',
				description: 'New copied emoji ID'
			}
		}
	}
};

export default define(meta, async (ps, me) => {
	const emoji = await Emojis.findOne(ps.emojiId);

	if (emoji == null) {
		throw new ApiError(meta.errors.noSuchEmoji);
	}

	let driveFile: DriveFile;

	try {
		// Create file
		driveFile = await uploadFromUrl(emoji.url, null, null, null, false, true);
	} catch (e) {
		throw new ApiError();
	}

	const copied = await Emojis.save({
		id: genId(),
		updatedAt: new Date(),
		name: emoji.name,
		host: null,
		aliases: [],
		url: driveFile.url,
		type: driveFile.type,
		fileId: driveFile.id,
	});

	await getConnection().queryResultCache!.remove(['meta_emojis']);

	return {
		id: copied.id
	};
});