2022-02-27 02:07:39 +00:00
|
|
|
import define from '../../../define.js';
|
|
|
|
import { Emojis } from '@/models/index.js';
|
|
|
|
import { genId } from '@/misc/gen-id.js';
|
2020-01-29 21:06:50 +00:00
|
|
|
import { getConnection } from 'typeorm';
|
2022-02-27 02:07:39 +00:00
|
|
|
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';
|
2020-01-29 21:06:50 +00:00
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
tags: ['admin'],
|
|
|
|
|
2022-01-18 13:27:10 +00:00
|
|
|
requireCredential: true,
|
2020-01-29 21:06:50 +00:00
|
|
|
requireModerator: true,
|
|
|
|
|
|
|
|
errors: {
|
|
|
|
noSuchEmoji: {
|
|
|
|
message: 'No such emoji.',
|
|
|
|
code: 'NO_SUCH_EMOJI',
|
2021-12-09 14:58:30 +00:00
|
|
|
id: 'e2785b66-dca3-4087-9cac-b93c541cc425',
|
|
|
|
},
|
2021-03-06 13:34:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
res: {
|
2022-01-18 13:27:10 +00:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2021-03-06 13:34:11 +00:00
|
|
|
properties: {
|
|
|
|
id: {
|
2022-01-18 13:27:10 +00:00
|
|
|
type: 'string',
|
|
|
|
optional: false, nullable: false,
|
2021-03-06 13:34:11 +00:00
|
|
|
format: 'id',
|
2021-12-09 14:58:30 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-01-18 13:27:10 +00:00
|
|
|
} as const;
|
2020-01-29 21:06:50 +00:00
|
|
|
|
2022-02-20 04:15:40 +00:00
|
|
|
export const paramDef = {
|
2022-02-19 05:05:32 +00:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
emojiId: { type: 'string', format: 'misskey:id' },
|
|
|
|
},
|
|
|
|
required: ['emojiId'],
|
|
|
|
} as const;
|
|
|
|
|
2022-01-02 17:12:50 +00:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-02-19 05:05:32 +00:00
|
|
|
export default define(meta, paramDef, async (ps, me) => {
|
2020-01-29 21:06:50 +00: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 13:52:35 +00:00
|
|
|
driveFile = await uploadFromUrl({ url: emoji.originalUrl, user: null, force: true });
|
2020-01-29 21:06:50 +00:00
|
|
|
} catch (e) {
|
|
|
|
throw new ApiError();
|
|
|
|
}
|
|
|
|
|
2021-06-23 04:42:02 +00:00
|
|
|
const copied = await Emojis.insert({
|
2020-01-29 21:06:50 +00:00
|
|
|
id: genId(),
|
|
|
|
updatedAt: new Date(),
|
|
|
|
name: emoji.name,
|
|
|
|
host: null,
|
|
|
|
aliases: [],
|
2022-01-21 09:47:02 +00:00
|
|
|
originalUrl: driveFile.url,
|
|
|
|
publicUrl: driveFile.webpublicUrl ?? driveFile.url,
|
|
|
|
type: driveFile.webpublicType ?? driveFile.type,
|
2021-06-23 04:42:02 +00:00
|
|
|
}).then(x => Emojis.findOneOrFail(x.identifiers[0]));
|
2020-01-29 21:06:50 +00:00
|
|
|
|
|
|
|
await getConnection().queryResultCache!.remove(['meta_emojis']);
|
|
|
|
|
2021-06-23 04:42:02 +00:00
|
|
|
publishBroadcastStream('emojiAdded', {
|
2021-12-09 14:58:30 +00:00
|
|
|
emoji: await Emojis.pack(copied.id),
|
2021-06-23 04:42:02 +00:00
|
|
|
});
|
|
|
|
|
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
|
|
|
};
|
|
|
|
});
|