2022-02-27 04:07:39 +02:00
|
|
|
import { renderActivity } from '@/remote/activitypub/renderer/index.js';
|
|
|
|
import renderFollow from '@/remote/activitypub/renderer/follow.js';
|
|
|
|
import renderReject from '@/remote/activitypub/renderer/reject.js';
|
|
|
|
import { deliver } from '@/queue/index.js';
|
|
|
|
import { publishMainStream, publishUserEvent } from '@/services/stream.js';
|
|
|
|
import { User, ILocalUser, IRemoteUser } from '@/models/entities/user.js';
|
|
|
|
import { Users, FollowRequests, Followings } from '@/models/index.js';
|
|
|
|
import { decrementFollowing } from './delete.js';
|
2021-12-03 04:43:05 +02:00
|
|
|
|
|
|
|
type Local = ILocalUser | { id: User['id']; host: User['host']; uri: User['host'] };
|
|
|
|
type Remote = IRemoteUser;
|
|
|
|
type Both = Local | Remote;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* API following/request/reject
|
|
|
|
*/
|
|
|
|
export async function rejectFollowRequest(user: Local, follower: Both) {
|
|
|
|
if (Users.isRemoteUser(follower)) {
|
|
|
|
deliverReject(user, follower);
|
|
|
|
}
|
|
|
|
|
|
|
|
await removeFollowRequest(user, follower);
|
|
|
|
|
|
|
|
if (Users.isLocalUser(follower)) {
|
|
|
|
publishUnfollow(user, follower);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* API following/reject
|
|
|
|
*/
|
|
|
|
export async function rejectFollow(user: Local, follower: Both) {
|
|
|
|
if (Users.isRemoteUser(follower)) {
|
|
|
|
deliverReject(user, follower);
|
|
|
|
}
|
|
|
|
|
|
|
|
await removeFollow(user, follower);
|
|
|
|
|
|
|
|
if (Users.isLocalUser(follower)) {
|
|
|
|
publishUnfollow(user, follower);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AP Reject/Follow
|
|
|
|
*/
|
|
|
|
export async function remoteReject(actor: Remote, follower: Local) {
|
|
|
|
await removeFollowRequest(actor, follower);
|
|
|
|
await removeFollow(actor, follower);
|
|
|
|
publishUnfollow(actor, follower);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove follow request record
|
|
|
|
*/
|
|
|
|
async function removeFollowRequest(followee: Both, follower: Both) {
|
|
|
|
const request = await FollowRequests.findOne({
|
|
|
|
followeeId: followee.id,
|
2021-12-09 16:58:30 +02:00
|
|
|
followerId: follower.id,
|
2021-12-03 04:43:05 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!request) return;
|
|
|
|
|
|
|
|
await FollowRequests.delete(request.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove follow record
|
|
|
|
*/
|
|
|
|
async function removeFollow(followee: Both, follower: Both) {
|
|
|
|
const following = await Followings.findOne({
|
|
|
|
followeeId: followee.id,
|
2021-12-09 16:58:30 +02:00
|
|
|
followerId: follower.id,
|
2021-12-03 04:43:05 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!following) return;
|
|
|
|
|
|
|
|
await Followings.delete(following.id);
|
|
|
|
decrementFollowing(follower, followee);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deliver Reject to remote
|
|
|
|
*/
|
|
|
|
async function deliverReject(followee: Local, follower: Remote) {
|
|
|
|
const request = await FollowRequests.findOne({
|
|
|
|
followeeId: followee.id,
|
2021-12-09 16:58:30 +02:00
|
|
|
followerId: follower.id,
|
2021-12-03 04:43:05 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const content = renderActivity(renderReject(renderFollow(follower, followee, request?.requestId || undefined), followee));
|
|
|
|
deliver(followee, content, follower.inbox);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Publish unfollow to local
|
|
|
|
*/
|
|
|
|
async function publishUnfollow(followee: Both, follower: Local) {
|
|
|
|
const packedFollowee = await Users.pack(followee.id, follower, {
|
2021-12-09 16:58:30 +02:00
|
|
|
detail: true,
|
2021-12-03 04:43:05 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
publishUserEvent(follower.id, 'unfollow', packedFollowee);
|
|
|
|
publishMainStream(follower.id, 'unfollow', packedFollowee);
|
|
|
|
}
|