Fetch actorObject and lookup inbox before sending object + addCommentAuthor

This commit is contained in:
Armin 2019-02-27 15:04:54 +01:00
parent adb674b98d
commit 25eef848f6
4 changed files with 67 additions and 11 deletions

View File

@ -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)
})
}
}

View File

@ -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 {

View File

@ -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":

View File

@ -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