Add and fix some code documentation

This commit is contained in:
Diogo Cordeiro 2018-07-09 23:43:34 +01:00
parent 80b9d1873c
commit fc5b344864
10 changed files with 615 additions and 456 deletions

View File

@ -2,9 +2,7 @@
/** /**
* GNU social - a federating social network * GNU social - a federating social network
* *
* Plugin that handles ActivityPub * ActivityPubPlugin implementation for GNU Social
*
* PHP version 5
* *
* LICENCE: This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU Affero General Public License as published by
@ -23,91 +21,130 @@
* @package GNUsocial * @package GNUsocial
* @author Daniel Supernault <danielsupernault@gmail.com> * @author Daniel Supernault <danielsupernault@gmail.com>
* @author Diogo Cordeiro <diogo@fc.up.pt> * @author Diogo Cordeiro <diogo@fc.up.pt>
* @copyright 2014 Free Software Foundation http://fsf.org * @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 * @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/ * @link https://www.gnu.org/software/social/
*/ */
if (!defined ('GNUSOCIAL')) {
exit(1);
}
if (!defined('GNUSOCIAL')) { exit(1); } /**
* @category Plugin
* @package GNUsocial
* @author Daniel Supernault <danielsupernault@gmail.com>
* @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 ActivityPubPlugin extends Plugin class ActivityPubPlugin extends Plugin
{ {
/**
* Route/Reroute urls
*
* @param URLMapper $m
* @return void
*/
public function onRouterInitialized(URLMapper $m) {
ActivityPubURLMapperOverwrite::overwrite_variable ($m, ':nickname',
['action' => 'showstream'],
['nickname' => Nickname::DISPLAY_FMT],
'apactorprofile');
public function onRouterInitialized(URLMapper $m) $m->connect (':nickname/liked.json',
{ ['action' => 'apActorLikedCollection'],
ActivityPubURLMapperOverwrite::overwrite_variable($m, ':nickname', ['nickname' => Nickname::DISPLAY_FMT]);
['action' => 'showstream'],
['nickname' => Nickname::DISPLAY_FMT],
'apactorprofile');
$m->connect(':nickname/liked.json',
['action' => 'apActorLikedCollection'],
['nickname' => Nickname::DISPLAY_FMT]);
$m->connect(':nickname/followers.json',
['action' => 'apActorFollowers'],
['nickname' => Nickname::DISPLAY_FMT]);
$m->connect(':nickname/following.json',
['action' => 'apActorFollowing'],
['nickname' => Nickname::DISPLAY_FMT]);
}
public function onPluginVersion(array &$versions) $m->connect (':nickname/followers.json',
{ ['action' => 'apActorFollowers'],
$versions[] = [ 'name' => 'ActivityPub', ['nickname' => Nickname::DISPLAY_FMT]);
'version' => GNUSOCIAL_VERSION,
'author' => 'Daniel Supernault, Diogo Cordeiro', $m->connect (':nickname/following.json',
'homepage' => 'https://www.gnu.org/software/social/', ['action' => 'apActorFollowing'],
'rawdescription' => ['nickname' => Nickname::DISPLAY_FMT]);
// Todo: Translation
'Adds ActivityPub Support']; $m->connect (':nickname/inbox.json',
return true; ['action' => 'apActorInbox'],
} ['nickname' => Nickname::DISPLAY_FMT]);
$m->connect ('inbox.json',
array('action' => 'apSharedInbox'));
}
/**
* Plugin version information
*
* @param array $versions
* @return boolean true
*/
public function onPluginVersion (array & $versions) {
$versions[] = [ 'name' => 'ActivityPub',
'version' => GNUSOCIAL_VERSION,
'author' => 'Daniel Supernault, Diogo Cordeiro',
'homepage' => 'https://www.gnu.org/software/social/',
'rawdescription' =>
// Todo: Translation
'Adds ActivityPub Support'];
return true;
}
} }
/** /**
* Overwrites variables in URL-mapping * Overwrites variables in URL-mapping
*
*/ */
class ActivityPubURLMapperOverwrite extends URLMapper class ActivityPubURLMapperOverwrite extends URLMapper
{ {
static function overwrite_variable($m, $path, $args, $paramPatterns, $newaction) static function overwrite_variable ($m, $path, $args, $paramPatterns, $newaction) {
{ $mimes = [
$mimes = [ 'application/activity+json',
'application/activity+json', 'application/ld+json',
'application/ld+json', 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
'application/ld+json; profile="https://www.w3.org/ns/activitystreams"' ];
];
if (in_array ($_SERVER["HTTP_ACCEPT"], $mimes) == false) { if (in_array ($_SERVER["HTTP_ACCEPT"], $mimes) == false) {
return true; return true;
}
$m->connect ($path, array('action' => $newaction), $paramPatterns);
$regex = self::makeRegex($path, $paramPatterns);
foreach ($m->variables as $n => $v) {
if ($v[1] == $regex) {
$m->variables[$n][0]['action'] = $newaction;
}
}
} }
$m->connect($path, array('action' => $newaction), $paramPatterns);
$regex = self::makeRegex($path, $paramPatterns);
foreach ($m->variables as $n => $v) {
if ($v[1] == $regex) {
$m->variables[$n][0]['action'] = $newaction;
}
}
}
} }
/**
* Plugin return handler
*/
class ActivityPubReturn class ActivityPubReturn
{ {
static function answer ($res) /**
{ * Return a valid answer
header('Content-Type: application/activity+json'); *
echo json_encode($res, JSON_UNESCAPED_SLASHES | (isset($_GET["pretty"]) ? JSON_PRETTY_PRINT : null)); * @param array $res
exit; * @return void
} */
static function error ($m, $code=500) static function answer ($res) {
{ header ('Content-Type: application/activity+json');
http_response_code ($code); echo json_encode ($res, JSON_UNESCAPED_SLASHES | (isset ($_GET["pretty"]) ? JSON_PRETTY_PRINT : null));
header('Content-Type: application/activity+json'); exit;
$res[] = Activitypub_error::errorMessageToObject($m); }
echo json_encode($res, JSON_UNESCAPED_SLASHES);
exit; /**
} * Return an error
*
* @param string $m
* @param int32 $code
* @return void
*/
static function error ($m, $code = 500) {
http_response_code ($code);
header ('Content-Type: application/activity+json');
$res[] = Activitypub_error::errorMessageToObject ($m);
echo json_encode ($res, JSON_UNESCAPED_SLASHES);
exit;
}
} }

View File

@ -2,9 +2,7 @@
/** /**
* GNU social - a federating social network * GNU social - a federating social network
* *
* Todo: Description * ActivityPubPlugin implementation for GNU Social
*
* PHP version 5
* *
* LICENCE: This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU Affero General Public License as published by
@ -23,76 +21,89 @@
* @package GNUsocial * @package GNUsocial
* @author Diogo Cordeiro <diogo@fc.up.pt> * @author Diogo Cordeiro <diogo@fc.up.pt>
* @author Daniel Supernault <danielsupernault@gmail.com> * @author Daniel Supernault <danielsupernault@gmail.com>
* @copyright 2015 Free Software Foundaction, Inc. * @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 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link https://gnu.io/social * @link https://www.gnu.org/software/social/
*/ */
if (!defined ('GNUSOCIAL')) {
exit(1);
}
if (!defined('GNUSOCIAL')) { exit(1); } /**
* @category Plugin
* @package GNUsocial
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @author Daniel Supernault <danielsupernault@gmail.com>
* @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 apActorFollowersAction extends ManagedAction class apActorFollowersAction extends ManagedAction
{ {
protected $needLogin = false; protected $needLogin = false;
protected $canPost = true; protected $canPost = true;
protected function handle() /**
{ * Handle the Followers Collection request
$nickname = $this->trimmed('nickname'); *
try { * @return void
$user = User::getByNickname($nickname); */
$profile = $user->getProfile(); protected function handle () {
$url = $profile->profileurl; $nickname = $this->trimmed ('nickname');
} catch (Exception $e) { try {
ActivityPubReturn::error ('Invalid username'); $user = User::getByNickname ($nickname);
$profile = $user->getProfile ();
$url = $profile->profileurl;
} catch (Exception $e) {
ActivityPubReturn::error ('Invalid username');
}
$page = intval ($this->trimmed ('page'));
if ($page <= 0) {
ActivityPubReturn::error ('Invalid page number');
}
/* Fetch Followers */
try {
$since = ($page - 1) * PROFILES_PER_MINILIST;
$limit = (($page - 1) == 0 ? 1 : $page) * PROFILES_PER_MINILIST;
$sub = $profile->getSubscribers ($since, $limit);
}
catch(NoResultException $e) {
ActivityPubReturn::error ('This user has no followers');
}
/* Calculate total items */
$total_subs = $profile->subscriberCount ();
$total_pages = ceil ($total_subs / PROFILES_PER_MINILIST);
if ($total_pages == 0) {
ActivityPubReturn::error ('This user has no followers');
}
if ($page > $total_pages) {
ActivityPubReturn::error ("There are only {$total_pages} pages");
}
/* Get followers' URLs */
$subs = array ();
while ($sub->fetch ()) {
$subs[] = $sub->profileurl;
}
$res = [
'@context' => [
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1",
],
'id' => "{$url}/followers.json",
'type' => ($page == 0 ? 'OrderedCollection' : 'OrderedCollectionPage'),
'totalItems' => $total_subs,
'next' => $page+1 > $total_pages ? null : "{$url}/followers.json?page=".($page+1 == 1 ? 2 : $page+1),
'prev' => $page == 1 ? null : "{$url}/followers.json?page=".($page-1 <= 0 ? 1 : $page-1),
'orderedItems' => $subs
];
ActivityPubReturn::answer ($res);
} }
$page = intval($this->trimmed('page'));
if ($page <= 0)
ActivityPubReturn::error ('Invalid page number');
/* Fetch Followers */
try {
$since = ($page-1) * PROFILES_PER_MINILIST;
$limit = (($page-1) == 0 ? 1 : $page)*PROFILES_PER_MINILIST;
$sub = $profile->getSubscribers($since, $limit);
} catch (NoResultException $e) {
ActivityPubReturn::error ('This user has no followers');
}
/* Calculate total items */
$total_subs = $profile->subscriberCount();
$total_pages = ceil($total_subs/PROFILES_PER_MINILIST);
if ($total_pages == 0)
ActivityPubReturn::error ('This user has no followers');
if ($page > $total_pages)
ActivityPubReturn::error ("There are only {$total_pages} pages");
/* Get followers' URLs */
$subs = [];
while ($sub->fetch())
$subs[] = $this->pretty_sub (clone($sub));
$res = [
'@context' => [
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1",
],
'id' => "{$url}/followers.json",
'type' => ($page == 0 ? 'OrderedCollection' : 'OrderedCollectionPage'),
'totalItems' => $total_subs,
'next' => $page+1 > $total_pages ? null : "{$url}/followers.json?page=".($page+1 == 1 ? 2 : $page+1),
'prev' => $page == 1 ? null : "{$url}/followers.json?page=".($page-1 <= 0 ? 1 : $page-1),
'orderedItems' => $subs
];
ActivityPubReturn::answer ($res);
}
protected function pretty_sub ($sub_object)
{
return $sub_object->profileurl;
}
} }

