Compare commits

..

47 Commits

Author SHA1 Message Date
Diogo Cordeiro
27d62f1135 Skell for ActivityPub feed 2018-07-13 21:32:54 +01:00
Diogo Cordeiro
6a9b649cd7 Add classes description on docblock 2018-07-13 12:32:27 +01:00
Diogo Cordeiro
d7627fb061 Add Delivery Handler (named Postman) and Subscription hook
Renamed Discovery to Explorer
Various bug fixes and minor improvements
2018-07-13 00:52:12 +01:00
Diogo Cordeiro
542c269885 Fix following collection error message 2018-07-10 19:52:27 +01:00
Diogo Cordeiro
295d4a254d Fix last merge issues 2018-07-10 02:08:06 +01:00
Diogo Cordeiro
ecb812d5e6 Merge branch 'feature-inbox' into 'dev'
Add Feature inbox

See merge request dansup/ActivityPub!7
2018-07-10 00:26:02 +00:00
Diogo Cordeiro
15fab762b5 Add Feature inbox 2018-07-10 00:26:02 +00:00
Diogo Cordeiro
08ec02561a Add Discovery module
See merge request dansup/ActivityPub!4
2018-07-09 23:19:07 +00:00
Diogo Cordeiro
cb70a8f6e8 Add and fix some code documentation 2018-07-10 00:17:18 +01:00
Diogo Cordeiro
fc5b344864 Add and fix some code documentation 2018-07-09 23:43:34 +01:00
Diogo Cordeiro
80b9d1873c Improved Notice Entity 2018-07-09 20:07:35 +01:00
Diogo Cordeiro
cc69c33113 Now discovery module is able to go through collections 2018-07-09 14:41:53 +01:00
Diogo Cordeiro
f533ef2d32 Minor corrections 2018-07-09 00:07:14 +01:00
Diogo Cordeiro
7d773729b9 Merge branch 'return-handler' into 'dev'
Add Return handler and plug it in the plugin

See merge request dansup/ActivityPub!3
2018-07-08 22:58:16 +00:00
Diogo Cordeiro
167f88a602 We are not using laravel helpers on GNU Social 2018-07-06 21:52:03 +01:00
Diogo Cordeiro
4d3321ac21 Minor bug fix and workaround a potential GNU Social issue 2018-07-06 21:31:21 +01:00
Diogo Cordeiro
4cb1ed2c9d Add discovery module 2018-07-06 12:16:09 +01:00
Diogo Cordeiro
c065c76f16 Add Return handler and plug it in the plugin 2018-07-06 11:57:39 +01:00
Diogo Cordeiro
67d52e1d13 Remove profile.json endpoint as it is double 2018-07-06 11:15:08 +01:00
Diogo Cordeiro
8d052e1d8c Merge branch 'feature-following' into 'dev'
Feature following

See merge request dansup/ActivityPub!2
2018-05-13 14:09:38 +00:00
Diogo Cordeiro
2ef79cfad3 Fix issue #11 2018-05-13 15:07:02 +01:00
Diogo Cordeiro
395ca52eae Implement Following 2018-05-10 23:57:43 +01:00
Diogo Cordeiro
370e2320a4 Added followers and following count on Profile entity 2018-05-10 16:26:22 +00:00
Diogo Cordeiro
bfc85fd485 Added followers and following count on Profile entitiy 2018-05-10 16:52:49 +01:00
Diogo Cordeiro
8480194479 Add implementation of Followers endpoint 2018-05-10 16:01:14 +01:00
Diogo Cordeiro
ea233925a8 Merge branch 'feature-like' into 'dev'
Add Liked Collection

