2021-08-19 14:55:45 +02:00
|
|
|
import { publishMainStream, publishUserEvent } from '@/services/stream';
|
|
|
|
import { renderActivity } from '@/remote/activitypub/renderer/index';
|
|
|
|
import renderFollow from '@/remote/activitypub/renderer/follow';
|
|
|
|
import renderUndo from '@/remote/activitypub/renderer/undo';
|
|
|
|
import renderBlock from '@/remote/activitypub/renderer/block';
|
|
|
|
import { deliver } from '@/queue/index';
|
|
|
|
import renderReject from '@/remote/activitypub/renderer/reject';
|
|
|
|
import { User } from '@/models/entities/user';
|
|
|
|
import { Blockings, Users, FollowRequests, Followings, UserListJoinings, UserLists } from '@/models/index';
|
|
|
|
import { perUserFollowingChart } from '@/services/chart/index';
|
|
|
|
import { genId } from '@/misc/gen-id';
|
2021-10-13 18:55:39 +02:00
|
|
|
import { IdentifiableError } from '@/misc/identifiable-error';
|
2018-10-29 12:32:42 +01:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
export default async function(blocker: User, blockee: User) {
|
2018-10-29 12:32:42 +01:00
|
|
|
await Promise.all([
|
|
|
|
cancelRequest(blocker, blockee),
|
|
|
|
cancelRequest(blockee, blocker),
|
|
|
|
unFollow(blocker, blockee),
|
2021-08-17 14:48:59 +02:00
|
|
|
unFollow(blockee, blocker),
|
|
|
|
removeFromList(blockee, blocker),
|
2018-10-29 12:32:42 +01:00
|
|
|
]);
|
|
|
|
|
2021-03-21 13:27:09 +01:00
|
|
|
await Blockings.insert({
|
2019-04-07 14:50:36 +02:00
|
|
|
id: genId(),
|
2018-10-29 12:32:42 +01:00
|
|
|
createdAt: new Date(),
|
2019-04-07 14:50:36 +02:00
|
|
|
blockerId: blocker.id,
|
|
|
|
blockeeId: blockee.id,
|
2018-10-29 12:32:42 +01:00
|
|
|
});
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
if (Users.isLocalUser(blocker) && Users.isRemoteUser(blockee)) {
|
2019-01-30 18:29:36 +01:00
|
|
|
const content = renderActivity(renderBlock(blocker, blockee));
|
2018-10-29 12:32:42 +01:00
|
|
|
deliver(blocker, content, blockee.inbox);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
async function cancelRequest(follower: User, followee: User) {
|
|
|
|
const request = await FollowRequests.findOne({
|
|
|
|
followeeId: followee.id,
|
2021-12-09 15:58:30 +01:00
|
|
|
followerId: follower.id,
|
2018-10-29 12:32:42 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (request == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
await FollowRequests.delete({
|
|
|
|
followeeId: followee.id,
|
2021-12-09 15:58:30 +01:00
|
|
|
followerId: follower.id,
|
2018-10-29 12:32:42 +01:00
|
|
|
});
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
if (Users.isLocalUser(followee)) {
|
|
|
|
Users.pack(followee, followee, {
|
2021-12-09 15:58:30 +01:00
|
|
|
detail: true,
|
2019-04-07 14:50:36 +02:00
|
|
|
}).then(packed => publishMainStream(followee.id, 'meUpdated', packed));
|
2018-10-29 12:32:42 +01:00
|
|
|
}
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
if (Users.isLocalUser(follower)) {
|
|
|
|
Users.pack(followee, follower, {
|
2021-12-09 15:58:30 +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-10-29 12:32:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// リモートにフォローリクエストをしていたらUndoFollow送信
|
2019-04-07 14:50:36 +02:00
|
|
|
if (Users.isLocalUser(follower) && Users.isRemoteUser(followee)) {
|
2019-01-30 18:29:36 +01:00
|
|
|
const content = renderActivity(renderUndo(renderFollow(follower, followee), follower));
|
2018-10-29 12:32:42 +01:00
|
|
|
deliver(follower, content, followee.inbox);
|
|
|
|
}
|
|
|
|
|
|
|
|
// リモートからフォローリクエストを受けていたらReject送信
|
2019-04-07 14:50:36 +02:00
|
|
|
if (Users.isRemoteUser(follower) && Users.isLocalUser(followee)) {
|
2019-04-12 18:43:22 +02:00
|
|
|
const content = renderActivity(renderReject(renderFollow(follower, followee, request.requestId!), followee));
|
2018-10-29 12:32:42 +01:00
|
|
|
deliver(followee, content, follower.inbox);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
async function unFollow(follower: User, followee: User) {
|
|
|
|
const following = await Followings.findOne({
|
|
|
|
followerId: follower.id,
|
2021-12-09 15:58:30 +01:00
|
|
|
followeeId: followee.id,
|
2018-10-29 12:32:42 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (following == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
Followings.delete(following.id);
|
2018-10-29 12:32:42 +01:00
|
|
|
|
|
|
|
//#region Decrement following count
|
2019-04-07 14:50:36 +02:00
|
|
|
Users.decrement({ id: follower.id }, 'followingCount', 1);
|
2018-10-29 12:32:42 +01:00
|
|
|
//#endregion
|
|
|
|
|
|
|
|
//#region Decrement followers count
|
2019-04-07 14:50:36 +02:00
|
|
|
Users.decrement({ id: followee.id }, 'followersCount', 1);
|
2018-10-29 12:32:42 +01:00
|
|
|
//#endregion
|
|
|
|
|
|
|
|
perUserFollowingChart.update(follower, followee, false);
|
|
|
|
|
|
|
|
// Publish unfollow event
|
2019-04-07 14:50:36 +02:00
|
|
|
if (Users.isLocalUser(follower)) {
|
|
|
|
Users.pack(followee, follower, {
|
2021-12-09 15:58:30 +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-10-29 12:32:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// リモートにフォローをしていたらUndoFollow送信
|
2019-04-07 14:50:36 +02:00
|
|
|
if (Users.isLocalUser(follower) && Users.isRemoteUser(followee)) {
|
2019-01-30 18:29:36 +01:00
|
|
|
const content = renderActivity(renderUndo(renderFollow(follower, followee), follower));
|
2018-10-29 12:32:42 +01:00
|
|
|
deliver(follower, content, followee.inbox);
|
|
|
|
}
|
|
|
|
}
|
2021-08-17 14:48:59 +02:00
|
|
|
|
|
|
|
async function removeFromList(listOwner: User, user: User) {
|
|
|
|
const userLists = await UserLists.find({
|
|
|
|
userId: listOwner.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const userList of userLists) {
|
|
|
|
await UserListJoinings.delete({
|
|
|
|
userListId: userList.id,
|
|
|
|
userId: user.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|