2018-06-18 00:28:43 -05:00
|
|
|
import User, { IUser, isRemoteUser, ILocalUser, pack as packUser } from '../../../models/user';
|
|
|
|
import FollowRequest from '../../../models/follow-request';
|
2018-06-01 10:15:17 -05:00
|
|
|
import pack from '../../../remote/activitypub/renderer';
|
|
|
|
import renderFollow from '../../../remote/activitypub/renderer/follow';
|
|
|
|
import renderAccept from '../../../remote/activitypub/renderer/accept';
|
|
|
|
import { deliver } from '../../../queue';
|
2018-06-18 00:28:43 -05:00
|
|
|
import Following from '../../../models/following';
|
2018-10-06 21:06:17 -05:00
|
|
|
import { publishMainStream } from '../../../stream';
|
2018-10-21 03:51:35 -05:00
|
|
|
import { followingStats } from '../../stats';
|
2018-05-31 08:56:02 -05:00
|
|
|
|
|
|
|
export default async function(followee: IUser, follower: IUser) {
|
2018-10-21 00:15:02 -05:00
|
|
|
await Following.insert({
|
2018-05-31 08:56:02 -05:00
|
|
|
createdAt: new Date(),
|
|
|
|
followerId: follower._id,
|
|
|
|
followeeId: followee._id,
|
|
|
|
|
|
|
|
// 非正規化
|
|
|
|
_follower: {
|
|
|
|
host: follower.host,
|
2018-07-21 05:33:56 -05:00
|
|
|
inbox: isRemoteUser(follower) ? follower.inbox : undefined,
|
|
|
|
sharedInbox: isRemoteUser(follower) ? follower.sharedInbox : undefined
|
2018-05-31 08:56:02 -05:00
|
|
|
},
|
|
|
|
_followee: {
|
|
|
|
host: followee.host,
|
2018-07-21 05:33:56 -05:00
|
|
|
inbox: isRemoteUser(followee) ? followee.inbox : undefined,
|
|
|
|
sharedInbox: isRemoteUser(followee) ? followee.sharedInbox : undefined
|
2018-05-31 08:56:02 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (isRemoteUser(follower)) {
|
2018-10-15 02:51:23 -05:00
|
|
|
const request = await FollowRequest.findOne({
|
|
|
|
followeeId: followee._id,
|
|
|
|
followerId: follower._id
|
|
|
|
});
|
|
|
|
|
|
|
|
const content = pack(renderAccept(renderFollow(follower, followee, request.requestId), followee as ILocalUser));
|
2018-05-31 08:56:02 -05:00
|
|
|
deliver(followee as ILocalUser, content, follower.inbox);
|
|
|
|
}
|
|
|
|
|
2018-06-02 02:01:32 -05:00
|
|
|
await FollowRequest.remove({
|
2018-05-31 08:56:02 -05:00
|
|
|
followeeId: followee._id,
|
|
|
|
followerId: follower._id
|
|
|
|
});
|
|
|
|
|
|
|
|
//#region Increment following count
|
2018-06-02 02:01:32 -05:00
|
|
|
await User.update({ _id: follower._id }, {
|
2018-05-31 08:56:02 -05:00
|
|
|
$inc: {
|
|
|
|
followingCount: 1
|
|
|
|
}
|
|
|
|
});
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
//#region Increment followers count
|
2018-06-02 02:01:32 -05:00
|
|
|
await User.update({ _id: followee._id }, {
|
2018-05-31 08:56:02 -05:00
|
|
|
$inc: {
|
|
|
|
followersCount: 1
|
|
|
|
}
|
|
|
|
});
|
|
|
|
//#endregion
|
2018-06-02 02:01:32 -05:00
|
|
|
|
2018-10-21 03:51:35 -05:00
|
|
|
followingStats.update(follower, followee, true);
|
|
|
|
|
2018-07-27 17:56:33 -05:00
|
|
|
await User.update({ _id: followee._id }, {
|
|
|
|
$inc: {
|
|
|
|
pendingReceivedFollowRequestsCount: -1
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-06-02 02:01:32 -05:00
|
|
|
packUser(followee, followee, {
|
|
|
|
detail: true
|
2018-10-06 21:06:17 -05:00
|
|
|
}).then(packed => publishMainStream(followee._id, 'meUpdated', packed));
|
2018-09-04 04:33:16 -05:00
|
|
|
|
2018-10-06 21:06:17 -05:00
|
|
|
packUser(followee, follower).then(packed => publishMainStream(follower._id, 'follow', packed));
|
2018-05-31 08:56:02 -05:00
|
|
|
}
|