See merge request dansup/ActivityPub!1
2018-05-05 23:31:35 +00:00
Diogo Cordeiro
35077111f4 Merge branch 'dev' into 'feature-like'
# Conflicts:
#   ActivityPubPlugin.php
#   classes/Activitypub_attachment.php
#   classes/Activitypub_notice.php
2018-05-05 23:30:45 +00:00
Daniel Supernault
24e8b1579d Add router overwrite method for actor responses 2018-05-05 17:25:02 -06:00
Diogo Cordeiro
81b9f17cb1 Query parameters implemented as per the doc
Kept Attachment entity compatible with GS Master
Fixed conversation id data type on Notice entity
2018-05-05 15:09:28 +01:00
Diogo Cordeiro
95fd0a75c4 Update endpoint URLs and lack of variable declaration fix 2018-05-05 01:32:27 +01:00
Diogo Cordeiro
3161b62b5d Update per the master branch 2018-05-05 01:31:25 +01:00
Diogo Cordeiro
aea8915903 Update per the dev branch 2018-05-05 01:14:27 +01:00
Diogo Cordeiro
be73d6bdf6 Remove text_url which is no longer in the doc spec 2018-05-05 01:06:43 +01:00
Diogo Cordeiro
9c4dc1a233 New Entities added
Moved "hard coded" profile from action to an Entity class
Added the following entities:
- Profile
- Attachment
- Notice
- Tag
2018-05-05 00:54:41 +01:00
Diogo Cordeiro
4e431ee4a1 Follow the dev branch 2018-04-29 13:45:31 +01:00
Diogo Cordeiro
2889b1ba7e Updated plugin as per the doc 2018-04-29 13:43:18 +01:00
Diogo Cordeiro
f2e98c1b73 README 2018-04-29 13:38:14 +01:00
Diogo Cordeiro
0938e7809f Updated the plugin as per the doc
TODO: Implement query parameters in apactorlikedcollection.php" as per the doc
2018-04-29 13:23:54 +01:00
Diogo Cordeiro
21abaca924 README 2018-04-29 02:54:07 +01:00
Diogo Cordeiro
16dcfe522d First draft of Liked Collection 2018-04-29 01:10:54 +01:00
Diogo Cordeiro
8efcc5742d Default Avatar URL is now returned 2018-04-28 20:59:47 +01:00
Daniel Supernault
dbeb3d0ce7 Catch NoAvatarException 2018-03-25 17:46:41 -06:00
Daniel Supernault
6a3e9a6df1 Add icon property to Actor action 2018-03-25 17:39:24 -06:00
Daniel Supernault
839e1f01af Add following/followers to ActivityPubActor action 2018-03-25 14:55:14 -06:00
Daniel Supernault
d44a5974da Update ActivityPubActor action 2018-03-25 14:45:00 -06:00
Daniel Supernault
80bf4c1219 Update ActivityPubActor action 2018-03-25 14:43:06 -06:00
Daniel Supernault
cc454c2a37 Add base plugin and the beginning of the actor method 2018-03-25 14:16:43 -06:00
22 changed files with 2426 additions and 5 deletions

244
ActivityPubPlugin.php Normal file
View File

@ -0,0 +1,244 @@
<?php
require_once __DIR__ . DIRECTORY_SEPARATOR . "utils" . DIRECTORY_SEPARATOR . "postman.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 Daniel Supernault <danielsupernault@gmail.com>
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @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 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
{
/**
* 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');
$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]);
$m->connect (':nickname/inbox.json',
['action' => 'apActorInbox'],
['nickname' => Nickname::DISPLAY_FMT]);
$m->connect ('inbox.json',
array ('action' => 'apSharedInbox'));
$m->connect ('api/statuses/public_timeline.as2',
array ('action' => 'apFeed'));
}
/**
* 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;
}
/**
* 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;
}
function onEndShowExportData (Action $action)
{
$action->elementStart ('div', array ('id' => 'export_data',
'class' => 'section'));
$action->elementStart ('ul', array ('class' => 'xoxo'));
$action->elementStart ('li');
$action->element ('a', array ('href' => common_root_url ()."api/statuses/public_timeline.as2",
'class' => "json",
'type' => "application/activity+json",
'title' => "Public Timeline Feed (Activity Streams 2 JSON)"),
"Activity Streams 2");
$action->elementEnd ('li');
$action->elementEnd ('ul');
$action->elementEnd ('div');
}
}
/**
* Overwrites variables in URL-mapping
*/
class ActivityPubURLMapperOverwrite extends URLMapper
{
static function overwrite_variable ($m, $path, $args, $paramPatterns, $newaction) {
$mimes = [
'application/activity+json',
'application/ld+json',
'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
];
if (in_array ($_SERVER["HTTP_ACCEPT"], $mimes) == false) {
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;
}
}
}
}
/**
* Plugin return handler
*/
class ActivityPubReturn
{
/**
* 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;
}
/**
* 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::error_message_to_array ($m);
echo json_encode ($res, JSON_UNESCAPED_SLASHES);
exit;
}
}

View File

@ -1,5 +1,2 @@
# ActivityPub GNU social Plugin
This plugin development was moved to [https://notabug.org/diogo/gnu-social/src/activitypub/plugins/ActivityPub](https://notabug.org/diogo/gnu-social/src/activitypub/plugins/ActivityPub)
This repository is just an archive of the initial work.
# ActivityPub GNU/Social Plugin
Warning: This is still a work in progress, not every ActivityPub property is fully implemented yet.

View File

@ -0,0 +1,114 @@
<?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);
}
/**
* Actor's Followers Collection
*
* @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 apActorFollowersAction extends ManagedAction
{
protected $needLogin = false;
protected $canPost = true;
/**
* Handle the Followers Collection request
*
* @return void
*/
protected function handle ()
{
$nickname = $this->trimmed ('nickname');
try {
$user = User::getByNickname ($nickname);
$profile = $user->getProfile ();
$url = $profile->profileurl;
} catch (Exception $e) {
ActivityPubReturn::error ('Invalid username.');
}
if (!isset ($_GET["page"])) {
$page = 1;
} else {
$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);
}
}

