-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added support for publishing newsfeed posts via the ActivityPub spec. #28
Open
hopeseekr
wants to merge
5
commits into
Minds:master
Choose a base branch
from
hopeseekr-contribs:activitypub_posts
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bfdab74
Added support for publishing newsfeed posts via the ActivityPub spec.
hopeseekr cfd7566
Refactored the ActivityPub client along code review suggestions.
hopeseekr 4587572
Fixed a typo in a file name.
hopeseekr 2a8a33e
Added spec tests.
hopeseekr 384d46c
Moved the ActivityPub to the Newsfeed directory.
hopeseekr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Minds\Helpers; | ||
|
||
use GuzzleHttp\Client as Guzzle_Client; | ||
use GuzzleHttp\Psr7\Response; | ||
use LogicException; | ||
use Minds\Interfaces\ActivityPubClient; | ||
|
||
class NewsfeedActivityActivityPubClient implements ActivityPubClient | ||
{ | ||
/** @var Guzzle_Client */ | ||
protected $client; | ||
|
||
/** @var Response */ | ||
public $response; | ||
|
||
/** @var string */ | ||
protected $activityPubURI; | ||
|
||
/** @var string */ | ||
protected $actorName; | ||
|
||
/** @var string */ | ||
protected $actorURI; | ||
|
||
public function __construct(Guzzle_Client $client = null) | ||
{ | ||
$this->client = $client ?? new Guzzle_Client(); | ||
} | ||
|
||
public function setActivityPubServer(string $serverURL) | ||
{ | ||
$this->activityPubURI = $serverURL; | ||
} | ||
|
||
public function setActor(string $actorName, string $actorURI) | ||
{ | ||
$this->actorName = $actorName; | ||
$this->actorURI = $actorURI; | ||
} | ||
|
||
private function assertPubSubURI() | ||
{ | ||
if (!$this->activityPubURI) { | ||
throw new LogicException('The PubSub URI has not been specified.'); | ||
} | ||
} | ||
|
||
private function assertActor() | ||
{ | ||
if (!$this->actorURI) { | ||
throw new LogicException('The PubSub actor has not been specified.'); | ||
} | ||
} | ||
|
||
protected function validate() | ||
{ | ||
$this->assertPubSubURI(); | ||
$this->assertActor(); | ||
} | ||
|
||
/** | ||
* See: https://w3c.github.io/activitypub/#create-activity-outbox | ||
* | ||
* @param string $title | ||
* @param string $body | ||
* @param string[] $to | ||
* @param string[]|null $cc | ||
* @return int The HTTP Status code of the request. | ||
*/ | ||
public function postArticle(string $title, string $body, array $to, ?array $cc = null) | ||
{ | ||
$this->validate(); | ||
|
||
$params = [ | ||
'@context' => [ | ||
'https://www.w3.org/ns/activitystreams', | ||
'@language' => 'en-GB' | ||
], | ||
'id' => 'https://rhiaro.co.uk/2016/05/minimal-activitypub', | ||
'type' => 'Article', | ||
'name' => $title, | ||
'content' => $body, | ||
'attributedTo' => $this->actorURI, | ||
'to' => $to, | ||
'cc' => $cc, | ||
]; | ||
|
||
$this->response = $this->client->post($this->activityPubURI, [ | ||
'Content-Type' => 'application/json', | ||
'json' => $params, | ||
]); | ||
|
||
return $this->response->getStatusCode(); | ||
} | ||
|
||
/** | ||
* See: https://w3c.github.io/activitypub/#create-activity-outbox | ||
*/ | ||
public function like(string $refObjectURI, array $to, ?string $summary = null, ?array $cc = null) | ||
{ | ||
$this->validate(); | ||
|
||
$params = [ | ||
'@context' => [ | ||
'https://www.w3.org/ns/activitystreams', | ||
'@language' => 'en-GB' | ||
], | ||
'id' => 'https://rhiaro.co.uk/2016/05/minimal-activitypub', | ||
'type' => 'Like', | ||
'actor' => $this->actorURI, | ||
'summary' => $summary ?? "{$this->actorName} liked the post", | ||
'object' => $refObjectURI, | ||
'to' => $to, | ||
'cc' => $cc, | ||
]; | ||
|
||
$this->response = $this->client->post($this->activityPubURI, [ | ||
'Content-Type' => 'application/json', | ||
'json' => $params, | ||
]); | ||
|
||
return $this->response->getStatusCode(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Minds\Interfaces; | ||
|
||
/** | ||
* The basic interface for creating a client/server Activity for Posting articles via the PubSub spec. | ||
* See: https://w3c.github.io/activitypub/#client-to-server-interactions | ||
*/ | ||
interface ActivityPubClient | ||
{ | ||
public function setActivityPubServer(string $serverURL); | ||
|
||
public function setActor(string $actorName, string $actorURI); | ||
|
||
/** | ||
* See: https://w3c.github.io/activitypub/#create-activity-outbox | ||
* @param $to string[] | ||
* @param $cc string[] | ||
*/ | ||
public function postArticle(string $title, string $body, array $to, ?array $cc = null); | ||
|
||
/** | ||
* See: https://w3c.github.io/activitypub/#create-activity-outbox | ||
*/ | ||
public function like(string $refObjectURI, array $to, ?string $sumary = null, ?array $cc = null); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pretty sure id needs to be unique per post.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All I can say is, "Oops!!" Yeah. It's supposed to be the newsfeed URL for that article.