From cc3704c3e94db4eb6f7e46bec2f68d19a7b0252e Mon Sep 17 00:00:00 2001 From: Akihiko Odaki Date: Tue, 3 Apr 2018 12:07:29 +0900 Subject: [PATCH] Accept remote follow --- src/remote/activitypub/act/follow.ts | 49 +++++++++++++---------- src/remote/activitypub/renderer/accept.ts | 4 ++ 2 files changed, 32 insertions(+), 21 deletions(-) create mode 100644 src/remote/activitypub/renderer/accept.ts diff --git a/src/remote/activitypub/act/follow.ts b/src/remote/activitypub/act/follow.ts index ec9e080dfc..d7ea15fa36 100644 --- a/src/remote/activitypub/act/follow.ts +++ b/src/remote/activitypub/act/follow.ts @@ -4,11 +4,13 @@ import Following from '../../../models/following'; import User from '../../../models/user'; import config from '../../../config'; import queue from '../../../queue'; +import context from '../renderer/context'; +import renderAccept from '../renderer/accept'; +import request from '../../request'; export default async (actor, activity) => { const prefix = config.url + '/@'; const id = activity.object.id || activity.object; - let following; if (!id.startsWith(prefix)) { return null; @@ -24,28 +26,33 @@ export default async (actor, activity) => { throw new Error(); } - try { - following = await Following.insert({ + const accept = renderAccept(activity); + accept['@context'] = context; + + await Promise.all([ + request(followee, actor.account.inbox, accept), + + Following.insert({ createdAt: new Date(), followerId: actor._id, followeeId: followee._id - }); - } catch (exception) { - // duplicate key error - if (exception instanceof MongoError && exception.code === 11000) { - return null; - } - - throw exception; - } - - await new Promise((resolve, reject) => { - queue.create('http', { type: 'follow', following: following._id }).save(error => { - if (error) { - reject(error); - } else { - resolve(null); + }).then(following => new Promise((resolve, reject) => { + queue.create('http', { type: 'follow', following: following._id }).save(error => { + if (error) { + reject(error); + } else { + resolve(); + } + }); + }), error => { + // duplicate key error + if (error instanceof MongoError && error.code === 11000) { + return; } - }); - }); + + throw error; + }) + ]); + + return null; }; diff --git a/src/remote/activitypub/renderer/accept.ts b/src/remote/activitypub/renderer/accept.ts new file mode 100644 index 0000000000..00c76883a9 --- /dev/null +++ b/src/remote/activitypub/renderer/accept.ts @@ -0,0 +1,4 @@ +export default object => ({ + type: 'Accept', + object +});