2023-07-27 07:31:52 +02:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
2023-09-15 07:28:29 +02:00
|
|
|
import type { DriveFilesRepository } from '@/models/_.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-01-22 15:53:24 +01:00
|
|
|
import { CustomEmojiService } from '@/core/CustomEmojiService.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { ModerationLogService } from '@/core/ModerationLogService.js';
|
2023-07-20 10:40:04 +02:00
|
|
|
import { EmojiEntityService } from '@/core/entities/EmojiEntityService.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { ApiError } from '../../../error.js';
|
2018-11-02 07:04:02 +01:00
|
|
|
|
|
|
|
export const meta = {
|
2019-02-23 03:20:58 +01:00
|
|
|
tags: ['admin'],
|
|
|
|
|
2022-01-18 14:27:10 +01:00
|
|
|
requireCredential: true,
|
2023-01-15 12:52:53 +01:00
|
|
|
requireRolePolicy: 'canManageCustomEmojis',
|
2018-11-02 07:04:02 +01:00
|
|
|
|
2019-10-15 21:03:18 +02:00
|
|
|
errors: {
|
2020-02-13 17:09:39 +01:00
|
|
|
noSuchFile: {
|
|
|
|
message: 'No such file.',
|
2023-02-26 06:14:57 +01:00
|
|
|
code: 'NO_SUCH_FILE',
|
2021-12-09 15:58:30 +01:00
|
|
|
id: 'fc46b5a4-6b92-4c33-ac66-b806659bb5cf',
|
|
|
|
},
|
|
|
|
},
|
2022-01-18 14:27:10 +01:00
|
|
|
} as const;
|
2018-11-02 07:04:02 +01:00
|
|
|
|
2022-02-20 05:15:40 +01:00
|
|
|
export const paramDef = {
|
2022-02-19 06:05:32 +01:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
2023-05-18 11:45:49 +02:00
|
|
|
name: { type: 'string', pattern: '^[a-zA-Z0-9_]+$' },
|
2022-02-19 06:05:32 +01:00
|
|
|
fileId: { type: 'string', format: 'misskey:id' },
|
2023-05-18 11:45:49 +02:00
|
|
|
category: {
|
|
|
|
type: 'string',
|
|
|
|
nullable: true,
|
|
|
|
description: 'Use `null` to reset the category.',
|
|
|
|
},
|
|
|
|
aliases: { type: 'array', items: {
|
|
|
|
type: 'string',
|
|
|
|
} },
|
|
|
|
license: { type: 'string', nullable: true },
|
|
|
|
isSensitive: { type: 'boolean' },
|
|
|
|
localOnly: { type: 'boolean' },
|
|
|
|
roleIdsThatCanBeUsedThisEmojiAsReaction: { type: 'array', items: {
|
|
|
|
type: 'string',
|
|
|
|
} },
|
2022-02-19 06:05:32 +01:00
|
|
|
},
|
2023-05-18 11:45:49 +02:00
|
|
|
required: ['name', 'fileId'],
|
2022-02-19 06:05:32 +01:00
|
|
|
} as const;
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
// TODO: ロジックをサービスに切り出す
|
|
|
|
|
|
|
|
@Injectable()
|
2023-08-17 14:20:58 +02:00
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
|
2022-09-17 20:27:08 +02:00
|
|
|
constructor(
|
|
|
|
@Inject(DI.driveFilesRepository)
|
|
|
|
private driveFilesRepository: DriveFilesRepository,
|
|
|
|
|
2023-01-22 15:53:24 +01:00
|
|
|
private customEmojiService: CustomEmojiService,
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2023-07-20 10:40:04 +02:00
|
|
|
private emojiEntityService: EmojiEntityService,
|
2022-09-17 20:27:08 +02:00
|
|
|
private moderationLogService: ModerationLogService,
|
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
2023-01-22 15:53:24 +01:00
|
|
|
const driveFile = await this.driveFilesRepository.findOneBy({ id: ps.fileId });
|
|
|
|
if (driveFile == null) throw new ApiError(meta.errors.noSuchFile);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2023-01-22 15:53:24 +01:00
|
|
|
const emoji = await this.customEmojiService.add({
|
|
|
|
driveFile,
|
2023-05-18 11:45:49 +02:00
|
|
|
name: ps.name,
|
|
|
|
category: ps.category ?? null,
|
|
|
|
aliases: ps.aliases ?? [],
|
2023-01-22 15:53:24 +01:00
|
|
|
host: null,
|
2023-05-18 11:45:49 +02:00
|
|
|
license: ps.license ?? null,
|
|
|
|
isSensitive: ps.isSensitive ?? false,
|
|
|
|
localOnly: ps.localOnly ?? false,
|
|
|
|
roleIdsThatCanBeUsedThisEmojiAsReaction: ps.roleIdsThatCanBeUsedThisEmojiAsReaction ?? [],
|
2022-09-17 20:27:08 +02:00
|
|
|
});
|
|
|
|
|
2023-09-23 11:28:16 +02:00
|
|
|
this.moderationLogService.log(me, 'addCustomEmoji', {
|
2022-09-17 20:27:08 +02:00
|
|
|
emojiId: emoji.id,
|
|
|
|
});
|
|
|
|
|
2023-07-20 10:40:04 +02:00
|
|
|
return this.emojiEntityService.packDetailed(emoji);
|
2022-09-17 20:27:08 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|