Compare commits
47 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
27d62f1135 | ||
|
6a9b649cd7 | ||
|
d7627fb061 | ||
|
542c269885 | ||
|
295d4a254d | ||
|
ecb812d5e6 | ||
|
15fab762b5 | ||
|
08ec02561a | ||
|
cb70a8f6e8 | ||
|
fc5b344864 | ||
|
80b9d1873c | ||
|
cc69c33113 | ||
|
f533ef2d32 | ||
|
7d773729b9 | ||
|
167f88a602 | ||
|
4d3321ac21 | ||
|
4cb1ed2c9d | ||
|
c065c76f16 | ||
|
67d52e1d13 | ||
|
8d052e1d8c | ||
|
2ef79cfad3 | ||
|
395ca52eae | ||
|
370e2320a4 | ||
|
bfc85fd485 | ||
|
8480194479 | ||
|
ea233925a8 | ||
|
35077111f4 | ||
|
24e8b1579d | ||
|
81b9f17cb1 | ||
|
95fd0a75c4 | ||
|
3161b62b5d | ||
|
aea8915903 | ||
|
be73d6bdf6 | ||
|
9c4dc1a233 | ||
|
4e431ee4a1 | ||
|
2889b1ba7e | ||
|
f2e98c1b73 | ||
|
0938e7809f | ||
|
21abaca924 | ||
|
16dcfe522d | ||
|
8efcc5742d | ||
|
dbeb3d0ce7 | ||
|
6a3e9a6df1 | ||
|
839e1f01af | ||
|
d44a5974da | ||
|
80bf4c1219 | ||
|
cc454c2a37 |
244
ActivityPubPlugin.php
Normal file
244
ActivityPubPlugin.php
Normal 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;
|
||||
}
|
||||
}
|
@ -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.
|
||||
|
114
actions/apactorfollowers.php
Normal file
114
actions/apactorfollowers.php
Normal 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);
|
||||
}
|
||||
}
|
114
actions/apactorfollowing.php
Normal file
114
actions/apactorfollowing.php
Normal 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
115
actions/apactorinbox.php
Normal 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.");
|
||||
}
|
||||
}
|
||||
}
|
146
actions/apactorlikedcollection.php
Normal file
146
actions/apactorlikedcollection.php
Normal 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;
|
||||
}
|
||||
}
|
67
actions/apactorprofile.php
Normal file
67
actions/apactorprofile.php
Normal 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
342
actions/apfeed.php
Normal 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
135
actions/apsharedinbox.php
Normal 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.");
|
||||
}
|
||||
}
|
||||
}
|
37
actions/inbox/Announce.php
Normal file
37
actions/inbox/Announce.php
Normal 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
93
actions/inbox/Create.php
Normal 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
41
actions/inbox/Delete.php
Normal 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
58
actions/inbox/Follow.php
Normal 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
41
actions/inbox/Like.php
Normal 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
78
actions/inbox/Undo.php
Normal 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;
|
||||
}
|
77
classes/Activitypub_attachment.php
Normal file
77
classes/Activitypub_attachment.php
Normal 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;
|
||||
}
|
||||
}
|
56
classes/Activitypub_error.php
Normal file
56
classes/Activitypub_error.php
Normal 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;
|
||||
}
|
||||
}
|
89
classes/Activitypub_notice.php
Normal file
89
classes/Activitypub_notice.php
Normal 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;
|
||||
}
|
||||
}
|
204
classes/Activitypub_profile.php
Normal file
204
classes/Activitypub_profile.php
Normal 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;
|
||||
}
|
||||
}
|
64
classes/Activitypub_tag.php
Normal file
64
classes/Activitypub_tag.php
Normal 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
213
utils/explorer.php
Normal 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
96
utils/postman.php
Normal 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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user