2020-01-29 23:06:50 +02:00
|
|
|
import $ from 'cafy';
|
2021-08-19 12:33:41 +03:00
|
|
|
import define from '../../../define.js';
|
|
|
|
import { Emojis } from '@/models/index.js';
|
|
|
|
import { genId } from '@/misc/gen-id.js';
|
2020-01-29 23:06:50 +02:00
|
|
|
import { getConnection } from 'typeorm';
|
2021-08-19 12:33:41 +03:00
|
|
|
import { ApiError } from '../../../error.js';
|
|
|
|
import { DriveFile } from '@/models/entities/drive-file.js';
|
|
|
|
import { ID } from '@/misc/cafy-id.js';
|
|
|
|
import uploadFromUrl from '@/services/drive/upload-from-url.js';
|
|
|
|
import { publishBroadcastStream } from '@/services/stream.js';
|
2020-01-29 23:06:50 +02:00
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
tags: ['admin'],
|
|
|
|
|
2020-02-15 14:33:32 +02:00
|
|
|
requireCredential: true as const,
|
2020-01-29 23:06:50 +02:00
|
|
|
requireModerator: true,
|
|
|
|
|
|
|
|
params: {
|
|
|
|
emojiId: {
|
|
|
|
validator: $.type(ID)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
errors: {
|
|
|
|
noSuchEmoji: {
|
|
|
|
message: 'No such emoji.',
|
|
|
|
code: 'NO_SUCH_EMOJI',
|
|
|
|
id: 'e2785b66-dca3-4087-9cac-b93c541cc425'
|
|
|
|
}
|
2021-03-06 15:34:11 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
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',
|
|
|
|
}
|
|
|
|
}
|
2020-01-29 23:06:50 +02: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();
|
|
|
|
}
|
|
|
|
|
2021-06-23 07:42:02 +03:00
|
|
|
const copied = await Emojis.insert({
|
2020-01-29 23:06:50 +02:00
|
|
|
id: genId(),
|
|
|
|
updatedAt: new Date(),
|
|
|
|
name: emoji.name,
|
|
|
|
host: null,
|
|
|
|
aliases: [],
|
|
|
|
url: driveFile.url,
|
|
|
|
type: driveFile.type,
|
|
|
|
fileId: driveFile.id,
|
2021-06-23 07:42:02 +03:00
|
|
|
}).then(x => Emojis.findOneOrFail(x.identifiers[0]));
|
2020-01-29 23:06:50 +02:00
|
|
|
|
|
|
|
await getConnection().queryResultCache!.remove(['meta_emojis']);
|
|
|
|
|
2021-06-23 07:42:02 +03:00
|
|
|
publishBroadcastStream('emojiAdded', {
|
|
|
|
emoji: await Emojis.pack(copied.id)
|
|
|
|
});
|
|
|
|
|
2020-01-29 23:06:50 +02:00
|
|
|
return {
|
|
|
|
id: copied.id
|
|
|
|
};
|
|
|
|
});
|