summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/users
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2018-04-24 20:58:29 +0900
committersyuilo <syuilotan@yahoo.co.jp>2018-04-24 20:58:29 +0900
commite14ea1fe61a55954866f39bfdaa365a040bf3963 (patch)
tree560e5feb91d385b34fecbae054d80e321fbb8ee0 /src/server/api/endpoints/users
parentcafy 5.xに移行 (diff)
downloadsharkey-e14ea1fe61a55954866f39bfdaa365a040bf3963.tar.gz
sharkey-e14ea1fe61a55954866f39bfdaa365a040bf3963.tar.bz2
sharkey-e14ea1fe61a55954866f39bfdaa365a040bf3963.zip
wip
Diffstat (limited to 'src/server/api/endpoints/users')
-rw-r--r--src/server/api/endpoints/users/list/push.ts48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/server/api/endpoints/users/list/push.ts b/src/server/api/endpoints/users/list/push.ts
index e69de29bb2..f21234775d 100644
--- a/src/server/api/endpoints/users/list/push.ts
+++ b/src/server/api/endpoints/users/list/push.ts
@@ -0,0 +1,48 @@
+import $ from 'cafy'; import ID from '../../../../../cafy-id';
+import UserList from '../../../../../models/user-list';
+import User from '../../../../../models/user';
+
+/**
+ * Add a user to a user list
+ */
+module.exports = async (params, me) => new Promise(async (res, rej) => {
+ // Get 'listId' parameter
+ const [listId, listIdErr] = $(params.listId).type(ID).$;
+ if (listIdErr) return rej('invalid listId param');
+
+ // Fetch the list
+ const userList = await UserList.findOne({
+ _id: listId,
+ userId: me._id,
+ });
+
+ if (userList == null) {
+ return rej('list not found');
+ }
+
+ // Get 'userId' parameter
+ const [userId, userIdErr] = $(params.userId).type(ID).$;
+ if (userIdErr) return rej('invalid userId param');
+
+ // Fetch the user
+ const user = await User.findOne({
+ _id: userId
+ });
+
+ if (user == null) {
+ return rej('user not found');
+ }
+
+ if (userList.userIds.map(id => id.toHexString()).includes(user._id.toHexString())) {
+ return rej('the user already added');
+ }
+
+ // Push the user
+ await UserList.update({ _id: userList._id }, {
+ $push: {
+ userIds: user._id
+ }
+ });
+
+ res();
+});