2022-09-17 20:27:08 +02:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2020-02-13 17:09:39 +01:00
|
|
|
import rndstr from 'rndstr';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { DataSource } from 'typeorm';
|
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
|
|
import { DriveFilesRepository, EmojisRepository } from '@/models/index.js';
|
|
|
|
import { IdService } from '@/core/IdService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
|
|
|
import { GlobalEventService } from '@/core/GlobalEventService.js';
|
|
|
|
import { ModerationLogService } from '@/core/ModerationLogService.js';
|
|
|
|
import { EmojiEntityService } from '@/core/entities/EmojiEntityService.js';
|
|
|
|
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,
|
2018-11-14 20:15:42 +01:00
|
|
|
requireModerator: true,
|
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.',
|
|
|
|
code: 'MO_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: {
|
|
|
|
fileId: { type: 'string', format: 'misskey:id' },
|
|
|
|
},
|
|
|
|
required: ['fileId'],
|
|
|
|
} as const;
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
// TODO: ロジックをサービスに切り出す
|
|
|
|
|
2022-01-02 18:12:50 +01:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-09-17 20:27:08 +02:00
|
|
|
@Injectable()
|
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.db)
|
|
|
|
private db: DataSource,
|
|
|
|
|
|
|
|
@Inject(DI.driveFilesRepository)
|
|
|
|
private driveFilesRepository: DriveFilesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.emojisRepository)
|
|
|
|
private emojisRepository: EmojisRepository,
|
|
|
|
|
|
|
|
private emojiEntityService: EmojiEntityService,
|
|
|
|
private idService: IdService,
|
|
|
|
private globalEventService: GlobalEventService,
|
|
|
|
private moderationLogService: ModerationLogService,
|
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
|
|
|
const file = await this.driveFilesRepository.findOneBy({ id: ps.fileId });
|
|
|
|
|
|
|
|
if (file == null) throw new ApiError(meta.errors.noSuchFile);
|
|
|
|
|
|
|
|
const name = file.name.split('.')[0].match(/^[a-z0-9_]+$/) ? file.name.split('.')[0] : `_${rndstr('a-z0-9', 8)}_`;
|
|
|
|
|
|
|
|
const emoji = await this.emojisRepository.insert({
|
|
|
|
id: this.idService.genId(),
|
|
|
|
updatedAt: new Date(),
|
|
|
|
name: name,
|
|
|
|
category: null,
|
|
|
|
host: null,
|
|
|
|
aliases: [],
|
|
|
|
originalUrl: file.url,
|
|
|
|
publicUrl: file.webpublicUrl ?? file.url,
|
|
|
|
type: file.webpublicType ?? file.type,
|
|
|
|
}).then(x => this.emojisRepository.findOneByOrFail(x.identifiers[0]));
|
|
|
|
|
|
|
|
await this.db.queryResultCache!.remove(['meta_emojis']);
|
|
|
|
|
|
|
|
this.globalEventService.publishBroadcastStream('emojiAdded', {
|
|
|
|
emoji: await this.emojiEntityService.pack(emoji.id),
|
|
|
|
});
|
|
|
|
|
|
|
|
this.moderationLogService.insertModerationLog(me, 'addEmoji', {
|
|
|
|
emojiId: emoji.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: emoji.id,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|