From d53d45f797946ced21f6eaeee0fa4c713b19f782 Mon Sep 17 00:00:00 2001
From: syuilo <syuilotan@yahoo.co.jp>
Date: Thu, 5 Jul 2018 22:35:35 +0900
Subject: [PATCH] wip

---
 src/server/api/endpoints/notes/create.ts | 39 ++++++++++++++++++------
 src/server/api/get-params.ts             | 30 ++++++++++++++++++
 2 files changed, 60 insertions(+), 9 deletions(-)
 create mode 100644 src/server/api/get-params.ts

diff --git a/src/server/api/endpoints/notes/create.ts b/src/server/api/endpoints/notes/create.ts
index 64f3b5ce26..2bd0b430d8 100644
--- a/src/server/api/endpoints/notes/create.ts
+++ b/src/server/api/endpoints/notes/create.ts
@@ -4,22 +4,43 @@ import User, { ILocalUser, IUser } from '../../../../models/user';
 import DriveFile from '../../../../models/drive-file';
 import create from '../../../../services/note/create';
 import { IApp } from '../../../../models/app';
+import getParams from '../../get-params';
+
+export const meta = {
+	params: {
+		visibility: {
+			def: $.str.optional().or(['public', 'home', 'followers', 'specified', 'private']),
+			default: 'public',
+			desc: {
+				ja: '投稿の公開範囲'
+			}
+		},
+		visibleUserIds: {
+			def: $.arr($.type(ID)).optional().unique().min(1),
+			desc: {
+				ja: '(投稿の公開範囲が specified の場合)投稿を閲覧できるユーザー'
+			}
+		},
+		text: {
+			def: $.str.optional().nullable().pipe(isValidText),
+			default: null,
+			desc: {
+				ja: '投稿内容'
+			}
+		},
+	}
+};
 
 /**
  * Create a note
  */
 module.exports = (params: any, user: ILocalUser, app: IApp) => new Promise(async (res, rej) => {
-	// Get 'visibility' parameter
-	const [visibility = 'public', visibilityErr] = $.str.optional().or(['public', 'home', 'followers', 'specified', 'private']).get(params.visibility);
-	if (visibilityErr) return rej('invalid visibility');
-
-	// Get 'visibleUserIds' parameter
-	const [visibleUserIds, visibleUserIdsErr] = $.arr($.type(ID)).optional().unique().min(1).get(params.visibleUserIds);
-	if (visibleUserIdsErr) return rej('invalid visibleUserIds');
+	const [ps, psErr] = getParams(meta, params);
+	if (psErr) return rej(psErr);
 
 	let visibleUsers: IUser[] = [];
-	if (visibleUserIds !== undefined) {
-		visibleUsers = await Promise.all(visibleUserIds.map(id => User.findOne({
+	if (ps.visibleUserIds !== undefined) {
+		visibleUsers = await Promise.all(ps.visibleUserIds.map(id => User.findOne({
 			_id: id
 		})));
 	}
diff --git a/src/server/api/get-params.ts b/src/server/api/get-params.ts
new file mode 100644
index 0000000000..6b047a28f4
--- /dev/null
+++ b/src/server/api/get-params.ts
@@ -0,0 +1,30 @@
+import { Query } from 'cafy';
+
+type Defs = {
+	params: {[key: string]: {
+		def: Query<any>;
+		default?: any;
+	}}
+};
+
+export default function <T extends Defs>(defs: T, params: any): [{
+	[P in keyof T['params']]: ReturnType<T['params'][P]['def']['get']>[0];
+}, Error] {
+	const x: any = {};
+	let err: Error = null;
+	Object.keys(defs.params).some(k => {
+		const [v, e] = defs.params[k].def.get(params[k]);
+		if (e) {
+			err = e;
+			return true;
+		} else {
+			if (v === undefined && defs.params[k].default) {
+				x[k] = defs.params[k].default;
+			} else {
+				x[k] = v;
+			}
+			return false;
+		}
+	});
+	return [x, err];
+}