Skip to content
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
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion Controllers/api/v1/newsfeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,28 @@
use Minds\Core\Security;
use Minds\Entities;
use Minds\Entities\Activity;
use Minds\Entities\User;
use Minds\Helpers;
use Minds\Entities\Factory as EntitiesFactory;
use Minds\Helpers\Counters;
use Minds\Helpers\NewsfeedActivityActivityPubClient;
use Minds\Interfaces;
use Minds\Interfaces\Flaggable;
use Minds\Core\Di\Di;
use Minds\Interfaces\ActivityPubClient;

class newsfeed implements Interfaces\Api
{
/** @var ActivityPubClient */
protected $pubSubClient;

public function __construct(ActivityPubClient $pubSubClient = null)
{
$this->pubSubClient = $pubSubClient ?? new NewsfeedActivityActivityPubClient();
// See https://project.hubzilla.org for how to set your own ActivityPub server.
$this->pubSubClient->setActivityPubServer('https://project.hubzilla.org');
}

/**
* Returns the newsfeed
* @param array $pages
Expand Down Expand Up @@ -420,6 +433,18 @@ public function post($pages)
Helpers\Wallet::createTransaction($embeded->owner_guid, 5, $activity->guid, 'Remind');
}

// Post via ActivityPub:
/** @var User $user */
$user = Core\Session::getLoggedinUser();

$this->pubSubClient->setActor($user->name, "https://pub.minds.com/{$user->username}");

$this->pubSubClient->postArticle(
$embeded->getTitle(),
$embeded->description,
"https://pub.minds.com/{$user->username}/friends"
);

// Follow activity
(new Core\Notification\PostSubscriptions\Manager())
->setEntityGuid($activity->guid)
Expand Down Expand Up @@ -746,7 +771,7 @@ public function delete($pages)
if (!$activity->canEdit()) {
return Factory::response(array('status' => 'error', 'message' => 'you don\'t have permission'));
}
/** @var Entities\User $owner */
/** @var User $owner */
$owner = $activity->getOwnerEntity();

if (
Expand Down
126 changes: 126 additions & 0 deletions Helpers/NewsfeedActivityActivityPubClient.php
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',

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.

Copy link
Author

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.

'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();
}
}
26 changes: 26 additions & 0 deletions Interfaces/ActivityPubClient.php
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);
}