Add Delivery Handler (named Postman) and Subscription hook
Renamed Discovery to Explorer Various bug fixes and minor improvements
This commit is contained in:
parent
542c269885
commit
d7627fb061
@ -1,4 +1,5 @@
|
||||
<?php
|
||||
require_once __DIR__ . DIRECTORY_SEPARATOR . "utils" . DIRECTORY_SEPARATOR . "postman.php";
|
||||
/**
|
||||
* GNU social - a federating social network
|
||||
*
|
||||
@ -45,11 +46,12 @@ class ActivityPubPlugin extends Plugin
|
||||
* @param URLMapper $m
|
||||
* @return void
|
||||
*/
|
||||
public function onRouterInitialized (URLMapper $m) {
|
||||
public function onRouterInitialized (URLMapper $m)
|
||||
{
|
||||
ActivityPubURLMapperOverwrite::overwrite_variable ($m, ':nickname',
|
||||
['action' => 'showstream'],
|
||||
['nickname' => Nickname::DISPLAY_FMT],
|
||||
'apactorprofile');
|
||||
'apActorProfile');
|
||||
|
||||
$m->connect (':nickname/liked.json',
|
||||
['action' => 'apActorLikedCollection'],
|
||||
@ -77,7 +79,8 @@ class ActivityPubPlugin extends Plugin
|
||||
* @param array $versions
|
||||
* @return boolean true
|
||||
*/
|
||||
public function onPluginVersion (array &$versions) {
|
||||
public function onPluginVersion (array &$versions)
|
||||
{
|
||||
$versions[] = [ 'name' => 'ActivityPub',
|
||||
'version' => GNUSOCIAL_VERSION,
|
||||
'author' => 'Daniel Supernault, Diogo Cordeiro',
|
||||
@ -88,6 +91,74 @@ class ActivityPubPlugin extends Plugin
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make sure necessary tables are filled out.
|
||||
*/
|
||||
function onCheckSchema ()
|
||||
{
|
||||
$schema = Schema::get ();
|
||||
$schema->ensureTable ('Activitypub_profile', Activitypub_profile::schemaDef());
|
||||
return true;
|
||||
}
|
||||
|
||||
/********************************************************
|
||||
* Delivery Events *
|
||||
********************************************************/
|
||||
|
||||
/**
|
||||
* Having established a remote subscription, send a notification to the
|
||||
* remote ActivityPub profile's endpoint.
|
||||
*
|
||||
* @param Profile $profile subscriber
|
||||
* @param Profile $other subscribee
|
||||
* @return hook return value
|
||||
* @throws Exception
|
||||
*/
|
||||
function onEndSubscribe (Profile $profile, Profile $other)
|
||||
{
|
||||
if (!$profile->isLocal () || $other->isLocal ()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
$other = Activitypub_profile::fromProfile ($other);
|
||||
} catch (Exception $e) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$postman = new Activitypub_postman ($profile, array ($other));
|
||||
|
||||
$postman->follow ();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify remote server on unsubscribe.
|
||||
*
|
||||
* @param Profile $profile
|
||||
* @param Profile $other
|
||||
* @return hook return value
|
||||
*/
|
||||
function onEndUnsubscribe (Profile $profile, Profile $other)
|
||||
{
|
||||
if (!$profile->isLocal () || $other->isLocal ()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
$other = Activitypub_profile::fromProfile ($other);
|
||||
} catch (Exception $e) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$postman = new Activitypub_postman ($profile, array ($other));
|
||||
|
||||
$postman->undo_follow ();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -127,7 +198,8 @@ class ActivityPubReturn
|
||||
* @param array $res
|
||||
* @return void
|
||||
*/
|
||||
static function answer ($res) {
|
||||
static function answer ($res)
|
||||
{
|
||||
header ('Content-Type: application/activity+json');
|
||||
echo json_encode ($res, JSON_UNESCAPED_SLASHES | (isset ($_GET["pretty"]) ? JSON_PRETTY_PRINT : null));
|
||||
exit;
|
||||
@ -140,10 +212,11 @@ class ActivityPubReturn
|
||||
* @param int32 $code
|
||||
* @return void
|
||||
*/
|
||||
static function error ($m, $code = 500) {
|
||||
static function error ($m, $code = 500)
|
||||
{
|
||||
http_response_code ($code);
|
||||
header ('Content-Type: application/activity+json');
|
||||
$res[] = Activitypub_error::errorMessageToObject ($m);
|
||||
$res[] = Activitypub_error::error_message_to_array ($m);
|
||||
echo json_encode ($res, JSON_UNESCAPED_SLASHES);
|
||||
exit;
|
||||
}
|
||||
|
@ -46,7 +46,8 @@ class apActorFollowersAction extends ManagedAction
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function handle () {
|
||||
protected function handle ()
|
||||
{
|
||||
$nickname = $this->trimmed ('nickname');
|
||||
try {
|
||||
$user = User::getByNickname ($nickname);
|
||||
@ -71,8 +72,7 @@ class apActorFollowersAction extends ManagedAction
|
||||
$since = ($page - 1) * PROFILES_PER_MINILIST;
|
||||
$limit = (($page - 1) == 0 ? 1 : $page) * PROFILES_PER_MINILIST;
|
||||
$sub = $profile->getSubscribers ($since, $limit);
|
||||
}
|
||||
catch(NoResultException $e) {
|
||||
} catch (NoResultException $e) {
|
||||
ActivityPubReturn::error ('This user has no followers.');
|
||||
}
|
||||
|
||||
|
@ -46,7 +46,8 @@ class apActorFollowingAction extends ManagedAction
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function handle () {
|
||||
protected function handle ()
|
||||
{
|
||||
$nickname = $this->trimmed ('nickname');
|
||||
try {
|
||||
$user = User::getByNickname ($nickname);
|
||||
|
@ -76,8 +76,8 @@ class apActorInboxAction extends ManagedAction
|
||||
|
||||
// Get valid Actor object
|
||||
try {
|
||||
require_once dirname (__DIR__) . DIRECTORY_SEPARATOR . "utils" . DIRECTORY_SEPARATOR . "discovery.php";
|
||||
$actor_profile = new Activitypub_Discovery;
|
||||
require_once dirname (__DIR__) . DIRECTORY_SEPARATOR . "utils" . DIRECTORY_SEPARATOR . "explorer.php";
|
||||
$actor_profile = new Activitypub_explorer;
|
||||
$actor_profile = $actor_profile->lookup ($data->actor);
|
||||
$actor_profile = $actor_profile[0];
|
||||
} catch (Exception $e) {
|
||||
|
@ -46,7 +46,8 @@ class apActorLikedCollectionAction extends ManagedAction
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function handle () {
|
||||
protected function handle ()
|
||||
{
|
||||
$nickname = $this->trimmed ('nickname');
|
||||
try {
|
||||
$user = User::getByNickname ($nickname);
|
||||
@ -96,13 +97,14 @@ class apActorLikedCollectionAction extends ManagedAction
|
||||
* Take a fave object and turns it in a pretty array to be used
|
||||
* as a plugin answer
|
||||
*
|
||||
* @param \Fave $fave_object
|
||||
* @param Fave $fave_object
|
||||
* @return array pretty array representating a Fave
|
||||
*/
|
||||
protected function pretty_fave ($fave_object) {
|
||||
protected function pretty_fave ($fave_object)
|
||||
{
|
||||
$res = array("uri" => $fave_object->uri,
|
||||
"created" => $fave_object->created,
|
||||
"object" => Activitypub_notice::noticeToObject (Notice::getByID ($fave_object->notice_id)));
|
||||
"object" => Activitypub_notice::notice_to_array (Notice::getByID ($fave_object->notice_id)));
|
||||
|
||||
return $res;
|
||||
}
|
||||
@ -114,10 +116,11 @@ class apActorLikedCollectionAction extends ManagedAction
|
||||
* @param int32 $limit
|
||||
* @param int32 $since_id
|
||||
* @param int32 $max_id
|
||||
* @return \Fave fetchable fave collection
|
||||
* @return Fave fetchable fave collection
|
||||
*/
|
||||
private static function fetch_faves ($user_id, $limit = 40, $since_id = null,
|
||||
$max_id = null) {
|
||||
$max_id = null)
|
||||
{
|
||||
$fav = new Fave ();
|
||||
|
||||
$fav->user_id = $user_id;
|
||||
|
@ -47,7 +47,8 @@ class apActorProfileAction extends ManagedAction
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function handle() {
|
||||
protected function handle()
|
||||
{
|
||||
$nickname = $this->trimmed ('nickname');
|
||||
try {
|
||||
$user = User::getByNickname ($nickname);
|
||||
@ -57,7 +58,7 @@ class apActorProfileAction extends ManagedAction
|
||||
ActivityPubReturn::error ('Invalid username.', 404);
|
||||
}
|
||||
|
||||
$res = Activitypub_profile::profileToObject ($profile);
|
||||
$res = Activitypub_profile::profile_to_array ($profile);
|
||||
|
||||
ActivityPubReturn::answer ($res);
|
||||
}
|
||||
|
@ -1,10 +1,5 @@
|
||||
<?php
|
||||
|
||||
require_once dirname (__DIR__) . DIRECTORY_SEPARATOR . "utils" . DIRECTORY_SEPARATOR . "discovery.php";
|
||||
use Activitypub_Discovery;
|
||||
use ActivityPubReturn;
|
||||
use Activitypub_notice;
|
||||
|
||||
require_once dirname (__DIR__) . DIRECTORY_SEPARATOR . "utils" . DIRECTORY_SEPARATOR . "explorer.php";
|
||||
/**
|
||||
* GNU social - a federating social network
|
||||
*
|
||||
@ -67,14 +62,11 @@ class apSharedInboxAction extends ManagedAction
|
||||
if (!isset($data->actor)) {
|
||||
ActivityPubReturn::error ("Actor was not specified.");
|
||||
}
|
||||
if (!isset($data->to)) {
|
||||
ActivityPubReturn::error ("To was not specified.");
|
||||
}
|
||||
if (!isset($data->object)) {
|
||||
ActivityPubReturn::error ("Object was not specified.");
|
||||
}
|
||||
|
||||
$discovery = new Activitypub_Discovery;
|
||||
$discovery = new Activitypub_explorer;
|
||||
|
||||
// Get valid Actor object
|
||||
try {
|
||||
@ -84,11 +76,20 @@ class apSharedInboxAction extends ManagedAction
|
||||
ActivityPubReturn::error ("Invalid Actor.", 404);
|
||||
}
|
||||
|
||||
unset ($discovery);
|
||||
|
||||
// Public To:
|
||||
$public_to = array ("https://www.w3.org/ns/activitystreams#Public",
|
||||
"Public",
|
||||
"as:Public");
|
||||
|
||||
// Process request
|
||||
switch ($data->type) {
|
||||
case "Create":
|
||||
if (!isset($data->to)) {
|
||||
ActivityPubReturn::error ("To was not specified.");
|
||||
}
|
||||
$discovery = new Activitypub_Discovery;
|
||||
$to_profiles = array ();
|
||||
// Generate To objects
|
||||
if (is_array ($data->to)) {
|
||||
@ -111,10 +112,6 @@ class apSharedInboxAction extends ManagedAction
|
||||
}
|
||||
}
|
||||
unset ($discovery);
|
||||
|
||||
// Process request
|
||||
switch ($data->type) {
|
||||
case "Create":
|
||||
require_once __DIR__ . DIRECTORY_SEPARATOR . "inbox" . DIRECTORY_SEPARATOR . "Create.php";
|
||||
break;
|
||||
case "Follow":
|
||||
@ -126,6 +123,9 @@ class apSharedInboxAction extends ManagedAction
|
||||
case "Announce":
|
||||
require_once __DIR__ . DIRECTORY_SEPARATOR . "inbox" . DIRECTORY_SEPARATOR . "Announce.php";
|
||||
break;
|
||||
case "Undo":
|
||||
require_once __DIR__ . DIRECTORY_SEPARATOR . "inbox" . DIRECTORY_SEPARATOR . "Undo.php";
|
||||
break;
|
||||
default:
|
||||
ActivityPubReturn::error ("Invalid type value.");
|
||||
}
|
||||
|
@ -30,8 +30,8 @@ if (!defined ('GNUSOCIAL')) {
|
||||
}
|
||||
|
||||
try {
|
||||
Notice::getByUri ($data->object)->repeat ($actor_profile, "api");
|
||||
Notice::getByUri ($data->object)->repeat ($actor_profile, "ActivityPub");
|
||||
ActivityPubReturn::answer ("Notice repeated successfully.");
|
||||
} catch(Exception $ex) {
|
||||
ActivityPubReturn::error ($ex->getMessage (), 403);
|
||||
} catch (Exception $e) {
|
||||
ActivityPubReturn::error ($e->getMessage (), 403);
|
||||
}
|
||||
|
@ -70,7 +70,7 @@ if (Notice::contentTooLong ($content)) {
|
||||
ActivityPubReturn::error ("That's too long. Maximum notice size is %d character.");
|
||||
}
|
||||
|
||||
$options = array ('source' => 'web', 'uri' => $data->id);
|
||||
$options = array ('source' => 'ActivityPub', 'uri' => $data->id);
|
||||
// $options gets filled with possible scoping settings
|
||||
ToSelector::fillActivity ($this, $act, $options);
|
||||
|
||||
@ -86,7 +86,7 @@ try {
|
||||
"id" => $data->id,
|
||||
"type" => "Create",
|
||||
"actor" => $data->actor,
|
||||
"object" => Activitypub_notice::noticeToObject (Notice::saveActivity ($act, $actor_profile, $options)));
|
||||
"object" => Activitypub_notice::notice_to_array (Notice::saveActivity ($act, $actor_profile, $options)));
|
||||
ActivityPubReturn::answer ($res);
|
||||
} catch (Exception $e) {
|
||||
ActivityPubReturn::error ($e->getMessage ());
|
||||
|
@ -31,7 +31,11 @@ if (!defined ('GNUSOCIAL')) {
|
||||
|
||||
try {
|
||||
Activitypub_notice::getByUri ($data->object)->deleteAs ($actor_profile);
|
||||
ActivityPubReturn::answer ("Notice deleted successfully.");
|
||||
} catch(Exception $ex) {
|
||||
ActivityPubReturn::error ($ex->getMessage (), 403);
|
||||
$res = array ("@context" => "https://www.w3.org/ns/activitystreams",
|
||||
"type" => "Delete",
|
||||
"actor" => $data->actor,
|
||||
"object" => $data->object);
|
||||
ActivityPubReturn::answer ($res);
|
||||
} catch (Exception $e) {
|
||||
ActivityPubReturn::error ($e->getMessage (), 403);
|
||||
}
|
||||
|
@ -29,10 +29,15 @@ if (!defined ('GNUSOCIAL')) {
|
||||
exit (1);
|
||||
}
|
||||
|
||||
// Validate Object
|
||||
if (!is_string ($data->object)) {
|
||||
ActivityPubReturn::error ("Invalid Object object, URL expected.");
|
||||
}
|
||||
|
||||
// Get valid Object profile
|
||||
try {
|
||||
$object_profile = new Activitypub_Discovery;
|
||||
$object_profile = $object_profile->lookup ($data->object);
|
||||
$object_profile = new Activitypub_explorer;
|
||||
$object_profile = $object_profile->lookup ($data->object)[0];
|
||||
} catch(Exception $e) {
|
||||
ActivityPubReturn::error ("Invalid Object Actor URL.", 404);
|
||||
}
|
||||
@ -40,10 +45,14 @@ try {
|
||||
try {
|
||||
if (!Subscription::exists ($actor_profile, $object_profile)) {
|
||||
Subscription::start ($actor_profile, $object_profile);
|
||||
ActivityPubReturn::answer ("You are now following this person.");
|
||||
$res = array ("@context" => "https://www.w3.org/ns/activitystreams",
|
||||
"type" => "Follow",
|
||||
"actor" => $data->actor,
|
||||
"object" => $data->object);
|
||||
ActivityPubReturn::answer ($res);
|
||||
} else {
|
||||
ActivityPubReturn::error ("Already following.", 409);
|
||||
}
|
||||
} catch (Exception $ex) {
|
||||
} catch (Exception $e) {
|
||||
ActivityPubReturn::error ("Invalid Object Actor URL.", 404);
|
||||
}
|
||||
|
@ -31,7 +31,11 @@ if (!defined ('GNUSOCIAL')) {
|
||||
|
||||
try {
|
||||
Fave::addNew ($actor_profile, Notice::getByUri ($data->object));
|
||||
ActivityPubReturn::answer ("Notice favorited successfully.");
|
||||
} catch (Exception $ex) {
|
||||
ActivityPubReturn::error ($ex->getMessage (), 403);
|
||||
$res = array ("@context" => "https://www.w3.org/ns/activitystreams",
|
||||
"type" => "Like",
|
||||
"actor" => $data->actor,
|
||||
"object" => $data->object);
|
||||
ActivityPubReturn::answer ($res);
|
||||
} catch (Exception $e) {
|
||||
ActivityPubReturn::error ($e->getMessage (), 403);
|
||||
}
|
||||
|
@ -1,7 +1,4 @@
|
||||
<?php
|
||||
|
||||
use Activitypub_Discovery;
|
||||
|
||||
/**
|
||||
* GNU social - a federating social network
|
||||
*
|
||||
@ -58,8 +55,8 @@ case "Follow":
|
||||
}
|
||||
// Get valid Object profile
|
||||
try {
|
||||
$object_profile = new Activitypub_Discovery;
|
||||
$object_profile = $object_profile->lookup ($data->object->object);
|
||||
$object_profile = new Activitypub_explorer;
|
||||
$object_profile = $object_profile->lookup ($data->object->object)[0];
|
||||
} catch (Exception $e) {
|
||||
ActivityPubReturn::error ("Invalid Object Actor URL.", 404);
|
||||
}
|
||||
@ -71,7 +68,7 @@ case "Follow":
|
||||
} else {
|
||||
ActivityPubReturn::error ("You are not following this person already.", 409);
|
||||
}
|
||||
} catch (Exception $ex) {
|
||||
} catch (Exception $e) {
|
||||
ActivityPubReturn::error ("Invalid Object Actor URL.", 404);
|
||||
}
|
||||
break;
|
||||
|
@ -41,10 +41,11 @@ class Activitypub_attachment extends Managed_DataObject
|
||||
/**
|
||||
* Generates a pretty array from an Attachment object
|
||||
*
|
||||
* @param \Attachment $attachment
|
||||
* @param Attachment $attachment
|
||||
* @return pretty array to be used in a response
|
||||
*/
|
||||
public static function attachmentToObject ($attachment) {
|
||||
public static function attachment_to_array ($attachment)
|
||||
{
|
||||
$res = [
|
||||
'@context' => [
|
||||
"https://www.w3.org/ns/activitystreams",
|
||||
|
@ -44,7 +44,8 @@ class Activitypub_error extends Managed_DataObject
|
||||
* @param string $m
|
||||
* @return pretty array to be used in a response
|
||||
*/
|
||||
public static function errorMessageToObject ($m) {
|
||||
public static function error_message_to_array ($m)
|
||||
{
|
||||
$res = [
|
||||
'error'=> $m
|
||||
];
|
||||
|
@ -42,19 +42,20 @@ class Activitypub_notice extends Managed_DataObject
|
||||
/**
|
||||
* Generates a pretty notice from a Notice object
|
||||
*
|
||||
* @param \Notice $notice
|
||||
* @param Notice $notice
|
||||
* @return pretty array to be used in a response
|
||||
*/
|
||||
public static function noticeToObject ($notice) {
|
||||
public static function notice_to_array ($notice)
|
||||
{
|
||||
$attachments = array ();
|
||||
foreach($notice->attachments () as $attachment) {
|
||||
$attachments[] = Activitypub_attachment::attachmentToObject ($attachment);
|
||||
$attachments[] = Activitypub_attachment::attachment_to_array ($attachment);
|
||||
}
|
||||
|
||||
$tags = array ();
|
||||
foreach($notice->getTags()as $tag) {
|
||||
if ($tag != "") { // Hacky workaround to avoid stupid outputs
|
||||
$tags[] = Activitypub_tag::tagNameToObject ($tag);
|
||||
$tags[] = Activitypub_tag::tag_to_array ($tag);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -37,15 +37,47 @@ if (!defined ('GNUSOCIAL')) {
|
||||
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
|
||||
* @link http://www.gnu.org/software/social/
|
||||
*/
|
||||
class Activitypub_profile extends Managed_DataObject
|
||||
class Activitypub_profile extends Profile
|
||||
{
|
||||
public $__table = 'Activitypub_profile';
|
||||
|
||||
protected $_profile = null;
|
||||
|
||||
/**
|
||||
* Return table definition for Schema setup and DB_DataObject usage.
|
||||
*
|
||||
* @return array array of column definitions
|
||||
*/
|
||||
static function schemaDef ()
|
||||
{
|
||||
return array (
|
||||
'fields' => array (
|
||||
'uri' => array ('type' => 'varchar', 'length' => 191, 'not null' => true),
|
||||
'profile_id' => array ('type' => 'integer'),
|
||||
'inboxuri' => array ('type' => 'varchar', 'length' => 191),
|
||||
'sharedInboxuri' => array ('type' => 'varchar', 'length' => 191),
|
||||
'created' => array ('type' => 'datetime', 'not null' => true),
|
||||
'modified' => array ('type' => 'datetime', 'not null' => true),
|
||||
),
|
||||
'primary key' => array ('uri'),
|
||||
'unique keys' => array (
|
||||
'Activitypub_profile_profile_id_key' => array ('profile_id'),
|
||||
'Activitypub_profile_inboxuri_key' => array ('inboxuri'),
|
||||
),
|
||||
'foreign keys' => array (
|
||||
'Activitypub_profile_profile_id_fkey' => array ('profile', array ('profile_id' => 'id')),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a pretty profile from a Profile object
|
||||
*
|
||||
* @param \Profile $profile
|
||||
* @param Profile $profile
|
||||
* @return pretty array to be used in a response
|
||||
*/
|
||||
public static function profileToObject($profile) {
|
||||
public static function profile_to_array ($profile)
|
||||
{
|
||||
$url = $profile->getURL ();
|
||||
$res = [
|
||||
'@context' => [
|
||||
@ -68,7 +100,7 @@ class Activitypub_profile extends Managed_DataObject
|
||||
'following_count' => $profile->subscriptionCount (),
|
||||
'liked' => "{$url}/liked.json",
|
||||
'liked_count' => Fave::countByProfile ($profile),
|
||||
'summary' => $profile->getDescription (),
|
||||
'summary' => ($desc = $profile->getDescription ()) == null ? "" : $desc,
|
||||
'url' => $profile->getURL (),
|
||||
'avatar' => [
|
||||
'type' => 'Image',
|
||||
@ -79,4 +111,92 @@ class Activitypub_profile extends Managed_DataObject
|
||||
];
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert the current objects variables into the database
|
||||
*
|
||||
* @access public
|
||||
* @throws ServerException
|
||||
*/
|
||||
public function doInsert ()
|
||||
{
|
||||
$profile = new Profile ();
|
||||
|
||||
$profile->created = $this->created = $this->modified = common_sql_now ();
|
||||
|
||||
$fields = array (
|
||||
'uri' => 'profileurl',
|
||||
'nickname' => 'nickname',
|
||||
'fullname' => 'fullname',
|
||||
'bio' => 'bio'
|
||||
);
|
||||
|
||||
foreach ($fields as $af => $pf) {
|
||||
$profile->$pf = $this->$af;
|
||||
}
|
||||
|
||||
$this->profile_id = $profile->insert ();
|
||||
if ($this->profile_id === false) {
|
||||
$profile->query ('ROLLBACK');
|
||||
throw new ServerException ('Profile insertion failed.');
|
||||
}
|
||||
|
||||
$ok = $this->insert ();
|
||||
|
||||
if ($ok === false) {
|
||||
$profile->query ('ROLLBACK');
|
||||
throw new ServerException ('Cannot save ActivityPub profile.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the locally stored profile for this Activitypub_profile
|
||||
* @return Profile
|
||||
* @throws NoProfileException if it was not found
|
||||
*/
|
||||
public function localProfile ()
|
||||
{
|
||||
$profile = Profile::getKV ('id', $this->profile_id);
|
||||
if (!$profile instanceof Profile) {
|
||||
throw new NoProfileException ($this->profile_id);
|
||||
}
|
||||
return $profile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates an Activitypub_profile from a Profile
|
||||
*
|
||||
* @param Profile $profile
|
||||
* @return Activitypub_profile
|
||||
* @throws Exception if no Activitypub_profile exists for given Profile
|
||||
*/
|
||||
static function fromProfile (Profile $profile)
|
||||
{
|
||||
$profile_id = $profile->getID ();
|
||||
|
||||
$aprofile = Activitypub_profile::getKV ('profile_id', $profile_id);
|
||||
if (!$aprofile instanceof Activitypub_profile) {
|
||||
throw new Exception('No Activitypub_profile for Profile ID: '.$profile_id);
|
||||
}
|
||||
|
||||
foreach ($profile as $key => $value) {
|
||||
$aprofile->$key = $value;
|
||||
}
|
||||
|
||||
return $aprofile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns sharedInbox if possible, inbox otherwise
|
||||
*
|
||||
* @return string Inbox URL
|
||||
*/
|
||||
public function getInbox ()
|
||||
{
|
||||
if (is_null ($this->sharedInboxuri)) {
|
||||
return $this->inboxuri;
|
||||
}
|
||||
|
||||
return $this->sharedInboxuri;
|
||||
}
|
||||
}
|
||||
|
@ -41,10 +41,11 @@ class Activitypub_tag extends Managed_DataObject
|
||||
/**
|
||||
* Generates a pretty tag from a Tag object
|
||||
*
|
||||
* @param \Tag $tag
|
||||
* @param Tag $tag
|
||||
* @return pretty array to be used in a response
|
||||
*/
|
||||
public static function tagNameToObject ($tag) {
|
||||
public static function tag_to_array ($tag)
|
||||
{
|
||||
$res = [
|
||||
'@context' => [
|
||||
"https://www.w3.org/ns/activitystreams",
|
||||
|
@ -1,7 +1,4 @@
|
||||
<?php
|
||||
|
||||
use Profile;
|
||||
|
||||
/**
|
||||
* GNU social - a federating social network
|
||||
*
|
||||
@ -39,7 +36,7 @@ if (!defined ('GNUSOCIAL')) {
|
||||
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
|
||||
* @link http://www.gnu.org/software/social/
|
||||
*/
|
||||
class Activitypub_Discovery
|
||||
class Activitypub_explorer
|
||||
{
|
||||
private $discovered_actor_profiles = array ();
|
||||
|
||||
@ -49,7 +46,7 @@ class Activitypub_Discovery
|
||||
* so that there is no erroneous data
|
||||
*
|
||||
* @param string $url User's url
|
||||
* @return array of \Profile objects
|
||||
* @return array of Profile objects
|
||||
*/
|
||||
public function lookup ($url)
|
||||
{
|
||||
@ -64,13 +61,16 @@ class Activitypub_Discovery
|
||||
* $discovered_actor_profiles array
|
||||
*
|
||||
* @param string $url User's url
|
||||
* @return array of \Profile objects
|
||||
* @return array of Profile objects
|
||||
*/
|
||||
private function _lookup ($url)
|
||||
{
|
||||
// First check if we already have it locally and, if so, return it
|
||||
// If the local fetch fails: grab it remotely, store locally and return
|
||||
$this->grab_local_user ($url) || $this->grab_remote_user($url);
|
||||
if (! ($this->grab_local_user ($url) || $this->grab_remote_user ($url))) {
|
||||
throw new Exception ("User not found");
|
||||
}
|
||||
|
||||
|
||||
return $this->discovered_actor_profiles;
|
||||
}
|
||||
@ -84,7 +84,7 @@ class Activitypub_Discovery
|
||||
*/
|
||||
private function grab_local_user ($url)
|
||||
{
|
||||
if (($actor_profile = Profile::getKV ("profileurl", $url)) != false) {
|
||||
if (($actor_profile = self::get_profile_by_url ($url)) != false) {
|
||||
$this->discovered_actor_profiles[]= $actor_profile;
|
||||
return true;
|
||||
} else {
|
||||
@ -115,14 +115,15 @@ class Activitypub_Discovery
|
||||
* @param string $url User's url
|
||||
* @return boolean success state
|
||||
*/
|
||||
private function grab_remote_user ($url) {
|
||||
private function grab_remote_user ($url)
|
||||
{
|
||||
$client = new HTTPClient ();
|
||||
$headers = array();
|
||||
$headers[] = 'Accept: application/ld+json; profile="https://www.w3.org/ns/activitystreams"';
|
||||
$headers[] = 'User-Agent: GNUSocialBot v0.1 - https://gnu.io/social';
|
||||
$response = $client->get ($url, $headers);
|
||||
if (!$response->isOk ()) {
|
||||
throw new NoResultException ("Invalid Actor URL.");
|
||||
throw new Exception ("Invalid Actor URL.");
|
||||
}
|
||||
$res = json_decode ($response->getBody (), JSON_UNESCAPED_SLASHES);
|
||||
if (isset ($res["orderedItems"])) { // It's a potential collection of actors!!!
|
||||
@ -148,17 +149,21 @@ class Activitypub_Discovery
|
||||
* Save remote user profile in local instance
|
||||
*
|
||||
* @param array $res remote response
|
||||
* @return \Profile remote Profile object
|
||||
* @return Profile remote Profile object
|
||||
*/
|
||||
private function store_profile ($res) {
|
||||
$profile = new Profile;
|
||||
$profile->profileurl = $res["url"];
|
||||
$profile->nickname = $res["nickname"];
|
||||
$profile->fullname = $res["display_name"];
|
||||
$profile->bio = substr ($res["summary"], 0, 1000);
|
||||
$profile->insert ();
|
||||
private function store_profile ($res)
|
||||
{
|
||||
$aprofile = new Activitypub_profile;
|
||||
$aprofile->uri = $res["url"];
|
||||
$aprofile->nickname = $res["nickname"];
|
||||
$aprofile->fullname = $res["display_name"];
|
||||
$aprofile->bio = substr ($res["summary"], 0, 1000);
|
||||
$aprofile->inboxuri = $res["inbox"];
|
||||
$aprofile->sharedInboxuri = $res["sharedInbox"];
|
||||
|
||||
return $profile;
|
||||
$aprofile->doInsert ();
|
||||
|
||||
return $aprofile->localProfile ();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -168,11 +173,37 @@ class Activitypub_Discovery
|
||||
* @param array $res remote response
|
||||
* @return boolean success state
|
||||
*/
|
||||
private function validate_remote_response ($res) {
|
||||
if (!isset ($res["url"], $res["nickname"], $res["display_name"], $res["summary"])) {
|
||||
private function validate_remote_response ($res)
|
||||
{
|
||||
if (!isset ($res["url"], $res["nickname"], $res["display_name"], $res["summary"], $res["inbox"], $res["sharedInbox"])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a profile from it's profileurl
|
||||
* Unfortunately GNU Social cache is not truly reliable when handling
|
||||
* potential ActivityPub remote profiles, as so it is important to use
|
||||
* this hacky workaround (at least for now)
|
||||
*
|
||||
* @param string $v URL
|
||||
* @return boolean|Profile false if fails | Profile object if successful
|
||||
*/
|
||||
static function get_profile_by_url ($v)
|
||||
{
|
||||
$i = Managed_DataObject::getcached(Profile, "profileurl", $v);
|
||||
if (empty ($i)) { // false = cache miss
|
||||
$i = new Profile;
|
||||
$result = $i->get ("profileurl", $v);
|
||||
if ($result) {
|
||||
// Hit!
|
||||
$i->encache();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return $i;
|
||||
}
|
||||
}
|
91
utils/postman.php
Normal file
91
utils/postman.php
Normal file
@ -0,0 +1,91 @@
|
||||
<?php
|
||||
/**
|
||||
* GNU social - a federating social network
|
||||
*
|
||||
* ActivityPubPlugin implementation for GNU Social
|
||||
*
|
||||
* LICENCE: This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* @category Plugin
|
||||
* @package GNUsocial
|
||||
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
||||
* @author Daniel Supernault <danielsupernault@gmail.com>
|
||||
* @copyright 2018 Free Software Foundation http://fsf.org
|
||||
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
|
||||
* @link https://www.gnu.org/software/social/
|
||||
*/
|
||||
if (!defined ('GNUSOCIAL')) {
|
||||
exit (1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @category Plugin
|
||||
* @package GNUsocial
|
||||
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
||||
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
|
||||
* @link http://www.gnu.org/software/social/
|
||||
*/
|
||||
class Activitypub_postman
|
||||
{
|
||||
private $actor;
|
||||
private $to = array ();
|
||||
private $client;
|
||||
private $headers;
|
||||
|
||||
/**
|
||||
* Create a postman to deliver something to someone
|
||||
*
|
||||
* @param Activitypub_profile $to array of destinataries
|
||||
*/
|
||||
public function __construct ($from, $to)
|
||||
{
|
||||
$this->actor = $from;
|
||||
$this->to = $to;
|
||||
$this->headers = array();
|
||||
$this->headers[] = 'Accept: application/ld+json; profile="https://www.w3.org/ns/activitystreams"';
|
||||
$this->headers[] = 'User-Agent: GNUSocialBot v0.1 - https://gnu.io/social';
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a follow notification to remote instance
|
||||
*/
|
||||
public function follow ()
|
||||
{
|
||||
$this->client = new HTTPClient ();
|
||||
$data = array ("@context" => "https://www.w3.org/ns/activitystreams",
|
||||
"type" => "Follow",
|
||||
"actor" => $this->actor->getUrl (),
|
||||
"object" => $this->to[0]->getUrl ());
|
||||
$this->client->setBody (json_encode ($data));
|
||||
$response = $this->client->post ($this->to[0]->getInbox (), $this->headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a Undo Follow notification to remote instance
|
||||
*/
|
||||
public function undo_follow ()
|
||||
{
|
||||
$this->client = new HTTPClient ();
|
||||
$data = array ("@context" => "https://www.w3.org/ns/activitystreams",
|
||||
"type" => "Undo",
|
||||
"actor" => $this->actor->getUrl (),
|
||||
"object" => array (
|
||||
"type" => "Follow",
|
||||
"object" => $this->to[0]->getUrl ()
|
||||
)
|
||||
);
|
||||
$this->client->setBody (json_encode ($data));
|
||||
$response = $this->client->post ($this->to[0]->getInbox (), $this->headers);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user