2022-02-27 04:07:39 +02:00
|
|
|
import define from '../../../define.js';
|
|
|
|
import { Emojis } from '@/models/index.js';
|
|
|
|
import { genId } from '@/misc/gen-id.js';
|
|
|
|
import { ApiError } from '../../../error.js';
|
|
|
|
import { DriveFile } from '@/models/entities/drive-file.js';
|
|
|
|
import { uploadFromUrl } from '@/services/drive/upload-from-url.js';
|
|
|
|
import { publishBroadcastStream } from '@/services/stream.js';
|
2022-03-26 08:34:00 +02:00
|
|
|
import { db } from '@/db/postgre.js';
|
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) => {
|
2022-03-26 08:34:00 +02:00
|
|
|
const emoji = await Emojis.findOneBy({ id: ps.emojiId });
|
2020-01-29 23:06:50 +02:00
|
|
|
|
|
|
|
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,
|
2022-03-26 08:34:00 +02:00
|
|
|
}).then(x => Emojis.findOneByOrFail(x.identifiers[0]));
|
2020-01-29 23:06:50 +02:00
|
|
|
|
2022-03-26 08:34:00 +02:00
|
|
|
await db.queryResultCache!.remove(['meta_emojis']);
|
2020-01-29 23:06:50 +02:00
|
|
|
|
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
|
|
|
};
|
|
|
|
});
|