From e617ced1d35bbc68d685ebdb3be09894621237ec Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 3 Dec 2021 11:43:05 +0900 Subject: [PATCH] refactoring https: //github.com/misskey-dev/misskey/pull/7901 Co-Authored-By: MeiMei <30769358+mei23@users.noreply.github.com> --- .../activitypub/kernel/reject/follow.ts | 7 +- .../endpoints/following/requests/reject.ts | 2 +- .../backend/src/services/following/reject.ts | 105 ++++++++++++++++++ .../src/services/following/requests/reject.ts | 46 -------- 4 files changed, 110 insertions(+), 50 deletions(-) create mode 100644 packages/backend/src/services/following/reject.ts delete mode 100644 packages/backend/src/services/following/requests/reject.ts diff --git a/packages/backend/src/remote/activitypub/kernel/reject/follow.ts b/packages/backend/src/remote/activitypub/kernel/reject/follow.ts index 356547440..049437b18 100644 --- a/packages/backend/src/remote/activitypub/kernel/reject/follow.ts +++ b/packages/backend/src/remote/activitypub/kernel/reject/follow.ts @@ -1,8 +1,9 @@ import { IRemoteUser } from '@/models/entities/user'; -import reject from '@/services/following/requests/reject'; +import { remoteReject } from '@/services/following/reject'; import { IFollow } from '../../type'; import DbResolver from '../../db-resolver'; import { relayRejected } from '@/services/relay'; +import { Users } from '@/models'; export default async (actor: IRemoteUser, activity: IFollow): Promise => { // ※ activityはこっちから投げたフォローリクエストなので、activity.actorは存在するローカルユーザーである必要がある @@ -14,7 +15,7 @@ export default async (actor: IRemoteUser, activity: IFollow): Promise => return `skip: follower not found`; } - if (follower.host != null) { + if (!Users.isLocalUser(follower)) { return `skip: follower is not a local user`; } @@ -24,6 +25,6 @@ export default async (actor: IRemoteUser, activity: IFollow): Promise => return await relayRejected(match[1]); } - await reject(actor, follower); + await remoteReject(actor, follower); return `ok`; }; diff --git a/packages/backend/src/server/api/endpoints/following/requests/reject.ts b/packages/backend/src/server/api/endpoints/following/requests/reject.ts index 620324361..30d0e094c 100644 --- a/packages/backend/src/server/api/endpoints/following/requests/reject.ts +++ b/packages/backend/src/server/api/endpoints/following/requests/reject.ts @@ -1,6 +1,6 @@ import $ from 'cafy'; import { ID } from '@/misc/cafy-id'; -import rejectFollowRequest from '@/services/following/requests/reject'; +import { rejectFollowRequest } from '@/services/following/reject'; import define from '../../../define'; import { ApiError } from '../../../error'; import { getUser } from '../../../common/getters'; diff --git a/packages/backend/src/services/following/reject.ts b/packages/backend/src/services/following/reject.ts new file mode 100644 index 000000000..0ec4d7d00 --- /dev/null +++ b/packages/backend/src/services/following/reject.ts @@ -0,0 +1,105 @@ +import { renderActivity } from '@/remote/activitypub/renderer/index'; +import renderFollow from '@/remote/activitypub/renderer/follow'; +import renderReject from '@/remote/activitypub/renderer/reject'; +import { deliver } from '@/queue/index'; +import { publishMainStream, publishUserEvent } from '@/services/stream'; +import { User, ILocalUser, IRemoteUser } from '@/models/entities/user'; +import { Users, FollowRequests, Followings } from '@/models/index'; +import { decrementFollowing } from './delete'; + +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, + followerId: follower.id + }); + + 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, + followerId: follower.id + }); + + 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, + followerId: follower.id + }); + + 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, { + detail: true + }); + + publishUserEvent(follower.id, 'unfollow', packedFollowee); + publishMainStream(follower.id, 'unfollow', packedFollowee); +} diff --git a/packages/backend/src/services/following/requests/reject.ts b/packages/backend/src/services/following/requests/reject.ts deleted file mode 100644 index 41cebd9e4..000000000 --- a/packages/backend/src/services/following/requests/reject.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { renderActivity } from '@/remote/activitypub/renderer/index'; -import renderFollow from '@/remote/activitypub/renderer/follow'; -import renderReject from '@/remote/activitypub/renderer/reject'; -import { deliver } from '@/queue/index'; -import { publishMainStream, publishUserEvent } from '@/services/stream'; -import { User, ILocalUser } from '@/models/entities/user'; -import { Users, FollowRequests, Followings } from '@/models/index'; -import { decrementFollowing } from '../delete'; - -export default async function(followee: { id: User['id']; host: User['host']; uri: User['host'] }, follower: User) { - if (Users.isRemoteUser(follower) && Users.isLocalUser(followee)) { - const request = await FollowRequests.findOne({ - followeeId: followee.id, - followerId: follower.id - }); - - const content = renderActivity(renderReject(renderFollow(follower, followee, request!.requestId!), followee)); - deliver(followee, content, follower.inbox); - } - - const request = await FollowRequests.findOne({ - followeeId: followee.id, - followerId: follower.id - }); - - 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); - } - } - - Users.pack(followee.id, follower, { - detail: true - }).then(packed => { - publishUserEvent(follower.id, 'unfollow', packed); - publishMainStream(follower.id, 'unfollow', packed); - }); -}