diff options
| author | syuilo <Syuilotan@yahoo.co.jp> | 2018-04-26 16:10:25 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-04-26 16:10:25 +0900 |
| commit | 5d4b884528e0533e32b9c827ae8ccf64df0085dc (patch) | |
| tree | 1f6a3238dfbf1f77da78d96e993f6d76cad73089 /src/server/api/endpoints/users/lists | |
| parent | Refactor (diff) | |
| parent | wip (diff) | |
| download | sharkey-5d4b884528e0533e32b9c827ae8ccf64df0085dc.tar.gz sharkey-5d4b884528e0533e32b9c827ae8ccf64df0085dc.tar.bz2 sharkey-5d4b884528e0533e32b9c827ae8ccf64df0085dc.zip | |
Merge pull request #1550 from syuilo/user-list
User list
Diffstat (limited to 'src/server/api/endpoints/users/lists')
| -rw-r--r-- | src/server/api/endpoints/users/lists/create.ts | 25 | ||||
| -rw-r--r-- | src/server/api/endpoints/users/lists/list.ts | 13 | ||||
| -rw-r--r-- | src/server/api/endpoints/users/lists/push.ts | 51 | ||||
| -rw-r--r-- | src/server/api/endpoints/users/lists/show.ts | 23 |
4 files changed, 112 insertions, 0 deletions
diff --git a/src/server/api/endpoints/users/lists/create.ts b/src/server/api/endpoints/users/lists/create.ts new file mode 100644 index 0000000000..6ae510f52b --- /dev/null +++ b/src/server/api/endpoints/users/lists/create.ts @@ -0,0 +1,25 @@ +/** + * Module dependencies + */ +import $ from 'cafy'; +import UserList, { pack } from '../../../../../models/user-list'; + +/** + * Create a user list + */ +module.exports = async (params, user) => new Promise(async (res, rej) => { + // Get 'title' parameter + const [title, titleErr] = $(params.title).string().range(1, 100).$; + if (titleErr) return rej('invalid title param'); + + // insert + const userList = await UserList.insert({ + createdAt: new Date(), + userId: user._id, + title: title, + userIds: [] + }); + + // Response + res(await pack(userList)); +}); diff --git a/src/server/api/endpoints/users/lists/list.ts b/src/server/api/endpoints/users/lists/list.ts new file mode 100644 index 0000000000..d19339a1f5 --- /dev/null +++ b/src/server/api/endpoints/users/lists/list.ts @@ -0,0 +1,13 @@ +import UserList, { pack } from '../../../../../models/user-list'; + +/** + * Add a user to a user list + */ +module.exports = async (params, 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 new file mode 100644 index 0000000000..467c08efd4 --- /dev/null +++ b/src/server/api/endpoints/users/lists/push.ts @@ -0,0 +1,51 @@ +import $ from 'cafy'; import ID from '../../../../../cafy-id'; +import UserList from '../../../../../models/user-list'; +import User, { pack as packUser } from '../../../../../models/user'; +import { publishUserListStream } from '../../../../../publishers/stream'; + +/** + * 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(); + + publishUserListStream(userList._id, 'userAdded', await packUser(user)); +}); diff --git a/src/server/api/endpoints/users/lists/show.ts b/src/server/api/endpoints/users/lists/show.ts new file mode 100644 index 0000000000..61e0f0463f --- /dev/null +++ b/src/server/api/endpoints/users/lists/show.ts @@ -0,0 +1,23 @@ +import $ from 'cafy'; import ID from '../../../../../cafy-id'; +import UserList, { pack } from '../../../../../models/user-list'; + +/** + * Show 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'); + } + + res(await pack(userList)); +}); |