From 174f8022eb5828db3e9af01afcf72fe7526c4c57 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 2 Nov 2018 13:47:44 +0900 Subject: [PATCH] Refactor --- src/server/api/call.ts | 8 +-- src/server/api/define.ts | 51 +++++++++++++++++++ src/server/api/endpoints/admin/invite.ts | 5 +- .../api/endpoints/admin/suspend-user.ts | 9 ++-- .../api/endpoints/admin/unsuspend-user.ts | 9 ++-- .../api/endpoints/admin/unverify-user.ts | 9 ++-- src/server/api/endpoints/admin/update-meta.ts | 9 ++-- src/server/api/endpoints/admin/verify-user.ts | 9 ++-- src/server/api/endpoints/ap/show.ts | 13 ++--- src/server/api/endpoints/app/create.ts | 10 ++-- src/server/api/endpoints/app/show.ts | 12 ++--- src/server/api/endpoints/auth/accept.ts | 10 ++-- .../api/endpoints/auth/session/generate.ts | 9 ++-- src/server/api/endpoints/auth/session/show.ts | 10 ++-- .../api/endpoints/auth/session/userkey.ts | 9 ++-- src/server/api/endpoints/blocking/create.ts | 11 ++-- src/server/api/endpoints/blocking/delete.ts | 11 ++-- src/server/api/endpoints/blocking/list.ts | 10 ++-- src/server/api/endpoints/charts/drive.ts | 9 ++-- src/server/api/endpoints/charts/federation.ts | 9 ++-- src/server/api/endpoints/charts/hashtag.ts | 9 ++-- src/server/api/endpoints/charts/network.ts | 9 ++-- src/server/api/endpoints/charts/notes.ts | 9 ++-- src/server/api/endpoints/charts/user/drive.ts | 9 ++-- .../api/endpoints/charts/user/following.ts | 9 ++-- src/server/api/endpoints/charts/user/notes.ts | 9 ++-- .../api/endpoints/charts/user/reactions.ts | 9 ++-- src/server/api/endpoints/charts/users.ts | 9 ++-- src/server/api/endpoints/drive.ts | 6 +-- src/server/api/endpoints/drive/files.ts | 14 ++--- .../endpoints/drive/files/attached_notes.ts | 10 ++-- .../endpoints/drive/files/check_existence.ts | 10 ++-- .../api/endpoints/drive/files/create.ts | 29 +++-------- .../api/endpoints/drive/files/delete.ts | 10 ++-- src/server/api/endpoints/drive/files/find.ts | 10 ++-- src/server/api/endpoints/drive/files/show.ts | 10 ++-- .../api/endpoints/drive/files/update.ts | 10 ++-- .../endpoints/drive/files/upload_from_url.ts | 11 ++-- src/server/api/endpoints/drive/folders.ts | 10 ++-- .../api/endpoints/drive/folders/create.ts | 10 ++-- .../api/endpoints/drive/folders/delete.ts | 10 ++-- .../api/endpoints/drive/folders/find.ts | 10 ++-- .../api/endpoints/drive/folders/show.ts | 10 ++-- .../api/endpoints/drive/folders/update.ts | 10 ++-- src/server/api/endpoints/drive/stream.ts | 10 ++-- src/server/api/endpoints/following/create.ts | 11 ++-- src/server/api/endpoints/following/delete.ts | 11 ++-- .../endpoints/following/requests/accept.ts | 11 ++-- .../endpoints/following/requests/cancel.ts | 11 ++-- .../api/endpoints/following/requests/list.ts | 6 +-- .../endpoints/following/requests/reject.ts | 11 ++-- src/server/api/endpoints/following/stalk.ts | 10 ++-- src/server/api/endpoints/following/unstalk.ts | 10 ++-- .../api/endpoints/games/reversi/games.ts | 10 ++-- .../api/endpoints/games/reversi/games/show.ts | 10 ++-- .../games/reversi/games/surrender.ts | 10 ++-- .../endpoints/games/reversi/invitations.ts | 6 +-- .../api/endpoints/games/reversi/match.ts | 11 ++-- .../endpoints/games/reversi/match/cancel.ts | 6 +-- src/server/api/endpoints/hashtags/search.ts | 9 ++-- src/server/api/endpoints/hashtags/trend.ts | 12 +++-- src/server/api/endpoints/i.ts | 8 +-- src/server/api/endpoints/i/2fa/done.ts | 11 ++-- src/server/api/endpoints/i/2fa/register.ts | 11 ++-- src/server/api/endpoints/i/2fa/unregister.ts | 11 ++-- src/server/api/endpoints/i/authorized_apps.ts | 10 ++-- src/server/api/endpoints/i/change_password.ts | 11 ++-- src/server/api/endpoints/i/favorites.ts | 10 ++-- src/server/api/endpoints/i/notifications.ts | 10 ++-- src/server/api/endpoints/i/pin.ts | 10 ++-- .../api/endpoints/i/read_all_unread_notes.ts | 7 +-- .../api/endpoints/i/regenerate_token.ts | 11 ++-- src/server/api/endpoints/i/signin_history.ts | 10 ++-- src/server/api/endpoints/i/unpin.ts | 10 ++-- src/server/api/endpoints/i/update.ts | 9 ++-- .../api/endpoints/i/update_client_setting.ts | 11 ++-- src/server/api/endpoints/i/update_home.ts | 11 ++-- .../api/endpoints/i/update_mobile_home.ts | 11 ++-- src/server/api/endpoints/i/update_widget.ts | 11 ++-- src/server/api/endpoints/messaging/history.ts | 10 ++-- .../api/endpoints/messaging/messages.ts | 11 ++-- .../endpoints/messaging/messages/create.ts | 11 ++-- .../api/endpoints/messaging/messages/read.ts | 10 ++-- src/server/api/endpoints/meta.ts | 10 ++-- src/server/api/endpoints/mute/create.ts | 11 ++-- src/server/api/endpoints/mute/delete.ts | 11 ++-- src/server/api/endpoints/mute/list.ts | 10 ++-- src/server/api/endpoints/my/apps.ts | 10 ++-- src/server/api/endpoints/notes.ts | 9 ++-- .../api/endpoints/notes/conversation.ts | 10 ++-- src/server/api/endpoints/notes/create.ts | 12 ++--- src/server/api/endpoints/notes/delete.ts | 11 ++-- .../api/endpoints/notes/favorites/create.ts | 10 ++-- .../api/endpoints/notes/favorites/delete.ts | 10 ++-- src/server/api/endpoints/notes/featured.ts | 12 ++--- .../api/endpoints/notes/global-timeline.ts | 16 ++---- .../api/endpoints/notes/hybrid-timeline.ts | 16 ++---- .../api/endpoints/notes/local-timeline.ts | 16 ++---- src/server/api/endpoints/notes/mentions.ts | 10 ++-- .../endpoints/notes/polls/recommendation.ts | 10 ++-- src/server/api/endpoints/notes/polls/vote.ts | 10 ++-- src/server/api/endpoints/notes/reactions.ts | 10 ++-- .../api/endpoints/notes/reactions/create.ts | 10 ++-- .../api/endpoints/notes/reactions/delete.ts | 10 ++-- src/server/api/endpoints/notes/renotes.ts | 10 ++-- src/server/api/endpoints/notes/replies.ts | 10 ++-- src/server/api/endpoints/notes/search.ts | 10 ++-- .../api/endpoints/notes/search_by_tag.ts | 10 ++-- src/server/api/endpoints/notes/show.ts | 10 ++-- src/server/api/endpoints/notes/timeline.ts | 15 +++--- .../api/endpoints/notes/user-list-timeline.ts | 15 +++--- .../notifications/mark_all_as_read.ts | 10 ++-- src/server/api/endpoints/stats.ts | 19 +++++-- src/server/api/endpoints/sw/register.ts | 10 ++-- .../api/endpoints/username/available.ts | 9 ++-- src/server/api/endpoints/users.ts | 9 ++-- src/server/api/endpoints/users/followers.ts | 11 ++-- src/server/api/endpoints/users/following.ts | 11 ++-- .../users/get_frequently_replied_users.ts | 11 ++-- .../api/endpoints/users/lists/create.ts | 10 ++-- .../api/endpoints/users/lists/delete.ts | 10 ++-- src/server/api/endpoints/users/lists/list.ts | 6 +-- src/server/api/endpoints/users/lists/push.ts | 11 ++-- src/server/api/endpoints/users/lists/show.ts | 10 ++-- .../api/endpoints/users/lists/update.ts | 10 ++-- src/server/api/endpoints/users/notes.ts | 11 ++-- .../api/endpoints/users/recommendation.ts | 19 +++---- src/server/api/endpoints/users/relation.ts | 11 ++-- src/server/api/endpoints/users/search.ts | 14 ++--- src/server/api/endpoints/users/show.ts | 11 ++-- src/server/api/get-params.ts | 28 ---------- 131 files changed, 512 insertions(+), 917 deletions(-) create mode 100644 src/server/api/define.ts delete mode 100644 src/server/api/get-params.ts diff --git a/src/server/api/call.ts b/src/server/api/call.ts index b87707c348..673d449199 100644 --- a/src/server/api/call.ts +++ b/src/server/api/call.ts @@ -42,18 +42,12 @@ export default (endpoint: string, user: IUser, app: IApp, data: any, file?: any) } } - let exec = ep.exec; - - if (ep.meta.requireFile && file) { - exec = exec.bind(null, file); - } - let res; // API invoking try { const before = performance.now(); - res = await exec(data, user, app); + res = await ep.exec(data, user, app, file); const after = performance.now(); const time = after - before; diff --git a/src/server/api/define.ts b/src/server/api/define.ts new file mode 100644 index 0000000000..c66ade5fda --- /dev/null +++ b/src/server/api/define.ts @@ -0,0 +1,51 @@ +import * as fs from 'fs'; +import { ILocalUser } from '../../models/user'; +import { IApp } from '../../models/app'; +import { IEndpointMeta } from './endpoints'; + +type Params = { + [P in keyof T['params']]: T['params'][P]['transform'] extends Function + ? ReturnType + : ReturnType[0]; +}; + +export default function (meta: T, cb: (params: Params, user: ILocalUser, app: IApp, file?: any, cleanup?: Function) => Promise): (params: any, user: ILocalUser, app: IApp, file?: any) => Promise { + return (params: any, user: ILocalUser, app: IApp, file?: any) => { + function cleanup() { + fs.unlink(file.path, () => {}); + } + + if (meta.requireFile && file == null) return Promise.reject('file required'); + + const [ps, pserr] = getParams(meta, params); + if (pserr) { + if (file) cleanup(); + return Promise.reject(pserr); + } + + return cb(ps, user, app, file, cleanup); + }; +} + +function getParams(defs: T, params: any): [Params, Error] { + const x: any = {}; + let err: Error = null; + Object.entries(defs.params).some(([k, def]) => { + const [v, e] = def.validator.get(params[k]); + if (e) { + err = new Error(e.message); + err.name = 'INVALID_PARAM'; + (err as any).param = k; + return true; + } else { + if (v === undefined && def.default) { + x[k] = def.default; + } else { + x[k] = v; + } + if (def.transform) x[k] = def.transform(x[k]); + return false; + } + }); + return [x, err]; +} diff --git a/src/server/api/endpoints/admin/invite.ts b/src/server/api/endpoints/admin/invite.ts index 892b2579f2..056cb8aa75 100644 --- a/src/server/api/endpoints/admin/invite.ts +++ b/src/server/api/endpoints/admin/invite.ts @@ -1,5 +1,6 @@ import rndstr from 'rndstr'; import RegistrationTicket from '../../../../models/registration-tickets'; +import define from '../../define'; export const meta = { desc: { @@ -12,7 +13,7 @@ export const meta = { params: {} }; -export default (params: any) => new Promise(async (res, rej) => { +export default define(meta, (ps) => new Promise(async (res, rej) => { const code = rndstr({ length: 5, chars: '0-9' }); await RegistrationTicket.insert({ @@ -23,4 +24,4 @@ export default (params: any) => new Promise(async (res, rej) => { res({ code: code }); -}); +})); diff --git a/src/server/api/endpoints/admin/suspend-user.ts b/src/server/api/endpoints/admin/suspend-user.ts index 6d8b28932f..0ad0aab74c 100644 --- a/src/server/api/endpoints/admin/suspend-user.ts +++ b/src/server/api/endpoints/admin/suspend-user.ts @@ -1,6 +1,6 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; -import getParams from '../../get-params'; +import define from '../../define'; import User from '../../../../models/user'; export const meta = { @@ -24,10 +24,7 @@ export const meta = { } }; -export default (params: any) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps) => new Promise(async (res, rej) => { const user = await User.findOne({ _id: ps.userId }); @@ -49,4 +46,4 @@ export default (params: any) => new Promise(async (res, rej) => { }); res(); -}); +})); diff --git a/src/server/api/endpoints/admin/unsuspend-user.ts b/src/server/api/endpoints/admin/unsuspend-user.ts index 5c736122bb..7c5eedee46 100644 --- a/src/server/api/endpoints/admin/unsuspend-user.ts +++ b/src/server/api/endpoints/admin/unsuspend-user.ts @@ -1,6 +1,6 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; -import getParams from '../../get-params'; +import define from '../../define'; import User from '../../../../models/user'; export const meta = { @@ -24,10 +24,7 @@ export const meta = { } }; -export default (params: any) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps) => new Promise(async (res, rej) => { const user = await User.findOne({ _id: ps.userId }); @@ -45,4 +42,4 @@ export default (params: any) => new Promise(async (res, rej) => { }); res(); -}); +})); diff --git a/src/server/api/endpoints/admin/unverify-user.ts b/src/server/api/endpoints/admin/unverify-user.ts index fc55bd7476..d749e002e3 100644 --- a/src/server/api/endpoints/admin/unverify-user.ts +++ b/src/server/api/endpoints/admin/unverify-user.ts @@ -1,6 +1,6 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; -import getParams from '../../get-params'; +import define from '../../define'; import User from '../../../../models/user'; export const meta = { @@ -24,10 +24,7 @@ export const meta = { } }; -export default (params: any) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps) => new Promise(async (res, rej) => { const user = await User.findOne({ _id: ps.userId }); @@ -45,4 +42,4 @@ export default (params: any) => new Promise(async (res, rej) => { }); res(); -}); +})); diff --git a/src/server/api/endpoints/admin/update-meta.ts b/src/server/api/endpoints/admin/update-meta.ts index aed53e12ce..c2bb107f87 100644 --- a/src/server/api/endpoints/admin/update-meta.ts +++ b/src/server/api/endpoints/admin/update-meta.ts @@ -1,6 +1,6 @@ import $ from 'cafy'; import Meta from '../../../../models/meta'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { desc: { @@ -55,10 +55,7 @@ export const meta = { } }; -export default (params: any) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps) => new Promise(async (res, rej) => { const set = {} as any; if (ps.broadcasts) { @@ -90,4 +87,4 @@ export default (params: any) => new Promise(async (res, rej) => { }, { upsert: true }); res(); -}); +})); diff --git a/src/server/api/endpoints/admin/verify-user.ts b/src/server/api/endpoints/admin/verify-user.ts index b8c0bbaa8d..09efc2e803 100644 --- a/src/server/api/endpoints/admin/verify-user.ts +++ b/src/server/api/endpoints/admin/verify-user.ts @@ -1,6 +1,6 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; -import getParams from '../../get-params'; +import define from '../../define'; import User from '../../../../models/user'; export const meta = { @@ -24,10 +24,7 @@ export const meta = { } }; -export default (params: any) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps) => new Promise(async (res, rej) => { const user = await User.findOne({ _id: ps.userId }); @@ -45,4 +42,4 @@ export default (params: any) => new Promise(async (res, rej) => { }); res(); -}); +})); diff --git a/src/server/api/endpoints/ap/show.ts b/src/server/api/endpoints/ap/show.ts index c5286583d0..778d1e5099 100644 --- a/src/server/api/endpoints/ap/show.ts +++ b/src/server/api/endpoints/ap/show.ts @@ -1,5 +1,5 @@ import $ from 'cafy'; -import getParams from '../../get-params'; +import define from '../../define'; import config from '../../../../config'; import * as mongo from 'mongodb'; import User, { pack as packUser, IUser } from '../../../../models/user'; @@ -25,15 +25,12 @@ export const meta = { }, }; -export default async (params: any) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) throw psErr; - +export default define(meta, (ps) => new Promise(async (res, rej) => { const object = await fetchAny(ps.uri); - if (object !== null) return object; + if (object == null) return rej('object not found'); - throw new Error('object not found'); -}; + res(object); +})); /*** * URIからUserかNoteを解決する diff --git a/src/server/api/endpoints/app/create.ts b/src/server/api/endpoints/app/create.ts index 40da5144f6..af66bb9d5a 100644 --- a/src/server/api/endpoints/app/create.ts +++ b/src/server/api/endpoints/app/create.ts @@ -1,8 +1,7 @@ import rndstr from 'rndstr'; import $ from 'cafy'; import App, { pack } from '../../../../models/app'; -import { ILocalUser } from '../../../../models/user'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { requireCredential: false, @@ -28,10 +27,7 @@ export const meta = { } }; -export default async (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Generate secret const secret = rndstr('a-zA-Z0-9', 32); @@ -51,4 +47,4 @@ export default async (params: any, user: ILocalUser) => new Promise(async (res, detail: true, includeSecret: true })); -}); +})); diff --git a/src/server/api/endpoints/app/show.ts b/src/server/api/endpoints/app/show.ts index 0d73985b48..21e6350113 100644 --- a/src/server/api/endpoints/app/show.ts +++ b/src/server/api/endpoints/app/show.ts @@ -1,7 +1,6 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; -import App, { pack, IApp } from '../../../../models/app'; -import { ILocalUser } from '../../../../models/user'; -import getParams from '../../get-params'; +import App, { pack } from '../../../../models/app'; +import define from '../../define'; export const meta = { params: { @@ -12,10 +11,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser, app: IApp) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user, app) => new Promise(async (res, rej) => { const isSecure = user != null && app == null; // Lookup app @@ -30,4 +26,4 @@ export default (params: any, user: ILocalUser, app: IApp) => new Promise(async ( detail: true, includeSecret: isSecure && ap.userId.equals(user._id) })); -}); +})); diff --git a/src/server/api/endpoints/auth/accept.ts b/src/server/api/endpoints/auth/accept.ts index ed1c2fa390..b7e11bd463 100644 --- a/src/server/api/endpoints/auth/accept.ts +++ b/src/server/api/endpoints/auth/accept.ts @@ -4,8 +4,7 @@ import $ from 'cafy'; import App from '../../../../models/app'; import AuthSess from '../../../../models/auth-session'; import AccessToken from '../../../../models/access-token'; -import { ILocalUser } from '../../../../models/user'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { requireCredential: true, @@ -19,10 +18,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Fetch token const session = await AuthSess .findOne({ token: ps.token }); @@ -70,4 +66,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = // Response res(); -}); +})); diff --git a/src/server/api/endpoints/auth/session/generate.ts b/src/server/api/endpoints/auth/session/generate.ts index 79eb34d96a..5ef3404b73 100644 --- a/src/server/api/endpoints/auth/session/generate.ts +++ b/src/server/api/endpoints/auth/session/generate.ts @@ -3,7 +3,7 @@ import $ from 'cafy'; import App from '../../../../../models/app'; import AuthSess from '../../../../../models/auth-session'; import config from '../../../../../config'; -import getParams from '../../../get-params'; +import define from '../../../define'; export const meta = { requireCredential: false, @@ -15,10 +15,7 @@ export const meta = { } }; -export default (params: any) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps) => new Promise(async (res, rej) => { // Lookup app const app = await App.findOne({ secret: ps.appSecret @@ -43,4 +40,4 @@ export default (params: any) => new Promise(async (res, rej) => { token: doc.token, url: `${config.auth_url}/${doc.token}` }); -}); +})); diff --git a/src/server/api/endpoints/auth/session/show.ts b/src/server/api/endpoints/auth/session/show.ts index 9e7f4f52d4..cc63c43ad3 100644 --- a/src/server/api/endpoints/auth/session/show.ts +++ b/src/server/api/endpoints/auth/session/show.ts @@ -1,7 +1,6 @@ import $ from 'cafy'; import AuthSess, { pack } from '../../../../../models/auth-session'; -import { ILocalUser } from '../../../../../models/user'; -import getParams from '../../../get-params'; +import define from '../../../define'; export const meta = { requireCredential: false, @@ -13,10 +12,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Lookup session const session = await AuthSess.findOne({ token: ps.token @@ -28,4 +24,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = // Response res(await pack(session, user)); -}); +})); diff --git a/src/server/api/endpoints/auth/session/userkey.ts b/src/server/api/endpoints/auth/session/userkey.ts index 50edf12abc..9f924d27a0 100644 --- a/src/server/api/endpoints/auth/session/userkey.ts +++ b/src/server/api/endpoints/auth/session/userkey.ts @@ -3,7 +3,7 @@ import App from '../../../../../models/app'; import AuthSess from '../../../../../models/auth-session'; import AccessToken from '../../../../../models/access-token'; import { pack } from '../../../../../models/user'; -import getParams from '../../../get-params'; +import define from '../../../define'; export const meta = { requireCredential: false, @@ -19,10 +19,7 @@ export const meta = { } }; -export default (params: any) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps) => new Promise(async (res, rej) => { // Lookup app const app = await App.findOne({ secret: ps.appSecret @@ -71,4 +68,4 @@ export default (params: any) => new Promise(async (res, rej) => { detail: true }) }); -}); +})); diff --git a/src/server/api/endpoints/blocking/create.ts b/src/server/api/endpoints/blocking/create.ts index 7ec01b4237..7166505309 100644 --- a/src/server/api/endpoints/blocking/create.ts +++ b/src/server/api/endpoints/blocking/create.ts @@ -1,9 +1,9 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; const ms = require('ms'); -import User, { pack, ILocalUser } from '../../../../models/user'; +import User, { pack } from '../../../../models/user'; import Blocking from '../../../../models/blocking'; import create from '../../../../services/blocking/create'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { stability: 'stable', @@ -34,10 +34,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { const blocker = user; // 自分自身 @@ -76,4 +73,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = res(await pack(blockee._id, user, { detail: true })); -}); +})); diff --git a/src/server/api/endpoints/blocking/delete.ts b/src/server/api/endpoints/blocking/delete.ts index adf8d8c500..667255a66b 100644 --- a/src/server/api/endpoints/blocking/delete.ts +++ b/src/server/api/endpoints/blocking/delete.ts @@ -1,9 +1,9 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; const ms = require('ms'); -import User, { pack, ILocalUser } from '../../../../models/user'; +import User, { pack } from '../../../../models/user'; import Blocking from '../../../../models/blocking'; import deleteBlocking from '../../../../services/blocking/delete'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { stability: 'stable', @@ -34,10 +34,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { const blocker = user; // Check if the blockee is yourself @@ -76,4 +73,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = res(await pack(blockee._id, user, { detail: true })); -}); +})); diff --git a/src/server/api/endpoints/blocking/list.ts b/src/server/api/endpoints/blocking/list.ts index 52f55805d0..e2649573d5 100644 --- a/src/server/api/endpoints/blocking/list.ts +++ b/src/server/api/endpoints/blocking/list.ts @@ -1,7 +1,6 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; import Blocking, { packMany } from '../../../../models/blocking'; -import { ILocalUser } from '../../../../models/user'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { desc: { @@ -31,10 +30,7 @@ export const meta = { } }; -export default (params: any, me: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, me) => new Promise(async (res, rej) => { // Check if both of sinceId and untilId is specified if (ps.sinceId && ps.untilId) { return rej('cannot set sinceId and untilId'); @@ -66,4 +62,4 @@ export default (params: any, me: ILocalUser) => new Promise(async (res, rej) => }); res(await packMany(blockings, me)); -}); +})); diff --git a/src/server/api/endpoints/charts/drive.ts b/src/server/api/endpoints/charts/drive.ts index 2200081330..ca0b6c53d2 100644 --- a/src/server/api/endpoints/charts/drive.ts +++ b/src/server/api/endpoints/charts/drive.ts @@ -1,5 +1,5 @@ import $ from 'cafy'; -import getParams from '../../get-params'; +import define from '../../define'; import driveChart from '../../../../chart/drive'; export const meta = { @@ -25,11 +25,8 @@ export const meta = { } }; -export default (params: any) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps) => new Promise(async (res, rej) => { const stats = await driveChart.getChart(ps.span as any, ps.limit); res(stats); -}); +})); diff --git a/src/server/api/endpoints/charts/federation.ts b/src/server/api/endpoints/charts/federation.ts index ba19aae04c..c774f0b891 100644 --- a/src/server/api/endpoints/charts/federation.ts +++ b/src/server/api/endpoints/charts/federation.ts @@ -1,5 +1,5 @@ import $ from 'cafy'; -import getParams from '../../get-params'; +import define from '../../define'; import federationChart from '../../../../chart/federation'; export const meta = { @@ -25,11 +25,8 @@ export const meta = { } }; -export default (params: any) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps) => new Promise(async (res, rej) => { const stats = await federationChart.getChart(ps.span as any, ps.limit); res(stats); -}); +})); diff --git a/src/server/api/endpoints/charts/hashtag.ts b/src/server/api/endpoints/charts/hashtag.ts index 2ef6a06062..322708ed74 100644 --- a/src/server/api/endpoints/charts/hashtag.ts +++ b/src/server/api/endpoints/charts/hashtag.ts @@ -1,5 +1,5 @@ import $ from 'cafy'; -import getParams from '../../get-params'; +import define from '../../define'; import hashtagChart from '../../../../chart/hashtag'; export const meta = { @@ -32,11 +32,8 @@ export const meta = { } }; -export default (params: any) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps) => new Promise(async (res, rej) => { const stats = await hashtagChart.getChart(ps.span as any, ps.limit, ps.tag); res(stats); -}); +})); diff --git a/src/server/api/endpoints/charts/network.ts b/src/server/api/endpoints/charts/network.ts index 249504f2e1..a34eab845c 100644 --- a/src/server/api/endpoints/charts/network.ts +++ b/src/server/api/endpoints/charts/network.ts @@ -1,5 +1,5 @@ import $ from 'cafy'; -import getParams from '../../get-params'; +import define from '../../define'; import networkChart from '../../../../chart/network'; export const meta = { @@ -25,11 +25,8 @@ export const meta = { } }; -export default (params: any) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps) => new Promise(async (res, rej) => { const stats = await networkChart.getChart(ps.span as any, ps.limit); res(stats); -}); +})); diff --git a/src/server/api/endpoints/charts/notes.ts b/src/server/api/endpoints/charts/notes.ts index 0f04a4bd90..1795c85c8b 100644 --- a/src/server/api/endpoints/charts/notes.ts +++ b/src/server/api/endpoints/charts/notes.ts @@ -1,5 +1,5 @@ import $ from 'cafy'; -import getParams from '../../get-params'; +import define from '../../define'; import notesChart from '../../../../chart/notes'; export const meta = { @@ -25,11 +25,8 @@ export const meta = { } }; -export default (params: any) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps) => new Promise(async (res, rej) => { const stats = await notesChart.getChart(ps.span as any, ps.limit); res(stats); -}); +})); diff --git a/src/server/api/endpoints/charts/user/drive.ts b/src/server/api/endpoints/charts/user/drive.ts index beb6b5bea3..934d38c44d 100644 --- a/src/server/api/endpoints/charts/user/drive.ts +++ b/src/server/api/endpoints/charts/user/drive.ts @@ -1,5 +1,5 @@ import $ from 'cafy'; -import getParams from '../../../get-params'; +import define from '../../../define'; import perUserDriveChart from '../../../../../chart/per-user-drive'; import ID, { transform } from '../../../../../misc/cafy-id'; @@ -35,11 +35,8 @@ export const meta = { } }; -export default (params: any) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps) => new Promise(async (res, rej) => { const stats = await perUserDriveChart.getChart(ps.span as any, ps.limit, ps.userId); res(stats); -}); +})); diff --git a/src/server/api/endpoints/charts/user/following.ts b/src/server/api/endpoints/charts/user/following.ts index e0aebf4255..9eba6d2467 100644 --- a/src/server/api/endpoints/charts/user/following.ts +++ b/src/server/api/endpoints/charts/user/following.ts @@ -1,5 +1,5 @@ import $ from 'cafy'; -import getParams from '../../../get-params'; +import define from '../../../define'; import perUserFollowingChart from '../../../../../chart/per-user-following'; import ID, { transform } from '../../../../../misc/cafy-id'; @@ -35,11 +35,8 @@ export const meta = { } }; -export default (params: any) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps) => new Promise(async (res, rej) => { const stats = await perUserFollowingChart.getChart(ps.span as any, ps.limit, ps.userId); res(stats); -}); +})); diff --git a/src/server/api/endpoints/charts/user/notes.ts b/src/server/api/endpoints/charts/user/notes.ts index 251de4d399..20fc49cde7 100644 --- a/src/server/api/endpoints/charts/user/notes.ts +++ b/src/server/api/endpoints/charts/user/notes.ts @@ -1,5 +1,5 @@ import $ from 'cafy'; -import getParams from '../../../get-params'; +import define from '../../../define'; import perUserNotesChart from '../../../../../chart/per-user-notes'; import ID, { transform } from '../../../../../misc/cafy-id'; @@ -35,11 +35,8 @@ export const meta = { } }; -export default (params: any) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps) => new Promise(async (res, rej) => { const stats = await perUserNotesChart.getChart(ps.span as any, ps.limit, ps.userId); res(stats); -}); +})); diff --git a/src/server/api/endpoints/charts/user/reactions.ts b/src/server/api/endpoints/charts/user/reactions.ts index f812400709..bf2204a4e4 100644 --- a/src/server/api/endpoints/charts/user/reactions.ts +++ b/src/server/api/endpoints/charts/user/reactions.ts @@ -1,5 +1,5 @@ import $ from 'cafy'; -import getParams from '../../../get-params'; +import define from '../../../define'; import perUserReactionsChart from '../../../../../chart/per-user-reactions'; import ID, { transform } from '../../../../../misc/cafy-id'; @@ -35,11 +35,8 @@ export const meta = { } }; -export default (params: any) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps) => new Promise(async (res, rej) => { const stats = await perUserReactionsChart.getChart(ps.span as any, ps.limit, ps.userId); res(stats); -}); +})); diff --git a/src/server/api/endpoints/charts/users.ts b/src/server/api/endpoints/charts/users.ts index 9c23235b23..a4a3f2f4b9 100644 --- a/src/server/api/endpoints/charts/users.ts +++ b/src/server/api/endpoints/charts/users.ts @@ -1,5 +1,5 @@ import $ from 'cafy'; -import getParams from '../../get-params'; +import define from '../../define'; import usersChart from '../../../../chart/users'; export const meta = { @@ -25,11 +25,8 @@ export const meta = { } }; -export default (params: any) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps) => new Promise(async (res, rej) => { const stats = await usersChart.getChart(ps.span as any, ps.limit); res(stats); -}); +})); diff --git a/src/server/api/endpoints/drive.ts b/src/server/api/endpoints/drive.ts index 063cd475d4..43fe771385 100644 --- a/src/server/api/endpoints/drive.ts +++ b/src/server/api/endpoints/drive.ts @@ -1,6 +1,6 @@ import DriveFile from '../../../models/drive-file'; -import { ILocalUser } from '../../../models/user'; import config from '../../../config'; +import define from '../define'; export const meta = { desc: { @@ -13,7 +13,7 @@ export const meta = { kind: 'drive-read' }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Calculate drive usage const usage = await DriveFile .aggregate([{ @@ -42,4 +42,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = capacity: 1024 * 1024 * config.localDriveCapacityMb, usage: usage }); -}); +})); diff --git a/src/server/api/endpoints/drive/files.ts b/src/server/api/endpoints/drive/files.ts index ae4e2249ff..27f101562d 100644 --- a/src/server/api/endpoints/drive/files.ts +++ b/src/server/api/endpoints/drive/files.ts @@ -1,7 +1,6 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; import DriveFile, { packMany } from '../../../../models/drive-file'; -import { ILocalUser } from '../../../../models/user'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { desc: { @@ -41,13 +40,10 @@ export const meta = { } }; -export default async (params: any, user: ILocalUser) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) throw psErr; - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Check if both of sinceId and untilId is specified if (ps.sinceId && ps.untilId) { - throw 'cannot set sinceId and untilId'; + return rej('cannot set sinceId and untilId'); } const sort = { @@ -81,5 +77,5 @@ export default async (params: any, user: ILocalUser) => { sort: sort }); - return await packMany(files); -}; + res(await packMany(files)); +})); diff --git a/src/server/api/endpoints/drive/files/attached_notes.ts b/src/server/api/endpoints/drive/files/attached_notes.ts index ad9a2370b1..ec5acbd751 100644 --- a/src/server/api/endpoints/drive/files/attached_notes.ts +++ b/src/server/api/endpoints/drive/files/attached_notes.ts @@ -1,7 +1,6 @@ import $ from 'cafy'; import ID, { transform } from '../../../../../misc/cafy-id'; import DriveFile from '../../../../../models/drive-file'; -import { ILocalUser } from '../../../../../models/user'; -import getParams from '../../../get-params'; +import define from '../../../define'; import { packMany } from '../../../../../models/note'; export const meta = { @@ -28,10 +27,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Fetch file const file = await DriveFile .findOne({ @@ -47,4 +43,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = res(await packMany(file.metadata.attachedNoteIds || [], user, { detail: true })); -}); +})); diff --git a/src/server/api/endpoints/drive/files/check_existence.ts b/src/server/api/endpoints/drive/files/check_existence.ts index a5bdad5fb9..d3ba4b386d 100644 --- a/src/server/api/endpoints/drive/files/check_existence.ts +++ b/src/server/api/endpoints/drive/files/check_existence.ts @@ -1,7 +1,6 @@ import $ from 'cafy'; import DriveFile, { pack } from '../../../../../models/drive-file'; -import { ILocalUser } from '../../../../../models/user'; -import getParams from '../../../get-params'; +import define from '../../../define'; export const meta = { desc: { @@ -23,10 +22,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { const file = await DriveFile.findOne({ md5: ps.md5, 'metadata.userId': user._id, @@ -38,4 +34,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = } else { res({ file: await pack(file) }); } -}); +})); diff --git a/src/server/api/endpoints/drive/files/create.ts b/src/server/api/endpoints/drive/files/create.ts index 2653eba655..3c334a7c1f 100644 --- a/src/server/api/endpoints/drive/files/create.ts +++ b/src/server/api/endpoints/drive/files/create.ts @@ -1,10 +1,8 @@ -import * as fs from 'fs'; const ms = require('ms'); import $ from 'cafy'; import ID, { transform } from '../../../../../misc/cafy-id'; import { validateFileName, pack } from '../../../../../models/drive-file'; import create from '../../../../../services/drive/add-file'; -import { ILocalUser } from '../../../../../models/user'; -import getParams from '../../../get-params'; +import define from '../../../define'; export const meta = { desc: { @@ -52,11 +50,7 @@ export const meta = { } }; -export default async (file: any, params: any, user: ILocalUser): Promise => { - if (file == null) { - throw 'file is required'; - } - +export default define(meta, (ps, user, app, file, cleanup) => new Promise(async (res, rej) => { // Get 'name' parameter let name = file.originalname; if (name !== undefined && name !== null) { @@ -66,35 +60,24 @@ export default async (file: any, params: any, user: ILocalUser): Promise => } else if (name === 'blob') { name = null; } else if (!validateFileName(name)) { - throw 'invalid name'; + return rej('invalid name'); } } else { name = null; } - function cleanup() { - fs.unlink(file.path, () => {}); - } - - const [ps, psErr] = getParams(meta, params); - if (psErr) { - cleanup(); - throw psErr; - } - try { // Create file const driveFile = await create(user, file.path, name, null, ps.folderId, ps.force, false, null, null, ps.isSensitive); cleanup(); - // Serialize - return pack(driveFile); + res(pack(driveFile)); } catch (e) { console.error(e); cleanup(); - throw e; + rej(e); } -}; +})); diff --git a/src/server/api/endpoints/drive/files/delete.ts b/src/server/api/endpoints/drive/files/delete.ts index af7e192655..7367c8fbb6 100644 --- a/src/server/api/endpoints/drive/files/delete.ts +++ b/src/server/api/endpoints/drive/files/delete.ts @@ -2,8 +2,7 @@ import $ from 'cafy'; import ID, { transform } from '../../../../../misc/cafy-id import DriveFile from '../../../../../models/drive-file'; import del from '../../../../../services/drive/delete-file'; import { publishDriveStream } from '../../../../../stream'; -import { ILocalUser } from '../../../../../models/user'; -import getParams from '../../../get-params'; +import define from '../../../define'; export const meta = { stability: 'stable', @@ -29,10 +28,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Fetch file const file = await DriveFile .findOne({ @@ -51,4 +47,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = publishDriveStream(user._id, 'fileDeleted', file._id); res(); -}); +})); diff --git a/src/server/api/endpoints/drive/files/find.ts b/src/server/api/endpoints/drive/files/find.ts index 0ac110c110..1f9c3de1cf 100644 --- a/src/server/api/endpoints/drive/files/find.ts +++ b/src/server/api/endpoints/drive/files/find.ts @@ -1,7 +1,6 @@ import $ from 'cafy'; import ID, { transform } from '../../../../../misc/cafy-id'; import DriveFile, { pack } from '../../../../../models/drive-file'; -import { ILocalUser } from '../../../../../models/user'; -import getParams from '../../../get-params'; +import define from '../../../define'; export const meta = { requireCredential: true, @@ -24,10 +23,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { const files = await DriveFile .find({ filename: name, @@ -36,4 +32,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = }); res(await Promise.all(files.map(file => pack(file)))); -}); +})); diff --git a/src/server/api/endpoints/drive/files/show.ts b/src/server/api/endpoints/drive/files/show.ts index ce0812c508..450a97065b 100644 --- a/src/server/api/endpoints/drive/files/show.ts +++ b/src/server/api/endpoints/drive/files/show.ts @@ -1,7 +1,6 @@ import $ from 'cafy'; import ID, { transform } from '../../../../../misc/cafy-id'; import DriveFile, { pack } from '../../../../../models/drive-file'; -import { ILocalUser } from '../../../../../models/user'; -import getParams from '../../../get-params'; +import define from '../../../define'; export const meta = { stability: 'stable', @@ -27,10 +26,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Fetch file const file = await DriveFile .findOne({ @@ -49,4 +45,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = }); res(_file); -}); +})); diff --git a/src/server/api/endpoints/drive/files/update.ts b/src/server/api/endpoints/drive/files/update.ts index 7c335e5bc6..4efec3dc2a 100644 --- a/src/server/api/endpoints/drive/files/update.ts +++ b/src/server/api/endpoints/drive/files/update.ts @@ -2,8 +2,7 @@ import $ from 'cafy'; import ID, { transform } from '../../../../../misc/cafy-id import DriveFolder from '../../../../../models/drive-folder'; import DriveFile, { validateFileName, pack } from '../../../../../models/drive-file'; import { publishDriveStream } from '../../../../../stream'; -import { ILocalUser } from '../../../../../models/user'; -import getParams from '../../../get-params'; +import define from '../../../define'; import Note from '../../../../../models/note'; export const meta = { @@ -54,10 +53,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Fetch file const file = await DriveFile .findOne({ @@ -122,4 +118,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = // Publish fileUpdated event publishDriveStream(user._id, 'fileUpdated', fileObj); -}); +})); diff --git a/src/server/api/endpoints/drive/files/upload_from_url.ts b/src/server/api/endpoints/drive/files/upload_from_url.ts index 3d19725275..35309d623e 100644 --- a/src/server/api/endpoints/drive/files/upload_from_url.ts +++ b/src/server/api/endpoints/drive/files/upload_from_url.ts @@ -3,7 +3,7 @@ const ms = require('ms'); import { pack } from '../../../../../models/drive-file'; import uploadFromUrl from '../../../../../services/drive/upload-from-url'; import { ILocalUser } from '../../../../../models/user'; -import getParams from '../../../get-params'; +import define from '../../../define'; export const meta = { desc: { @@ -33,9 +33,6 @@ export const meta = { } }; -export default async (params: any, user: ILocalUser): Promise => { - const [ps, psErr] = getParams(meta, params); - if (psErr) throw psErr; - - return pack(await uploadFromUrl(ps.url, user, ps.folderId)); -}; +export default define(meta, (ps, user) => new Promise(async (res, rej) => { + res(pack(await uploadFromUrl(ps.url, user, ps.folderId))); +})); diff --git a/src/server/api/endpoints/drive/folders.ts b/src/server/api/endpoints/drive/folders.ts index 95700ee26c..3a76ecd27d 100644 --- a/src/server/api/endpoints/drive/folders.ts +++ b/src/server/api/endpoints/drive/folders.ts @@ -1,7 +1,6 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; import DriveFolder, { pack } from '../../../../models/drive-folder'; -import { ILocalUser } from '../../../../models/user'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { desc: { @@ -37,10 +36,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Check if both of sinceId and untilId is specified if (ps.sinceId && ps.untilId) { return rej('cannot set sinceId and untilId'); @@ -71,4 +67,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = }); res(await Promise.all(folders.map(folder => pack(folder)))); -}); +})); diff --git a/src/server/api/endpoints/drive/folders/create.ts b/src/server/api/endpoints/drive/folders/create.ts index b51fb0264e..63c6a436d4 100644 --- a/src/server/api/endpoints/drive/folders/create.ts +++ b/src/server/api/endpoints/drive/folders/create.ts @@ -1,8 +1,7 @@ import $ from 'cafy'; import ID, { transform } from '../../../../../misc/cafy-id'; import DriveFolder, { isValidFolderName, pack } from '../../../../../models/drive-folder'; import { publishDriveStream } from '../../../../../stream'; -import { ILocalUser } from '../../../../../models/user'; -import getParams from '../../../get-params'; +import define from '../../../define'; export const meta = { stability: 'stable', @@ -37,10 +36,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // If the parent folder is specified let parent = null; if (ps.parentId) { @@ -72,4 +68,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = // Publish folderCreated event publishDriveStream(user._id, 'folderCreated', folderObj); -}); +})); diff --git a/src/server/api/endpoints/drive/folders/delete.ts b/src/server/api/endpoints/drive/folders/delete.ts index 304666bdc9..019c5eb00b 100644 --- a/src/server/api/endpoints/drive/folders/delete.ts +++ b/src/server/api/endpoints/drive/folders/delete.ts @@ -1,7 +1,6 @@ import $ from 'cafy'; import ID, { transform } from '../../../../../misc/cafy-id'; import DriveFolder from '../../../../../models/drive-folder'; -import { ILocalUser } from '../../../../../models/user'; -import getParams from '../../../get-params'; +import define from '../../../define'; import { publishDriveStream } from '../../../../../stream'; import DriveFile from '../../../../../models/drive-file'; @@ -29,10 +28,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Get folder const folder = await DriveFolder .findOne({ @@ -59,4 +55,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = publishDriveStream(user._id, 'folderDeleted', folder._id); res(); -}); +})); diff --git a/src/server/api/endpoints/drive/folders/find.ts b/src/server/api/endpoints/drive/folders/find.ts index 3d24d18685..a8ce012f10 100644 --- a/src/server/api/endpoints/drive/folders/find.ts +++ b/src/server/api/endpoints/drive/folders/find.ts @@ -1,7 +1,6 @@ import $ from 'cafy'; import ID, { transform } from '../../../../../misc/cafy-id'; import DriveFolder, { pack } from '../../../../../models/drive-folder'; -import { ILocalUser } from '../../../../../models/user'; -import getParams from '../../../get-params'; +import define from '../../../define'; export const meta = { requireCredential: true, @@ -24,10 +23,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { const folders = await DriveFolder .find({ name: name, @@ -36,4 +32,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = }); res(await Promise.all(folders.map(folder => pack(folder)))); -}); +})); diff --git a/src/server/api/endpoints/drive/folders/show.ts b/src/server/api/endpoints/drive/folders/show.ts index b7d8f0a29b..49e51544b6 100644 --- a/src/server/api/endpoints/drive/folders/show.ts +++ b/src/server/api/endpoints/drive/folders/show.ts @@ -1,7 +1,6 @@ import $ from 'cafy'; import ID, { transform } from '../../../../../misc/cafy-id'; import DriveFolder, { pack } from '../../../../../models/drive-folder'; -import { ILocalUser } from '../../../../../models/user'; -import getParams from '../../../get-params'; +import define from '../../../define'; export const meta = { stability: 'stable', @@ -27,10 +26,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Get folder const folder = await DriveFolder .findOne({ @@ -46,4 +42,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = res(await pack(folder, { detail: true })); -}); +})); diff --git a/src/server/api/endpoints/drive/folders/update.ts b/src/server/api/endpoints/drive/folders/update.ts index 53bb14bfe7..f10f9c820d 100644 --- a/src/server/api/endpoints/drive/folders/update.ts +++ b/src/server/api/endpoints/drive/folders/update.ts @@ -1,8 +1,7 @@ import $ from 'cafy'; import ID, { transform } from '../../../../../misc/cafy-id'; import DriveFolder, { isValidFolderName, pack } from '../../../../../models/drive-folder'; import { publishDriveStream } from '../../../../../stream'; -import { ILocalUser } from '../../../../../models/user'; -import getParams from '../../../get-params'; +import define from '../../../define'; export const meta = { stability: 'stable', @@ -45,10 +44,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Fetch folder const folder = await DriveFolder .findOne({ @@ -122,4 +118,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = // Publish folderUpdated event publishDriveStream(user._id, 'folderUpdated', folderObj); -}); +})); diff --git a/src/server/api/endpoints/drive/stream.ts b/src/server/api/endpoints/drive/stream.ts index ecf405fe64..804ecf50d9 100644 --- a/src/server/api/endpoints/drive/stream.ts +++ b/src/server/api/endpoints/drive/stream.ts @@ -1,7 +1,6 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; import DriveFile, { packMany } from '../../../../models/drive-file'; -import { ILocalUser } from '../../../../models/user'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { requireCredential: true, @@ -30,10 +29,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Check if both of sinceId and untilId is specified if (ps.sinceId && ps.untilId) { return rej('cannot set sinceId and untilId'); @@ -70,4 +66,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = }); res(await packMany(files)); -}); +})); diff --git a/src/server/api/endpoints/following/create.ts b/src/server/api/endpoints/following/create.ts index 7795bbcc61..f826776892 100644 --- a/src/server/api/endpoints/following/create.ts +++ b/src/server/api/endpoints/following/create.ts @@ -1,9 +1,9 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; const ms = require('ms'); -import User, { pack, ILocalUser } from '../../../../models/user'; +import User, { pack } from '../../../../models/user'; import Following from '../../../../models/following'; import create from '../../../../services/following/create'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { stability: 'stable', @@ -34,10 +34,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { const follower = user; // 自分自身 @@ -78,4 +75,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = // Send response res(await pack(followee._id, user)); -}); +})); diff --git a/src/server/api/endpoints/following/delete.ts b/src/server/api/endpoints/following/delete.ts index ae0edd34ee..92a50ce722 100644 --- a/src/server/api/endpoints/following/delete.ts +++ b/src/server/api/endpoints/following/delete.ts @@ -1,9 +1,9 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; const ms = require('ms'); -import User, { pack, ILocalUser } from '../../../../models/user'; +import User, { pack } from '../../../../models/user'; import Following from '../../../../models/following'; import deleteFollowing from '../../../../services/following/delete'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { stability: 'stable', @@ -34,10 +34,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { const follower = user; // Check if the followee is yourself @@ -74,4 +71,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = // Send response res(await pack(followee._id, user)); -}); +})); diff --git a/src/server/api/endpoints/following/requests/accept.ts b/src/server/api/endpoints/following/requests/accept.ts index 1172f463d6..36c3694eed 100644 --- a/src/server/api/endpoints/following/requests/accept.ts +++ b/src/server/api/endpoints/following/requests/accept.ts @@ -1,7 +1,7 @@ import $ from 'cafy'; import ID, { transform } from '../../../../../misc/cafy-id'; import acceptFollowRequest from '../../../../../services/following/requests/accept'; -import User, { ILocalUser } from '../../../../../models/user'; -import getParams from '../../../get-params'; +import User from '../../../../../models/user'; +import define from '../../../define'; export const meta = { desc: { @@ -21,10 +21,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Fetch follower const follower = await User.findOne({ _id: ps.userId @@ -37,4 +34,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = await acceptFollowRequest(user, follower); res(); -}); +})); diff --git a/src/server/api/endpoints/following/requests/cancel.ts b/src/server/api/endpoints/following/requests/cancel.ts index 77bfcfe15e..bfb7d0713c 100644 --- a/src/server/api/endpoints/following/requests/cancel.ts +++ b/src/server/api/endpoints/following/requests/cancel.ts @@ -1,7 +1,7 @@ import $ from 'cafy'; import ID, { transform } from '../../../../../misc/cafy-id'; import cancelFollowRequest from '../../../../../services/following/requests/cancel'; -import User, { pack, ILocalUser } from '../../../../../models/user'; -import getParams from '../../../get-params'; +import User, { pack } from '../../../../../models/user'; +import define from '../../../define'; export const meta = { desc: { @@ -21,10 +21,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Fetch followee const followee = await User.findOne({ _id: ps.userId @@ -41,4 +38,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = } res(await pack(followee._id, user)); -}); +})); diff --git a/src/server/api/endpoints/following/requests/list.ts b/src/server/api/endpoints/following/requests/list.ts index 6d42d06db2..c561ea066c 100644 --- a/src/server/api/endpoints/following/requests/list.ts +++ b/src/server/api/endpoints/following/requests/list.ts @@ -1,6 +1,6 @@ //import $ from 'cafy'; import ID, { transform } from '../../../../../cafy-id'; import FollowRequest, { pack } from '../../../../../models/follow-request'; -import { ILocalUser } from '../../../../../models/user'; +import define from '../../../define'; export const meta = { desc: { @@ -13,11 +13,11 @@ export const meta = { kind: 'following-read' }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { +export default define(meta, (ps, user) => new Promise(async (res, rej) => { const reqs = await FollowRequest.find({ followeeId: user._id }); // Send response res(await Promise.all(reqs.map(req => pack(req)))); -}); +})); diff --git a/src/server/api/endpoints/following/requests/reject.ts b/src/server/api/endpoints/following/requests/reject.ts index 45ebb7807b..bd0b3bba22 100644 --- a/src/server/api/endpoints/following/requests/reject.ts +++ b/src/server/api/endpoints/following/requests/reject.ts @@ -1,7 +1,7 @@ import $ from 'cafy'; import ID, { transform } from '../../../../../misc/cafy-id'; import rejectFollowRequest from '../../../../../services/following/requests/reject'; -import User, { ILocalUser } from '../../../../../models/user'; -import getParams from '../../../get-params'; +import User from '../../../../../models/user'; +import define from '../../../define'; export const meta = { desc: { @@ -21,10 +21,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Fetch follower const follower = await User.findOne({ _id: ps.userId @@ -37,4 +34,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = await rejectFollowRequest(user, follower); res(); -}); +})); diff --git a/src/server/api/endpoints/following/stalk.ts b/src/server/api/endpoints/following/stalk.ts index 434bc52b59..1fef9a429a 100644 --- a/src/server/api/endpoints/following/stalk.ts +++ b/src/server/api/endpoints/following/stalk.ts @@ -1,7 +1,6 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; import Following from '../../../../models/following'; -import { ILocalUser } from '../../../../models/user'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { desc: { @@ -21,10 +20,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { const follower = user; // Fetch following @@ -47,4 +43,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = res(); // TODO: イベント -}); +})); diff --git a/src/server/api/endpoints/following/unstalk.ts b/src/server/api/endpoints/following/unstalk.ts index 00e91357ec..66e99bed58 100644 --- a/src/server/api/endpoints/following/unstalk.ts +++ b/src/server/api/endpoints/following/unstalk.ts @@ -1,7 +1,6 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; import Following from '../../../../models/following'; -import { ILocalUser } from '../../../../models/user'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { desc: { @@ -21,10 +20,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { const follower = user; // Fetch following @@ -47,4 +43,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = res(); // TODO: イベント -}); +})); diff --git a/src/server/api/endpoints/games/reversi/games.ts b/src/server/api/endpoints/games/reversi/games.ts index 9c7a7c4d74..2bebe36ef8 100644 --- a/src/server/api/endpoints/games/reversi/games.ts +++ b/src/server/api/endpoints/games/reversi/games.ts @@ -1,7 +1,6 @@ import $ from 'cafy'; import ID, { transform } from '../../../../../misc/cafy-id'; import ReversiGame, { pack } from '../../../../../models/games/reversi/game'; -import { ILocalUser } from '../../../../../models/user'; -import getParams from '../../../get-params'; +import define from '../../../define'; export const meta = { params: { @@ -27,10 +26,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Check if both of sinceId and untilId is specified if (ps.sinceId && ps.untilId) { return rej('cannot set sinceId and untilId'); @@ -72,4 +68,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = res(Promise.all(games.map(async (g) => await pack(g, user, { detail: false })))); -}); +})); diff --git a/src/server/api/endpoints/games/reversi/games/show.ts b/src/server/api/endpoints/games/reversi/games/show.ts index a2c84ae82d..c747202354 100644 --- a/src/server/api/endpoints/games/reversi/games/show.ts +++ b/src/server/api/endpoints/games/reversi/games/show.ts @@ -1,8 +1,7 @@ import $ from 'cafy'; import ID, { transform } from '../../../../../../misc/cafy-id'; import ReversiGame, { pack } from '../../../../../../models/games/reversi/game'; import Reversi from '../../../../../../games/reversi/core'; -import { ILocalUser } from '../../../../../../models/user'; -import getParams from '../../../../get-params'; +import define from '../../../../define'; export const meta = { params: { @@ -13,10 +12,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { const game = await ReversiGame.findOne({ _id: ps.gameId }); if (game == null) { @@ -39,4 +35,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = board: o.board, turn: o.turn }, packed)); -}); +})); diff --git a/src/server/api/endpoints/games/reversi/games/surrender.ts b/src/server/api/endpoints/games/reversi/games/surrender.ts index 2860c154fe..aab88da664 100644 --- a/src/server/api/endpoints/games/reversi/games/surrender.ts +++ b/src/server/api/endpoints/games/reversi/games/surrender.ts @@ -1,8 +1,7 @@ import $ from 'cafy'; import ID, { transform } from '../../../../../../misc/cafy-id'; import ReversiGame, { pack } from '../../../../../../models/games/reversi/game'; -import { ILocalUser } from '../../../../../../models/user'; -import getParams from '../../../../get-params'; import { publishReversiGameStream } from '../../../../../../stream'; +import define from '../../../../define'; export const meta = { desc: { @@ -22,10 +21,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { const game = await ReversiGame.findOne({ _id: ps.gameId }); if (game == null) { @@ -58,4 +54,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = }); res(); -}); +})); diff --git a/src/server/api/endpoints/games/reversi/invitations.ts b/src/server/api/endpoints/games/reversi/invitations.ts index 3962282759..d808ff2e78 100644 --- a/src/server/api/endpoints/games/reversi/invitations.ts +++ b/src/server/api/endpoints/games/reversi/invitations.ts @@ -1,11 +1,11 @@ import Matching, { pack as packMatching } from '../../../../../models/games/reversi/matching'; -import { ILocalUser } from '../../../../../models/user'; +import define from '../../../define'; export const meta = { requireCredential: true }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Find session const invitations = await Matching.find({ childId: user._id @@ -17,4 +17,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = // Reponse res(Promise.all(invitations.map(async (i) => await packMatching(i, user)))); -}); +})); diff --git a/src/server/api/endpoints/games/reversi/match.ts b/src/server/api/endpoints/games/reversi/match.ts index f43650aeeb..e0f9c20cbb 100644 --- a/src/server/api/endpoints/games/reversi/match.ts +++ b/src/server/api/endpoints/games/reversi/match.ts @@ -1,10 +1,10 @@ import $ from 'cafy'; import ID, { transform } from '../../../../../misc/cafy-id'; import Matching, { pack as packMatching } from '../../../../../models/games/reversi/matching'; import ReversiGame, { pack as packGame } from '../../../../../models/games/reversi/game'; -import User, { ILocalUser } from '../../../../../models/user'; +import User from '../../../../../models/user'; import { publishMainStream, publishReversiStream } from '../../../../../stream'; import { eighteight } from '../../../../../games/reversi/maps'; -import getParams from '../../../get-params'; +import define from '../../../define'; export const meta = { requireCredential: true, @@ -17,10 +17,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Myself if (ps.userId.equals(user._id)) { return rej('invalid userId param'); @@ -103,4 +100,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = publishMainStream(child._id, 'reversiInvited', packed); } -}); +})); diff --git a/src/server/api/endpoints/games/reversi/match/cancel.ts b/src/server/api/endpoints/games/reversi/match/cancel.ts index d5c186409c..ac51e416ed 100644 --- a/src/server/api/endpoints/games/reversi/match/cancel.ts +++ b/src/server/api/endpoints/games/reversi/match/cancel.ts @@ -1,14 +1,14 @@ import Matching from '../../../../../../models/games/reversi/matching'; -import { ILocalUser } from '../../../../../../models/user'; +import define from '../../../../define'; export const meta = { requireCredential: true }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { +export default define(meta, (ps, user) => new Promise(async (res, rej) => { await Matching.remove({ parentId: user._id }); res(); -}); +})); diff --git a/src/server/api/endpoints/hashtags/search.ts b/src/server/api/endpoints/hashtags/search.ts index 35ae2a83b3..4d753808d4 100644 --- a/src/server/api/endpoints/hashtags/search.ts +++ b/src/server/api/endpoints/hashtags/search.ts @@ -1,6 +1,6 @@ import $ from 'cafy'; import Hashtag from '../../../../models/hashtag'; -import getParams from '../../get-params'; +import define from '../../define'; const escapeRegexp = require('escape-regexp'); export const meta = { @@ -36,10 +36,7 @@ export const meta = { } }; -export default (params: any) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps) => new Promise(async (res, rej) => { const hashtags = await Hashtag .find({ tag: new RegExp('^' + escapeRegexp(ps.query.toLowerCase())) @@ -52,4 +49,4 @@ export default (params: any) => new Promise(async (res, rej) => { }); res(hashtags.map(tag => tag.tag)); -}); +})); diff --git a/src/server/api/endpoints/hashtags/trend.ts b/src/server/api/endpoints/hashtags/trend.ts index 0ec6a4ffec..02d398a683 100644 --- a/src/server/api/endpoints/hashtags/trend.ts +++ b/src/server/api/endpoints/hashtags/trend.ts @@ -1,6 +1,7 @@ import Note from '../../../../models/note'; import { erase } from '../../../../prelude/array'; import Meta from '../../../../models/meta'; +import define from '../../define'; /* トレンドに載るためには「『直近a分間のユニーク投稿数が今からa分前~今からb分前の間のユニーク投稿数のn倍以上』のハッシュタグの上位5位以内に入る」ことが必要 @@ -14,10 +15,11 @@ const requiredUsers = 3; // 最低何人がそのタグを投稿している必 const max = 5; -/** - * Get trends of hashtags - */ -export default () => new Promise(async (res, rej) => { +export const meta = { + requireCredential: false, +}; + +export default define(meta, () => new Promise(async (res, rej) => { const meta = await Meta.findOne({}); const hidedTags = meta ? (meta.hidedTags || []).map(t => t.toLowerCase()) : []; @@ -143,4 +145,4 @@ export default () => new Promise(async (res, rej) => { })); res(stats); -}); +})); diff --git a/src/server/api/endpoints/i.ts b/src/server/api/endpoints/i.ts index 1c488d94c6..aea47ad795 100644 --- a/src/server/api/endpoints/i.ts +++ b/src/server/api/endpoints/i.ts @@ -1,5 +1,5 @@ -import User, { pack, ILocalUser } from '../../../models/user'; -import { IApp } from '../../../models/app'; +import User, { pack } from '../../../models/user'; +import define from '../define'; export const meta = { stability: 'stable', @@ -18,7 +18,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser, app: IApp) => new Promise(async (res, rej) => { +export default define(meta, (ps, user, app) => new Promise(async (res, rej) => { const isSecure = user != null && app == null; // Serialize @@ -34,4 +34,4 @@ export default (params: any, user: ILocalUser, app: IApp) => new Promise(async ( lastUsedAt: new Date() } }); -}); +})); diff --git a/src/server/api/endpoints/i/2fa/done.ts b/src/server/api/endpoints/i/2fa/done.ts index 40b45a3d0b..2ee0c8886e 100644 --- a/src/server/api/endpoints/i/2fa/done.ts +++ b/src/server/api/endpoints/i/2fa/done.ts @@ -1,7 +1,7 @@ import $ from 'cafy'; import * as speakeasy from 'speakeasy'; -import User, { ILocalUser } from '../../../../../models/user'; -import getParams from '../../../get-params'; +import User from '../../../../../models/user'; +import define from '../../../define'; export const meta = { requireCredential: true, @@ -15,10 +15,7 @@ export const meta = { } }; -export default async (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { const _token = ps.token.replace(/\s/g, ''); if (user.twoFactorTempSecret == null) { @@ -43,4 +40,4 @@ export default async (params: any, user: ILocalUser) => new Promise(async (res, }); res(); -}); +})); diff --git a/src/server/api/endpoints/i/2fa/register.ts b/src/server/api/endpoints/i/2fa/register.ts index 4d6b05b787..38e3ca7806 100644 --- a/src/server/api/endpoints/i/2fa/register.ts +++ b/src/server/api/endpoints/i/2fa/register.ts @@ -2,9 +2,9 @@ import $ from 'cafy'; import * as bcrypt from 'bcryptjs'; import * as speakeasy from 'speakeasy'; import * as QRCode from 'qrcode'; -import User, { ILocalUser } from '../../../../../models/user'; +import User from '../../../../../models/user'; import config from '../../../../../config'; -import getParams from '../../../get-params'; +import define from '../../../define'; export const meta = { requireCredential: true, @@ -18,10 +18,7 @@ export const meta = { } }; -export default async (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Compare password const same = await bcrypt.compare(ps.password, user.password); @@ -54,4 +51,4 @@ export default async (params: any, user: ILocalUser) => new Promise(async (res, issuer: config.host }); }); -}); +})); diff --git a/src/server/api/endpoints/i/2fa/unregister.ts b/src/server/api/endpoints/i/2fa/unregister.ts index e3a1bd43de..2cb48d93cd 100644 --- a/src/server/api/endpoints/i/2fa/unregister.ts +++ b/src/server/api/endpoints/i/2fa/unregister.ts @@ -1,7 +1,7 @@ import $ from 'cafy'; import * as bcrypt from 'bcryptjs'; -import User, { ILocalUser } from '../../../../../models/user'; -import getParams from '../../../get-params'; +import User from '../../../../../models/user'; +import define from '../../../define'; export const meta = { requireCredential: true, @@ -15,10 +15,7 @@ export const meta = { } }; -export default async (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Compare password const same = await bcrypt.compare(ps.password, user.password); @@ -34,4 +31,4 @@ export default async (params: any, user: ILocalUser) => new Promise(async (res, }); res(); -}); +})); diff --git a/src/server/api/endpoints/i/authorized_apps.ts b/src/server/api/endpoints/i/authorized_apps.ts index 09bcd2dcb6..c4b992b52f 100644 --- a/src/server/api/endpoints/i/authorized_apps.ts +++ b/src/server/api/endpoints/i/authorized_apps.ts @@ -1,8 +1,7 @@ import $ from 'cafy'; import AccessToken from '../../../../models/access-token'; import { pack } from '../../../../models/app'; -import { ILocalUser } from '../../../../models/user'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { requireCredential: true, @@ -27,10 +26,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Get tokens const tokens = await AccessToken .find({ @@ -46,4 +42,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = res(await Promise.all(tokens.map(token => pack(token.appId, user, { detail: true })))); -}); +})); diff --git a/src/server/api/endpoints/i/change_password.ts b/src/server/api/endpoints/i/change_password.ts index 818637e224..a2cfc6f70e 100644 --- a/src/server/api/endpoints/i/change_password.ts +++ b/src/server/api/endpoints/i/change_password.ts @@ -1,7 +1,7 @@ import $ from 'cafy'; import * as bcrypt from 'bcryptjs'; -import User, { ILocalUser } from '../../../../models/user'; -import getParams from '../../get-params'; +import User from '../../../../models/user'; +import define from '../../define'; export const meta = { requireCredential: true, @@ -19,10 +19,7 @@ export const meta = { } }; -export default async (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Compare password const same = await bcrypt.compare(ps.currentPassword, user.password); @@ -41,4 +38,4 @@ export default async (params: any, user: ILocalUser) => new Promise(async (res, }); res(); -}); +})); diff --git a/src/server/api/endpoints/i/favorites.ts b/src/server/api/endpoints/i/favorites.ts index 847ac64d9a..67a3638885 100644 --- a/src/server/api/endpoints/i/favorites.ts +++ b/src/server/api/endpoints/i/favorites.ts @@ -1,7 +1,6 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; import Favorite, { packMany } from '../../../../models/favorite'; -import { ILocalUser } from '../../../../models/user'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { desc: { @@ -31,10 +30,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Check if both of sinceId and untilId is specified if (ps.sinceId && ps.untilId) { return rej('cannot set sinceId and untilId'); @@ -67,4 +63,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = }); res(await packMany(favorites, user)); -}); +})); diff --git a/src/server/api/endpoints/i/notifications.ts b/src/server/api/endpoints/i/notifications.ts index d16ba63bdc..b1ddd40d13 100644 --- a/src/server/api/endpoints/i/notifications.ts +++ b/src/server/api/endpoints/i/notifications.ts @@ -4,8 +4,7 @@ import Mute from '../../../../models/mute'; import { packMany } from '../../../../models/notification'; import { getFriendIds } from '../../common/get-friends'; import read from '../../common/read-notification'; -import { ILocalUser } from '../../../../models/user'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { desc: { @@ -45,10 +44,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Check if both of sinceId and untilId is specified if (ps.sinceId && ps.untilId) { return rej('cannot set sinceId and untilId'); @@ -105,4 +101,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = if (notifications.length > 0 && ps.markAsRead) { read(user._id, notifications); } -}); +})); diff --git a/src/server/api/endpoints/i/pin.ts b/src/server/api/endpoints/i/pin.ts index 4341906a57..ab8d8a1853 100644 --- a/src/server/api/endpoints/i/pin.ts +++ b/src/server/api/endpoints/i/pin.ts @@ -1,8 +1,7 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; -import { ILocalUser } from '../../../../models/user'; import { pack } from '../../../../models/user'; import { addPinned } from '../../../../services/i/pin'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { stability: 'stable', @@ -27,10 +26,7 @@ export const meta = { } }; -export default async (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Processing try { await addPinned(user, ps.noteId); @@ -45,4 +41,4 @@ export default async (params: any, user: ILocalUser) => new Promise(async (res, // Send response res(iObj); -}); +})); diff --git a/src/server/api/endpoints/i/read_all_unread_notes.ts b/src/server/api/endpoints/i/read_all_unread_notes.ts index 6a4f72ea29..7d63de4e19 100644 --- a/src/server/api/endpoints/i/read_all_unread_notes.ts +++ b/src/server/api/endpoints/i/read_all_unread_notes.ts @@ -1,6 +1,7 @@ -import User, { ILocalUser } from '../../../../models/user'; +import User from '../../../../models/user'; import { publishMainStream } from '../../../../stream'; import NoteUnread from '../../../../models/note-unread'; +import define from '../../define'; export const meta = { desc: { @@ -16,7 +17,7 @@ export const meta = { } }; -export default async (params: any, user: ILocalUser) => new Promise(async (res, rej) => { +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Remove documents await NoteUnread.remove({ userId: user._id @@ -34,4 +35,4 @@ export default async (params: any, user: ILocalUser) => new Promise(async (res, publishMainStream(user._id, 'readAllUnreadSpecifiedNotes'); res(); -}); +})); diff --git a/src/server/api/endpoints/i/regenerate_token.ts b/src/server/api/endpoints/i/regenerate_token.ts index 81997362fc..5dfc3cd664 100644 --- a/src/server/api/endpoints/i/regenerate_token.ts +++ b/src/server/api/endpoints/i/regenerate_token.ts @@ -1,9 +1,9 @@ import $ from 'cafy'; import * as bcrypt from 'bcryptjs'; -import User, { ILocalUser } from '../../../../models/user'; +import User from '../../../../models/user'; import { publishMainStream } from '../../../../stream'; import generateUserToken from '../../common/generate-native-user-token'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { requireCredential: true, @@ -17,10 +17,7 @@ export const meta = { } }; -export default async (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Compare password const same = await bcrypt.compare(ps.password, user.password); @@ -41,4 +38,4 @@ export default async (params: any, user: ILocalUser) => new Promise(async (res, // Publish event publishMainStream(user._id, 'myTokenRegenerated'); -}); +})); diff --git a/src/server/api/endpoints/i/signin_history.ts b/src/server/api/endpoints/i/signin_history.ts index df1cd34c8c..acf9349cd2 100644 --- a/src/server/api/endpoints/i/signin_history.ts +++ b/src/server/api/endpoints/i/signin_history.ts @@ -1,7 +1,6 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; import Signin, { pack } from '../../../../models/signin'; -import { ILocalUser } from '../../../../models/user'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { requireCredential: true, @@ -26,10 +25,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Check if both of sinceId and untilId is specified if (ps.sinceId && ps.untilId) { return rej('cannot set sinceId and untilId'); @@ -62,4 +58,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = // Serialize res(await Promise.all(history.map(record => pack(record)))); -}); +})); diff --git a/src/server/api/endpoints/i/unpin.ts b/src/server/api/endpoints/i/unpin.ts index 26ff8ccda1..27342fb43c 100644 --- a/src/server/api/endpoints/i/unpin.ts +++ b/src/server/api/endpoints/i/unpin.ts @@ -1,8 +1,7 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; -import { ILocalUser } from '../../../../models/user'; import { pack } from '../../../../models/user'; import { removePinned } from '../../../../services/i/pin'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { stability: 'stable', @@ -27,10 +26,7 @@ export const meta = { } }; -export default async (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Processing try { await removePinned(user, ps.noteId); @@ -45,4 +41,4 @@ export default async (params: any, user: ILocalUser) => new Promise(async (res, // Send response res(iObj); -}); +})); diff --git a/src/server/api/endpoints/i/update.ts b/src/server/api/endpoints/i/update.ts index 93d4448092..7a16248305 100644 --- a/src/server/api/endpoints/i/update.ts +++ b/src/server/api/endpoints/i/update.ts @@ -5,7 +5,7 @@ import DriveFile from '../../../../models/drive-file'; import acceptAllFollowRequests from '../../../../services/following/requests/accept-all'; import { IApp } from '../../../../models/app'; import { publishToFollowers } from '../../../../services/i/update'; -import getParams from '../../get-params'; +import define from '../../define'; import getDriveFileUrl from '../../../../misc/get-drive-file-url'; export const meta = { @@ -115,10 +115,7 @@ export const meta = { } }; -export default async (params: any, user: ILocalUser, app: IApp) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user, app) => new Promise(async (res, rej) => { const isSecure = user != null && app == null; const updates = {} as any; @@ -209,4 +206,4 @@ export default async (params: any, user: ILocalUser, app: IApp) => new Promise(a // フォロワーにUpdateを配信 publishToFollowers(user._id); -}); +})); diff --git a/src/server/api/endpoints/i/update_client_setting.ts b/src/server/api/endpoints/i/update_client_setting.ts index a1631b1d95..03eb23a5a2 100644 --- a/src/server/api/endpoints/i/update_client_setting.ts +++ b/src/server/api/endpoints/i/update_client_setting.ts @@ -1,7 +1,7 @@ import $ from 'cafy'; -import User, { ILocalUser } from '../../../../models/user'; +import User from '../../../../models/user'; import { publishMainStream } from '../../../../stream'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { requireCredential: true, @@ -19,10 +19,7 @@ export const meta = { } }; -export default async (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { const x: any = {}; x[`clientSettings.${name}`] = ps.value; @@ -37,4 +34,4 @@ export default async (params: any, user: ILocalUser) => new Promise(async (res, key: name, value: ps.value }); -}); +})); diff --git a/src/server/api/endpoints/i/update_home.ts b/src/server/api/endpoints/i/update_home.ts index eadd0290ab..f472600ee0 100644 --- a/src/server/api/endpoints/i/update_home.ts +++ b/src/server/api/endpoints/i/update_home.ts @@ -1,7 +1,7 @@ import $ from 'cafy'; -import User, { ILocalUser } from '../../../../models/user'; +import User from '../../../../models/user'; import { publishMainStream } from '../../../../stream'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { requireCredential: true, @@ -20,10 +20,7 @@ export const meta = { } }; -export default async (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { await User.update(user._id, { $set: { 'clientSettings.home': ps.home @@ -33,4 +30,4 @@ export default async (params: any, user: ILocalUser) => new Promise(async (res, res(); publishMainStream(user._id, 'homeUpdated', ps.home); -}); +})); diff --git a/src/server/api/endpoints/i/update_mobile_home.ts b/src/server/api/endpoints/i/update_mobile_home.ts index c886574f23..792c4b7877 100644 --- a/src/server/api/endpoints/i/update_mobile_home.ts +++ b/src/server/api/endpoints/i/update_mobile_home.ts @@ -1,7 +1,7 @@ import $ from 'cafy'; -import User, { ILocalUser } from '../../../../models/user'; +import User from '../../../../models/user'; import { publishMainStream } from '../../../../stream'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { requireCredential: true, @@ -19,10 +19,7 @@ export const meta = { } }; -export default async (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { await User.update(user._id, { $set: { 'clientSettings.mobileHome': ps.home @@ -32,4 +29,4 @@ export default async (params: any, user: ILocalUser) => new Promise(async (res, res(); publishMainStream(user._id, 'mobileHomeUpdated', ps.home); -}); +})); diff --git a/src/server/api/endpoints/i/update_widget.ts b/src/server/api/endpoints/i/update_widget.ts index 947a29074c..90fe8fbe23 100644 --- a/src/server/api/endpoints/i/update_widget.ts +++ b/src/server/api/endpoints/i/update_widget.ts @@ -1,7 +1,7 @@ import $ from 'cafy'; -import User, { ILocalUser } from '../../../../models/user'; +import User from '../../../../models/user'; import { publishMainStream } from '../../../../stream'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { requireCredential: true, @@ -19,10 +19,7 @@ export const meta = { } }; -export default async (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { if (ps.id == null && ps.data == null) return rej('you need to set id and data params if home param unset'); let widget; @@ -88,4 +85,4 @@ export default async (params: any, user: ILocalUser) => new Promise(async (res, } else { rej('widget not found'); } -}); +})); diff --git a/src/server/api/endpoints/messaging/history.ts b/src/server/api/endpoints/messaging/history.ts index 078af21f27..c026e5dd91 100644 --- a/src/server/api/endpoints/messaging/history.ts +++ b/src/server/api/endpoints/messaging/history.ts @@ -2,8 +2,7 @@ import $ from 'cafy'; import History from '../../../../models/messaging-history'; import Mute from '../../../../models/mute'; import { pack } from '../../../../models/messaging-message'; -import { ILocalUser } from '../../../../models/user'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { desc: { @@ -23,10 +22,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { const mute = await Mute.find({ muterId: user._id, deletedAt: { $exists: false } @@ -47,4 +43,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = }); res(await Promise.all(history.map(h => pack(h.messageId, user)))); -}); +})); diff --git a/src/server/api/endpoints/messaging/messages.ts b/src/server/api/endpoints/messaging/messages.ts index 43d96e2ee4..50a5eb00d8 100644 --- a/src/server/api/endpoints/messaging/messages.ts +++ b/src/server/api/endpoints/messaging/messages.ts @@ -1,9 +1,9 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; import Message from '../../../../models/messaging-message'; -import User, { ILocalUser } from '../../../../models/user'; +import User from '../../../../models/user'; import { pack } from '../../../../models/messaging-message'; import read from '../../common/read-messaging-message'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { desc: { @@ -43,10 +43,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Check if both of sinceId and untilId is specified if (ps.sinceId && ps.untilId) { return rej('cannot set sinceId and untilId'); @@ -108,4 +105,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = if (ps.markAsRead) { read(user._id, recipient._id, messages); } -}); +})); diff --git a/src/server/api/endpoints/messaging/messages/create.ts b/src/server/api/endpoints/messaging/messages/create.ts index ff44e192d1..bcd4d6b63c 100644 --- a/src/server/api/endpoints/messaging/messages/create.ts +++ b/src/server/api/endpoints/messaging/messages/create.ts @@ -2,14 +2,14 @@ import $ from 'cafy'; import ID, { transform } from '../../../../../misc/cafy-id import Message from '../../../../../models/messaging-message'; import { isValidText } from '../../../../../models/messaging-message'; import History from '../../../../../models/messaging-history'; -import User, { ILocalUser } from '../../../../../models/user'; +import User from '../../../../../models/user'; import Mute from '../../../../../models/mute'; import DriveFile from '../../../../../models/drive-file'; import { pack } from '../../../../../models/messaging-message'; import { publishMainStream } from '../../../../../stream'; import { publishMessagingStream, publishMessagingIndexStream } from '../../../../../stream'; import pushSw from '../../../../../push-sw'; -import getParams from '../../../get-params'; +import define from '../../../define'; export const meta = { desc: { @@ -38,10 +38,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Myself if (ps.userId.equals(user._id)) { return rej('cannot send message to myself'); @@ -155,4 +152,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = }, { upsert: true }); -}); +})); diff --git a/src/server/api/endpoints/messaging/messages/read.ts b/src/server/api/endpoints/messaging/messages/read.ts index 122034fdf0..eb1799cfc6 100644 --- a/src/server/api/endpoints/messaging/messages/read.ts +++ b/src/server/api/endpoints/messaging/messages/read.ts @@ -1,8 +1,7 @@ import $ from 'cafy'; import ID, { transform } from '../../../../../misc/cafy-id'; import Message from '../../../../../models/messaging-message'; -import { ILocalUser } from '../../../../../models/user'; import read from '../../../common/read-messaging-message'; -import getParams from '../../../get-params'; +import define from '../../../define'; export const meta = { desc: { @@ -26,10 +25,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { const message = await Message.findOne({ _id: ps.messageId, recipientId: user._id @@ -42,4 +38,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = read(user._id, message.userId, message); res(); -}); +})); diff --git a/src/server/api/endpoints/meta.ts b/src/server/api/endpoints/meta.ts index e33023b07b..be42c070ec 100644 --- a/src/server/api/endpoints/meta.ts +++ b/src/server/api/endpoints/meta.ts @@ -2,9 +2,8 @@ import $ from 'cafy'; import * as os from 'os'; import config from '../../../config'; import Meta from '../../../models/meta'; -import { ILocalUser } from '../../../models/user'; import Emoji from '../../../models/emoji'; -import getParams from '../get-params'; +import define from '../define'; const pkg = require('../../../../package.json'); const client = require('../../../../built/client/meta.json'); @@ -27,10 +26,7 @@ export const meta = { }, }; -export default (params: any, me: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, me) => new Promise(async (res, rej) => { const met: any = (await Meta.findOne()) || {}; const emojis = await Emoji.find({ host: null }); @@ -76,4 +72,4 @@ export default (params: any, me: ILocalUser) => new Promise(async (res, rej) => userRecommendation: config.user_recommendation ? config.user_recommendation : {} } : undefined }); -}); +})); diff --git a/src/server/api/endpoints/mute/create.ts b/src/server/api/endpoints/mute/create.ts index 442d3f51d8..f3bb3da14f 100644 --- a/src/server/api/endpoints/mute/create.ts +++ b/src/server/api/endpoints/mute/create.ts @@ -1,7 +1,7 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; -import User, { ILocalUser } from '../../../../models/user'; +import User from '../../../../models/user'; import Mute from '../../../../models/mute'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { desc: { @@ -21,10 +21,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { const muter = user; // 自分自身 @@ -64,4 +61,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = }); res(); -}); +})); diff --git a/src/server/api/endpoints/mute/delete.ts b/src/server/api/endpoints/mute/delete.ts index c1d4f35425..68de0feb28 100644 --- a/src/server/api/endpoints/mute/delete.ts +++ b/src/server/api/endpoints/mute/delete.ts @@ -1,7 +1,7 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; -import User, { ILocalUser } from '../../../../models/user'; +import User from '../../../../models/user'; import Mute from '../../../../models/mute'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { desc: { @@ -21,10 +21,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { const muter = user; // Check if the mutee is yourself @@ -62,4 +59,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = }); res(); -}); +})); diff --git a/src/server/api/endpoints/mute/list.ts b/src/server/api/endpoints/mute/list.ts index e33e709773..c337ba4ec9 100644 --- a/src/server/api/endpoints/mute/list.ts +++ b/src/server/api/endpoints/mute/list.ts @@ -1,7 +1,6 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; import Mute, { packMany } from '../../../../models/mute'; -import { ILocalUser } from '../../../../models/user'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { desc: { @@ -31,10 +30,7 @@ export const meta = { } }; -export default (params: any, me: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, me) => new Promise(async (res, rej) => { // Check if both of sinceId and untilId is specified if (ps.sinceId && ps.untilId) { return rej('cannot set sinceId and untilId'); @@ -66,4 +62,4 @@ export default (params: any, me: ILocalUser) => new Promise(async (res, rej) => }); res(await packMany(mutes, me)); -}); +})); diff --git a/src/server/api/endpoints/my/apps.ts b/src/server/api/endpoints/my/apps.ts index 0b051f600c..646ffcc263 100644 --- a/src/server/api/endpoints/my/apps.ts +++ b/src/server/api/endpoints/my/apps.ts @@ -1,7 +1,6 @@ import $ from 'cafy'; import App, { pack } from '../../../../models/app'; -import { ILocalUser } from '../../../../models/user'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { desc: { @@ -24,10 +23,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { const query = { userId: user._id }; @@ -46,4 +42,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = res(await Promise.all(apps.map(app => pack(app, user, { detail: true })))); -}); +})); diff --git a/src/server/api/endpoints/notes.ts b/src/server/api/endpoints/notes.ts index 83776c59d0..5edc56165e 100644 --- a/src/server/api/endpoints/notes.ts +++ b/src/server/api/endpoints/notes.ts @@ -1,6 +1,6 @@ import $ from 'cafy'; import ID, { transform } from '../../../misc/cafy-id'; import Note, { packMany } from '../../../models/note'; -import getParams from '../get-params'; +import define from '../define'; export const meta = { desc: { @@ -67,10 +67,7 @@ export const meta = { } }; -export default (params: any) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps) => new Promise(async (res, rej) => { // Check if both of sinceId and untilId is specified if (ps.sinceId && ps.untilId) { return rej('cannot set sinceId and untilId'); @@ -130,4 +127,4 @@ export default (params: any) => new Promise(async (res, rej) => { // Serialize res(await packMany(notes)); -}); +})); diff --git a/src/server/api/endpoints/notes/conversation.ts b/src/server/api/endpoints/notes/conversation.ts index c7be6d6e3f..7703ef7d2e 100644 --- a/src/server/api/endpoints/notes/conversation.ts +++ b/src/server/api/endpoints/notes/conversation.ts @@ -1,7 +1,6 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; import Note, { packMany, INote } from '../../../../models/note'; -import { ILocalUser } from '../../../../models/user'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { desc: { @@ -29,10 +28,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Lookup note const note = await Note.findOne({ _id: ps.noteId @@ -67,4 +63,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = } res(await packMany(conversation, user)); -}); +})); diff --git a/src/server/api/endpoints/notes/create.ts b/src/server/api/endpoints/notes/create.ts index 9eff8c27bf..8a8813daba 100644 --- a/src/server/api/endpoints/notes/create.ts +++ b/src/server/api/endpoints/notes/create.ts @@ -1,11 +1,10 @@ import $ from 'cafy'; import ID, { transform, transformMany } from '../../../../misc/cafy-id'; const ms = require('ms'); import Note, { INote, isValidText, isValidCw, pack } from '../../../../models/note'; -import User, { ILocalUser, IUser } from '../../../../models/user'; +import User, { IUser } from '../../../../models/user'; import DriveFile, { IDriveFile } from '../../../../models/drive-file'; import create from '../../../../services/note/create'; -import { IApp } from '../../../../models/app'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { stability: 'stable', @@ -139,10 +138,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser, app: IApp) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user, app) => new Promise(async (res, rej) => { let visibleUsers: IUser[] = []; if (ps.visibleUserIds) { visibleUsers = await Promise.all(ps.visibleUserIds.map(id => User.findOne({ @@ -229,4 +225,4 @@ export default (params: any, user: ILocalUser, app: IApp) => new Promise(async ( res({ createdNote: noteObj }); -}); +})); diff --git a/src/server/api/endpoints/notes/delete.ts b/src/server/api/endpoints/notes/delete.ts index 580cb344e1..a8f22ad405 100644 --- a/src/server/api/endpoints/notes/delete.ts +++ b/src/server/api/endpoints/notes/delete.ts @@ -1,8 +1,8 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; import Note from '../../../../models/note'; import deleteNote from '../../../../services/note/delete'; -import User, { ILocalUser } from '../../../../models/user'; -import getParams from '../../get-params'; +import User from '../../../../models/user'; +import define from '../../define'; export const meta = { stability: 'stable', @@ -28,10 +28,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Fetch note const note = await Note.findOne({ _id: ps.noteId @@ -48,4 +45,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = await deleteNote(await User.findOne({ _id: note.userId }), note); res(); -}); +})); diff --git a/src/server/api/endpoints/notes/favorites/create.ts b/src/server/api/endpoints/notes/favorites/create.ts index e489c77761..a50b5002c2 100644 --- a/src/server/api/endpoints/notes/favorites/create.ts +++ b/src/server/api/endpoints/notes/favorites/create.ts @@ -1,8 +1,7 @@ import $ from 'cafy'; import ID, { transform } from '../../../../../misc/cafy-id'; import Favorite from '../../../../../models/favorite'; import Note from '../../../../../models/note'; -import { ILocalUser } from '../../../../../models/user'; -import getParams from '../../../get-params'; +import define from '../../../define'; export const meta = { stability: 'stable', @@ -28,10 +27,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Get favoritee const note = await Note.findOne({ _id: ps.noteId @@ -60,4 +56,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = // Send response res(); -}); +})); diff --git a/src/server/api/endpoints/notes/favorites/delete.ts b/src/server/api/endpoints/notes/favorites/delete.ts index ac1b35d194..0aad61fedd 100644 --- a/src/server/api/endpoints/notes/favorites/delete.ts +++ b/src/server/api/endpoints/notes/favorites/delete.ts @@ -1,8 +1,7 @@ import $ from 'cafy'; import ID, { transform } from '../../../../../misc/cafy-id'; import Favorite from '../../../../../models/favorite'; import Note from '../../../../../models/note'; -import { ILocalUser } from '../../../../../models/user'; -import getParams from '../../../get-params'; +import define from '../../../define'; export const meta = { stability: 'stable', @@ -28,10 +27,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Get favoritee const note = await Note.findOne({ _id: ps.noteId @@ -58,4 +54,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = // Send response res(); -}); +})); diff --git a/src/server/api/endpoints/notes/featured.ts b/src/server/api/endpoints/notes/featured.ts index c031308824..823137ac21 100644 --- a/src/server/api/endpoints/notes/featured.ts +++ b/src/server/api/endpoints/notes/featured.ts @@ -1,8 +1,7 @@ import $ from 'cafy'; import Note from '../../../../models/note'; import { packMany } from '../../../../models/note'; -import { ILocalUser } from '../../../../models/user'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { desc: { @@ -23,10 +22,7 @@ export const meta = { } }; -export default async (params: any, user: ILocalUser) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) throw psErr; - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { const day = 1000 * 60 * 60 * 24; const notes = await Note @@ -46,5 +42,5 @@ export default async (params: any, user: ILocalUser) => { } }); - return await packMany(notes, user); -}; + res(await packMany(notes, user)); +})); diff --git a/src/server/api/endpoints/notes/global-timeline.ts b/src/server/api/endpoints/notes/global-timeline.ts index fa7a76f12e..b7f765f27d 100644 --- a/src/server/api/endpoints/notes/global-timeline.ts +++ b/src/server/api/endpoints/notes/global-timeline.ts @@ -2,8 +2,7 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; import Note from '../../../../models/note'; import Mute from '../../../../models/mute'; import { packMany } from '../../../../models/note'; -import { ILocalUser } from '../../../../models/user'; -import getParams from '../../get-params'; +import define from '../../define'; import { countIf } from '../../../../prelude/array'; export const meta = { @@ -51,13 +50,10 @@ export const meta = { } }; -export default async (params: any, user: ILocalUser) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) throw psErr; - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Check if only one of sinceId, untilId, sinceDate, untilDate specified if (countIf(x => x != null, [ps.sinceId, ps.untilId, ps.sinceDate, ps.untilDate]) > 1) { - throw 'only one of sinceId, untilId, sinceDate, untilDate can be specified'; + return rej('only one of sinceId, untilId, sinceDate, untilDate can be specified'); } // ミュートしているユーザーを取得 @@ -120,13 +116,11 @@ export default async (params: any, user: ILocalUser) => { } //#endregion - // Issue query const timeline = await Note .find(query, { limit: ps.limit, sort: sort }); - // Serialize - return await packMany(timeline, user); -}; + res(await packMany(timeline, user)); +})); diff --git a/src/server/api/endpoints/notes/hybrid-timeline.ts b/src/server/api/endpoints/notes/hybrid-timeline.ts index 0008144823..4af182cb5c 100644 --- a/src/server/api/endpoints/notes/hybrid-timeline.ts +++ b/src/server/api/endpoints/notes/hybrid-timeline.ts @@ -3,8 +3,7 @@ import Note from '../../../../models/note'; import Mute from '../../../../models/mute'; import { getFriends } from '../../common/get-friends'; import { packMany } from '../../../../models/note'; -import { ILocalUser } from '../../../../models/user'; -import getParams from '../../get-params'; +import define from '../../define'; import { countIf } from '../../../../prelude/array'; export const meta = { @@ -91,13 +90,10 @@ export const meta = { } }; -export default async (params: any, user: ILocalUser) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) throw psErr; - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Check if only one of sinceId, untilId, sinceDate, untilDate specified if (countIf(x => x != null, [ps.sinceId, ps.untilId, ps.sinceDate, ps.untilDate]) > 1) { - throw 'only one of sinceId, untilId, sinceDate, untilDate can be specified'; + return rej('only one of sinceId, untilId, sinceDate, untilDate can be specified'); } const [followings, mutedUserIds] = await Promise.all([ @@ -246,13 +242,11 @@ export default async (params: any, user: ILocalUser) => { } //#endregion - // Issue query const timeline = await Note .find(query, { limit: ps.limit, sort: sort }); - // Serialize - return await packMany(timeline, user); -}; + res(await packMany(timeline, user)); +})); diff --git a/src/server/api/endpoints/notes/local-timeline.ts b/src/server/api/endpoints/notes/local-timeline.ts index 78ddf00626..4446f52cdc 100644 --- a/src/server/api/endpoints/notes/local-timeline.ts +++ b/src/server/api/endpoints/notes/local-timeline.ts @@ -2,8 +2,7 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; import Note from '../../../../models/note'; import Mute from '../../../../models/mute'; import { packMany } from '../../../../models/note'; -import { ILocalUser } from '../../../../models/user'; -import getParams from '../../get-params'; +import define from '../../define'; import { countIf } from '../../../../prelude/array'; export const meta = { @@ -66,13 +65,10 @@ export const meta = { } }; -export default async (params: any, user: ILocalUser) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) throw psErr; - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Check if only one of sinceId, untilId, sinceDate, untilDate specified if (countIf(x => x != null, [ps.sinceId, ps.untilId, ps.sinceDate, ps.untilDate]) > 1) { - throw 'only one of sinceId, untilId, sinceDate, untilDate can be specified'; + return rej('only one of sinceId, untilId, sinceDate, untilDate can be specified'); } // ミュートしているユーザーを取得 @@ -150,13 +146,11 @@ export default async (params: any, user: ILocalUser) => { } //#endregion - // Issue query const timeline = await Note .find(query, { limit: ps.limit, sort: sort }); - // Serialize - return await packMany(timeline, user); -}; + res(await packMany(timeline, user)); +})); diff --git a/src/server/api/endpoints/notes/mentions.ts b/src/server/api/endpoints/notes/mentions.ts index 3fc3977075..718f5e4403 100644 --- a/src/server/api/endpoints/notes/mentions.ts +++ b/src/server/api/endpoints/notes/mentions.ts @@ -2,8 +2,7 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; import Note from '../../../../models/note'; import { getFriendIds } from '../../common/get-friends'; import { packMany } from '../../../../models/note'; -import { ILocalUser } from '../../../../models/user'; -import getParams from '../../get-params'; +import define from '../../define'; import read from '../../../../services/note/read'; export const meta = { @@ -41,10 +40,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Check if both of sinceId and untilId is specified if (ps.sinceId && ps.untilId) { return rej('cannot set sinceId and untilId'); @@ -96,4 +92,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = res(await packMany(mentions, user)); mentions.forEach(note => read(user._id, note._id)); -}); +})); diff --git a/src/server/api/endpoints/notes/polls/recommendation.ts b/src/server/api/endpoints/notes/polls/recommendation.ts index 34250a32f9..cfcba788ec 100644 --- a/src/server/api/endpoints/notes/polls/recommendation.ts +++ b/src/server/api/endpoints/notes/polls/recommendation.ts @@ -1,8 +1,7 @@ import $ from 'cafy'; import Vote from '../../../../../models/poll-vote'; import Note, { pack } from '../../../../../models/note'; -import { ILocalUser } from '../../../../../models/user'; -import getParams from '../../../get-params'; +import define from '../../../define'; export const meta = { desc: { @@ -25,10 +24,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Get votes const votes = await Vote.find({ userId: user._id @@ -64,4 +60,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = res(await Promise.all(notes.map(note => pack(note, user, { detail: true })))); -}); +})); diff --git a/src/server/api/endpoints/notes/polls/vote.ts b/src/server/api/endpoints/notes/polls/vote.ts index 32dcefec7b..f50e84ba13 100644 --- a/src/server/api/endpoints/notes/polls/vote.ts +++ b/src/server/api/endpoints/notes/polls/vote.ts @@ -5,8 +5,7 @@ import Watching from '../../../../../models/note-watching'; import watch from '../../../../../services/note/watch'; import { publishNoteStream } from '../../../../../stream'; import notify from '../../../../../notify'; -import { ILocalUser } from '../../../../../models/user'; -import getParams from '../../../get-params'; +import define from '../../../define'; export const meta = { desc: { @@ -30,10 +29,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Get votee const note = await Note.findOne({ _id: ps.noteId @@ -114,4 +110,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = if (user.settings.autoWatch !== false) { watch(user._id, note); } -}); +})); diff --git a/src/server/api/endpoints/notes/reactions.ts b/src/server/api/endpoints/notes/reactions.ts index 1ab5afaba1..a1b249638f 100644 --- a/src/server/api/endpoints/notes/reactions.ts +++ b/src/server/api/endpoints/notes/reactions.ts @@ -1,8 +1,7 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; import Note from '../../../../models/note'; import Reaction, { pack } from '../../../../models/note-reaction'; -import { ILocalUser } from '../../../../models/user'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { desc: { @@ -44,10 +43,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Check if both of sinceId and untilId is specified if (ps.sinceId && ps.untilId) { return rej('cannot set sinceId and untilId'); @@ -90,4 +86,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = // Serialize res(await Promise.all(reactions.map(reaction => pack(reaction, user)))); -}); +})); diff --git a/src/server/api/endpoints/notes/reactions/create.ts b/src/server/api/endpoints/notes/reactions/create.ts index f2b06473a5..c9f70d9658 100644 --- a/src/server/api/endpoints/notes/reactions/create.ts +++ b/src/server/api/endpoints/notes/reactions/create.ts @@ -2,8 +2,7 @@ import $ from 'cafy'; import ID, { transform } from '../../../../../misc/cafy-id import Note from '../../../../../models/note'; import create from '../../../../../services/note/reaction/create'; import { validateReaction } from '../../../../../models/note-reaction'; -import { ILocalUser } from '../../../../../models/user'; -import getParams from '../../../get-params'; +import define from '../../../define'; export const meta = { stability: 'stable', @@ -35,10 +34,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Fetch reactee const note = await Note.findOne({ _id: ps.noteId @@ -59,4 +55,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = } res(); -}); +})); diff --git a/src/server/api/endpoints/notes/reactions/delete.ts b/src/server/api/endpoints/notes/reactions/delete.ts index 2a2577dfe6..60f49fb275 100644 --- a/src/server/api/endpoints/notes/reactions/delete.ts +++ b/src/server/api/endpoints/notes/reactions/delete.ts @@ -1,8 +1,7 @@ import $ from 'cafy'; import ID, { transform } from '../../../../../misc/cafy-id'; import Reaction from '../../../../../models/note-reaction'; import Note from '../../../../../models/note'; -import { ILocalUser } from '../../../../../models/user'; -import getParams from '../../../get-params'; +import define from '../../../define'; export const meta = { desc: { @@ -22,10 +21,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Fetch unreactee const note = await Note.findOne({ _id: ps.noteId @@ -64,4 +60,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = Note.update({ _id: note._id }, { $inc: dec }); -}); +})); diff --git a/src/server/api/endpoints/notes/renotes.ts b/src/server/api/endpoints/notes/renotes.ts index ff926806eb..85d5820e58 100644 --- a/src/server/api/endpoints/notes/renotes.ts +++ b/src/server/api/endpoints/notes/renotes.ts @@ -1,7 +1,6 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; import Note, { packMany } from '../../../../models/note'; -import { ILocalUser } from '../../../../models/user'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { desc: { @@ -34,10 +33,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Check if both of sinceId and untilId is specified if (ps.sinceId && ps.untilId) { return rej('cannot set sinceId and untilId'); @@ -78,4 +74,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = }); res(await packMany(renotes, user)); -}); +})); diff --git a/src/server/api/endpoints/notes/replies.ts b/src/server/api/endpoints/notes/replies.ts index 86a75c1cab..8b5a3593d1 100644 --- a/src/server/api/endpoints/notes/replies.ts +++ b/src/server/api/endpoints/notes/replies.ts @@ -1,7 +1,6 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; import Note, { packMany } from '../../../../models/note'; -import { ILocalUser } from '../../../../models/user'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { desc: { @@ -29,10 +28,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Lookup note const note = await Note.findOne({ _id: ps.noteId @@ -45,4 +41,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = const ids = (note._replyIds || []).slice(ps.offset, ps.offset + ps.limit); res(await packMany(ids, user)); -}); +})); diff --git a/src/server/api/endpoints/notes/search.ts b/src/server/api/endpoints/notes/search.ts index 63a87c0f56..e107ca4e15 100644 --- a/src/server/api/endpoints/notes/search.ts +++ b/src/server/api/endpoints/notes/search.ts @@ -1,10 +1,9 @@ import $ from 'cafy'; import * as mongo from 'mongodb'; import Note from '../../../../models/note'; -import { ILocalUser } from '../../../../models/user'; import { packMany } from '../../../../models/note'; import es from '../../../../db/elasticsearch'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { desc: { @@ -31,10 +30,7 @@ export const meta = { } }; -export default (params: any, me: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, me) => new Promise(async (res, rej) => { if (es == null) return rej('searching not available'); es.search({ @@ -79,4 +75,4 @@ export default (params: any, me: ILocalUser) => new Promise(async (res, rej) => res(await packMany(notes, me)); }); -}); +})); diff --git a/src/server/api/endpoints/notes/search_by_tag.ts b/src/server/api/endpoints/notes/search_by_tag.ts index 99a6dce834..fcc33d14f3 100644 --- a/src/server/api/endpoints/notes/search_by_tag.ts +++ b/src/server/api/endpoints/notes/search_by_tag.ts @@ -1,10 +1,9 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; import Note from '../../../../models/note'; -import { ILocalUser } from '../../../../models/user'; import Mute from '../../../../models/mute'; import { getFriendIds } from '../../common/get-friends'; import { packMany } from '../../../../models/note'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { desc: { @@ -103,10 +102,7 @@ export const meta = { } }; -export default (params: any, me: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, me) => new Promise(async (res, rej) => { const q: any = { $and: [ps.tag ? { tagsLower: ps.tag.toLowerCase() @@ -322,4 +318,4 @@ export default (params: any, me: ILocalUser) => new Promise(async (res, rej) => // Serialize res(await packMany(notes, me)); -}); +})); diff --git a/src/server/api/endpoints/notes/show.ts b/src/server/api/endpoints/notes/show.ts index 8b426c0053..53246102d0 100644 --- a/src/server/api/endpoints/notes/show.ts +++ b/src/server/api/endpoints/notes/show.ts @@ -1,7 +1,6 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; import Note, { pack } from '../../../../models/note'; -import { ILocalUser } from '../../../../models/user'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { stability: 'stable', @@ -25,10 +24,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Get note const note = await Note.findOne({ _id: ps.noteId @@ -42,4 +38,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = res(await pack(note, user, { detail: true })); -}); +})); diff --git a/src/server/api/endpoints/notes/timeline.ts b/src/server/api/endpoints/notes/timeline.ts index 8834645fc8..3c970c03a1 100644 --- a/src/server/api/endpoints/notes/timeline.ts +++ b/src/server/api/endpoints/notes/timeline.ts @@ -3,8 +3,7 @@ import Note from '../../../../models/note'; import Mute from '../../../../models/mute'; import { getFriends } from '../../common/get-friends'; import { packMany } from '../../../../models/note'; -import { ILocalUser } from '../../../../models/user'; -import getParams from '../../get-params'; +import define from '../../define'; import { countIf } from '../../../../prelude/array'; export const meta = { @@ -94,13 +93,11 @@ export const meta = { } }; -export default async (params: any, user: ILocalUser) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) throw psErr; - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Check if only one of sinceId, untilId, sinceDate, untilDate specified if (countIf(x => x != null, [ps.sinceId, ps.untilId, ps.sinceDate, ps.untilDate]) > 1) { - throw 'only one of sinceId, untilId, sinceDate, untilDate can be specified'; + rej('only one of sinceId, untilId, sinceDate, untilDate can be specified'); + return; } const [followings, mutedUserIds] = await Promise.all([ @@ -251,5 +248,5 @@ export default async (params: any, user: ILocalUser) => { }); // Serialize - return await packMany(timeline, user); -}; + res(await packMany(timeline, user)); +})); diff --git a/src/server/api/endpoints/notes/user-list-timeline.ts b/src/server/api/endpoints/notes/user-list-timeline.ts index 3bdcc6c121..156ffbbc32 100644 --- a/src/server/api/endpoints/notes/user-list-timeline.ts +++ b/src/server/api/endpoints/notes/user-list-timeline.ts @@ -3,8 +3,7 @@ import Note from '../../../../models/note'; import Mute from '../../../../models/mute'; import { packMany } from '../../../../models/note'; import UserList from '../../../../models/user-list'; -import { ILocalUser } from '../../../../models/user'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { desc: { @@ -101,10 +100,7 @@ export const meta = { } }; -export default async (params: any, user: ILocalUser) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) throw psErr; - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { const [list, mutedUserIds] = await Promise.all([ // リストを取得 // Fetch the list @@ -120,7 +116,8 @@ export default async (params: any, user: ILocalUser) => { ]); if (list.userIds.length == 0) { - return []; + res([]); + return; } //#region Construct query @@ -258,5 +255,5 @@ export default async (params: any, user: ILocalUser) => { }); // Serialize - return await packMany(timeline, user); -}; + res(await packMany(timeline, user)); +})); diff --git a/src/server/api/endpoints/notifications/mark_all_as_read.ts b/src/server/api/endpoints/notifications/mark_all_as_read.ts index 6487cd8b48..1b5208d447 100644 --- a/src/server/api/endpoints/notifications/mark_all_as_read.ts +++ b/src/server/api/endpoints/notifications/mark_all_as_read.ts @@ -1,6 +1,7 @@ import Notification from '../../../../models/notification'; import { publishMainStream } from '../../../../stream'; -import User, { ILocalUser } from '../../../../models/user'; +import User from '../../../../models/user'; +import define from '../../define'; export const meta = { desc: { @@ -13,10 +14,7 @@ export const meta = { kind: 'notification-write' }; -/** - * Mark all notifications as read - */ -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Update documents await Notification.update({ notifieeId: user._id, @@ -41,4 +39,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = // 全ての通知を読みましたよというイベントを発行 publishMainStream(user._id, 'readAllNotifications'); -}); +})); diff --git a/src/server/api/endpoints/stats.ts b/src/server/api/endpoints/stats.ts index fc195da22d..56549848dc 100644 --- a/src/server/api/endpoints/stats.ts +++ b/src/server/api/endpoints/stats.ts @@ -1,10 +1,19 @@ import Meta from '../../../models/meta'; +import define from '../define'; -/** - * Get the misskey's statistics - */ -export default () => new Promise(async (res, rej) => { +export const meta = { + requireCredential: false, + + desc: { + 'en-US': 'Get the instance\'s statistics' + }, + + params: { + } +}; + +export default define(meta, () => new Promise(async (res, rej) => { const meta = await Meta.findOne(); res(meta ? meta.stats : {}); -}); +})); diff --git a/src/server/api/endpoints/sw/register.ts b/src/server/api/endpoints/sw/register.ts index 69337bd8c0..26427452fd 100644 --- a/src/server/api/endpoints/sw/register.ts +++ b/src/server/api/endpoints/sw/register.ts @@ -1,8 +1,7 @@ import $ from 'cafy'; import Subscription from '../../../../models/sw-subscription'; -import { ILocalUser } from '../../../../models/user'; import config from '../../../../config'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { requireCredential: true, @@ -22,10 +21,7 @@ export const meta = { } }; -export default async (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // if already subscribed const exist = await Subscription.findOne({ userId: user._id, @@ -53,4 +49,4 @@ export default async (params: any, user: ILocalUser) => new Promise(async (res, state: 'subscribed', key: config.sw.public_key }); -}); +})); diff --git a/src/server/api/endpoints/username/available.ts b/src/server/api/endpoints/username/available.ts index b61be84c3b..a5f0b5943f 100644 --- a/src/server/api/endpoints/username/available.ts +++ b/src/server/api/endpoints/username/available.ts @@ -1,7 +1,7 @@ import $ from 'cafy'; import User from '../../../../models/user'; import { validateUsername } from '../../../../models/user'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { requireCredential: false, @@ -13,10 +13,7 @@ export const meta = { } }; -export default async (params: any) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps) => new Promise(async (res, rej) => { // Get exist const exist = await User .count({ @@ -30,4 +27,4 @@ export default async (params: any) => new Promise(async (res, rej) => { res({ available: exist === 0 }); -}); +})); diff --git a/src/server/api/endpoints/users.ts b/src/server/api/endpoints/users.ts index 81751504e7..6fb50e6712 100644 --- a/src/server/api/endpoints/users.ts +++ b/src/server/api/endpoints/users.ts @@ -1,6 +1,6 @@ import $ from 'cafy'; import User, { pack, ILocalUser } from '../../../models/user'; -import getParams from '../get-params'; +import define from '../define'; export const meta = { requireCredential: false, @@ -22,10 +22,7 @@ export const meta = { } }; -export default (params: any, me: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, me) => new Promise(async (res, rej) => { let _sort; if (ps.sort) { if (ps.sort == '+follower') { @@ -53,4 +50,4 @@ export default (params: any, me: ILocalUser) => new Promise(async (res, rej) => }); res(await Promise.all(users.map(user => pack(user, me)))); -}); +})); diff --git a/src/server/api/endpoints/users/followers.ts b/src/server/api/endpoints/users/followers.ts index 71081835c7..9e3a30f6f1 100644 --- a/src/server/api/endpoints/users/followers.ts +++ b/src/server/api/endpoints/users/followers.ts @@ -1,9 +1,9 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; -import User, { ILocalUser } from '../../../../models/user'; +import User from '../../../../models/user'; import Following from '../../../../models/following'; import { pack } from '../../../../models/user'; import { getFriendIds } from '../../common/get-friends'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { desc: { @@ -37,10 +37,7 @@ export const meta = { } }; -export default (params: any, me: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, me) => new Promise(async (res, rej) => { // Lookup user const user = await User.findOne({ _id: ps.userId @@ -95,4 +92,4 @@ export default (params: any, me: ILocalUser) => new Promise(async (res, rej) => users: users, next: inStock ? following[following.length - 1]._id : null, }); -}); +})); diff --git a/src/server/api/endpoints/users/following.ts b/src/server/api/endpoints/users/following.ts index 778ef54a27..57bf1e1e8b 100644 --- a/src/server/api/endpoints/users/following.ts +++ b/src/server/api/endpoints/users/following.ts @@ -1,9 +1,9 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; -import User, { ILocalUser } from '../../../../models/user'; +import User from '../../../../models/user'; import Following from '../../../../models/following'; import { pack } from '../../../../models/user'; import { getFriendIds } from '../../common/get-friends'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { desc: { @@ -37,10 +37,7 @@ export const meta = { } }; -export default (params: any, me: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, me) => new Promise(async (res, rej) => { // Lookup user const user = await User.findOne({ _id: ps.userId @@ -95,4 +92,4 @@ export default (params: any, me: ILocalUser) => new Promise(async (res, rej) => users: users, next: inStock ? following[following.length - 1]._id : null, }); -}); +})); diff --git a/src/server/api/endpoints/users/get_frequently_replied_users.ts b/src/server/api/endpoints/users/get_frequently_replied_users.ts index b0fd259588..6b8b985eea 100644 --- a/src/server/api/endpoints/users/get_frequently_replied_users.ts +++ b/src/server/api/endpoints/users/get_frequently_replied_users.ts @@ -1,7 +1,7 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; import Note from '../../../../models/note'; -import User, { pack, ILocalUser } from '../../../../models/user'; -import getParams from '../../get-params'; +import User, { pack } from '../../../../models/user'; +import define from '../../define'; export const meta = { requireCredential: false, @@ -19,10 +19,7 @@ export const meta = { } }; -export default (params: any, me: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, me) => new Promise(async (res, rej) => { // Lookup user const user = await User.findOne({ _id: ps.userId @@ -104,4 +101,4 @@ export default (params: any, me: ILocalUser) => new Promise(async (res, rej) => }))); res(repliesObj); -}); +})); diff --git a/src/server/api/endpoints/users/lists/create.ts b/src/server/api/endpoints/users/lists/create.ts index c14bb64daa..d93cf13d94 100644 --- a/src/server/api/endpoints/users/lists/create.ts +++ b/src/server/api/endpoints/users/lists/create.ts @@ -1,7 +1,6 @@ import $ from 'cafy'; import UserList, { pack } from '../../../../../models/user-list'; -import { ILocalUser } from '../../../../../models/user'; -import getParams from '../../../get-params'; +import define from '../../../define'; export const meta = { desc: { @@ -20,10 +19,7 @@ export const meta = { } }; -export default async (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // insert const userList = await UserList.insert({ createdAt: new Date(), @@ -34,4 +30,4 @@ export default async (params: any, user: ILocalUser) => new Promise(async (res, // Response res(await pack(userList)); -}); +})); diff --git a/src/server/api/endpoints/users/lists/delete.ts b/src/server/api/endpoints/users/lists/delete.ts index 1d4513a822..8d297198c9 100644 --- a/src/server/api/endpoints/users/lists/delete.ts +++ b/src/server/api/endpoints/users/lists/delete.ts @@ -1,8 +1,7 @@ import $ from 'cafy'; import ID, { transform } from '../../../../../misc/cafy-id'; import UserList from '../../../../../models/user-list'; -import { ILocalUser } from '../../../../../models/user'; -import getParams from '../../../get-params'; +import define from '../../../define'; export const meta = { desc: { @@ -26,10 +25,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { const userList = await UserList.findOne({ _id: ps.listId, userId: user._id @@ -44,4 +40,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = }); res(); -}); +})); diff --git a/src/server/api/endpoints/users/lists/list.ts b/src/server/api/endpoints/users/lists/list.ts index 966e1d3ad9..8f9775e19b 100644 --- a/src/server/api/endpoints/users/lists/list.ts +++ b/src/server/api/endpoints/users/lists/list.ts @@ -1,5 +1,5 @@ import UserList, { pack } from '../../../../../models/user-list'; -import { ILocalUser } from '../../../../../models/user'; +import define from '../../../define'; export const meta = { desc: { @@ -11,11 +11,11 @@ export const meta = { kind: 'account-read' }; -export default async (params: any, me: ILocalUser) => new Promise(async (res, rej) => { +export default define(meta, (ps, me) => new Promise(async (res, rej) => { // Fetch lists const userLists = await UserList.find({ userId: me._id, }); res(await Promise.all(userLists.map(x => pack(x)))); -}); +})); diff --git a/src/server/api/endpoints/users/lists/push.ts b/src/server/api/endpoints/users/lists/push.ts index 8208e627ed..5c11f25dc4 100644 --- a/src/server/api/endpoints/users/lists/push.ts +++ b/src/server/api/endpoints/users/lists/push.ts @@ -1,11 +1,11 @@ import $ from 'cafy'; import ID, { transform } from '../../../../../misc/cafy-id'; import UserList from '../../../../../models/user-list'; -import User, { pack as packUser, isRemoteUser, getGhost, ILocalUser } from '../../../../../models/user'; +import User, { pack as packUser, isRemoteUser, getGhost } from '../../../../../models/user'; import { publishUserListStream } from '../../../../../stream'; import ap from '../../../../../remote/activitypub/renderer'; import renderFollow from '../../../../../remote/activitypub/renderer/follow'; import { deliver } from '../../../../../queue'; -import getParams from '../../../get-params'; +import define from '../../../define'; export const meta = { desc: { @@ -30,10 +30,7 @@ export const meta = { } }; -export default async (params: any, me: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, me) => new Promise(async (res, rej) => { // Fetch the list const userList = await UserList.findOne({ _id: ps.listId, @@ -74,4 +71,4 @@ export default async (params: any, me: ILocalUser) => new Promise(async (res, re const content = ap(renderFollow(ghost, user)); deliver(ghost, content, user.inbox); } -}); +})); diff --git a/src/server/api/endpoints/users/lists/show.ts b/src/server/api/endpoints/users/lists/show.ts index 585833a2fe..2662ab2769 100644 --- a/src/server/api/endpoints/users/lists/show.ts +++ b/src/server/api/endpoints/users/lists/show.ts @@ -1,7 +1,6 @@ import $ from 'cafy'; import ID, { transform } from '../../../../../misc/cafy-id'; import UserList, { pack } from '../../../../../models/user-list'; -import { ILocalUser } from '../../../../../models/user'; -import getParams from '../../../get-params'; +import define from '../../../define'; export const meta = { desc: { @@ -21,10 +20,7 @@ export const meta = { } }; -export default async (params: any, me: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, me) => new Promise(async (res, rej) => { // Fetch the list const userList = await UserList.findOne({ _id: ps.listId, @@ -36,4 +32,4 @@ export default async (params: any, me: ILocalUser) => new Promise(async (res, re } res(await pack(userList)); -}); +})); diff --git a/src/server/api/endpoints/users/lists/update.ts b/src/server/api/endpoints/users/lists/update.ts index fb1a37b2f1..79a7da5658 100644 --- a/src/server/api/endpoints/users/lists/update.ts +++ b/src/server/api/endpoints/users/lists/update.ts @@ -1,8 +1,7 @@ import $ from 'cafy'; import ID, { transform } from '../../../../../misc/cafy-id'; import UserList, { pack } from '../../../../../models/user-list'; -import { ILocalUser } from '../../../../../models/user'; -import getParams from '../../../get-params'; +import define from '../../../define'; export const meta = { desc: { @@ -34,10 +33,7 @@ export const meta = { } }; -export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, user) => new Promise(async (res, rej) => { // Fetch the list const userList = await UserList.findOne({ _id: ps.listId, @@ -57,4 +53,4 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = // Response res(await pack(userList._id)); -}); +})); diff --git a/src/server/api/endpoints/users/notes.ts b/src/server/api/endpoints/users/notes.ts index ac6601f64f..4bbacab2bf 100644 --- a/src/server/api/endpoints/users/notes.ts +++ b/src/server/api/endpoints/users/notes.ts @@ -1,8 +1,8 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; import getHostLower from '../../common/get-host-lower'; import Note, { packMany } from '../../../../models/note'; -import User, { ILocalUser } from '../../../../models/user'; -import getParams from '../../get-params'; +import User from '../../../../models/user'; +import define from '../../define'; import { countIf } from '../../../../prelude/array'; export const meta = { @@ -126,10 +126,7 @@ export const meta = { } }; -export default (params: any, me: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, me) => new Promise(async (res, rej) => { if (ps.userId === undefined && ps.username === undefined) { return rej('userId or username is required'); } @@ -215,4 +212,4 @@ export default (params: any, me: ILocalUser) => new Promise(async (res, rej) => // Serialize res(await packMany(notes, me)); -}); +})); diff --git a/src/server/api/endpoints/users/recommendation.ts b/src/server/api/endpoints/users/recommendation.ts index 47c809ee4c..127029f83c 100644 --- a/src/server/api/endpoints/users/recommendation.ts +++ b/src/server/api/endpoints/users/recommendation.ts @@ -1,11 +1,11 @@ const ms = require('ms'); import $ from 'cafy'; -import User, { pack, ILocalUser } from '../../../../models/user'; +import User, { pack } from '../../../../models/user'; import { getFriendIds } from '../../common/get-friends'; import Mute from '../../../../models/mute'; import * as request from 'request'; import config from '../../../../config'; -import getParams from '../../get-params'; +import define from '../../define'; export const meta = { desc: { @@ -29,22 +29,19 @@ export const meta = { } }; -export default (params: any, me: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, me) => new Promise(async (res, rej) => { if (config.user_recommendation && config.user_recommendation.external) { const userName = me.username; const hostName = config.hostname; - const limit = params.limit; - const offset = params.offset; + const limit = ps.limit; + const offset = ps.offset; const timeout = config.user_recommendation.timeout; const engine = config.user_recommendation.engine; const url = engine .replace('{{host}}', hostName) .replace('{{user}}', userName) - .replace('{{limit}}', limit) - .replace('{{offset}}', offset); + .replace('{{limit}}', limit.toString()) + .replace('{{offset}}', offset.toString()); request({ url: url, @@ -89,4 +86,4 @@ export default (params: any, me: ILocalUser) => new Promise(async (res, rej) => res(await Promise.all(users.map(user => pack(user, me, { detail: true })))); } -}); +})); diff --git a/src/server/api/endpoints/users/relation.ts b/src/server/api/endpoints/users/relation.ts index 19643ceed0..c63ec337bf 100644 --- a/src/server/api/endpoints/users/relation.ts +++ b/src/server/api/endpoints/users/relation.ts @@ -1,6 +1,6 @@ import $ from 'cafy'; import ID, { transform, ObjectId } from '../../../../misc/cafy-id'; -import { ILocalUser, getRelation } from '../../../../models/user'; -import getParams from '../../get-params'; +import { getRelation } from '../../../../models/user'; +import define from '../../define'; export const meta = { desc: { @@ -20,13 +20,10 @@ export const meta = { } }; -export default (params: any, me: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, me) => new Promise(async (res, rej) => { const ids = Array.isArray(ps.userId) ? ps.userId : [ps.userId]; const relations = await Promise.all(ids.map(id => getRelation(me._id, id))); res(Array.isArray(ps.userId) ? relations : relations[0]); -}); +})); diff --git a/src/server/api/endpoints/users/search.ts b/src/server/api/endpoints/users/search.ts index a2077b589d..edc4d603ca 100644 --- a/src/server/api/endpoints/users/search.ts +++ b/src/server/api/endpoints/users/search.ts @@ -1,7 +1,7 @@ import $ from 'cafy'; const escapeRegexp = require('escape-regexp'); -import User, { pack, ILocalUser, validateUsername, IUser } from '../../../../models/user'; -import getParams from '../../get-params'; +import User, { pack, validateUsername, IUser } from '../../../../models/user'; +import define from '../../define'; export const meta = { desc: { @@ -44,13 +44,7 @@ export const meta = { }, }; -/** - * Search a user - */ -export default (params: any, me: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, me) => new Promise(async (res, rej) => { const isUsername = validateUsername(ps.query.replace('@', '')); let users: IUser[] = []; @@ -158,4 +152,4 @@ export default (params: any, me: ILocalUser) => new Promise(async (res, rej) => // Serialize res(await Promise.all(users.map(user => pack(user, me, { detail: true })))); -}); +})); diff --git a/src/server/api/endpoints/users/show.ts b/src/server/api/endpoints/users/show.ts index c6f01593d4..93223b3f9f 100644 --- a/src/server/api/endpoints/users/show.ts +++ b/src/server/api/endpoints/users/show.ts @@ -1,7 +1,7 @@ import $ from 'cafy'; import ID, { transform, transformMany } from '../../../../misc/cafy-id'; -import User, { pack, ILocalUser, isRemoteUser } from '../../../../models/user'; +import User, { pack, isRemoteUser } from '../../../../models/user'; import resolveRemoteUser from '../../../../remote/resolve-user'; -import getParams from '../../get-params'; +import define from '../../define'; const cursorOption = { fields: { data: false } }; @@ -39,10 +39,7 @@ export const meta = { } }; -export default (params: any, me: ILocalUser) => new Promise(async (res, rej) => { - const [ps, psErr] = getParams(meta, params); - if (psErr) return rej(psErr); - +export default define(meta, (ps, me) => new Promise(async (res, rej) => { let user; if (ps.userIds) { @@ -87,4 +84,4 @@ export default (params: any, me: ILocalUser) => new Promise(async (res, rej) => } } } -}); +})); diff --git a/src/server/api/get-params.ts b/src/server/api/get-params.ts deleted file mode 100644 index 60d038990e..0000000000 --- a/src/server/api/get-params.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { IEndpointMeta } from './endpoints'; - -export default function (defs: T, params: any): [{ - [P in keyof T['params']]: T['params'][P]['transform'] extends Function - ? ReturnType - : ReturnType[0]; -}, Error] { - const x: any = {}; - let err: Error = null; - Object.entries(defs.params).some(([k, def]) => { - const [v, e] = def.validator.get(params[k]); - if (e) { - err = new Error(e.message); - err.name = 'INVALID_PARAM'; - (err as any).param = k; - return true; - } else { - if (v === undefined && def.default) { - x[k] = def.default; - } else { - x[k] = v; - } - if (def.transform) x[k] = def.transform(x[k]); - return false; - } - }); - return [x, err]; -}