Updated the plugin as per the doc

TODO: Implement query parameters in apactorlikedcollection.php" as per the doc
This commit is contained in:
Diogo Cordeiro 2018-04-29 13:23:54 +01:00
parent 21abaca924
commit 0938e7809f
4 changed files with 108 additions and 27 deletions

View File

@ -22,6 +22,7 @@
* @category Plugin
* @package GNUsocial
* @author Daniel Supernault <danielsupernault@gmail.com>
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @copyright 2014 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/
@ -34,13 +35,13 @@ class ActivityPubPlugin extends Plugin
public function onRouterInitialized(URLMapper $m)
{
$m->connect('api/statuses/user_timeline/:id.ap',
['action' => 'activitypubactor'],
['id' => '[0-9]+']);
$m->connect(':nickname/profile.json',
['action' => 'apActorProfile'],
['nickname' => Nickname::DISPLAY_FMT]);
$m->connect('api/user/:id/liked.json',
['action' => 'likedactor'],
['id' => '[0-9]+']);
$m->connect(':nickname/liked.json',
['action' => 'apActorLikedCollection'],
['nickname' => Nickname::DISPLAY_FMT]);
}
public function onPluginVersion(array &$versions)

View File

@ -21,6 +21,7 @@
*
* @category Plugin
* @package GNUsocial
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @author Daniel Supernault <danielsupernault@gmail.com>
* @copyright 2015 Free Software Foundaction, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
@ -29,16 +30,23 @@
if (!defined('GNUSOCIAL')) { exit(1); }
class LikedActorAction extends ManagedAction
class apActorLikedCollectionAction extends ManagedAction
{
protected $needLogin = false;
protected $canPost = true;
protected function handle()
{
$user = User::getByID($this->trimmed('id'));
$profile = $user->getProfile();
$url = $profile->profileurl;
$nickname = $this->trimmed('nickname');
try {
$user = User::getByNickname($nickname);
$profile = $user->getProfile();
$url = $profile->profileurl;
} catch (Exception $e) {
throw new \Exception('Invalid username');
}
// TODO: Implement query parameters as per the doc
$total_faves = Fave::countByProfile ($profile);
@ -64,14 +72,14 @@ class LikedActorAction extends ManagedAction
header('Content-Type: application/json');
echo json_encode($res, JSON_PRETTY_PRINT);
echo json_encode($res, isset($_GET["pretty"]) ? JSON_PRETTY_PRINT : null);
}
protected function pretty_fave ($fave_object)
{
$res = array ("uri" => $fave_object->uri,
"created" => $fave_object->created,
"object" => Notice::getByID($fave_object->notice_id));
"object" => Activitypub_notice::noticeToObject(Notice::getByID($fave_object->notice_id)));
return $res;
}

View File

@ -22,6 +22,7 @@
* @category Plugin
* @package GNUsocial
* @author Daniel Supernault <danielsupernault@gmail.com>
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @copyright 2015 Free Software Foundaction, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link https://gnu.io/social
@ -29,18 +30,23 @@
if (!defined('GNUSOCIAL')) { exit(1); }
class ActivityPubActorAction extends ManagedAction
class apActorProfileAction extends ManagedAction
{
protected $needLogin = false;
protected $canPost = true;
protected function handle()
{
$user = User::getByID($this->trimmed('id'));
$profile = $user->getProfile();
$url = $profile->profileurl;
$nickname = $this->trimmed('nickname');
try {
$user = User::getByNickname($nickname);
$profile = $user->getProfile();
$url = $profile->profileurl;
} catch (Exception $e) {
throw new \Exception('Invalid username');
}
$avatar = $profile->avatarUrl(AVATAR_PROFILE_SIZE);
$avatar_url = $profile->avatarUrl(AVATAR_PROFILE_SIZE);
$res = [
'@context' => [
@ -49,27 +55,29 @@ class ActivityPubActorAction extends ManagedAction
"@language" => "en"
]
],
'id' => $url,
'id' => $user->getID(),
'type' => 'Person',
'following' => "{$url}/subscriptions",
'username' => $user->nickname,
'inbox' => "{$url}/inbox.json",
'outbox' => "{$url}/outbox.json",
'acct' => null, // TODO: Equals `username` for local users, includes `@domain` for remote ones
'display_name' => $profile->fullname,
'followers' => "{$url}/subscribers",
'inbox' => null,
'outbox' => null,
'liked' => "{$url}/favorites",
'preferredUsername' => $user->nickname,
'name' => $user->nickname,
'following' => "{$url}/subscriptions",
'liked' => "{$url}/liked.json",
'liked_count' => Fave::countByProfile ($profile),
'summary' => $profile->bio,
'url' => $url,
'icon' => [
'avatar' => [
'type' => 'Image',
'width' => 96,
'height' => 96,
'url' => $avatar
'url' => $avatar_url
]
];
header('Content-Type: application/json');
echo json_encode($res, JSON_PRETTY_PRINT);
echo json_encode($res, isset($_GET["pretty"]) ? JSON_PRETTY_PRINT : null);
}
}

View File

@ -0,0 +1,64 @@
<?php
/**
* GNU social - a federating social network
*
* Todo: Description
*
* PHP version 5
*
* 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 2015 Free Software Foundaction, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link https://gnu.io/social
*/
if (!defined('GNUSOCIAL')) { exit(1); }
class Activitypub_notice extends Managed_DataObject
{
public static function noticeToObject($notice)
{
// todo: fix timestamp formats
$item = [
'id' => $notice->getUrl(),
// TODO: handle other types
'type' => 'Notice',
'actor' => $notice->getProfile()->getUrl(),
'published' => $notice->created,
'to' => [
// TODO: handle proper scope
'https://www.w3.org/ns/activitystreams#Public'
],
'cc' => [
// TODO: add cc's
"{$notice->getProfile()->getUrl()}/subscribers",
],
'content' => $notice->getContent(),
'rendered' => $notice->getRendered(),
'url' => $notice->getUrl(),
'reply_to' => empty($notice->reply_to) ? null : Notice::getById($notice->reply_to)->getUrl(),
'is_local' => $notice->is_local,
'conversation' => $notice->conversation,
'attachment' => [],
'tag' => []
];
return $item;
}
}