View File

@ -0,0 +1,114 @@
<?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);
}
/**
* Actor's Following Collection
*
* @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 apActorFollowingAction extends ManagedAction
{
protected $needLogin = false;
protected $canPost = true;
/**
* Handle the Following Collection request
*
* @return void
*/
protected function handle ()
{
$nickname = $this->trimmed ('nickname');
try {
$user = User::getByNickname ($nickname);
$profile = $user->getProfile ();
$url = $profile->profileurl;
} catch (Exception $e) {
ActivityPubReturn::error ('Invalid username.');
}
if (!isset ($_GET["page"])) {
$page = 1;
} else {
$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 is not following anyone.');
}
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);
}
}

115
actions/apactorinbox.php Normal file
View File

@ -0,0 +1,115 @@
<?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);
}
/**
* Actor's Inbox
*
* @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 apActorInboxAction extends ManagedAction
{
protected $needLogin = false;
protected $canPost = true;
/**
* Handle the Actor Inbox request
*
* @return void
*/
protected function handle ()
{
$nickname = $this->trimmed ('nickname');
try {
$user = User::getByNickname ($nickname);
$profile = $user->getProfile ();
$url = $profile->profileurl;
} catch (Exception $e) {
ActivityPubReturn::error ("Invalid username.");
}
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
ActivityPubReturn::error ("C2S not implemented just yet.");
}
$data = json_decode (file_get_contents ('php://input'));
// Validate data
if (!(isset($data->type))) {
ActivityPubReturn::error ("Type was not specified.");
}
if (!isset($data->actor)) {
ActivityPubReturn::error ("Actor was not specified.");
}
if (!isset($data->object)) {
ActivityPubReturn::error ("Object was not specified.");
}
// Get valid Actor object
try {
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) {
ActivityPubReturn::error ("Invalid Actor.", 404);
}
$to_profiles = array ($user);
// Process request
switch ($data->type) {
case "Create":
require_once __DIR__ . DIRECTORY_SEPARATOR . "inbox" . DIRECTORY_SEPARATOR . "Create.php";
break;
case "Delete":
require_once __DIR__ . DIRECTORY_SEPARATOR . "inbox" . DIRECTORY_SEPARATOR . "Delete.php";
break;
case "Follow":
require_once __DIR__ . DIRECTORY_SEPARATOR . "inbox" . DIRECTORY_SEPARATOR . "Follow.php";
break;
case "Like":
require_once __DIR__ . DIRECTORY_SEPARATOR . "inbox" . DIRECTORY_SEPARATOR . "Like.php";
break;
case "Undo":
require_once __DIR__ . DIRECTORY_SEPARATOR . "inbox" . DIRECTORY_SEPARATOR . "Undo.php";
break;
case "Announce":
require_once __DIR__ . DIRECTORY_SEPARATOR . "inbox" . DIRECTORY_SEPARATOR . "Announce.php";
break;
default:
ActivityPubReturn::error ("Invalid type value.");
}
}
}

View File

