From 49dbd7f9d2bcf7c18114bb369d4730855a9f8391 Mon Sep 17 00:00:00 2001 From: MeiMei <30769358+mei23@users.noreply.github.com> Date: Mon, 15 Oct 2018 16:51:23 +0900 Subject: [PATCH] Fix following from Preroma does not complete (#2905) * In Follow Accept/Reject, send previous received id * In Follow Accept/Reject, send Activity.actor --- src/models/follow-request.ts | 1 + src/remote/activitypub/kernel/follow.ts | 2 +- src/remote/activitypub/renderer/accept.ts | 6 +++++- src/remote/activitypub/renderer/follow.ts | 16 +++++++++++----- src/remote/activitypub/renderer/reject.ts | 6 +++++- src/services/following/create.ts | 6 +++--- src/services/following/requests/accept.ts | 7 ++++++- src/services/following/requests/create.ts | 3 ++- src/services/following/requests/reject.ts | 7 ++++++- 9 files changed, 40 insertions(+), 14 deletions(-) diff --git a/src/models/follow-request.ts b/src/models/follow-request.ts index 8ed131c80e..b84fc5f5f5 100644 --- a/src/models/follow-request.ts +++ b/src/models/follow-request.ts @@ -12,6 +12,7 @@ export type IFollowRequest = { createdAt: Date; followeeId: mongo.ObjectID; followerId: mongo.ObjectID; + requestId?: string; // id of Follow Activity // 非正規化 _followee: { diff --git a/src/remote/activitypub/kernel/follow.ts b/src/remote/activitypub/kernel/follow.ts index 464f8582b7..e2db70b20d 100644 --- a/src/remote/activitypub/kernel/follow.ts +++ b/src/remote/activitypub/kernel/follow.ts @@ -23,5 +23,5 @@ export default async (actor: IRemoteUser, activity: IFollow): Promise => { throw new Error('フォローしようとしているユーザーはローカルユーザーではありません'); } - await follow(actor, followee); + await follow(actor, followee, activity.id); }; diff --git a/src/remote/activitypub/renderer/accept.ts b/src/remote/activitypub/renderer/accept.ts index 76326285fd..fdbdff3f12 100644 --- a/src/remote/activitypub/renderer/accept.ts +++ b/src/remote/activitypub/renderer/accept.ts @@ -1,4 +1,8 @@ -export default (object: any) => ({ +import config from '../../../config'; +import { ILocalUser } from '../../../models/user'; + +export default (object: any, user: ILocalUser) => ({ type: 'Accept', + actor: `${config.url}/users/${user._id}`, object }); diff --git a/src/remote/activitypub/renderer/follow.ts b/src/remote/activitypub/renderer/follow.ts index 522422bcff..98d4cdd020 100644 --- a/src/remote/activitypub/renderer/follow.ts +++ b/src/remote/activitypub/renderer/follow.ts @@ -1,8 +1,14 @@ import config from '../../../config'; import { IUser, isLocalUser } from '../../../models/user'; -export default (follower: IUser, followee: IUser) => ({ - type: 'Follow', - actor: isLocalUser(follower) ? `${config.url}/users/${follower._id}` : follower.uri, - object: isLocalUser(followee) ? `${config.url}/users/${followee._id}` : followee.uri -}); +export default (follower: IUser, followee: IUser, requestId?: string) => { + const follow = { + type: 'Follow', + actor: isLocalUser(follower) ? `${config.url}/users/${follower._id}` : follower.uri, + object: isLocalUser(followee) ? `${config.url}/users/${followee._id}` : followee.uri + } as any; + + if (requestId) follow.id = requestId; + + return follow; +}; diff --git a/src/remote/activitypub/renderer/reject.ts b/src/remote/activitypub/renderer/reject.ts index 2464486c2f..6d7d23708a 100644 --- a/src/remote/activitypub/renderer/reject.ts +++ b/src/remote/activitypub/renderer/reject.ts @@ -1,4 +1,8 @@ -export default (object: any) => ({ +import config from '../../../config'; +import { ILocalUser } from '../../../models/user'; + +export default (object: any, user: ILocalUser) => ({ type: 'Reject', + actor: `${config.url}/users/${user._id}`, object }); diff --git a/src/services/following/create.ts b/src/services/following/create.ts index 01b6b4aed9..1768083d16 100644 --- a/src/services/following/create.ts +++ b/src/services/following/create.ts @@ -10,13 +10,13 @@ import renderAccept from '../../remote/activitypub/renderer/accept'; import { deliver } from '../../queue'; import createFollowRequest from './requests/create'; -export default async function(follower: IUser, followee: IUser) { +export default async function(follower: IUser, followee: IUser, requestId?: string) { // フォロー対象が鍵アカウントである or // フォロワーがBotであり、フォロー対象がBotからのフォローに慎重である or // フォロワーがローカルユーザーであり、フォロー対象がリモートユーザーである // 上記のいずれかに当てはまる場合はすぐフォローせずにフォローリクエストを発行しておく if (followee.isLocked || (followee.carefulBot && follower.isBot) || (isLocalUser(follower) && isRemoteUser(followee))) { - await createFollowRequest(follower, followee); + await createFollowRequest(follower, followee, requestId); return; } @@ -79,7 +79,7 @@ export default async function(follower: IUser, followee: IUser) { } if (isRemoteUser(follower) && isLocalUser(followee)) { - const content = pack(renderAccept(renderFollow(follower, followee))); + const content = pack(renderAccept(renderFollow(follower, followee, requestId), followee)); deliver(followee, content, follower.inbox); } } diff --git a/src/services/following/requests/accept.ts b/src/services/following/requests/accept.ts index e7c8df844a..2a32db6a4d 100644 --- a/src/services/following/requests/accept.ts +++ b/src/services/following/requests/accept.ts @@ -29,7 +29,12 @@ export default async function(followee: IUser, follower: IUser) { }); if (isRemoteUser(follower)) { - const content = pack(renderAccept(renderFollow(follower, followee))); + const request = await FollowRequest.findOne({ + followeeId: followee._id, + followerId: follower._id + }); + + const content = pack(renderAccept(renderFollow(follower, followee, request.requestId), followee as ILocalUser)); deliver(followee as ILocalUser, content, follower.inbox); } diff --git a/src/services/following/requests/create.ts b/src/services/following/requests/create.ts index 5e613fd053..d28c93929a 100644 --- a/src/services/following/requests/create.ts +++ b/src/services/following/requests/create.ts @@ -6,11 +6,12 @@ import renderFollow from '../../../remote/activitypub/renderer/follow'; import { deliver } from '../../../queue'; import FollowRequest from '../../../models/follow-request'; -export default async function(follower: IUser, followee: IUser) { +export default async function(follower: IUser, followee: IUser, requestId?: string) { await FollowRequest.insert({ createdAt: new Date(), followerId: follower._id, followeeId: followee._id, + requestId, // 非正規化 _follower: { diff --git a/src/services/following/requests/reject.ts b/src/services/following/requests/reject.ts index 91a49db997..73dbbb92e0 100644 --- a/src/services/following/requests/reject.ts +++ b/src/services/following/requests/reject.ts @@ -8,7 +8,12 @@ import { publishMainStream } from '../../../stream'; export default async function(followee: IUser, follower: IUser) { if (isRemoteUser(follower)) { - const content = pack(renderReject(renderFollow(follower, followee))); + const request = await FollowRequest.findOne({ + followeeId: followee._id, + followerId: follower._id + }); + + const content = pack(renderReject(renderFollow(follower, followee, request.requestId), followee as ILocalUser)); deliver(followee as ILocalUser, content, follower.inbox); }