2018-04-04 21:56:04 +09:00
|
|
|
import User, { isLocalUser, isRemoteUser, pack as packUser } from '../../models/user';
|
2018-04-01 19:43:26 +09:00
|
|
|
import Following from '../../models/following';
|
2018-04-02 21:57:36 +09:00
|
|
|
import FollowingLog from '../../models/following-log';
|
|
|
|
import FollowedLog from '../../models/followed-log';
|
2018-04-02 13:33:46 +09:00
|
|
|
import event from '../../publishers/stream';
|
|
|
|
import notify from '../../publishers/notify';
|
2018-04-02 04:15:27 +09:00
|
|
|
import context from '../../remote/activitypub/renderer/context';
|
|
|
|
import render from '../../remote/activitypub/renderer/follow';
|
2018-04-02 20:16:13 +09:00
|
|
|
import request from '../../remote/request';
|
2018-04-04 21:56:04 +09:00
|
|
|
import Logger from '../../utils/logger';
|
2018-04-01 19:43:26 +09:00
|
|
|
|
2018-04-04 21:56:04 +09:00
|
|
|
export default async ({ data }) => {
|
|
|
|
const { followerId, followeeId } = await Following.findOne({ _id: data.following });
|
|
|
|
const [follower, followee] = await Promise.all([
|
|
|
|
User.findOne({ _id: followerId }),
|
|
|
|
User.findOne({ _id: followeeId })
|
|
|
|
]);
|
2018-04-01 19:43:26 +09:00
|
|
|
|
2018-04-04 21:56:04 +09:00
|
|
|
if (isLocalUser(follower) && isRemoteUser(followee)) {
|
|
|
|
const rendered = render(follower, followee);
|
|
|
|
rendered['@context'] = context;
|
2018-04-02 21:57:36 +09:00
|
|
|
|
2018-04-04 21:56:04 +09:00
|
|
|
await request(follower, followee.account.inbox, rendered);
|
|
|
|
}
|
2018-04-01 19:43:26 +09:00
|
|
|
|
2018-04-04 21:56:04 +09:00
|
|
|
try {
|
|
|
|
await Promise.all([
|
|
|
|
// Increment following count
|
|
|
|
User.update(followerId, {
|
|
|
|
$inc: {
|
|
|
|
followingCount: 1
|
|
|
|
}
|
|
|
|
}),
|
2018-04-02 21:57:36 +09:00
|
|
|
|
2018-04-04 21:56:04 +09:00
|
|
|
FollowingLog.insert({
|
|
|
|
createdAt: data.following.createdAt,
|
|
|
|
userId: followerId,
|
|
|
|
count: follower.followingCount + 1
|
|
|
|
}),
|
2018-04-01 19:43:26 +09:00
|
|
|
|
2018-04-04 21:56:04 +09:00
|
|
|
// Increment followers count
|
|
|
|
User.update({ _id: followeeId }, {
|
|
|
|
$inc: {
|
|
|
|
followersCount: 1
|
|
|
|
}
|
|
|
|
}),
|
2018-04-01 19:43:26 +09:00
|
|
|
|
2018-04-04 21:56:04 +09:00
|
|
|
FollowedLog.insert({
|
|
|
|
createdAt: data.following.createdAt,
|
|
|
|
userId: followerId,
|
|
|
|
count: followee.followersCount + 1
|
|
|
|
}),
|
2018-04-02 19:50:40 +09:00
|
|
|
|
2018-04-04 21:56:04 +09:00
|
|
|
// Publish follow event
|
|
|
|
isLocalUser(follower) && packUser(followee, follower)
|
|
|
|
.then(packed => event(follower._id, 'follow', packed)),
|
2018-04-01 19:43:26 +09:00
|
|
|
|
2018-04-04 21:56:04 +09:00
|
|
|
isLocalUser(followee) && Promise.all([
|
|
|
|
packUser(follower, followee)
|
|
|
|
.then(packed => event(followee._id, 'followed', packed)),
|
2018-04-01 19:43:26 +09:00
|
|
|
|
2018-04-04 21:56:04 +09:00
|
|
|
// Notify
|
|
|
|
isLocalUser(followee) && notify(followeeId, followerId, 'follow')
|
|
|
|
])
|
|
|
|
]);
|
|
|
|
} catch (error) {
|
|
|
|
Logger.error(error.toString());
|
|
|
|
}
|
|
|
|
};
|