@ -0,0 +1,146 @@
<?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);
}
/**
* Actor's Liked Collection
*
* @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 apActorLikedCollectionAction extends ManagedAction
{
protected $needLogin = false;
protected $canPost = true;
/**
* Handle the Liked Collection request
*
* @return void
*/
protected function handle ()
{
$nickname = $this->trimmed ('nickname');
try {
$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);
}
/**
* 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::notice_to_array (Notice::getByID ($fave_object->notice_id)));
return $res;
}
/**
* 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) {
$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

@ -0,0 +1,67 @@
<?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 Daniel Supernault <danielsupernault@gmail.com>
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @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);
}
/**
* Actor's profile (Local users only)
*
* @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;
/**
* 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) {
ActivityPubReturn::error ('Invalid username.', 404);
}
$res = Activitypub_profile::profile_to_array ($profile);
ActivityPubReturn::answer ($res);
}
}

342
actions/apfeed.php Normal file
View File

@ -0,0 +1,342 @@
<?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);
}
/**
* ActivityPub Feed
*
* @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 apFeedAction extends ManagedAction
{
protected $needLogin = false;
protected $canPost = true;
var $page = 1;
var $count = 20;
var $max_id = 0;
var $since_id = 0;
/**
* Handle the Actor Inbox request
*
* @return void
*/
protected function handle ()
{
$this->showJsonTimeline($this->getNotices());
}
/**
* Get notices
*
* @return array notices
*/
function getNotices()
{
$notices = array ();
$stream = new PublicNoticeStream (null);
$notice = $stream->getNotices (($this->page - 1) * $this->count,
$this->count,
$this->since_id, $this->max_id);
$notices = $notice->fetchAll ();
NoticeList::prefill ($notices);
return $notices;
}
function showJsonTimeline ($notice)
{
header ('Content-Type: application/json; charset=utf-8');
$statuses = array ();
if (is_array ($notice)) {
//FIXME: make everything calling showJsonTimeline use only Notice objects
$ids = array ();
foreach($notice as $n) {
$ids[] = $n->getID ();
}
$notice = Notice::multiGet ('id', $ids);
}
while ($notice->fetch ()) {
try {
$twitter_status = $this->twitterStatusArray ($notice);
array_push ($statuses, $twitter_status);
} catch (Exception $e) {
common_log (LOG_ERR, $e->getMessage ());
continue;
}
}
$this->showJsonObjects ($statuses);
}
function showJsonObjects ($objects)
{
$json_objects = json_encode ($objects);
if ($json_objects === false) {
$this-> clientError(_('JSON encoding failed. Error: ').json_last_error_msg ());
} else {
print $json_objects;
}
}
function twitterStatusArray ($notice, $include_user = true)
{
$base = $this->twitterSimpleStatusArray ($notice, $include_user);
// FIXME: MOVE TO SHARE PLUGIN
if (!empty ($notice->repeat_of)) {
$original = Notice::getKV ('id', $notice->repeat_of);
if ($original instanceof Notice) {
$orig_array = $this->twitterSimpleStatusArray($original, $include_user);
$base['retweeted_status'] = $orig_array;
}
}
return $base;
}
function twitterSimpleStatusArray ($notice, $include_user = true)
{
$profile = $notice->getProfile ();
$twitter_status = array ();
$twitter_status['text'] = $notice->content;
$twitter_status['truncated'] = false;
$twitter_status['created_at'] = self::dateTwitter ($notice->created);
try {
// We could just do $notice->reply_to but maybe the future holds a
// different story for parenting.
$parent = $notice->getParent ();
$in_reply_to = $parent->id;
} catch (NoParentNoticeException $e) {
$in_reply_to = null;
} catch (NoResultException $e) {
// the in_reply_to message has probably been deleted
$in_reply_to = null;
}
$twitter_status['in_reply_to_status_id'] = $in_reply_to;
$source = null;
$source_link = null;
$ns = $notice->getSource ();
if ($ns instanceof Notice_source) {
$source = $ns->code;
if (!empty ($ns->url)) {
$source_link = $ns->url;
if (!empty ($ns->name)) {
$source = $ns->name;
}
}
}
$twitter_status['uri'] = $notice->getUri ();
$twitter_status['source'] = $source;
$twitter_status['source_link'] = $source_link;
$twitter_status['id'] = intval ($notice->id);
$replier_profile = null;
if ($notice->reply_to) {
$reply = Notice::getKV (intval ($notice->reply_to));
if ($reply) {
$replier_profile = $reply->getProfile ();
}
}
$twitter_status['in_reply_to_user_id'] =
($replier_profile) ? intval ($replier_profile->id) : null;
$twitter_status['in_reply_to_screen_name'] =
($replier_profile) ? $replier_profile->nickname : null;
try {
$notloc = Notice_location::locFromStored ($notice);
// This is the format that GeoJSON expects stuff to be in
$twitter_status['geo'] = array ('type' => 'Point',
'coordinates' => array (
(float) $notloc->lat,
(float) $notloc->lon));
} catch (ServerException $e) {
$twitter_status['geo'] = null;
}
// Enclosures
$attachments = $notice->attachments ();
if (!empty ($attachments)) {
$twitter_status['attachments'] = array ();
foreach ($attachments as $attachment) {
try {
$enclosure_o = $attachment->getEnclosure ();
$enclosure = array();
$enclosure['url'] = $enclosure_o->url;
$enclosure['mimetype'] = $enclosure_o->mimetype;
$enclosure['size'] = $enclosure_o->size;
$twitter_status['attachments'][] = $enclosure;
} catch (ServerException $e) {
// There was not enough metadata available
}
}
}
if ($include_user && $profile) {
// Don't get notice (recursive!)
$twitter_user = $this->twitterUserArray($profile, false);
$twitter_status['user'] = $twitter_user;
}
// StatusNet-specific
$twitter_status['statusnet_html'] = $notice->getRendered();
$twitter_status['statusnet_conversation_id'] = intval($notice->conversation);
// The event call to handle NoticeSimpleStatusArray lets plugins add data to the output array
Event::handle('NoticeSimpleStatusArray',
array ($notice, &$twitter_status, $this->scoped,
array ('include_user' => $include_user)));
return $twitter_status;
}
static function dateTwitter($dt)
{
$dateStr = date ('d F Y H:i:s', strtotime ($dt));
$d = new DateTime ($dateStr, new DateTimeZone ('UTC'));
$d->setTimezone (new DateTimeZone(common_timezone()));
return $d->format ('D M d H:i:s O Y');
}
function twitterUserArray ($profile, $get_notice = false)
{
$twitter_user = array ();
try {
$user = $profile->getUser ();
} catch (NoSuchUserException $e) {
$user = null;
}
$twitter_user['id'] = $profile->getID ();
$twitter_user['name'] = $profile->getBestName ();
$twitter_user['screen_name'] = $profile->getNickname ();
$twitter_user['location'] = $profile->location;
$twitter_user['description'] = $profile->getDescription ();
// TODO: avatar url template (example.com/user/avatar?size={x}x{y})
$twitter_user['profile_image_url'] = Avatar::urlByProfile($profile, AVATAR_STREAM_SIZE);
$twitter_user['profile_image_url_https'] = $twitter_user['profile_image_url'];
// START introduced by qvitter API, not necessary for StatusNet API
$twitter_user['profile_image_url_profile_size'] = Avatar::urlByProfile($profile, AVATAR_PROFILE_SIZE);
try {
$avatar = Avatar::getUploaded ($profile);
$origurl = $avatar->displayUrl ();
} catch(Exception $e) {
$origurl = $twitter_user['profile_image_url_profile_size'];
}
$twitter_user['profile_image_url_original'] = $origurl;
$twitter_user['groups_count'] = $profile->getGroupCount();
foreach (array('linkcolor', 'backgroundcolor') as $key) {
$twitter_user[$key] = Profile_prefs::getConfigData($profile, 'theme', $key);
}
// END introduced by qvitter API, not necessary for StatusNet API
$twitter_user['url'] = ($profile->homepage) ? $profile->homepage : null;
$twitter_user['protected'] = (!empty($user) && $user->private_stream) ? true : false;
$twitter_user['followers_count'] = $profile->subscriberCount();
// Note: some profiles don't have an associated user
$twitter_user['friends_count'] = $profile->subscriptionCount ();
$twitter_user['created_at'] = self::dateTwitter($profile->created);
$timezone = 'UTC';
if (!empty($user) && $user->timezone) {
$timezone = $user->timezone;
}
$t = new DateTime;
$t->setTimezone (new DateTimeZone ($timezone));
$twitter_user['utc_offset'] = $t->format('Z');
$twitter_user['time_zone'] = $timezone;
$twitter_user['statuses_count'] = $profile->noticeCount();
// Is the requesting user following this user?
// These values might actually also mean "unknown". Ambiguity issues?
$twitter_user['following'] = false;
$twitter_user['statusnet_blocking'] = false;
$twitter_user['notifications'] = false;
if ($this->scoped instanceof Profile) {
try {
$sub = Subscription::getSubscription($this->scoped, $profile);
// Notifications on?
$twitter_user['following'] = true;
$twitter_user['notifications'] = ($sub->jabber || $sub->sms);
} catch (NoResultException $e) {
// well, the values are already false...
}
$twitter_user['statusnet_blocking'] = $this->scoped->hasBlocked($profile);
}
if ($get_notice) {
$notice = $profile->getCurrentNotice ();
if ($notice instanceof Notice) {
// don't get user!
$twitter_user['status'] = $this->twitterStatusArray($notice, false);
}
}
// StatusNet-specific
$twitter_user['statusnet_profile_url'] = $profile->profileurl;
// The event call to handle NoticeSimpleStatusArray lets plugins add data to the output array
Event::handle('TwitterUserArray', array ($profile, &$twitter_user, $this->scoped, array()));
return $twitter_user;
}
}

