2023-07-27 14:31:52 +09:00
|
|
|
/*
|
2024-02-13 15:59:27 +00:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 14:31:52 +09:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
*/
|
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-11-24 18:19:01 +01:00
|
|
|
import { IsNull } from 'typeorm';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
2023-09-15 14:28:29 +09:00
|
|
|
import type { EmojisRepository } from '@/models/_.js';
|
2023-09-20 11:33:36 +09:00
|
|
|
import type { MiDriveFile } from '@/models/DriveFile.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { DI } from '@/di-symbols.js';
|
|
|
|
|
import { DriveService } from '@/core/DriveService.js';
|
2023-12-02 17:07:57 +09:00
|
|
|
import { CustomEmojiService } from '@/core/CustomEmojiService.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { EmojiEntityService } from '@/core/entities/EmojiEntityService.js';
|
2022-02-27 11:07:39 +09:00
|
|
|
import { ApiError } from '../../../error.js';
|
2020-01-30 06:06:50 +09:00
|
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
|
tags: ['admin'],
|
|
|
|
|
|
2022-01-18 22:27:10 +09:00
|
|
|
requireCredential: true,
|
2023-01-15 20:52:53 +09:00
|
|
|
requireRolePolicy: 'canManageCustomEmojis',
|
2023-12-27 15:08:59 +09:00
|
|
|
kind: 'write:admin:emoji',
|
2020-01-30 06:06:50 +09:00
|
|
|
|
|
|
|
|
errors: {
|
|
|
|
|
noSuchEmoji: {
|
|
|
|
|
message: 'No such emoji.',
|
|
|
|
|
code: 'NO_SUCH_EMOJI',
|
2021-12-09 23:58:30 +09:00
|
|
|
id: 'e2785b66-dca3-4087-9cac-b93c541cc425',
|
|
|
|
|
},
|
2023-11-24 18:19:01 +01:00
|
|
|
duplicateName: {
|
|
|
|
|
message: 'Duplicate name.',
|
|
|
|
|
code: 'DUPLICATE_NAME',
|
|
|
|
|
id: 'f7a3462c-4e6e-4069-8421-b9bd4f4c3975',
|
|
|
|
|
},
|
2021-03-06 22:34:11 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
res: {
|
2022-01-18 22:27:10 +09:00
|
|
|
type: 'object',
|
|
|
|
|
optional: false, nullable: false,
|
2021-03-06 22:34:11 +09:00
|
|
|
properties: {
|
|
|
|
|
id: {
|
2022-01-18 22:27:10 +09:00
|
|
|
type: 'string',
|
|
|
|
|
optional: false, nullable: false,
|
2021-03-06 22:34:11 +09:00
|
|
|
format: 'id',
|
2021-12-09 23:58:30 +09:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2022-01-18 22:27:10 +09:00
|
|
|
} as const;
|
2020-01-30 06:06:50 +09:00
|
|
|
|
2022-02-20 13:15:40 +09:00
|
|
|
export const paramDef = {
|
2022-02-19 14:05:32 +09:00
|
|
|
type: 'object',
|
|
|
|
|
properties: {
|
|
|
|
|
emojiId: { type: 'string', format: 'misskey:id' },
|
|
|
|
|
},
|
|
|
|
|
required: ['emojiId'],
|
|
|
|
|
} as const;
|
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
// TODO: ロジックをサービスに切り出す
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
2023-08-17 21:20:58 +09:00
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
|
2022-09-18 03:27:08 +09:00
|
|
|
constructor(
|
|
|
|
|
@Inject(DI.emojisRepository)
|
|
|
|
|
private emojisRepository: EmojisRepository,
|
|
|
|
|
private emojiEntityService: EmojiEntityService,
|
2023-12-02 17:07:57 +09:00
|
|
|
private customEmojiService: CustomEmojiService,
|
2022-09-18 03:27:08 +09:00
|
|
|
private driveService: DriveService,
|
|
|
|
|
) {
|
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
|
|
|
|
const emoji = await this.emojisRepository.findOneBy({ id: ps.emojiId });
|
|
|
|
|
if (emoji == null) {
|
|
|
|
|
throw new ApiError(meta.errors.noSuchEmoji);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-16 17:51:28 +09:00
|
|
|
let driveFile: MiDriveFile;
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// Create file
|
|
|
|
|
driveFile = await this.driveService.uploadFromUrl({ url: emoji.originalUrl, user: null, force: true });
|
|
|
|
|
} catch (e) {
|
2023-12-02 17:07:57 +09:00
|
|
|
// TODO: need to return Drive Error
|
2022-09-18 03:27:08 +09:00
|
|
|
throw new ApiError();
|
|
|
|
|
}
|
2020-01-30 06:06:50 +09:00
|
|
|
|
2024-03-09 12:17:48 +00:00
|
|
|
const nameNfc = emoji.name.normalize('NFC');
|
2023-12-02 17:07:57 +09:00
|
|
|
// Duplication Check
|
2024-03-09 12:17:48 +00:00
|
|
|
const isDuplicate = await this.customEmojiService.checkDuplicate(nameNfc);
|
2023-12-02 17:07:57 +09:00
|
|
|
if (isDuplicate) throw new ApiError(meta.errors.duplicateName);
|
|
|
|
|
|
|
|
|
|
const addedEmoji = await this.customEmojiService.add({
|
|
|
|
|
driveFile,
|
2024-03-09 12:17:48 +00:00
|
|
|
name: nameNfc,
|
2024-05-11 13:40:28 +01:00
|
|
|
category: emoji.category?.normalize('NFC') ?? null,
|
2024-03-09 12:17:48 +00:00
|
|
|
aliases: emoji.aliases?.map(a => a.normalize('NFC')),
|
2022-09-18 03:27:08 +09:00
|
|
|
host: null,
|
2023-03-16 15:08:48 +09:00
|
|
|
license: emoji.license,
|
2023-12-02 17:07:57 +09:00
|
|
|
isSensitive: emoji.isSensitive,
|
|
|
|
|
localOnly: emoji.localOnly,
|
|
|
|
|
roleIdsThatCanBeUsedThisEmojiAsReaction: emoji.roleIdsThatCanBeUsedThisEmojiAsReaction,
|
|
|
|
|
}, me);
|
2022-09-18 03:27:08 +09:00
|
|
|
|
2023-12-02 17:07:57 +09:00
|
|
|
return this.emojiEntityService.packDetailed(addedEmoji);
|
2022-09-18 03:27:08 +09:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|