2021-08-19 12:55:45 +00: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';
|
2018-10-29 11:32:42 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
export default async function(blocker: User, blockee: User) {
|
2018-10-29 11:32:42 +00:00
|
|
|
await Promise.all([
|
|
|
|
cancelRequest(blocker, blockee),
|
|
|
|
cancelRequest(blockee, blocker),
|
|
|
|
unFollow(blocker, blockee),
|
2021-08-17 12:48:59 +00:00
|
|
|
unFollow(blockee, blocker),
|
|
|
|
removeFromList(blockee, blocker),
|
2018-10-29 11:32:42 +00:00
|
|
|
]);
|
|
|
|
|
2021-03-21 12:27:09 +00:00
|
|
|
await Blockings.insert({
|
2019-04-07 12:50:36 +00:00
|
|
|
id: genId(),
|
2018-10-29 11:32:42 +00:00
|
|
|
createdAt: new Date(),
|
2019-04-07 12:50:36 +00:00
|
|
|
blockerId: blocker.id,
|
|
|
|
blockeeId: blockee.id,
|
2018-10-29 11:32:42 +00:00
|
|
|
});
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
if (Users.isLocalUser(blocker) && Users.isRemoteUser(blockee)) {
|
2019-01-30 17:29:36 +00:00
|
|
|
const content = renderActivity(renderBlock(blocker, blockee));
|
2018-10-29 11:32:42 +00:00
|
|
|
deliver(blocker, content, blockee.inbox);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
async function cancelRequest(follower: User, followee: User) {
|
|
|
|
const request = await FollowRequests.findOne({
|
|
|
|
followeeId: followee.id,
|
|
|
|
followerId: follower.id
|
2018-10-29 11:32:42 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (request == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
await FollowRequests.delete({
|
|
|
|
followeeId: followee.id,
|
|
|
|
followerId: follower.id
|
2018-10-29 11:32:42 +00:00
|
|
|
});
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
if (Users.isLocalUser(followee)) {
|
|
|
|
Users.pack(followee, followee, {
|
2018-10-29 11:32:42 +00:00
|
|
|
detail: true
|
2019-04-07 12:50:36 +00:00
|
|
|
}).then(packed => publishMainStream(followee.id, 'meUpdated', packed));
|
2018-10-29 11:32:42 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
if (Users.isLocalUser(follower)) {
|
|
|
|
Users.pack(followee, follower, {
|
2018-10-30 05:34:32 +00:00
|
|
|
detail: true
|
2021-03-21 06:14:03 +00:00
|
|
|
}).then(packed => {
|
|
|
|
publishUserEvent(follower.id, 'unfollow', packed);
|
|
|
|
publishMainStream(follower.id, 'unfollow', packed);
|
|
|
|
});
|
2018-10-29 11:32:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// リモートにフォローリクエストをしていたらUndoFollow送信
|
2019-04-07 12:50:36 +00:00
|
|
|
if (Users.isLocalUser(follower) && Users.isRemoteUser(followee)) {
|
2019-01-30 17:29:36 +00:00
|
|
|
const content = renderActivity(renderUndo(renderFollow(follower, followee), follower));
|
2018-10-29 11:32:42 +00:00
|
|
|
deliver(follower, content, followee.inbox);
|
|
|
|
}
|
|
|
|
|
|
|
|
// リモートからフォローリクエストを受けていたらReject送信
|
2019-04-07 12:50:36 +00:00
|
|
|
if (Users.isRemoteUser(follower) && Users.isLocalUser(followee)) {
|
2019-04-12 16:43:22 +00:00
|
|
|
const content = renderActivity(renderReject(renderFollow(follower, followee, request.requestId!), followee));
|
2018-10-29 11:32:42 +00:00
|
|
|
deliver(followee, content, follower.inbox);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
async function unFollow(follower: User, followee: User) {
|
|
|
|
const following = await Followings.findOne({
|
|
|
|
followerId: follower.id,
|
|
|
|
followeeId: followee.id
|
2018-10-29 11:32:42 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (following == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
Followings.delete(following.id);
|
2018-10-29 11:32:42 +00:00
|
|
|
|
|
|
|
//#region Decrement following count
|
2019-04-07 12:50:36 +00:00
|
|
|
Users.decrement({ id: follower.id }, 'followingCount', 1);
|
2018-10-29 11:32:42 +00:00
|
|
|
//#endregion
|
|
|
|
|
|
|
|
//#region Decrement followers count
|
2019-04-07 12:50:36 +00:00
|
|
|
Users.decrement({ id: followee.id }, 'followersCount', 1);
|
2018-10-29 11:32:42 +00:00
|
|
|
//#endregion
|
|
|
|
|
|
|
|
perUserFollowingChart.update(follower, followee, false);
|
|
|
|
|
|
|
|
// Publish unfollow event
|
2019-04-07 12:50:36 +00:00
|
|
|
if (Users.isLocalUser(follower)) {
|
|
|
|
Users.pack(followee, follower, {
|
2018-10-30 05:34:32 +00:00
|
|
|
detail: true
|
2021-03-21 06:14:03 +00:00
|
|
|
}).then(packed => {
|
|
|
|
publishUserEvent(follower.id, 'unfollow', packed);
|
|
|
|
publishMainStream(follower.id, 'unfollow', packed);
|
|
|
|
});
|
2018-10-29 11:32:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// リモートにフォローをしていたらUndoFollow送信
|
2019-04-07 12:50:36 +00:00
|
|
|
if (Users.isLocalUser(follower) && Users.isRemoteUser(followee)) {
|
2019-01-30 17:29:36 +00:00
|
|
|
const content = renderActivity(renderUndo(renderFollow(follower, followee), follower));
|
2018-10-29 11:32:42 +00:00
|
|
|
deliver(follower, content, followee.inbox);
|
|
|
|
}
|
|
|
|
}
|
2021-08-17 12:48:59 +00: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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|