Improve custom emoji managemant

This commit is contained in:
syuilo
2020-02-14 01:09:39 +09:00
parent 5ac4c48ad1
commit f5f7654f4b
5 changed files with 86 additions and 81 deletions

View File

@ -1,11 +1,12 @@
import $ from 'cafy';
import define from '../../../define';
import { detectUrlMime } from '../../../../../misc/detect-url-mime';
import { Emojis } from '../../../../../models';
import { Emojis, DriveFiles } from '../../../../../models';
import { genId } from '../../../../../misc/gen-id';
import { getConnection } from 'typeorm';
import { insertModerationLog } from '../../../../../services/insert-moderation-log';
import { ApiError } from '../../../error';
import { ID } from '../../../../../misc/cafy-id';
import rndstr from 'rndstr';
export const meta = {
desc: {
@ -18,52 +19,36 @@ export const meta = {
requireModerator: true,
params: {
name: {
validator: $.str.min(1)
fileId: {
validator: $.type(ID)
},
url: {
validator: $.str.min(1)
},
category: {
validator: $.optional.str
},
aliases: {
validator: $.optional.arr($.str.min(1)),
default: [] as string[]
}
},
errors: {
emojiAlredyExists: {
message: 'Emoji already exists.',
code: 'EMOJI_ALREADY_EXISTS',
noSuchFile: {
message: 'No such file.',
code: 'MO_SUCH_FILE',
id: 'fc46b5a4-6b92-4c33-ac66-b806659bb5cf'
}
}
};
export default define(meta, async (ps, me) => {
const type = await detectUrlMime(ps.url);
const file = await DriveFiles.findOne(ps.fileId);
const exists = await Emojis.findOne({
name: ps.name,
host: null
});
if (file == null) throw new ApiError(meta.errors.noSuchFile);
if (exists != null) throw new ApiError(meta.errors.emojiAlredyExists);
const name = file.name.split('.')[0].match(/^[a-z0-9_]+$/) ? file.name.split('.')[0] : `_${rndstr('a-z0-9', 8)}_`;
const emoji = await Emojis.save({
id: genId(),
updatedAt: new Date(),
name: ps.name,
category: ps.category,
name: name,
category: null,
host: null,
aliases: ps.aliases,
url: ps.url,
type,
aliases: [],
url: file.url,
type: file.type,
});
await getConnection().queryResultCache!.remove(['meta_emojis']);

View File

@ -1,6 +1,5 @@
import $ from 'cafy';
import define from '../../../define';
import { detectUrlMime } from '../../../../../misc/detect-url-mime';
import { ID } from '../../../../../misc/cafy-id';
import { Emojis } from '../../../../../models';
import { getConnection } from 'typeorm';
@ -29,10 +28,6 @@ export const meta = {
validator: $.optional.str
},
url: {
validator: $.str
},
aliases: {
validator: $.arr($.str)
}
@ -52,15 +47,11 @@ export default define(meta, async (ps) => {
if (emoji == null) throw new ApiError(meta.errors.noSuchEmoji);
const type = await detectUrlMime(ps.url);
await Emojis.update(emoji.id, {
updatedAt: new Date(),
name: ps.name,
category: ps.category,
aliases: ps.aliases,
url: ps.url,
type,
});
await getConnection().queryResultCache!.remove(['meta_emojis']);