2019-01-30 18:29:36 +01:00
|
|
|
import { renderActivity } from '../../../remote/activitypub/renderer';
|
2018-06-01 17:15:17 +02:00
|
|
|
import renderFollow from '../../../remote/activitypub/renderer/follow';
|
|
|
|
import renderReject from '../../../remote/activitypub/renderer/reject';
|
|
|
|
import { deliver } from '../../../queue';
|
2021-03-21 07:14:03 +01:00
|
|
|
import { publishMainStream, publishUserEvent } from '../../stream';
|
2019-04-07 14:50:36 +02:00
|
|
|
import { User, ILocalUser } from '../../../models/entities/user';
|
2019-11-09 10:24:41 +01:00
|
|
|
import { Users, FollowRequests, Followings } from '../../../models';
|
|
|
|
import { decrementFollowing } from '../delete';
|
2018-05-31 17:42:37 +02:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
export default async function(followee: User, follower: User) {
|
|
|
|
if (Users.isRemoteUser(follower)) {
|
|
|
|
const request = await FollowRequests.findOne({
|
|
|
|
followeeId: followee.id,
|
|
|
|
followerId: follower.id
|
2018-10-15 09:51:23 +02:00
|
|
|
});
|
|
|
|
|
2019-04-12 18:43:22 +02:00
|
|
|
const content = renderActivity(renderReject(renderFollow(follower, followee, request!.requestId!), followee as ILocalUser));
|
2018-05-31 17:42:37 +02:00
|
|
|
deliver(followee as ILocalUser, content, follower.inbox);
|
|
|
|
}
|
|
|
|
|
2019-11-09 10:24:41 +01:00
|
|
|
const request = await FollowRequests.findOne({
|
2019-04-07 14:50:36 +02:00
|
|
|
followeeId: followee.id,
|
|
|
|
followerId: follower.id
|
2018-05-31 17:42:37 +02:00
|
|
|
});
|
2018-05-31 18:12:02 +02:00
|
|
|
|
2019-11-09 10:24:41 +01:00
|
|
|
if (request) {
|
|
|
|
await FollowRequests.delete(request.id);
|
|
|
|
} else {
|
|
|
|
const following = await Followings.findOne({
|
|
|
|
followeeId: followee.id,
|
|
|
|
followerId: follower.id
|
|
|
|
});
|
|
|
|
|
|
|
|
if (following) {
|
|
|
|
await Followings.delete(following.id);
|
|
|
|
decrementFollowing(follower, followee);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
Users.pack(followee, follower, {
|
2018-10-30 06:34:32 +01:00
|
|
|
detail: true
|
2021-03-21 07:14:03 +01:00
|
|
|
}).then(packed => {
|
|
|
|
publishUserEvent(follower.id, 'unfollow', packed);
|
|
|
|
publishMainStream(follower.id, 'unfollow', packed);
|
|
|
|
});
|
2018-05-31 17:42:37 +02:00
|
|
|
}
|