View File

@ -2,9 +2,7 @@
/** /**
* GNU social - a federating social network * GNU social - a federating social network
* *
* Todo: Description * ActivityPubPlugin implementation for GNU Social
*
* PHP version 5
* *
* LICENCE: This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU Affero General Public License as published by
@ -23,76 +21,88 @@
* @package GNUsocial * @package GNUsocial
* @author Diogo Cordeiro <diogo@fc.up.pt> * @author Diogo Cordeiro <diogo@fc.up.pt>
* @author Daniel Supernault <danielsupernault@gmail.com> * @author Daniel Supernault <danielsupernault@gmail.com>
* @copyright 2015 Free Software Foundaction, Inc. * @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 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link https://gnu.io/social * @link https://www.gnu.org/software/social/
*/ */
if (!defined ('GNUSOCIAL')) {
exit(1);
}
if (!defined('GNUSOCIAL')) { exit(1); } /**
* @category Plugin
* @package GNUsocial
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @author Daniel Supernault <danielsupernault@gmail.com>
* @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 apActorFollowingAction extends ManagedAction class apActorFollowingAction extends ManagedAction
{ {
protected $needLogin = false; protected $needLogin = false;
protected $canPost = true; protected $canPost = true;
protected function handle() /**
{ * Handle the Following Collection request
$nickname = $this->trimmed('nickname'); *
try { * @return void
$user = User::getByNickname($nickname); */
$profile = $user->getProfile(); protected function handle () {
$url = $profile->profileurl; $nickname = $this->trimmed ('nickname');
} catch (Exception $e) { try {
ActivityPubReturn::error ('Invalid username'); $user = User::getByNickname ($nickname);
$profile = $user->getProfile ();
$url = $profile->profileurl;
} catch (Exception $e) {
ActivityPubReturn::error ('Invalid username');
}
$page = intval ($this->trimmed ('page'));
if ($page <= 0) {
ActivityPubReturn::error ('Invalid page number');
}
/* Fetch Following */
try {
$since = ($page - 1) * PROFILES_PER_MINILIST;
$limit = (($page - 1) == 0 ? 1 : $page) * PROFILES_PER_MINILIST;
$sub = $profile->getSubscribed($since, $limit);
} catch(NoResultException $e) {
ActivityPubReturn::error ('This user is not following anyone');
}
/* Calculate total items */
$total_subs = $profile->subscriptionCount();
$total_pages = ceil ($total_subs / PROFILES_PER_MINILIST);
if ($total_pages == 0) {
ActivityPubReturn::error ('This user has no followers');
}
if ($page > $total_pages) {
ActivityPubReturn::error ("There are only {$total_pages} pages");
}
/* Get followed' URLs */
$subs = array ();
while ($sub->fetch ()) {
$subs[] = $sub->profileurl;
}
$res = [
'@context' => [
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1",
],
'id' => "{$url}/following.json",
'type' => ($page == 0 ? 'OrderedCollection' : 'OrderedCollectionPage'),
'totalItems' => $total_subs,
'next' => $page+1 > $total_pages ? null : "{$url}/followers.json?page=".($page+1 == 1 ? 2 : $page+1),
'prev' => $page == 1 ? null : "{$url}/followers.json?page=".($page-1 <= 0 ? 1 : $page-1),
'orderedItems' => $subs
];
ActivityPubReturn::answer ($res);
} }
$page = intval($this->trimmed('page'));
if ($page <= 0)
ActivityPubReturn::error ('Invalid page number');
/* Fetch Following */
try {
$since = ($page-1) * PROFILES_PER_MINILIST;
$limit = (($page-1) == 0 ? 1 : $page)*PROFILES_PER_MINILIST;
$sub = $profile->getSubscribed($since, $limit);
} catch (NoResultException $e) {
ActivityPubReturn::error ('This user is not following anyone');
}
/* Calculate total items */
$total_subs = $profile->subscriptionCount();
$total_pages = ceil($total_subs/PROFILES_PER_MINILIST);
if ($total_pages == 0)
ActivityPubReturn::error ('This user has no followers');
if ($page > $total_pages)
ActivityPubReturn::error ("There are only {$total_pages} pages");
/* Get followed' URLs */
$subs = [];
while ($sub->fetch())
$subs[] = $this->pretty_sub (clone($sub));
$res = [
'@context' => [
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1",
],
'id' => "{$url}/following.json",
'type' => ($page == 0 ? 'OrderedCollection' : 'OrderedCollectionPage'),
'totalItems' => $total_subs,
'next' => $page+1 > $total_pages ? null : "{$url}/followers.json?page=".($page+1 == 1 ? 2 : $page+1),
'prev' => $page == 1 ? null : "{$url}/followers.json?page=".($page-1 <= 0 ? 1 : $page-1),
'orderedItems' => $subs
];
ActivityPubReturn::answer ($res);
}
protected function pretty_sub ($sub_object)
{
return $sub_object->profileurl;
}
} }

