2018-06-18 07:28:43 +02:00
|
|
|
import User, { IUser, isRemoteUser, ILocalUser, pack as packUser } from '../../../models/user';
|
|
|
|
import FollowRequest from '../../../models/follow-request';
|
2019-01-30 18:29:36 +01:00
|
|
|
import { renderActivity } from '../../../remote/activitypub/renderer';
|
2018-06-01 17:38:31 +02:00
|
|
|
import renderFollow from '../../../remote/activitypub/renderer/follow';
|
|
|
|
import renderUndo from '../../../remote/activitypub/renderer/undo';
|
|
|
|
import { deliver } from '../../../queue';
|
2018-10-07 04:06:17 +02:00
|
|
|
import { publishMainStream } from '../../../stream';
|
2018-06-01 17:38:31 +02:00
|
|
|
|
|
|
|
export default async function(followee: IUser, follower: IUser) {
|
|
|
|
if (isRemoteUser(followee)) {
|
2019-01-30 18:29:36 +01:00
|
|
|
const content = renderActivity(renderUndo(renderFollow(follower, followee), follower));
|
2018-06-01 17:38:31 +02:00
|
|
|
deliver(follower as ILocalUser, content, followee.inbox);
|
|
|
|
}
|
|
|
|
|
2018-08-23 07:56:39 +02:00
|
|
|
const request = await FollowRequest.findOne({
|
|
|
|
followeeId: followee._id,
|
|
|
|
followerId: follower._id
|
|
|
|
});
|
|
|
|
|
|
|
|
if (request == null) {
|
|
|
|
throw 'request not found';
|
|
|
|
}
|
|
|
|
|
2018-06-01 17:38:31 +02:00
|
|
|
await FollowRequest.remove({
|
|
|
|
followeeId: followee._id,
|
|
|
|
followerId: follower._id
|
|
|
|
});
|
|
|
|
|
2018-06-02 09:01:32 +02:00
|
|
|
await User.update({ _id: followee._id }, {
|
2018-06-01 17:38:31 +02:00
|
|
|
$inc: {
|
|
|
|
pendingReceivedFollowRequestsCount: -1
|
|
|
|
}
|
|
|
|
});
|
2018-06-02 09:01:32 +02:00
|
|
|
|
|
|
|
packUser(followee, followee, {
|
|
|
|
detail: true
|
2018-10-07 04:06:17 +02:00
|
|
|
}).then(packed => publishMainStream(followee._id, 'meUpdated', packed));
|
2018-06-01 17:38:31 +02:00
|
|
|
}
|