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
*
* Plugin that handles ActivityPub
*
* PHP version 5
* 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
@ -23,18 +21,31 @@
* @package GNUsocial
* @author Daniel Supernault <danielsupernault@gmail.com>
* @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
* @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
{
public function onRouterInitialized(URLMapper $m)
{
/**
* Route/Reroute urls
*
* @param URLMapper $m
* @return void
*/
public function onRouterInitialized(URLMapper $m) {
ActivityPubURLMapperOverwrite::overwrite_variable ($m, ':nickname',
['action' => 'showstream'],
['nickname' => Nickname::DISPLAY_FMT],
@ -51,10 +62,22 @@ class ActivityPubPlugin extends Plugin
$m->connect (':nickname/following.json',
['action' => 'apActorFollowing'],
['nickname' => Nickname::DISPLAY_FMT]);
$m->connect (':nickname/inbox.json',
['action' => 'apActorInbox'],
['nickname' => Nickname::DISPLAY_FMT]);
$m->connect ('inbox.json',
array('action' => 'apSharedInbox'));
}
public function onPluginVersion(array &$versions)
{
/**
* 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',
@ -62,18 +85,17 @@ class ActivityPubPlugin extends Plugin
'rawdescription' =>
// Todo: Translation
'Adds ActivityPub Support'];
return true;
}
}
/**
* Overwrites variables in URL-mapping
*
*/
class ActivityPubURLMapperOverwrite extends URLMapper
{
static function overwrite_variable($m, $path, $args, $paramPatterns, $newaction)
{
static function overwrite_variable ($m, $path, $args, $paramPatterns, $newaction) {
$mimes = [
'application/activity+json',
'application/ld+json',
@ -94,16 +116,31 @@ class ActivityPubURLMapperOverwrite extends URLMapper
}
}
/**
* Plugin return handler
*/
class ActivityPubReturn
{
static function answer ($res)
{
/**
* Return a valid answer
*
* @param array $res
* @return void
*/
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;
}
static function error ($m, $code=500)
{
/**
* 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);

View File

@ -2,9 +2,7 @@
/**
* GNU social - a federating social network
*
* Todo: Description
*
* PHP version 5
* 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
@ -23,20 +21,33 @@
* @package GNUsocial
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @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
* @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
{
protected $needLogin = false;
protected $canPost = true;
protected function handle()
{
/**
* Handle the Followers Collection request
*
* @return void
*/
protected function handle () {
$nickname = $this->trimmed ('nickname');
try {
$user = User::getByNickname ($nickname);
@ -48,15 +59,17 @@ class apActorFollowersAction extends ManagedAction
$page = intval ($this->trimmed ('page'));
if ($page <= 0)
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) {
}
catch(NoResultException $e) {
ActivityPubReturn::error ('This user has no followers');
}
@ -64,16 +77,19 @@ class apActorFollowersAction extends ManagedAction
$total_subs = $profile->subscriberCount ();
$total_pages = ceil ($total_subs / PROFILES_PER_MINILIST);
if ($total_pages == 0)
if ($total_pages == 0) {
ActivityPubReturn::error ('This user has no followers');
}
if ($page > $total_pages)
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));
$subs = array ();
while ($sub->fetch ()) {
$subs[] = $sub->profileurl;
}
$res = [
'@context' => [
@ -90,9 +106,4 @@ class apActorFollowersAction extends ManagedAction
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
*
* Todo: Description
*
* PHP version 5
* 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
@ -23,20 +21,33 @@
* @package GNUsocial
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @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
* @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
{
protected $needLogin = false;
protected $canPost = true;
protected function handle()
{
/**
* Handle the Following Collection request
*
* @return void
*/
protected function handle () {
$nickname = $this->trimmed ('nickname');
try {
$user = User::getByNickname ($nickname);
@ -48,8 +59,9 @@ class apActorFollowingAction extends ManagedAction
$page = intval ($this->trimmed ('page'));
if ($page <= 0)
if ($page <= 0) {
ActivityPubReturn::error ('Invalid page number');
}
/* Fetch Following */
try {
@ -64,16 +76,19 @@ class apActorFollowingAction extends ManagedAction
$total_subs = $profile->subscriptionCount();
$total_pages = ceil ($total_subs / PROFILES_PER_MINILIST);
if ($total_pages == 0)
if ($total_pages == 0) {
ActivityPubReturn::error ('This user has no followers');
}
if ($page > $total_pages)
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));
$subs = array ();
while ($sub->fetch ()) {
$subs[] = $sub->profileurl;
}
$res = [
'@context' => [
@ -90,9 +105,4 @@ class apActorFollowingAction extends ManagedAction
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
*
* Todo: Description
*
* PHP version 5
* 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
@ -23,20 +21,33 @@
* @package GNUsocial
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @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
* @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
{
protected $needLogin = false;
protected $canPost = true;
protected function handle()
{
/**
* Handle the Liked Collection request
*
* @return void
*/
protected function handle () {
$nickname = $this->trimmed ('nickname');
try {
$user = User::getByNickname ($nickname);
@ -54,13 +65,18 @@ class apActorLikedCollectionAction extends ManagedAction
$since_id = empty ($since_id) ? null : $since_id;
$max_id = empty ($max_id) ? null : $max_id;
if ($limit > 80) $limit = 80; // Max is 80
// Max is 80
if ($limit > 80) {
$limit = 80;
}
$fave = $this->fetch_faves ($user->getID(), $limit, $since_id, $max_id);
$fave =
$this->fetch_faves($user->getID(), $limit, $since_id, $max_id);
$faves = [];
while ($fave->fetch())
$faves = array();
while ($fave->fetch ()) {
$faves[] = $this->pretty_fave (clone ($fave));
}
$res = [
'@context' => [
@ -78,8 +94,14 @@ class apActorLikedCollectionAction extends ManagedAction
ActivityPubReturn::answer ($res);
}
protected function pretty_fave ($fave_object)
{
/**
* Take a fave object and turns it in a pretty array to be used
* 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)));
@ -87,18 +109,30 @@ class apActorLikedCollectionAction extends ManagedAction
return $res;
}
private static function fetch_faves ($user_id, $limit = 40, $since_id = null, $max_id = null)
{
/**
* 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 ();
$fav->user_id = $user_id;
$fav->orderBy ('modified DESC');
if ($since_id != null)
if ($since_id != null) {
$fav->whereAdd ("notice_id > {$since_id}");
if ($max_id != null)
}
if ($max_id != null) {
$fav->whereAdd ("notice_id < {$max_id}");
}
$fav->limit ($limit);

View File

@ -2,9 +2,7 @@
/**
* GNU social - a federating social network
*
* Todo: Description
*
* PHP version 5
* 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
@ -23,25 +21,39 @@
* @package GNUsocial
* @author Daniel Supernault <danielsupernault@gmail.com>
* @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
* @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
{
protected $needLogin = false;
protected $canPost = true;
protected function handle()
{
/**
* Handle the Actor Profile request
*
* @return void
*/
protected function handle() {
$nickname = $this->trimmed ('nickname');
try {
$user = User::getByNickname ($nickname);
$profile = $user->getProfile ();
} catch (Exception $e) {
}
catch (Exception $e) {
ActivityPubReturn::error ('Invalid username', 404);
}

View File

@ -2,9 +2,7 @@
/**
* GNU social - a federating social network
*
* Todo: Description
*
* PHP version 5
* 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
@ -23,18 +21,31 @@
* @package GNUsocial
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @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
* @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
{
public static function attachmentToObject($attachment)
{
/**
* 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",

View File

@ -2,9 +2,7 @@
/**
* GNU social - a federating social network
*
* Todo: Description
*
* PHP version 5
* 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
@ -23,21 +21,34 @@
* @package GNUsocial
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @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
* @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
{
public static function errorMessageToObject($m)
{
/**
* Generates a pretty error from a string
*
* @param string $m
* @return pretty array to be used in a response
*/
public static function errorMessageToObject ($m) {
$res = [
'error'=> $m
];
return $res;
}
}

View File

@ -2,9 +2,7 @@
/**
* GNU social - a federating social network
*
* Todo: Description
*
* PHP version 5
* 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
@ -23,27 +21,38 @@
* @package GNUsocial
* @author Daniel Supernault <danielsupernault@gmail.com>
* @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
* @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
{
public static function noticeToObject($notice)
{
$attachments = [];
foreach ($notice->attachments () as $attachment)
{
/**
* 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);
}
$tags = [];
foreach ($notice->getTags () as $tag)
{
$tags = array ();
foreach($notice->getTags()as $tag) {
if ($tag != "") { // Hacky workaround to avoid stupid outputs
$tags[] = Activitypub_tag::tagNameToObject($tag);
}

View File

@ -2,9 +2,7 @@
/**
* GNU social - a federating social network
*
* Todo: Description
*
* PHP version 5
* 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
@ -23,18 +21,31 @@
* @package GNUsocial
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @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
* @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
{
public static function profileToObject($profile)
{
/**
* Generates a pretty profile from a Profile object
*
* @param \Profile $profile
* @return pretty array to be used in a response
*/
public static function profileToObject($profile) {
$url = $profile->getURL ();
$res = [
'@context' => [
@ -48,6 +59,7 @@ class Activitypub_profile extends Managed_DataObject
'nickname' => $profile->getNickname (),
'is_local' => $profile->isLocal (),
'inbox' => "{$url}/inbox.json",
'sharedInbox' => common_root_url ()."inbox.json",
'outbox' => "{$url}/outbox.json",
'display_name' => $profile->getFullname (),
'followers' => "{$url}/followers.json",

View File

@ -2,9 +2,7 @@
/**
* GNU social - a federating social network
*
* Todo: Description
*
* PHP version 5
* 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
@ -23,17 +21,31 @@
* @package GNUsocial
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @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
* @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
{
public static function tagNameToObject($tag)
{
/**
* Generates a pretty tag from a Tag object
*
* @param \Tag $tag
* @return pretty array to be used in a response
*/
public static function tagNameToObject ($tag) {
$res = [
'@context' => [
"https://www.w3.org/ns/activitystreams",