View File

@ -2,9 +2,7 @@
/** /**
* GNU social - a federating social network * GNU social - a federating social network
* *
* Todo: Description * ActivityPubPlugin implementation for GNU Social
*
* PHP version 5
* *
* LICENCE: This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU Affero General Public License as published by
@ -23,87 +21,123 @@
* @package GNUsocial * @package GNUsocial
* @author Diogo Cordeiro <diogo@fc.up.pt> * @author Diogo Cordeiro <diogo@fc.up.pt>
* @author Daniel Supernault <danielsupernault@gmail.com> * @author Daniel Supernault <danielsupernault@gmail.com>
* @copyright 2015 Free Software Foundaction, Inc. * @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 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link https://gnu.io/social * @link https://www.gnu.org/software/social/
*/ */
if (!defined ('GNUSOCIAL')) {
exit(1);
}
if (!defined('GNUSOCIAL')) { exit(1); } /**
* @category Plugin
* @package GNUsocial
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @author Daniel Supernault <danielsupernault@gmail.com>
* @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 apActorLikedCollectionAction extends ManagedAction class apActorLikedCollectionAction extends ManagedAction
{ {
protected $needLogin = false; protected $needLogin = false;
protected $canPost = true; protected $canPost = true;
protected function handle() /**
{ * Handle the Liked Collection request
$nickname = $this->trimmed('nickname'); *
try { * @return void
$user = User::getByNickname($nickname); */
$profile = $user->getProfile(); protected function handle () {
$url = $profile->profileurl; $nickname = $this->trimmed ('nickname');
} catch (Exception $e) { try {
ActivityPubReturn::error ('Invalid username'); $user = User::getByNickname ($nickname);
$profile = $user->getProfile ();
$url = $profile->profileurl;
} catch (Exception $e) {
ActivityPubReturn::error ('Invalid username');
}
$limit = intval ($this->trimmed ('limit'));
$since_id = intval ($this->trimmed ('since_id'));
$max_id = intval ($this->trimmed ('max_id'));
$limit = empty ($limit) ? 40 : $limit; // Default is 40
$since_id = empty ($since_id) ? null : $since_id;
$max_id = empty ($max_id) ? null : $max_id;
// Max is 80
if ($limit > 80) {
$limit = 80;
}
$fave =
$this->fetch_faves($user->getID(), $limit, $since_id, $max_id);
$faves = array();
while ($fave->fetch ()) {
$faves[] = $this->pretty_fave (clone ($fave));
}
$res = [
'@context' => [
"https://www.w3.org/ns/activitystreams",
[
"@language" => "en"
]
],
'id' => "{$url}/liked.json",
'type' => 'OrderedCollection',
'totalItems' => Fave::countByProfile ($profile),
'orderedItems' => $faves
];
ActivityPubReturn::answer ($res);
} }
$limit = intval($this->trimmed('limit')); /**
$since_id = intval($this->trimmed('since_id')); * Take a fave object and turns it in a pretty array to be used
$max_id = intval($this->trimmed('max_id')); * as a plugin answer
*
* @param \Fave $fave_object
* @return array pretty array representating a Fave
*/
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)));
$limit = empty ($limit) ? 40 : $limit; // Default is 40 return $res;
$since_id = empty ($since_id) ? null : $since_id; }
$max_id = empty ($max_id) ? null : $max_id;
if ($limit > 80) $limit = 80; // Max is 80 /**
* Fetch faves
*
* @param int32 $user_id
* @param int32 $limit
* @param int32 $since_id
* @param int32 $max_id
* @return \Fave fetchable fave collection
*/
private static function fetch_faves ($user_id, $limit = 40, $since_id = null,
$max_id = null) {
$fav = new Fave ();
$fave = $this->fetch_faves ($user->getID(), $limit, $since_id, $max_id); $fav->user_id = $user_id;
$faves = []; $fav->orderBy ('modified DESC');
while ($fave->fetch())
$faves[] = $this->pretty_fave (clone($fave));
$res = [ if ($since_id != null) {
'@context' => [ $fav->whereAdd ("notice_id > {$since_id}");
"https://www.w3.org/ns/activitystreams", }
[
"@language" => "en"
]
],
'id' => "{$url}/liked.json",
'type' => 'OrderedCollection',
'totalItems' => Fave::countByProfile ($profile),
'orderedItems' => $faves
];
ActivityPubReturn::answer ($res); if ($max_id != null) {
} $fav->whereAdd ("notice_id < {$max_id}");
}
protected function pretty_fave ($fave_object) $fav->limit ($limit);
{
$res = array ("uri" => $fave_object->uri,
"created" => $fave_object->created,
"object" => Activitypub_notice::noticeToObject(Notice::getByID($fave_object->notice_id)));
return $res; $fav->find ();
}
private static function fetch_faves ($user_id, $limit = 40, $since_id = null, $max_id = null) return $fav;
{ }
$fav = new Fave();
$fav->user_id = $user_id;
$fav->orderBy('modified DESC');
if ($since_id != null)
$fav->whereAdd("notice_id > {$since_id}");
if ($max_id != null)
$fav->whereAdd("notice_id < {$max_id}");
$fav->limit($limit);
$fav->find();
return $fav;
}
} }