135
actions/apsharedinbox.php Normal file
View File

@ -0,0 +1,135 @@
<?php
require_once dirname (__DIR__) . DIRECTORY_SEPARATOR . "utils" . DIRECTORY_SEPARATOR . "explorer.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);
}
/**
* Shared Inbox Handler
*
* @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 apSharedInboxAction extends ManagedAction
{
protected $needLogin = false;
protected $canPost = true;
/**
* Handle the Shared Inbox request
*
* @return void
*/
protected function handle ()
{
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
ActivityPubReturn::error ("Only POST requests allowed.");
}
$data = json_decode (file_get_contents ('php://input'));
// Validate data
if (!isset($data->type)) {
ActivityPubReturn::error ("Type was not specified.");
}
if (!isset($data->actor)) {
ActivityPubReturn::error ("Actor was not specified.");
}
if (!isset($data->object)) {
ActivityPubReturn::error ("Object was not specified.");
}
$discovery = new Activitypub_explorer;
// Get valid Actor object
try {
$actor_profile = $discovery->lookup ($data->actor);
$actor_profile = $actor_profile[0];
} catch (Exception $e) {
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)) {
// Remove duplicates from To actors set
array_unique ($data->to);
foreach ($data->to as $to_url) {
try {
$to_profiles = array_merge ($to_profiles, $discovery->lookup ($to_url));
} catch (Exception $e) {
// XXX: Invalid actor found, not sure how we handle those
}
}
} else if (empty ($data->to) || in_array ($data->to, $public_to)) {
// No need to do anything else at this point, let's just break out the if
} else {
try {
$to_profiles[]= $discovery->lookup ($data->to);
} catch (Exception $e) {
ActivityPubReturn::error ("Invalid Actor.", 404);
}
}
unset ($discovery);
require_once __DIR__ . DIRECTORY_SEPARATOR . "inbox" . DIRECTORY_SEPARATOR . "Create.php";
break;
case "Follow":
require_once __DIR__ . DIRECTORY_SEPARATOR . "inbox" . DIRECTORY_SEPARATOR . "Follow.php";
break;
case "Like":
require_once __DIR__ . DIRECTORY_SEPARATOR . "inbox" . DIRECTORY_SEPARATOR . "Like.php";
break;
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.");
}
}
}

