2023-07-27 05:31:52 +00:00
|
|
|
/*
|
2024-02-13 15:59:27 +00:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 05:31:52 +00:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
2023-04-06 02:14:43 +00:00
|
|
|
import { CustomEmojiService } from '@/core/CustomEmojiService.js';
|
2023-09-15 05:28:29 +00:00
|
|
|
import type { DriveFilesRepository } from '@/models/_.js';
|
2023-05-18 09:45:49 +00:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-02-17 06:36:36 +00:00
|
|
|
import { ApiError } from '../../../error.js';
|
2018-11-03 18:18:32 +00:00
|
|
|
|
|
|
|
export const meta = {
|
2019-02-23 02:20:58 +00:00
|
|
|
tags: ['admin'],
|
|
|
|
|
2022-01-18 13:27:10 +00:00
|
|
|
requireCredential: true,
|
2023-01-15 11:52:53 +00:00
|
|
|
requireRolePolicy: 'canManageCustomEmojis',
|
2023-12-27 06:08:59 +00:00
|
|
|
kind: 'write:admin:emoji',
|
2018-11-03 18:18:32 +00:00
|
|
|
|
2019-10-15 19:03:18 +00:00
|
|
|
errors: {
|
|
|
|
noSuchEmoji: {
|
|
|
|
message: 'No such emoji.',
|
|
|
|
code: 'NO_SUCH_EMOJI',
|
2021-12-09 14:58:30 +00:00
|
|
|
id: '684dec9d-a8c2-4364-9aa8-456c49cb1dc8',
|
|
|
|
},
|
2023-05-18 09:45:49 +00:00
|
|
|
noSuchFile: {
|
|
|
|
message: 'No such file.',
|
|
|
|
code: 'NO_SUCH_FILE',
|
|
|
|
id: '14fb9fd9-0731-4e2f-aeb9-f09e4740333d',
|
|
|
|
},
|
2023-03-22 09:45:55 +00:00
|
|
|
sameNameEmojiExists: {
|
|
|
|
message: 'Emoji that have same name already exists.',
|
|
|
|
code: 'SAME_NAME_EMOJI_EXISTS',
|
2023-03-20 05:24:18 +00:00
|
|
|
id: '7180fe9d-1ee3-bff9-647d-fe9896d2ffb8',
|
|
|
|
},
|
2021-12-09 14:58:30 +00:00
|
|
|
},
|
2022-01-18 13:27:10 +00:00
|
|
|
} as const;
|
2018-11-03 18:18:32 +00:00
|
|
|
|
2022-02-20 04:15:40 +00:00
|
|
|
export const paramDef = {
|
2022-02-19 05:05:32 +00:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
id: { type: 'string', format: 'misskey:id' },
|
2023-03-20 05:24:18 +00:00
|
|
|
name: { type: 'string', pattern: '^[a-zA-Z0-9_]+$' },
|
2023-05-18 09:45:49 +00:00
|
|
|
fileId: { type: 'string', format: 'misskey:id' },
|
2022-04-03 04:57:26 +00:00
|
|
|
category: {
|
|
|
|
type: 'string',
|
|
|
|
nullable: true,
|
|
|
|
description: 'Use `null` to reset the category.',
|
|
|
|
},
|
2022-02-19 05:05:32 +00:00
|
|
|
aliases: { type: 'array', items: {
|
|
|
|
type: 'string',
|
|
|
|
} },
|
2023-03-16 06:08:48 +00:00
|
|
|
license: { type: 'string', nullable: true },
|
2023-05-18 09:45:49 +00:00
|
|
|
isSensitive: { type: 'boolean' },
|
|
|
|
localOnly: { type: 'boolean' },
|
|
|
|
roleIdsThatCanBeUsedThisEmojiAsReaction: { type: 'array', items: {
|
|
|
|
type: 'string',
|
|
|
|
} },
|
2022-02-19 05:05:32 +00:00
|
|
|
},
|
2024-03-02 16:36:49 +00:00
|
|
|
anyOf: [
|
|
|
|
{ required: ['id'] },
|
|
|
|
{ required: ['name'] },
|
|
|
|
],
|
2022-02-19 05:05:32 +00:00
|
|
|
} as const;
|
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
@Injectable()
|
2023-08-17 12:20:58 +00:00
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
|
2022-09-17 18:27:08 +00:00
|
|
|
constructor(
|
2023-05-18 09:45:49 +00:00
|
|
|
@Inject(DI.driveFilesRepository)
|
|
|
|
private driveFilesRepository: DriveFilesRepository,
|
|
|
|
|
2023-04-06 02:14:43 +00:00
|
|
|
private customEmojiService: CustomEmojiService,
|
2022-09-17 18:27:08 +00:00
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
2023-05-18 09:45:49 +00:00
|
|
|
let driveFile;
|
|
|
|
if (ps.fileId) {
|
|
|
|
driveFile = await this.driveFilesRepository.findOneBy({ id: ps.fileId });
|
|
|
|
if (driveFile == null) throw new ApiError(meta.errors.noSuchFile);
|
|
|
|
}
|
2024-03-02 16:36:49 +00:00
|
|
|
|
|
|
|
let emojiId;
|
|
|
|
if (ps.id) {
|
|
|
|
emojiId = ps.id;
|
|
|
|
const emoji = await this.customEmojiService.getEmojiById(ps.id);
|
|
|
|
if (!emoji) throw new ApiError(meta.errors.noSuchEmoji);
|
|
|
|
if (ps.name && (ps.name !== emoji.name)) {
|
2023-10-06 12:05:44 +00:00
|
|
|
const isDuplicate = await this.customEmojiService.checkDuplicate(ps.name);
|
|
|
|
if (isDuplicate) throw new ApiError(meta.errors.sameNameEmojiExists);
|
|
|
|
}
|
|
|
|
} else {
|
2024-03-02 16:36:49 +00:00
|
|
|
if (!ps.name) throw new Error('Invalid Params unexpectedly passed. This is a BUG. Please report it to the development team.');
|
|
|
|
const emoji = await this.customEmojiService.getEmojiByName(ps.name);
|
|
|
|
if (!emoji) throw new ApiError(meta.errors.noSuchEmoji);
|
|
|
|
emojiId = emoji.id;
|
2023-10-06 12:05:44 +00:00
|
|
|
}
|
2023-07-07 22:08:16 +00:00
|
|
|
|
2024-03-02 16:36:49 +00:00
|
|
|
await this.customEmojiService.update(emojiId, {
|
2023-05-18 09:45:49 +00:00
|
|
|
driveFile,
|
2022-09-17 18:27:08 +00:00
|
|
|
name: ps.name,
|
2024-03-02 16:36:49 +00:00
|
|
|
category: ps.category,
|
2022-09-17 18:27:08 +00:00
|
|
|
aliases: ps.aliases,
|
2024-03-02 16:36:49 +00:00
|
|
|
license: ps.license,
|
2023-05-18 09:45:49 +00:00
|
|
|
isSensitive: ps.isSensitive,
|
|
|
|
localOnly: ps.localOnly,
|
|
|
|
roleIdsThatCanBeUsedThisEmojiAsReaction: ps.roleIdsThatCanBeUsedThisEmojiAsReaction,
|
2023-09-24 01:57:24 +00:00
|
|
|
}, me);
|
2022-09-17 18:27:08 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|