From 3487ddabea925c810cd712c8681273c3818a2482 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 4 Nov 2018 03:18:32 +0900 Subject: [PATCH] [API] Implement some Emoji APIs --- src/models/emoji.ts | 2 + .../admin/{add-emoji.ts => emoji/add.ts} | 4 +- src/server/api/endpoints/admin/emoji/list.ts | 33 +++++++++++++ .../api/endpoints/admin/emoji/remove.ts | 31 ++++++++++++ .../api/endpoints/admin/emoji/update.ts | 49 +++++++++++++++++++ 5 files changed, 117 insertions(+), 2 deletions(-) rename src/server/api/endpoints/admin/{add-emoji.ts => emoji/add.ts} (85%) create mode 100644 src/server/api/endpoints/admin/emoji/list.ts create mode 100644 src/server/api/endpoints/admin/emoji/remove.ts create mode 100644 src/server/api/endpoints/admin/emoji/update.ts diff --git a/src/models/emoji.ts b/src/models/emoji.ts index 62fee3726..fdedd2851 100644 --- a/src/models/emoji.ts +++ b/src/models/emoji.ts @@ -1,3 +1,4 @@ +import * as mongo from 'mongodb'; import db from '../db/mongodb'; const Emoji = db.get('emoji'); @@ -8,6 +9,7 @@ Emoji.createIndex(['name', 'host'], { unique: true }); export default Emoji; export type IEmoji = { + _id: mongo.ObjectID; name: string; host: string; url: string; diff --git a/src/server/api/endpoints/admin/add-emoji.ts b/src/server/api/endpoints/admin/emoji/add.ts similarity index 85% rename from src/server/api/endpoints/admin/add-emoji.ts rename to src/server/api/endpoints/admin/emoji/add.ts index 9ae258d06..09e142b17 100644 --- a/src/server/api/endpoints/admin/add-emoji.ts +++ b/src/server/api/endpoints/admin/emoji/add.ts @@ -1,6 +1,6 @@ import $ from 'cafy'; -import Emoji from '../../../../models/emoji'; -import define from '../../define'; +import Emoji from '../../../../../models/emoji'; +import define from '../../../define'; export const meta = { desc: { diff --git a/src/server/api/endpoints/admin/emoji/list.ts b/src/server/api/endpoints/admin/emoji/list.ts new file mode 100644 index 000000000..fd69fb0b2 --- /dev/null +++ b/src/server/api/endpoints/admin/emoji/list.ts @@ -0,0 +1,33 @@ +import $ from 'cafy'; +import Emoji from '../../../../../models/emoji'; +import define from '../../../define'; + +export const meta = { + desc: { + 'ja-JP': 'カスタム絵文字を取得します。' + }, + + requireCredential: true, + requireAdmin: true, + + params: { + host: { + validator: $.str.optional.nullable, + default: null as any + } + } +}; + +export default define(meta, (ps) => new Promise(async (res, rej) => { + const emojis = await Emoji.find({ + host: ps.host + }); + + res(emojis.map(e => ({ + id: e._id, + name: e.name, + aliases: e.aliases, + host: e.host, + url: e.url + }))); +})); diff --git a/src/server/api/endpoints/admin/emoji/remove.ts b/src/server/api/endpoints/admin/emoji/remove.ts new file mode 100644 index 000000000..32f1ced0c --- /dev/null +++ b/src/server/api/endpoints/admin/emoji/remove.ts @@ -0,0 +1,31 @@ +import $ from 'cafy'; +import Emoji from '../../../../../models/emoji'; +import define from '../../../define'; +import ID from '../../../../../misc/cafy-id'; + +export const meta = { + desc: { + 'ja-JP': 'カスタム絵文字を削除します。' + }, + + requireCredential: true, + requireAdmin: true, + + params: { + id: { + validator: $.type(ID) + } + } +}; + +export default define(meta, (ps) => new Promise(async (res, rej) => { + const emoji = await Emoji.findOne({ + _id: ps.id + }); + + if (emoji == null) return rej('emoji not found'); + + await Emoji.remove({ _id: emoji._id }); + + res(); +})); diff --git a/src/server/api/endpoints/admin/emoji/update.ts b/src/server/api/endpoints/admin/emoji/update.ts new file mode 100644 index 000000000..842ca8924 --- /dev/null +++ b/src/server/api/endpoints/admin/emoji/update.ts @@ -0,0 +1,49 @@ +import $ from 'cafy'; +import Emoji from '../../../../../models/emoji'; +import define from '../../../define'; +import ID from '../../../../../misc/cafy-id'; + +export const meta = { + desc: { + 'ja-JP': 'カスタム絵文字を更新します。' + }, + + requireCredential: true, + requireAdmin: true, + + params: { + id: { + validator: $.type(ID) + }, + + name: { + validator: $.str + }, + + url: { + validator: $.str + }, + + aliases: { + validator: $.arr($.str) + } + } +}; + +export default define(meta, (ps) => new Promise(async (res, rej) => { + const emoji = await Emoji.findOne({ + _id: ps.id + }); + + if (emoji == null) return rej('emoji not found'); + + await Emoji.update({ _id: emoji._id }, { + $set: { + name: ps.name, + aliases: ps.aliases, + url: ps.url + } + }); + + res(); +}));