View File

@ -0,0 +1,37 @@
<?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);
}
try {
Notice::getByUri ($data->object)->repeat ($actor_profile, "ActivityPub");
ActivityPubReturn::answer ("Notice repeated successfully.");
} catch (Exception $e) {
ActivityPubReturn::error ($e->getMessage (), 403);
}

93
actions/inbox/Create.php Normal file
View File

@ -0,0 +1,93 @@
<?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);
}
$valid_object_types = array ("Note");
// Validate data
if (!(isset ($data->object->type) && in_array ($data->object->type, $valid_object_types))) {
ActivityPubReturn::error ("Invalid Object type.");
}
if (!isset ($data->object->content)) {
ActivityPubReturn::error ("Object content was not specified.");
}
$content = $data->object->content;
$act = new Activity ();
$act->verb = ActivityVerb::POST;
$act->time = time ();
$act->actor = $actor_profile->asActivityObject ();
$act->context = new ActivityContext ();
// Is this a reply?
if (isset ($data->object->reply_to)) {
$reply_to = Notice::getByUri ($data->object->reply_to);
$act->context->replyToID = $reply_to->getUri ();
$act->context->replyToUrl = $data->object->reply_to;
} else {
$reply_to = null;
}
$act->context->attention = common_get_attentions ($content, $actor_profile, $reply_to);
foreach ($to_profiles as $to)
{
$act->context->attention[$to->getUri ()] = "http://activitystrea.ms/schema/1.0/person";
}
// Reject notice if it is too long (without the HTML)
// This is done after MediaFile::fromUpload etc. just to act the same as the ApiStatusesUpdateAction
if (Notice::contentTooLong ($content)) {
ActivityPubReturn::error ("That's too long. Maximum notice size is %d character.");
}
$options = array ('source' => 'ActivityPub', 'uri' => $data->id);
// $options gets filled with possible scoping settings
ToSelector::fillActivity ($this, $act, $options);
$actobj = new ActivityObject ();
$actobj->type = ActivityObject::NOTE;
$actobj->content = common_render_content ($content, $actor_profile, $reply_to);
// Finally add the activity object to our activity
$act->objects[] = $actobj;
try {
$res = array ("@context" => "https://www.w3.org/ns/activitystreams",
"id" => $data->id,
"type" => "Create",
"actor" => $data->actor,
"object" => Activitypub_notice::notice_to_array (Notice::saveActivity ($act, $actor_profile, $options)));
ActivityPubReturn::answer ($res);
} catch (Exception $e) {
ActivityPubReturn::error ($e->getMessage ());
}

