monkeeShark/packages/backend/src/server/api/endpoints/admin/emoji/copy.ts

83 lines
1.8 KiB
TypeScript
Raw Normal View History

2020-01-29 21:06:50 +00:00
import $ from 'cafy';
import define from '../../../define';
import { Emojis } from '@/models/index';
import { genId } from '@/misc/gen-id';
2020-01-29 21:06:50 +00:00
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';
import { publishBroadcastStream } from '@/services/stream';
2020-01-29 21:06:50 +00:00
export const meta = {
tags: ['admin'],
2020-02-15 12:33:32 +00:00
requireCredential: true as const,
2020-01-29 21:06:50 +00:00
requireModerator: true,
params: {
emojiId: {
2021-12-09 14:58:30 +00:00
validator: $.type(ID),
2020-01-29 21:06:50 +00:00
},
},
errors: {
noSuchEmoji: {
message: 'No such emoji.',
code: 'NO_SUCH_EMOJI',
2021-12-09 14:58:30 +00:00
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',
2021-12-09 14:58:30 +00:00
},
},
},
2020-01-29 21:06:50 +00:00
};
2022-01-02 17:12:50 +00:00
// eslint-disable-next-line import/no-default-export
2020-01-29 21:06:50 +00:00
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.insert({
2020-01-29 21:06:50 +00:00
id: genId(),
updatedAt: new Date(),
name: emoji.name,
host: null,
aliases: [],
url: driveFile.url,
type: driveFile.type,
fileId: driveFile.id,
}).then(x => Emojis.findOneOrFail(x.identifiers[0]));
2020-01-29 21:06:50 +00:00
await getConnection().queryResultCache!.remove(['meta_emojis']);
publishBroadcastStream('emojiAdded', {
2021-12-09 14:58:30 +00:00
emoji: await Emojis.pack(copied.id),
});
2020-01-29 21:06:50 +00:00
return {
2021-12-09 14:58:30 +00:00
id: copied.id,
2020-01-29 21:06:50 +00:00
};
});