2021-08-19 15:55:45 +03:00
|
|
|
import define from '../../../define';
|
|
|
|
import { Emojis } from '@/models/index';
|
|
|
|
import { genId } from '@/misc/gen-id';
|
2020-01-29 23:06:50 +02:00
|
|
|
import { getConnection } from 'typeorm';
|
2021-08-19 15:55:45 +03:00
|
|
|
import { ApiError } from '../../../error';
|
|
|
|
import { DriveFile } from '@/models/entities/drive-file';
|
2022-01-23 15:52:35 +02:00
|
|
|
import { uploadFromUrl } from '@/services/drive/upload-from-url';
|
2021-08-19 15:55:45 +03:00
|
|
|
import { publishBroadcastStream } from '@/services/stream';
|
2020-01-29 23:06:50 +02:00
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
tags: ['admin'],
|
|
|
|
|
2022-01-18 15:27:10 +02:00
|
|
|
requireCredential: true,
|
2020-01-29 23:06:50 +02:00
|
|
|
requireModerator: true,
|
|
|
|
|
|
|
|
errors: {
|
|
|
|
noSuchEmoji: {
|
|
|
|
message: 'No such emoji.',
|
|
|
|
code: 'NO_SUCH_EMOJI',
|
2021-12-09 16:58:30 +02:00
|
|
|
id: 'e2785b66-dca3-4087-9cac-b93c541cc425',
|
|
|
|
},
|
2021-03-06 15:34:11 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
res: {
|
2022-01-18 15:27:10 +02:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2021-03-06 15:34:11 +02:00
|
|
|
properties: {
|
|
|
|
id: {
|
2022-01-18 15:27:10 +02:00
|
|
|
type: 'string',
|
|
|
|
optional: false, nullable: false,
|
2021-03-06 15:34:11 +02:00
|
|
|
format: 'id',
|
2021-12-09 16:58:30 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-01-18 15:27:10 +02:00
|
|
|
} as const;
|
2020-01-29 23:06:50 +02:00
|
|
|
|
2022-02-20 06:15:40 +02:00
|
|
|
export const paramDef = {
|
2022-02-19 07:05:32 +02:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
emojiId: { type: 'string', format: 'misskey:id' },
|
|
|
|
},
|
|
|
|
required: ['emojiId'],
|
|
|
|
} as const;
|
|
|
|
|
2022-01-02 19:12:50 +02:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-02-19 07:05:32 +02:00
|
|
|
export default define(meta, paramDef, async (ps, me) => {
|
2020-01-29 23:06:50 +02:00
|
|
|
const emoji = await Emojis.findOne(ps.emojiId);
|
|
|
|
|
|
|
|
if (emoji == null) {
|
|
|
|
throw new ApiError(meta.errors.noSuchEmoji);
|
|
|
|
}
|
|
|
|
|
|
|
|
let driveFile: DriveFile;
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Create file
|
2022-01-23 15:52:35 +02:00
|
|
|
driveFile = await uploadFromUrl({ url: emoji.originalUrl, user: null, force: true });
|
2020-01-29 23:06:50 +02:00
|
|
|
} 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: [],
|
2022-01-21 11:47:02 +02:00
|
|
|
originalUrl: driveFile.url,
|
|
|
|
publicUrl: driveFile.webpublicUrl ?? driveFile.url,
|
|
|
|
type: driveFile.webpublicType ?? driveFile.type,
|
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', {
|
2021-12-09 16:58:30 +02:00
|
|
|
emoji: await Emojis.pack(copied.id),
|
2021-06-23 07:42:02 +03:00
|
|
|
});
|
|
|
|
|
2020-01-29 23:06:50 +02:00
|
|
|
return {
|
2021-12-09 16:58:30 +02:00
|
|
|
id: copied.id,
|
2020-01-29 23:06:50 +02:00
|
|
|
};
|
|
|
|
});
|