2018-04-24 14:58:29 +03:00
|
|
|
import $ from 'cafy'; import ID from '../../../../../cafy-id';
|
|
|
|
import UserList from '../../../../../models/user-list';
|
2018-04-26 14:30:49 +03:00
|
|
|
import User, { pack as packUser, isRemoteUser, getGhost } from '../../../../../models/user';
|
2018-04-25 12:04:16 +03:00
|
|
|
import { publishUserListStream } from '../../../../../publishers/stream';
|
2018-04-26 14:30:49 +03:00
|
|
|
import ap from '../../../../../remote/activitypub/renderer';
|
|
|
|
import renderFollow from '../../../../../remote/activitypub/renderer/follow';
|
|
|
|
import { deliver } from '../../../../../queue';
|
2018-04-24 14:58:29 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a user to a user list
|
|
|
|
*/
|
|
|
|
module.exports = async (params, me) => new Promise(async (res, rej) => {
|
|
|
|
// Get 'listId' parameter
|
2018-05-02 12:06:16 +03:00
|
|
|
const [listId, listIdErr] = $.type(ID).get(params.listId);
|
2018-04-24 14:58:29 +03:00
|
|
|
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
|
2018-05-02 12:06:16 +03:00
|
|
|
const [userId, userIdErr] = $.type(ID).get(params.userId);
|
2018-04-24 14:58:29 +03:00
|
|
|
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();
|
2018-04-25 12:04:16 +03:00
|
|
|
|
|
|
|
publishUserListStream(userList._id, 'userAdded', await packUser(user));
|
2018-04-26 14:30:49 +03:00
|
|
|
|
|
|
|
// このインスタンス内にこのリモートユーザーをフォローしているユーザーがいなくても投稿を受け取るためにダミーのユーザーがフォローしたということにする
|
|
|
|
if (isRemoteUser(user)) {
|
|
|
|
const ghost = await getGhost();
|
|
|
|
const content = ap(renderFollow(ghost, user));
|
|
|
|
deliver(ghost, content, user.inbox);
|
|
|
|
}
|
2018-04-24 14:58:29 +03:00
|
|
|
});
|