From 2ed5ecd6a1f5814e5604ec4ae8ecce2c87cf586d Mon Sep 17 00:00:00 2001 From: Johann150 Date: Fri, 9 Aug 2024 00:25:12 +0200 Subject: [PATCH] server: code improvements in following service --- .../backend/src/services/following/create.ts | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/packages/backend/src/services/following/create.ts b/packages/backend/src/services/following/create.ts index 7ad85dd99..e50fd0d26 100644 --- a/packages/backend/src/services/following/create.ts +++ b/packages/backend/src/services/following/create.ts @@ -51,7 +51,7 @@ export async function insertFollowingDoc(followee: { id: User['id']; host: User[ followerId: follower.id, }); - if (requested) { + if (requested > 0) { await FollowRequests.delete({ followeeId: followee.id, followerId: follower.id, @@ -132,7 +132,7 @@ export default async function(_follower: { id: User['id'] }, _followee: { id: Us ]); // check blocking - const [blocking, blocked] = await Promise.all([ + const [sender_block, receiver_blocks] = await Promise.all([ Blockings.findOneBy({ blockerId: follower.id, blockeeId: followee.id, @@ -143,30 +143,33 @@ export default async function(_follower: { id: User['id'] }, _followee: { id: Us }), ]); - if (Users.isRemoteUser(follower) && Users.isLocalUser(followee) && blocked) { - // リモートフォローを受けてブロックしていた場合は、エラーにするのではなくRejectを送り返しておしまい。 + if (Users.isRemoteUser(follower) && Users.isLocalUser(followee) && receiver_blocks) { + // remote actor is trying to follow a local user which blocks them + // -> follow request is immediately rejected const content = renderActivity(renderReject(renderFollow(follower, followee, requestId), followee)); - deliver(followee , content, follower.inbox); + deliver(followee, content, follower.inbox); return; - } else if (Users.isRemoteUser(follower) && Users.isLocalUser(followee) && blocking) { - // リモートフォローを受けてブロックされているはずの場合だったら、ブロック解除しておく。 - await Blockings.delete(blocking.id); + } else if (Users.isRemoteUser(follower) && Users.isLocalUser(followee) && sender_block) { + // receiving a follow from a remote user that supposedly blocks the followee + // this is not possible, so the block must be in error + // -> delete the block + await Blockings.delete(sender_block.id); } else { - // それ以外は単純に例外 - if (blocking != null) throw new IdentifiableError('710e8fb0-b8c3-4922-be49-d5d93d8e6a6e', 'blocking'); - if (blocked != null) throw new IdentifiableError('3338392a-f764-498d-8855-db939dcf8c48', 'blocked'); + // everything else can just be an error on our side + if (sender_block != null) throw new IdentifiableError('710e8fb0-b8c3-4922-be49-d5d93d8e6a6e', 'blocking'); + if (receiver_blocks != null) throw new IdentifiableError('3338392a-f764-498d-8855-db939dcf8c48', 'blocked'); } const followeeProfile = await UserProfiles.findOneByOrFail({ userId: followee.id }); - // フォロー対象が鍵アカウントである or - // フォロワーがBotであり、フォロー対象がBotからのフォローに慎重である or - // フォロワーがローカルユーザーであり、フォロー対象がリモートユーザーである - // 上記のいずれかに当てはまる場合はすぐフォローせずにフォローリクエストを発行しておく + // The followee is a locked account or + // The follower is a bot and the followee is cautios about being followed by bots or + // The follower is local and the followee is remote if (followee.isLocked || (followeeProfile.carefulBot && follower.isBot) || (Users.isLocalUser(follower) && Users.isRemoteUser(followee))) { + // Can not follow immediately, create a follow request instead. let autoAccept = false; - // Even for locked accounts, if they are already following, go through immediately. + // If the follow already exists, nothing more needs to be done. const following = await Followings.countBy({ followerId: follower.id, followeeId: followee.id,