41
actions/inbox/Delete.php Normal file
View File

@ -0,0 +1,41 @@
<?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);
}
try {
Activitypub_notice::getByUri ($data->object)->deleteAs ($actor_profile);
$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);
}

58
actions/inbox/Follow.php Normal file
View File

@ -0,0 +1,58 @@
<?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);
}
// Validate Object
if (!is_string ($data->object)) {
ActivityPubReturn::error ("Invalid Object object, URL expected.");
}
// Get valid Object profile
try {
$object_profile = new Activitypub_explorer;
$object_profile = $object_profile->lookup ($data->object)[0];
} catch(Exception $e) {
ActivityPubReturn::error ("Invalid Object Actor URL.", 404);
}
try {
if (!Subscription::exists ($actor_profile, $object_profile)) {
Subscription::start ($actor_profile, $object_profile);
$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 $e) {
ActivityPubReturn::error ("Invalid Object Actor URL.", 404);
}

41
actions/inbox/Like.php Normal file
View File

@ -0,0 +1,41 @@
<?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);
}
try {
Fave::addNew ($actor_profile, Notice::getByUri ($data->object));
$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);
}

78
actions/inbox/Undo.php Normal file
View File

@ -0,0 +1,78 @@
<?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);
}
// Validate data
if (!isset ($data->type)) {
ActivityPubReturn::error ("Type was not specified.");
}
switch ($data->object->type) {
case "Like":
try {
// Validate data
if (!isset ($data->object->object)) {
ActivityPubReturn::error ("Object Notice URL was not specified.");
}
Fave::removeEntry ($actor_profile, Notice::getByUri ($data->object->object));
ActivityPubReturn::answer ("Notice disfavorited successfully.");
} catch (Exception $e) {
ActivityPubReturn::error ($e->getMessage (), 403);
}
break;
case "Follow":
// Validate data
if (!isset ($data->object->object)) {
ActivityPubReturn::error ("Object Actor URL was not specified.");
}
// Get valid Object profile
try {
$object_profile = new Activitypub_explorer;
$object_profile = $object_profile->lookup ($data->object->object)[0];
} catch (Exception $e) {
ActivityPubReturn::error ("Invalid Object Actor URL.", 404);
}
try {
if (Subscription::exists ($actor_profile, $object_profile)) {
Subscription::cancel ($actor_profile, $object_profile);
ActivityPubReturn::answer ("You are no longer following this person.");
} else {
ActivityPubReturn::error ("You are not following this person already.", 409);
}
} catch (Exception $e) {
ActivityPubReturn::error ("Invalid Object Actor URL.", 404);
}
break;
default:
ActivityPubReturn::error ("Invalid object type.");
break;
}

View File

@ -0,0 +1,77 @@
<?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);
}
/**
* ActivityPub Attachment representation
*
* @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_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 attachment_to_array ($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
];
// Image
if (substr ($res["mimetype"], 0, 5) == "image")
{
$res["meta"]= [
'width' => $attachment->width,
'height' => $attachment->height
];
}
return $res;
}
}

View File

@ -0,0 +1,56 @@
<?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);
}
/**
* ActivityPub error representation
*
* @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_error extends Managed_DataObject
{
/**
* Generates a pretty error from a string
*
* @param string $m
* @return pretty array to be used in a response
*/
public static function error_message_to_array ($m)
{
$res = [
'error'=> $m
];
return $res;
}
}

View File

@ -0,0 +1,89 @@
<?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 Daniel Supernault <danielsupernault@gmail.com>
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @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);
}
/**
* ActivityPub notice representation
*
* @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
{
/**
* Generates a pretty notice from a Notice object
*
* @param Notice $notice
* @return pretty array to be used in a response
*/
public static function notice_to_array ($notice)
{
$attachments = array ();
foreach($notice->attachments () as $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::tag_to_array ($tag);
}
}
$to = array ();
foreach ($notice->getAttentionProfileIDs () as $to_id) {
$to[] = Profile::getById ($to_id)->getUri ();
}
if (!is_null($to)) {
$to = array ("https://www.w3.org/ns/activitystreams#Public");
}
$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