View File

@ -2,9 +2,7 @@
/** /**
* GNU social - a federating social network * GNU social - a federating social network
* *
* Todo: Description * ActivityPubPlugin implementation for GNU Social
*
* PHP version 5
* *
* LICENCE: This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU Affero General Public License as published by
@ -23,30 +21,44 @@
* @package GNUsocial * @package GNUsocial
* @author Daniel Supernault <danielsupernault@gmail.com> * @author Daniel Supernault <danielsupernault@gmail.com>
* @author Diogo Cordeiro <diogo@fc.up.pt> * @author Diogo Cordeiro <diogo@fc.up.pt>
* @copyright 2015 Free Software Foundaction, Inc. * @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 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link https://gnu.io/social * @link https://www.gnu.org/software/social/
*/ */
if (!defined ('GNUSOCIAL')) {
exit(1);
}
if (!defined('GNUSOCIAL')) { exit(1); } /**
* @category Plugin
* @package GNUsocial
* @author Daniel Supernault <danielsupernault@gmail.com>
* @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 apActorProfileAction extends ManagedAction class apActorProfileAction extends ManagedAction
{ {
protected $needLogin = false; protected $needLogin = false;
protected $canPost = true; protected $canPost = true;
protected function handle() /**
{ * Handle the Actor Profile request
$nickname = $this->trimmed('nickname'); *
try { * @return void
$user = User::getByNickname($nickname); */
$profile = $user->getProfile(); protected function handle() {
} catch (Exception $e) { $nickname = $this->trimmed ('nickname');
ActivityPubReturn::error ('Invalid username', 404); try {
$user = User::getByNickname ($nickname);
$profile = $user->getProfile ();
}
catch (Exception $e) {
ActivityPubReturn::error ('Invalid username', 404);
}
$res = Activitypub_profile::profileToObject ($profile);
ActivityPubReturn::answer ($res);
} }
$res = Activitypub_profile::profileToObject($profile);
ActivityPubReturn::answer ($res);
}
} }

