summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/users
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2018-04-26 16:10:25 +0900
committerGitHub <noreply@github.com>2018-04-26 16:10:25 +0900
commit5d4b884528e0533e32b9c827ae8ccf64df0085dc (patch)
tree1f6a3238dfbf1f77da78d96e993f6d76cad73089 /src/server/api/endpoints/users
parentRefactor (diff)
parentwip (diff)
downloadsharkey-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')
-rw-r--r--src/server/api/endpoints/users/followers.ts10
-rw-r--r--src/server/api/endpoints/users/following.ts6
-rw-r--r--src/server/api/endpoints/users/get_frequently_replied_users.ts4
-rw-r--r--src/server/api/endpoints/users/lists/create.ts25
-rw-r--r--src/server/api/endpoints/users/lists/list.ts13
-rw-r--r--src/server/api/endpoints/users/lists/push.ts51
-rw-r--r--src/server/api/endpoints/users/lists/show.ts23
-rw-r--r--src/server/api/endpoints/users/notes.ts8
-rw-r--r--src/server/api/endpoints/users/search_by_username.ts4
-rw-r--r--src/server/api/endpoints/users/show.ts62
10 files changed, 161 insertions, 45 deletions
diff --git a/src/server/api/endpoints/users/followers.ts b/src/server/api/endpoints/users/followers.ts
index 5f03326be8..940b5ed9bc 100644
--- a/src/server/api/endpoints/users/followers.ts
+++ b/src/server/api/endpoints/users/followers.ts
@@ -1,7 +1,7 @@
/**
* Module dependencies
*/
-import $ from 'cafy';
+import $ from 'cafy'; import ID from '../../../../cafy-id';
import User from '../../../../models/user';
import Following from '../../../../models/following';
import { pack } from '../../../../models/user';
@@ -9,14 +9,10 @@ import { getFriendIds } from '../../common/get-friends';
/**
* Get followers of a user
- *
- * @param {any} params
- * @param {any} me
- * @return {Promise<any>}
*/
module.exports = (params, me) => new Promise(async (res, rej) => {
// Get 'userId' parameter
- const [userId, userIdErr] = $(params.userId).id().$;
+ const [userId, userIdErr] = $(params.userId).type(ID).$;
if (userIdErr) return rej('invalid userId param');
// Get 'iknow' parameter
@@ -28,7 +24,7 @@ module.exports = (params, me) => new Promise(async (res, rej) => {
if (limitErr) return rej('invalid limit param');
// Get 'cursor' parameter
- const [cursor = null, cursorErr] = $(params.cursor).optional.id().$;
+ const [cursor = null, cursorErr] = $(params.cursor).optional.type(ID).$;
if (cursorErr) return rej('invalid cursor param');
// Lookup user
diff --git a/src/server/api/endpoints/users/following.ts b/src/server/api/endpoints/users/following.ts
index 9fb135b24d..63a73a2e27 100644
--- a/src/server/api/endpoints/users/following.ts
+++ b/src/server/api/endpoints/users/following.ts
@@ -1,7 +1,7 @@
/**
* Module dependencies
*/
-import $ from 'cafy';
+import $ from 'cafy'; import ID from '../../../../cafy-id';
import User from '../../../../models/user';
import Following from '../../../../models/following';
import { pack } from '../../../../models/user';
@@ -16,7 +16,7 @@ import { getFriendIds } from '../../common/get-friends';
*/
module.exports = (params, me) => new Promise(async (res, rej) => {
// Get 'userId' parameter
- const [userId, userIdErr] = $(params.userId).id().$;
+ const [userId, userIdErr] = $(params.userId).type(ID).$;
if (userIdErr) return rej('invalid userId param');
// Get 'iknow' parameter
@@ -28,7 +28,7 @@ module.exports = (params, me) => new Promise(async (res, rej) => {
if (limitErr) return rej('invalid limit param');
// Get 'cursor' parameter
- const [cursor = null, cursorErr] = $(params.cursor).optional.id().$;
+ const [cursor = null, cursorErr] = $(params.cursor).optional.type(ID).$;
if (cursorErr) return rej('invalid cursor param');
// Lookup user
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 7a98f44e98..4c00620a52 100644
--- a/src/server/api/endpoints/users/get_frequently_replied_users.ts
+++ b/src/server/api/endpoints/users/get_frequently_replied_users.ts
@@ -1,13 +1,13 @@
/**
* Module dependencies
*/
-import $ from 'cafy';
+import $ from 'cafy'; import ID from '../../../../cafy-id';
import Note from '../../../../models/note';
import User, { pack } from '../../../../models/user';
module.exports = (params, me) => new Promise(async (res, rej) => {
// Get 'userId' parameter
- const [userId, userIdErr] = $(params.userId).id().$;
+ const [userId, userIdErr] = $(params.userId).type(ID).$;
if (userIdErr) return rej('invalid userId param');
// Get 'limit' parameter
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));
+});
diff --git a/src/server/api/endpoints/users/notes.ts b/src/server/api/endpoints/users/notes.ts
index bd4247c79c..dafa18bcc9 100644
--- a/src/server/api/endpoints/users/notes.ts
+++ b/src/server/api/endpoints/users/notes.ts
@@ -1,7 +1,7 @@
/**
* Module dependencies
*/
-import $ from 'cafy';
+import $ from 'cafy'; import ID from '../../../../cafy-id';
import getHostLower from '../../common/get-host-lower';
import Note, { pack } from '../../../../models/note';
import User from '../../../../models/user';
@@ -11,7 +11,7 @@ import User from '../../../../models/user';
*/
module.exports = (params, me) => new Promise(async (res, rej) => {
// Get 'userId' parameter
- const [userId, userIdErr] = $(params.userId).optional.id().$;
+ const [userId, userIdErr] = $(params.userId).optional.type(ID).$;
if (userIdErr) return rej('invalid userId param');
// Get 'username' parameter
@@ -43,11 +43,11 @@ module.exports = (params, me) => new Promise(async (res, rej) => {
if (limitErr) return rej('invalid limit param');
// Get 'sinceId' parameter
- const [sinceId, sinceIdErr] = $(params.sinceId).optional.id().$;
+ const [sinceId, sinceIdErr] = $(params.sinceId).optional.type(ID).$;
if (sinceIdErr) return rej('invalid sinceId param');
// Get 'untilId' parameter
- const [untilId, untilIdErr] = $(params.untilId).optional.id().$;
+ const [untilId, untilIdErr] = $(params.untilId).optional.type(ID).$;
if (untilIdErr) return rej('invalid untilId param');
// Get 'sinceDate' parameter
diff --git a/src/server/api/endpoints/users/search_by_username.ts b/src/server/api/endpoints/users/search_by_username.ts
index 5f6ececff9..91d9ad1f3a 100644
--- a/src/server/api/endpoints/users/search_by_username.ts
+++ b/src/server/api/endpoints/users/search_by_username.ts
@@ -6,10 +6,6 @@ import User, { pack } from '../../../../models/user';
/**
* Search a user by username
- *
- * @param {any} params
- * @param {any} me
- * @return {Promise<any>}
*/
module.exports = (params, me) => new Promise(async (res, rej) => {
// Get 'query' parameter
diff --git a/src/server/api/endpoints/users/show.ts b/src/server/api/endpoints/users/show.ts
index 7e7f5dc488..141565ece6 100644
--- a/src/server/api/endpoints/users/show.ts
+++ b/src/server/api/endpoints/users/show.ts
@@ -1,22 +1,26 @@
/**
* Module dependencies
*/
-import $ from 'cafy';
+import $ from 'cafy'; import ID from '../../../../cafy-id';
import User, { pack } from '../../../../models/user';
import resolveRemoteUser from '../../../../remote/resolve-user';
const cursorOption = { fields: { data: false } };
/**
- * Show a user
+ * Show user(s)
*/
module.exports = (params, me) => new Promise(async (res, rej) => {
let user;
// Get 'userId' parameter
- const [userId, userIdErr] = $(params.userId).optional.id().$;
+ const [userId, userIdErr] = $(params.userId).optional.type(ID).$;
if (userIdErr) return rej('invalid userId param');
+ // Get 'userIds' parameter
+ const [userIds, userIdsErr] = $(params.userIds).optional.array($().type(ID)).$;
+ if (userIdsErr) return rej('invalid userIds param');
+
// Get 'username' parameter
const [username, usernameErr] = $(params.username).optional.string().$;
if (usernameErr) return rej('invalid username param');
@@ -25,32 +29,40 @@ module.exports = (params, me) => new Promise(async (res, rej) => {
const [host, hostErr] = $(params.host).nullable.optional.string().$;
if (hostErr) return rej('invalid host param');
- if (userId === undefined && typeof username !== 'string') {
- return rej('userId or pair of username and host is required');
- }
+ if (userIds) {
+ const users = await User.find({
+ _id: {
+ $in: userIds
+ }
+ });
- // Lookup user
- if (typeof host === 'string') {
- try {
- user = await resolveRemoteUser(username, host, cursorOption);
- } catch (e) {
- console.warn(`failed to resolve remote user: ${e}`);
- return rej('failed to resolve remote user');
- }
+ res(await Promise.all(users.map(u => pack(u, me, {
+ detail: true
+ }))));
} else {
- const q = userId !== undefined
- ? { _id: userId }
- : { usernameLower: username.toLowerCase(), host: null };
+ // Lookup user
+ if (typeof host === 'string') {
+ try {
+ user = await resolveRemoteUser(username, host, cursorOption);
+ } catch (e) {
+ console.warn(`failed to resolve remote user: ${e}`);
+ return rej('failed to resolve remote user');
+ }
+ } else {
+ const q = userId !== undefined
+ ? { _id: userId }
+ : { usernameLower: username.toLowerCase(), host: null };
- user = await User.findOne(q, cursorOption);
+ user = await User.findOne(q, cursorOption);
- if (user === null) {
- return rej('user not found');
+ if (user === null) {
+ return rej('user not found');
+ }
}
- }
- // Send response
- res(await pack(user, me, {
- detail: true
- }));
+ // Send response
+ res(await pack(user, me, {
+ detail: true
+ }));
+ }
});