@ -0,0 +1,204 @@
<?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);
}
/**
* ActivityPub Profile
*
* @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 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
* @return pretty array to be used in a response
*/
public static function profile_to_array ($profile)
{
$url = $profile->getURL ();
$res = [
'@context' => [
"https://www.w3.org/ns/activitystreams",
[
"@language" => "en"
]
],
'id' => $profile->getID (),
'type' => 'Person',
'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",
'followers_count' => $profile->subscriberCount (),
'following' => "{$url}/following.json",
'following_count' => $profile->subscriptionCount (),
'liked' => "{$url}/liked.json",
'liked_count' => Fave::countByProfile ($profile),
'summary' => ($desc = $profile->getDescription ()) == null ? "" : $desc,
'url' => $profile->getURL (),
'avatar' => [
'type' => 'Image',
'width' => 96,
'height' => 96,
'url' => $profile->avatarUrl (AVATAR_PROFILE_SIZE)
]
];
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;
}
}

View File

@ -0,0 +1,64 @@
<?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);
}
/**
* ActivityPub representation of a Tag
*
* @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_tag extends Managed_DataObject
{
/**
* Generates a pretty tag from a Tag object
*
* @param Tag $tag
* @return pretty array to be used in a response
*/
public static function tag_to_array ($tag)
{
$res = [
'@context' => [
"https://www.w3.org/ns/activitystreams",
[
"@language" => "en"
]
],
'name' => $tag,
'url' => common_local_url ('tag', array('tag' => $tag))
];
return $res;
}
}

213
utils/explorer.php Normal file
View File

@ -0,0 +1,213 @@
<?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);
}
/**
* ActivityPub's own Explorer
*
* Allows to discovery new (or the same) ActivityPub profiles
*
* @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_explorer
{
private $discovered_actor_profiles = array ();
/**
* Get every profile from the given URL
* This function cleans the $this->discovered_actor_profiles array
* so that there is no erroneous data
*
* @param string $url User's url
* @return array of Profile objects
*/
public function lookup ($url)
{
$this->discovered_actor_profiles = array ();
return $this->_lookup ($url);
}
/**
* Get every profile from the given URL
* This is a recursive function that will accumulate the results on
* $discovered_actor_profiles array
*
* @param string $url User's url
* @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
if (! ($this->grab_local_user ($url) || $this->grab_remote_user ($url))) {
throw new Exception ("User not found");
}
return $this->discovered_actor_profiles;
}
/**
* Get a local user profiles from its URL and joins it on
* $this->discovered_actor_profiles
*
* @param string $url User's url
* @return boolean success state
*/
private function grab_local_user ($url)
{
if (($actor_profile = self::get_profile_by_url ($url)) != false) {
$this->discovered_actor_profiles[]= $actor_profile;
return true;
} else {
/******************************** XXX: ********************************
* Sometimes it is not true that the user is not locally available, *
* mostly when it is a local user and URLs slightly changed *
* e.g.: GS instance owner changed from standard urls to pretty urls *
* (not sure if this is necessary, but anyway) *
**********************************************************************/
// Iff we really are in the same instance
$root_url_len = strlen (common_root_url ());
if (substr ($url, 0, $root_url_len) == common_root_url ()) {
// Grab the nickname and try to get the user
if (($actor_profile = Profile::getKV ("nickname", substr ($url, $root_url_len))) != false) {
$this->discovered_actor_profiles[]= $actor_profile;
return true;
}
}
}
return false;
}
/**
* Get a remote user(s) profile(s) from its URL and joins it on
* $this->discovered_actor_profiles
*
* @param string $url User's url
* @return boolean success state
*/
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 Exception ("Invalid Actor URL.");
}
$res = json_decode ($response->getBody (), JSON_UNESCAPED_SLASHES);
if (isset ($res["orderedItems"])) { // It's a potential collection of actors!!!
foreach ($res["orderedItems"] as $profile) {
if ($this->_lookup ($profile) == false) {
// XXX: Invalid actor found, not sure how we handle those
}
}
// Go through entire collection
if (!is_null ($res["next"])) {
$this->_lookup ($res["next"]);
}
return true;
} else if ($this->validate_remote_response ($res)) {
$this->discovered_actor_profiles[]= $this->store_profile ($res);
return true;
}
return false;
}
/**
* Save remote user profile in local instance
*
* @param array $res remote response
* @return Profile remote Profile object
*/
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"];
$aprofile->doInsert ();
return $aprofile->localProfile ();
}
/**
* Validates a remote response in order to determine whether this
* response is a valid profile or not
*
* @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"], $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;
}
}

96
utils/postman.php Normal file
View File

@ -0,0 +1,96 @@
<?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);
}
/**
* ActivityPub's own Postman
*
* Standard workflow expects that we send an Explorer to find out destinataries'
* inbox address. Then we send our postman to deliver whatever we want to send them.
*
* @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);
}
}