View File

@ -2,10 +2,8 @@
/** /**
* GNU social - a federating social network * GNU social - a federating social network
* *
* Todo: Description * ActivityPubPlugin implementation for GNU Social
* *
* PHP version 5
*
* LICENCE: This program is free software: you can redistribute it and/or modify * 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 * 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 * the Free Software Foundation, either version 3 of the License, or
@ -23,42 +21,55 @@
* @package GNUsocial * @package GNUsocial
* @author Diogo Cordeiro <diogo@fc.up.pt> * @author Diogo Cordeiro <diogo@fc.up.pt>
* @author Daniel Supernault <danielsupernault@gmail.com> * @author Daniel Supernault <danielsupernault@gmail.com>
* @copyright 2015 Free Software Foundaction, Inc. * @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 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link https://gnu.io/social * @link https://www.gnu.org/software/social/
*/ */
if (!defined ('GNUSOCIAL')) {
exit(1);
}
if (!defined('GNUSOCIAL')) { exit(1); } /**
* @category Plugin
* @package GNUsocial
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @author Daniel Supernault <danielsupernault@gmail.com>
* @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_attachment extends Managed_DataObject class Activitypub_attachment extends Managed_DataObject
{ {
/**
* Generates a pretty array from an Attachment object
*
* @param \Attachment $attachment
* @return pretty array to be used in a response
*/
public static function attachmentToObject ($attachment) {
$res = [
'@context' => [
"https://www.w3.org/ns/activitystreams",
[
"@language" => "en"
]
],
'id' => $attachment->getID (),
'mimetype' => $attachment->mimetype,
'url' => $attachment->getUrl (),
'size' => intval($attachment->size), // $attachment->getSize ()
'title' => $attachment->getTitle (),
'meta' => null
];
public static function attachmentToObject($attachment) // Image
{ if (substr ($res["mimetype"], 0, 5) == "image")
$res = [ {
'@context' => [ $res["meta"]= [
"https://www.w3.org/ns/activitystreams", 'width' => $attachment->width,
[ 'height' => $attachment->height
"@language" => "en" ];
] }
],
'id' => $attachment->getID (),
'mimetype' => $attachment->mimetype,
'url' => $attachment->getUrl (),
'size' => intval($attachment->size), // $attachment->getSize ()
'title' => $attachment->getTitle (),
'meta' => null
];
// Image
if (substr($res["mimetype"], 0, 5) == "image")
{
$res["meta"]= [
'width' => $attachment->width,
'height' => $attachment->height
];
}
return $res; return $res;
} }
} }

