summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints
diff options
context:
space:
mode:
Diffstat (limited to 'src/server/api/endpoints')
-rw-r--r--src/server/api/endpoints/following/create.ts4
-rw-r--r--src/server/api/endpoints/following/delete.ts4
-rw-r--r--src/server/api/endpoints/following/requests/accept.ts26
-rw-r--r--src/server/api/endpoints/following/requests/cancel.ts26
-rw-r--r--src/server/api/endpoints/following/requests/list.ts14
-rw-r--r--src/server/api/endpoints/following/requests/reject.ts26
-rw-r--r--src/server/api/endpoints/i/update.ts58
-rw-r--r--src/server/api/endpoints/users/recommendation.ts1
8 files changed, 127 insertions, 32 deletions
diff --git a/src/server/api/endpoints/following/create.ts b/src/server/api/endpoints/following/create.ts
index 766a8c03d0..48205232e6 100644
--- a/src/server/api/endpoints/following/create.ts
+++ b/src/server/api/endpoints/following/create.ts
@@ -2,7 +2,7 @@
* Module dependencies
*/
import $ from 'cafy'; import ID from '../../../../cafy-id';
-import User from '../../../../models/user';
+import User, { pack } from '../../../../models/user';
import Following from '../../../../models/following';
import create from '../../../../services/following/create';
@@ -49,5 +49,5 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
create(follower, followee);
// Send response
- res();
+ res(await pack(followee, user));
});
diff --git a/src/server/api/endpoints/following/delete.ts b/src/server/api/endpoints/following/delete.ts
index 396b19a6f6..f4030c247a 100644
--- a/src/server/api/endpoints/following/delete.ts
+++ b/src/server/api/endpoints/following/delete.ts
@@ -2,7 +2,7 @@
* Module dependencies
*/
import $ from 'cafy'; import ID from '../../../../cafy-id';
-import User from '../../../../models/user';
+import User, { pack } from '../../../../models/user';
import Following from '../../../../models/following';
import deleteFollowing from '../../../../services/following/delete';
@@ -49,5 +49,5 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
deleteFollowing(follower, followee);
// Send response
- res();
+ res(await pack(followee, user));
});
diff --git a/src/server/api/endpoints/following/requests/accept.ts b/src/server/api/endpoints/following/requests/accept.ts
new file mode 100644
index 0000000000..705d3b161a
--- /dev/null
+++ b/src/server/api/endpoints/following/requests/accept.ts
@@ -0,0 +1,26 @@
+import $ from 'cafy'; import ID from '../../../../../cafy-id';
+import acceptFollowRequest from '../../../../../services/following/requests/accept';
+import User from '../../../../../models/user';
+
+/**
+ * Accept a follow request
+ */
+module.exports = (params, user) => new Promise(async (res, rej) => {
+ // Get 'userId' parameter
+ const [followerId, followerIdErr] = $.type(ID).get(params.userId);
+ if (followerIdErr) return rej('invalid userId param');
+
+ // Fetch follower
+ const follower = await User.findOne({
+ _id: followerId
+ });
+
+ if (follower === null) {
+ return rej('follower not found');
+ }
+
+ await acceptFollowRequest(user, follower);
+
+ // Send response
+ res();
+});
diff --git a/src/server/api/endpoints/following/requests/cancel.ts b/src/server/api/endpoints/following/requests/cancel.ts
new file mode 100644
index 0000000000..388a54890b
--- /dev/null
+++ b/src/server/api/endpoints/following/requests/cancel.ts
@@ -0,0 +1,26 @@
+import $ from 'cafy'; import ID from '../../../../../cafy-id';
+import cancelFollowRequest from '../../../../../services/following/requests/cancel';
+import User, { pack } from '../../../../../models/user';
+
+/**
+ * Cancel a follow request
+ */
+module.exports = (params, user) => new Promise(async (res, rej) => {
+ // Get 'userId' parameter
+ const [followeeId, followeeIdErr] = $.type(ID).get(params.userId);
+ if (followeeIdErr) return rej('invalid userId param');
+
+ // Fetch followee
+ const followee = await User.findOne({
+ _id: followeeId
+ });
+
+ if (followee === null) {
+ return rej('followee not found');
+ }
+
+ await cancelFollowRequest(followee, user);
+
+ // Send response
+ 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
new file mode 100644
index 0000000000..e8364335d1
--- /dev/null
+++ b/src/server/api/endpoints/following/requests/list.ts
@@ -0,0 +1,14 @@
+//import $ from 'cafy'; import ID from '../../../../../cafy-id';
+import FollowRequest, { pack } from '../../../../../models/follow-request';
+
+/**
+ * Get all pending received follow requests
+ */
+module.exports = (params, 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
new file mode 100644
index 0000000000..1cfb562b55
--- /dev/null
+++ b/src/server/api/endpoints/following/requests/reject.ts
@@ -0,0 +1,26 @@
+import $ from 'cafy'; import ID from '../../../../../cafy-id';
+import rejectFollowRequest from '../../../../../services/following/requests/reject';
+import User from '../../../../../models/user';
+
+/**
+ * Reject a follow request
+ */
+module.exports = (params, user) => new Promise(async (res, rej) => {
+ // Get 'userId' parameter
+ const [followerId, followerIdErr] = $.type(ID).get(params.userId);
+ if (followerIdErr) return rej('invalid userId param');
+
+ // Fetch follower
+ const follower = await User.findOne({
+ _id: followerId
+ });
+
+ if (follower === null) {
+ return rej('follower not found');
+ }
+
+ await rejectFollowRequest(user, follower);
+
+ // Send response
+ res();
+});
diff --git a/src/server/api/endpoints/i/update.ts b/src/server/api/endpoints/i/update.ts
index 6e0c5b8515..b94f073d2c 100644
--- a/src/server/api/endpoints/i/update.ts
+++ b/src/server/api/endpoints/i/update.ts
@@ -5,6 +5,7 @@ import $ from 'cafy'; import ID from '../../../../cafy-id';
import User, { isValidName, isValidDescription, isValidLocation, isValidBirthday, pack } from '../../../../models/user';
import event from '../../../../publishers/stream';
import DriveFile from '../../../../models/drive-file';
+import acceptAllFollowRequests from '../../../../services/following/requests/accept-all';
/**
* Update myself
@@ -12,50 +13,57 @@ import DriveFile from '../../../../models/drive-file';
module.exports = async (params, user, app) => new Promise(async (res, rej) => {
const isSecure = user != null && app == null;
+ const updates = {} as any;
+
// Get 'name' parameter
const [name, nameErr] = $.str.optional().nullable().pipe(isValidName).get(params.name);
if (nameErr) return rej('invalid name param');
- if (name) user.name = name;
+ if (name) updates.name = name;
// Get 'description' parameter
const [description, descriptionErr] = $.str.optional().nullable().pipe(isValidDescription).get(params.description);
if (descriptionErr) return rej('invalid description param');
- if (description !== undefined) user.description = description;
+ if (description !== undefined) updates.description = description;
// Get 'location' parameter
const [location, locationErr] = $.str.optional().nullable().pipe(isValidLocation).get(params.location);
if (locationErr) return rej('invalid location param');
- if (location !== undefined) user.profile.location = location;
+ if (location !== undefined) updates['profile.location'] = location;
// Get 'birthday' parameter
const [birthday, birthdayErr] = $.str.optional().nullable().pipe(isValidBirthday).get(params.birthday);
if (birthdayErr) return rej('invalid birthday param');
- if (birthday !== undefined) user.profile.birthday = birthday;
+ if (birthday !== undefined) updates['profile.birthday'] = birthday;
// Get 'avatarId' parameter
- const [avatarId, avatarIdErr] = $.type(ID).optional().get(params.avatarId);
+ const [avatarId, avatarIdErr] = $.type(ID).optional().nullable().get(params.avatarId);
if (avatarIdErr) return rej('invalid avatarId param');
- if (avatarId) user.avatarId = avatarId;
+ if (avatarId !== undefined) updates.avatarId = avatarId;
// Get 'bannerId' parameter
- const [bannerId, bannerIdErr] = $.type(ID).optional().get(params.bannerId);
+ const [bannerId, bannerIdErr] = $.type(ID).optional().nullable().get(params.bannerId);
if (bannerIdErr) return rej('invalid bannerId param');
- if (bannerId) user.bannerId = bannerId;
+ if (bannerId !== undefined) updates.bannerId = bannerId;
+
+ // Get 'isLocked' parameter
+ const [isLocked, isLockedErr] = $.bool.optional().get(params.isLocked);
+ if (isLockedErr) return rej('invalid isLocked param');
+ if (isLocked != null) updates.isLocked = isLocked;
// Get 'isBot' parameter
const [isBot, isBotErr] = $.bool.optional().get(params.isBot);
if (isBotErr) return rej('invalid isBot param');
- if (isBot != null) user.isBot = isBot;
+ if (isBot != null) updates.isBot = isBot;
// Get 'isCat' parameter
const [isCat, isCatErr] = $.bool.optional().get(params.isCat);
if (isCatErr) return rej('invalid isCat param');
- if (isCat != null) user.isCat = isCat;
+ if (isCat != null) updates.isCat = isCat;
// Get 'autoWatch' parameter
const [autoWatch, autoWatchErr] = $.bool.optional().get(params.autoWatch);
if (autoWatchErr) return rej('invalid autoWatch param');
- if (autoWatch != null) user.settings.autoWatch = autoWatch;
+ if (autoWatch != null) updates['settings.autoWatch'] = autoWatch;
if (avatarId) {
const avatar = await DriveFile.findOne({
@@ -63,7 +71,7 @@ module.exports = async (params, user, app) => new Promise(async (res, rej) => {
});
if (avatar != null && avatar.metadata.properties.avgColor) {
- user.avatarColor = avatar.metadata.properties.avgColor;
+ updates.avatarColor = avatar.metadata.properties.avgColor;
}
}
@@ -73,27 +81,16 @@ module.exports = async (params, user, app) => new Promise(async (res, rej) => {
});
if (banner != null && banner.metadata.properties.avgColor) {
- user.bannerColor = banner.metadata.properties.avgColor;
+ updates.bannerColor = banner.metadata.properties.avgColor;
}
}
await User.update(user._id, {
- $set: {
- name: user.name,
- description: user.description,
- avatarId: user.avatarId,
- avatarColor: user.avatarColor,
- bannerId: user.bannerId,
- bannerColor: user.bannerColor,
- profile: user.profile,
- isBot: user.isBot,
- isCat: user.isCat,
- settings: user.settings
- }
+ $set: updates
});
// Serialize
- const iObj = await pack(user, user, {
+ const iObj = await pack(user._id, user, {
detail: true,
includeSecrets: isSecure
});
@@ -101,6 +98,11 @@ module.exports = async (params, user, app) => new Promise(async (res, rej) => {
// Send response
res(iObj);
- // Publish i updated event
- event(user._id, 'i_updated', iObj);
+ // Publish meUpdated event
+ event(user._id, 'meUpdated', iObj);
+
+ // 鍵垢を解除したとき、溜まっていたフォローリクエストがあるならすべて承認
+ if (user.isLocked && isLocked === false) {
+ acceptAllFollowRequests(user);
+ }
});
diff --git a/src/server/api/endpoints/users/recommendation.ts b/src/server/api/endpoints/users/recommendation.ts
index 620ae17ca2..23821a552f 100644
--- a/src/server/api/endpoints/users/recommendation.ts
+++ b/src/server/api/endpoints/users/recommendation.ts
@@ -36,6 +36,7 @@ module.exports = (params, me) => new Promise(async (res, rej) => {
_id: {
$nin: followingIds.concat(mutedUserIds)
},
+ isLocked: false,
$or: [{
lastUsedAt: {
$gte: new Date(Date.now() - ms('7days'))