From 25eef848f6ae55af9b3e3faf354eb6b5fbb8bf98 Mon Sep 17 00:00:00 2001 From: Armin Date: Wed, 27 Feb 2019 15:04:54 +0100 Subject: [PATCH] Fetch actorObject and lookup inbox before sending object + addCommentAuthor --- src/activitypub/ActivityPub.js | 32 ++++++++++++++++++++++----- src/activitypub/NitroDatasource.js | 15 +++++++++++-- test/features/activity-follow.feature | 7 +++--- test/features/activity-like.feature | 24 ++++++++++++++++++++ 4 files changed, 67 insertions(+), 11 deletions(-) create mode 100644 test/features/activity-like.feature diff --git a/src/activitypub/ActivityPub.js b/src/activitypub/ActivityPub.js index 74e047e12..c2ea1b2c1 100644 --- a/src/activitypub/ActivityPub.js +++ b/src/activitypub/ActivityPub.js @@ -122,6 +122,8 @@ export default class ActivityPub { case 'Follow': const followActivity = activity.object return this.dataSource.undoFollowActivity(followActivity.actor, followActivity.object) + case 'Like': + return this.dataSource.deleteShouted(activity) default: } } @@ -180,10 +182,25 @@ export default class ActivityPub { } } + getActorObject (url) { + return new Promise((resolve, reject) => { + request({ + url: url, + headers: { + 'Accept': 'application/json' + } + }, (err, response, body) => { + if (err) { + reject(err) + } + resolve(JSON.parse(body)) + }) + }) + } + async sendActivity (activity) { delete activity.send const fromName = extractNameFromId(activity.actor) - if (Array.isArray(activity.to) && isPublicAddressed(activity)) { const sharedInboxEndpoints = await this.dataSource.getSharedInboxEndpoints() // serve shared inbox endpoints @@ -194,14 +211,17 @@ export default class ActivityPub { return !(isPublicAddressed({ to: el })) }) // serve the rest - activity.to.map((el) => { - return this.trySend(activity, fromName, new URL(el).host, el) + activity.to.map(async (el) => { + const actorObject = await this.getActorObject(el) + return this.trySend(activity, fromName, new URL(el).host, actorObject.inbox) }) } else if (typeof activity.to === 'string') { - return this.trySend(activity, fromName, new URL(activity.to).host, activity.to) + const actorObject = await this.getActorObject(activity.to) + return this.trySend(activity, fromName, new URL(activity.to).host, actorObject.inbox) } else if (Array.isArray(activity.to)) { - activity.to.map((el) => { - return this.trySend(activity, fromName, new URL(el).host, el) + activity.to.map(async (el) => { + const actorObject = await this.getActorObject(el) + return this.trySend(activity, fromName, new URL(el).host, actorObject.inbox) }) } } diff --git a/src/activitypub/NitroDatasource.js b/src/activitypub/NitroDatasource.js index 19e931eef..054b77854 100644 --- a/src/activitypub/NitroDatasource.js +++ b/src/activitypub/NitroDatasource.js @@ -472,10 +472,21 @@ export default class NitroDatasource { } ` }) - throwErrorIfApolloErrorOccurred(result) - const postId = extractIdFromActivityId(postObject.inReplyTo) + const toUserId = await this.ensureUser(activity.actor) + const result2 = await this.client.mutate({ + mutation: gql` + mutation { + AddCommentAuthor(from: {id: "${result.data.CreateComment.id}"}, to: {id: "${toUserId}"}) { + id + } + } + ` + }) + throwErrorIfApolloErrorOccurred(result2) + + const postId = extractIdFromActivityId(postObject.inReplyTo) result = await this.client.mutate({ mutation: gql` mutation { diff --git a/test/features/activity-follow.feature b/test/features/activity-follow.feature index 578e57cb1..a6974309c 100644 --- a/test/features/activity-follow.feature +++ b/test/features/activity-follow.feature @@ -6,9 +6,10 @@ Feature: Follow a user Background: Given our own server runs at "http://localhost:4123" And we have the following users in our database: - | Slug | - | karl-heinz | - | peter-lustiger | + | Slug | + | peter-lustiger | + | bob-der-baumeister | + | karl-heinz | Scenario: Send a follow to a user inbox and make sure it's added to the right followers collection When I send a POST request with the following activity to "/activitypub/users/peter-lustiger/inbox": diff --git a/test/features/activity-like.feature b/test/features/activity-like.feature new file mode 100644 index 000000000..c605bdb76 --- /dev/null +++ b/test/features/activity-like.feature @@ -0,0 +1,24 @@ +Feature: Like an object like an article or note + As a user I want to like others posts + Also if I do not want to follow a previous followed user anymore, + I want to undo the follow. + + Background: + Given our own server runs at "http://localhost:4123" + And we have the following users in our database: + | Slug | + | karl-heinz | + | peter-lustiger | + + Scenario: Send a like of a person to an users inbox and make sure it's added to the likes collection + When I send a POST request with the following activity to "/activitypub/users/karl-heinz/inbox": + """ + { + "@context": "https://www.w3.org/ns/activitystreams", + "id": "https://localhost:4123/activitypub/users/peter-lustiger/status/83J23549sda1k72fsa4567na42312455kad83", + "type": "Like", + "actor": "http://localhost:4123/activitypub/users/peter-lustiger + "object": "http://localhost:4123/activitypub/users/karl-heinz" + } + """ + Then I expect the status code to be 200