View File

@ -2,10 +2,8 @@
/** /**
* GNU social - a federating social network * GNU social - a federating social network
* *
* Todo: Description * ActivityPubPlugin implementation for GNU Social
* *
* PHP version 5
*
* LICENCE: This program is free software: you can redistribute it and/or modify * 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 * 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 * the Free Software Foundation, either version 3 of the License, or
@ -23,21 +21,34 @@
* @package GNUsocial * @package GNUsocial
* @author Diogo Cordeiro <diogo@fc.up.pt> * @author Diogo Cordeiro <diogo@fc.up.pt>
* @author Daniel Supernault <danielsupernault@gmail.com> * @author Daniel Supernault <danielsupernault@gmail.com>
* @copyright 2015 Free Software Foundaction, Inc. * @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 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link https://gnu.io/social * @link https://www.gnu.org/software/social/
*/ */
if (!defined ('GNUSOCIAL')) {
exit(1);
}
if (!defined('GNUSOCIAL')) { exit(1); } /**
* @category Plugin
* @package GNUsocial
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @author Daniel Supernault <danielsupernault@gmail.com>
* @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_error extends Managed_DataObject class Activitypub_error extends Managed_DataObject
{ {
public static function errorMessageToObject($m) /**
{ * Generates a pretty error from a string
$res = [ *
'error'=> $m * @param string $m
]; * @return pretty array to be used in a response
*/
return $res; public static function errorMessageToObject ($m) {
} $res = [
'error'=> $m
];
return $res;
}
} }

View File

@ -2,9 +2,7 @@
/** /**
* GNU social - a federating social network * GNU social - a federating social network
* *
* Todo: Description * ActivityPubPlugin implementation for GNU Social
*
* PHP version 5
* *
* LICENCE: This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU Affero General Public License as published by
@ -23,55 +21,66 @@
* @package GNUsocial * @package GNUsocial
* @author Daniel Supernault <danielsupernault@gmail.com> * @author Daniel Supernault <danielsupernault@gmail.com>
* @author Diogo Cordeiro <diogo@fc.up.pt> * @author Diogo Cordeiro <diogo@fc.up.pt>
* @copyright 2015 Free Software Foundaction, Inc. * @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 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link https://gnu.io/social * @link https://www.gnu.org/software/social/
*/ */
if (!defined ('GNUSOCIAL')) {
exit(1);
}
if (!defined('GNUSOCIAL')) { exit(1); } /**
* @category Plugin
* @package GNUsocial
* @author Daniel Supernault <danielsupernault@gmail.com>
* @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_notice extends Managed_DataObject class Activitypub_notice extends Managed_DataObject
{ {
/**
* Generates a pretty notice from a Notice object
*
* @param \Notice $notice
* @return pretty array to be used in a response
*/
public static function noticeToObject($notice) {
$attachments = array ();
foreach($notice->attachments()as $attachment) {
$attachments[] = Activitypub_attachment::attachmentToObject ($attachment);
}
public static function noticeToObject($notice) $tags = array ();
{ foreach($notice->getTags()as $tag) {
$attachments = []; if ($tag != "") { // Hacky workaround to avoid stupid outputs
foreach ($notice->attachments () as $attachment) $tags[] = Activitypub_tag::tagNameToObject($tag);
{ }
$attachments[] = Activitypub_attachment::attachmentToObject ($attachment); }
}
$tags = []; $to = array ();
foreach ($notice->getTags () as $tag) foreach ($notice->getAttentionProfileIDs()as $to_id) {
{ $to[] = Profile::getById($to_id)->getUri();
if ($tag != "") { // Hacky workaround to avoid stupid outputs }
$tags[] = Activitypub_tag::tagNameToObject ($tag); if (!is_null($to)) {
} $to = array ("https://www.w3.org/ns/activitystreams#Public");
} }
$to = array (); $item = [
foreach ($notice->getAttentionProfileIDs () as $to_id) { 'id' => $notice->getUrl (),
$to[] = Profile::getById ($to_id)->getUri (); 'type' => 'Notice',
} 'actor' => $notice->getProfile ()->getUrl (),
if (!is_null ($to)) { 'published' => $notice->getCreated (),
$to = array("https://www.w3.org/ns/activitystreams#Public"); 'to' => $to,
'content' => $notice->getContent (),
'url' => $notice->getUrl (),
'reply_to' => empty($notice->reply_to) ? null : Notice::getById($notice->reply_to)->getUrl (),
'is_local' => $notice->isLocal (),
'conversation' => intval ($notice->conversation),
'attachment' => $attachments,
'tag' => $tags
];
return $item;
} }
$item = [
'id' => $notice->getUrl (),
'type' => 'Notice',
'actor' => $notice->getProfile ()->getUrl (),
'published' => $notice->getCreated (),
'to' => $to,
'content' => $notice->getContent (),
'url' => $notice->getUrl (),
'reply_to' => empty($notice->reply_to) ? null : Notice::getById($notice->reply_to)->getUrl (),
'is_local' => $notice->isLocal (),
'conversation' => intval ($notice->conversation),
'attachment' => $attachments,
'tag' => $tags
];
return $item;
}
} }

View File

@ -2,9 +2,7 @@
/** /**
* GNU social - a federating social network * GNU social - a federating social network
* *
* Todo: Description * ActivityPubPlugin implementation for GNU Social
*
* PHP version 5
* *
* LICENCE: This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU Affero General Public License as published by
@ -23,48 +21,62 @@
* @package GNUsocial * @package GNUsocial
* @author Diogo Cordeiro <diogo@fc.up.pt> * @author Diogo Cordeiro <diogo@fc.up.pt>
* @author Daniel Supernault <danielsupernault@gmail.com> * @author Daniel Supernault <danielsupernault@gmail.com>
* @copyright 2015 Free Software Foundaction, Inc. * @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 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link https://gnu.io/social * @link https://www.gnu.org/software/social/
*/ */
if (!defined ('GNUSOCIAL')) {
exit(1);
}
if (!defined('GNUSOCIAL')) { exit(1); } /**
* @category Plugin
* @package GNUsocial
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @author Daniel Supernault <danielsupernault@gmail.com>
* @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 Managed_DataObject
{ {
/**
public static function profileToObject($profile) * Generates a pretty profile from a Profile object
{ *
$url = $profile->getURL (); * @param \Profile $profile
$res = [ * @return pretty array to be used in a response
'@context' => [ */
"https://www.w3.org/ns/activitystreams", public static function profileToObject($profile) {
[ $url = $profile->getURL ();
"@language" => "en" $res = [
] '@context' => [
], "https://www.w3.org/ns/activitystreams",
'id' => $profile->getID(), [
'type' => 'Person', "@language" => "en"
'nickname' => $profile->getNickname (), ]
'is_local' => $profile->isLocal(), ],
'inbox' => "{$url}/inbox.json", 'id' => $profile->getID (),
'outbox' => "{$url}/outbox.json", 'type' => 'Person',
'display_name' => $profile->getFullname(), 'nickname' => $profile->getNickname (),
'followers' => "{$url}/followers.json", 'is_local' => $profile->isLocal (),
'followers_count' => $profile->subscriberCount(), 'inbox' => "{$url}/inbox.json",
'following' => "{$url}/following.json", 'sharedInbox' => common_root_url ()."inbox.json",
'following_count' => $profile->subscriptionCount(), 'outbox' => "{$url}/outbox.json",
'liked' => "{$url}/liked.json", 'display_name' => $profile->getFullname (),
'liked_count' => Fave::countByProfile ($profile), 'followers' => "{$url}/followers.json",
'summary' => $profile->getDescription(), 'followers_count' => $profile->subscriberCount (),
'url' => $profile->getURL(), 'following' => "{$url}/following.json",
'avatar' => [ 'following_count' => $profile->subscriptionCount (),
'type' => 'Image', 'liked' => "{$url}/liked.json",
'width' => 96, 'liked_count' => Fave::countByProfile ($profile),
'height' => 96, 'summary' => $profile->getDescription (),
'url' => $profile->avatarUrl(AVATAR_PROFILE_SIZE) 'url' => $profile->getURL (),
] 'avatar' => [
]; 'type' => 'Image',
return $res; 'width' => 96,
} 'height' => 96,
'url' => $profile->avatarUrl (AVATAR_PROFILE_SIZE)
]
];
return $res;
}
} }

View File

@ -2,9 +2,7 @@
/** /**
* GNU social - a federating social network * GNU social - a federating social network
* *
* Todo: Description * ActivityPubPlugin implementation for GNU Social
*
* PHP version 5
* *
* LICENCE: This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU Affero General Public License as published by
@ -23,28 +21,42 @@
* @package GNUsocial * @package GNUsocial
* @author Diogo Cordeiro <diogo@fc.up.pt> * @author Diogo Cordeiro <diogo@fc.up.pt>
* @author Daniel Supernault <danielsupernault@gmail.com> * @author Daniel Supernault <danielsupernault@gmail.com>
* @copyright 2015 Free Software Foundaction, Inc. * @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 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link https://gnu.io/social * @link https://www.gnu.org/software/social/
*/ */
if (!defined ('GNUSOCIAL')) {
exit(1);
}
if (!defined('GNUSOCIAL')) { exit(1); } /**
* @category Plugin
* @package GNUsocial
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @author Daniel Supernault <danielsupernault@gmail.com>
* @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_tag extends Managed_DataObject class Activitypub_tag extends Managed_DataObject
{ {
public static function tagNameToObject($tag) /**
{ * Generates a pretty tag from a Tag object
$res = [ *
'@context' => [ * @param \Tag $tag
"https://www.w3.org/ns/activitystreams", * @return pretty array to be used in a response
[ */
"@language" => "en" public static function tagNameToObject ($tag) {
] $res = [
], '@context' => [
'name' => $tag, "https://www.w3.org/ns/activitystreams",
'url' => common_local_url('tag', array('tag' => $tag)) [
]; "@language" => "en"
]
],
'name' => $tag,
'url' => common_local_url('tag', array('tag' => $tag))
];
return $res; return $res;
} }
} }