Merge branch 'actor_outbox' into 'dev'

Add a modern version of original Daniel's Outbox

See merge request dansup/ActivityPub!18
This commit is contained in:
Diogo Cordeiro 2018-08-04 04:58:20 +00:00
commit 7cf223fb24
8 changed files with 216 additions and 53 deletions

View File

@ -184,6 +184,12 @@ class ActivityPubPlugin extends Plugin
['id' => '[0-9]+']
);
$m->connect(
'user/:id/outbox.json',
['action' => 'apActorOutbox'],
['id' => '[0-9]+']
);
$m->connect(
'inbox.json',
['action' => 'apInbox']

View File

@ -59,7 +59,7 @@ class apActorFollowersAction extends ManagedAction
}
if (!$profile->isLocal()) {
ActivityPubReturn::error("This is not a local user.");
ActivityPubReturn::error("This is not a local user.", 403);
}
if (!isset($_GET["page"])) {
@ -75,23 +75,10 @@ class apActorFollowersAction extends ManagedAction
$since = ($page - 1) * PROFILES_PER_MINILIST;
$limit = (($page - 1) == 0 ? 1 : $page) * PROFILES_PER_MINILIST;
/* Fetch Followers */
try {
$sub = $profile->getSubscribers($since, $limit);
} catch (NoResultException $e) {
// Just let the exception go on its merry way
}
/* Calculate total items */
$total_subs = $profile->subscriberCount();
$total_pages = ceil($total_subs / PROFILES_PER_MINILIST);
/* Get followers' URLs */
$subs = array();
while ($sub->fetch()) {
$subs[] = ActivityPubPlugin::actor_uri($sub);
}
$res = [
'@context' => [
"https://www.w3.org/ns/activitystreams",
@ -99,13 +86,13 @@ class apActorFollowersAction extends ManagedAction
],
'id' => common_local_url('apActorFollowers', ['id' => $profile_id]).(($page != 0) ? '?page='.$page : ''),
'type' => ($page == 0 ? 'OrderedCollection' : 'OrderedCollectionPage'),
'totalItems' => $total_subs,
'orderedItems' => $subs
'totalItems' => $total_subs
];
if ($page == 0) {
$res['first'] = common_local_url('apActorFollowers', ['id' => $profile_id]).'?page=1';
} else {
$res['orderedItems'] = $this->generate_followers($profile, $since, $limit);
$res['partOf'] = common_local_url('apActorFollowers', ['id' => $profile_id]);
if ($page+1 < $total_pages) {
@ -119,4 +106,30 @@ class apActorFollowersAction extends ManagedAction
ActivityPubReturn::answer($res);
}
/**
* Generates a list of followers for a given profile.
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @param Profile $profile
* @param int32 $since
* @param int32 $limit
* @return Array of URIs
*/
public function generate_followers($profile, $since, $limit)
{
/* Fetch Followers */
try {
$sub = $profile->getSubscribers($since, $limit);
} catch (NoResultException $e) {
// Just let the exception go on its merry way
}
/* Get followers' URLs */
$subs = [];
while ($sub->fetch()) {
$subs[] = ActivityPubPlugin::actor_uri($sub);
}
return $subs;
}
}

View File

@ -59,7 +59,7 @@ class apActorFollowingAction extends ManagedAction
}
if (!$profile->isLocal()) {
ActivityPubReturn::error("This is not a local user.");
ActivityPubReturn::error("This is not a local user.", 403);
}
if (!isset($_GET["page"])) {
@ -75,23 +75,10 @@ class apActorFollowingAction extends ManagedAction
$since = ($page - 1) * PROFILES_PER_MINILIST;
$limit = (($page - 1) == 0 ? 1 : $page) * PROFILES_PER_MINILIST;
/* Fetch Following */
try {
$sub = $profile->getSubscribed($since, $limit);
} catch (NoResultException $e) {
// Just let the exception go on its merry way
}
/* Calculate total items */
$total_subs = $profile->subscriptionCount();
$total_pages = ceil($total_subs / PROFILES_PER_MINILIST);
/* Get followed' URLs */
$subs = array();
while ($sub->fetch()) {
$subs[] = ActivityPubPlugin::actor_uri($sub);
}
$res = [
'@context' => [
"https://www.w3.org/ns/activitystreams",
@ -99,13 +86,13 @@ class apActorFollowingAction extends ManagedAction
],
'id' => common_local_url('apActorFollowing', ['id' => $profile_id]).(($page != 0) ? '?page='.$page : ''),
'type' => ($page == 0 ? 'OrderedCollection' : 'OrderedCollectionPage'),
'totalItems' => $total_subs,
'orderedItems' => $subs
'totalItems' => $total_subs
];
if ($page == 0) {
$res['first'] = common_local_url('apActorFollowing', ['id' => $profile_id]).'?page=1';
} else {
$res['orderedItems'] = $this->generate_following($profile, $since, $limit);
$res['partOf'] = common_local_url('apActorFollowing', ['id' => $profile_id]);
if ($page+1 < $total_pages) {
@ -119,4 +106,30 @@ class apActorFollowingAction extends ManagedAction
ActivityPubReturn::answer($res);
}
/**
* Generates a list of people following given profile.
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @param Profile $profile
* @param int32 $since
* @param int32 $limit
* @return Array of URIs
*/
public function generate_following($profile, $since, $limit)
{
/* Fetch Following */
try {
$sub = $profile->getSubscribed($since, $limit);
} catch (NoResultException $e) {
// Just let the exception go on its merry way
}
/* Get followed' URLs */
$subs = [];
while ($sub->fetch()) {
$subs[] = ActivityPubPlugin::actor_uri($sub);
}
return $subs;
}
}

View File

@ -53,13 +53,13 @@ class apActorLikedAction extends ManagedAction
{
try {
$profile = Profile::getByID($this->trimmed('id'));
$url = ActivityPubPlugin::actor_url($profile);
$profile_id = $profile->getID();
} catch (Exception $e) {
ActivityPubReturn::error('Invalid Actor URI.', 404);
}
if (!$profile->isLocal()) {
ActivityPubReturn::error("This is not a local user.");
ActivityPubReturn::error("This is not a local user.", 403);
}
$limit = intval($this->trimmed('limit'));
@ -75,7 +75,7 @@ class apActorLikedAction extends ManagedAction
$limit = 80;
}
$fave = $this->fetch_faves($profile->getID(), $limit, $since_id, $max_id);
$fave = $this->fetch_faves($profile_id, $limit, $since_id, $max_id);
$faves = array();
while ($fave->fetch()) {
@ -83,17 +83,15 @@ class apActorLikedAction extends ManagedAction
}
$res = [
'@context' => [
"https://www.w3.org/ns/activitystreams",
[
"@language" => "en"
]
],
'id' => "{$url}/liked.json",
'type' => 'OrderedCollection',
'totalItems' => Fave::countByProfile($profile),
'orderedItems' => $faves
];
'@context' => [
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1",
],
'id' => common_local_url('apActorLiked', ['id' => $profile_id]),
'type' => 'OrderedCollection',
'totalItems' => Fave::countByProfile($profile),
'orderedItems' => $faves
];
ActivityPubReturn::answer($res);
}
@ -130,7 +128,7 @@ class apActorLikedAction extends ManagedAction
$user_id,
$limit = 40,
$since_id = null,
$max_id = null
$max_id = null
) {
$fav = new Fave();

132
actions/apactoroutbox.php Normal file
View File

@ -0,0 +1,132 @@
<?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);
}
/**
* Inbox Request 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 apActorOutboxAction extends ManagedAction
{
protected $needLogin = false;
protected $canPost = true;
/**
* Handle the Outbox request
*
* @author Daniel Supernault <danielsupernault@gmail.com>
*/
protected function handle()
{
try {
$profile = Profile::getByID($this->trimmed('id'));
$profile_id = $profile->getID();
} catch (Exception $e) {
ActivityPubReturn::error('Invalid Actor URI.', 404);
}
if (!$profile->isLocal()) {
ActivityPubReturn::error("This is not a local user.", 403);
}
if (!isset($_GET["page"])) {
$page = 0;
} else {
$page = intval($this->trimmed('page'));
}
if ($page < 0) {
ActivityPubReturn::error('Invalid page number.');
}
$since = ($page - 1) * PROFILES_PER_MINILIST;
$limit = (($page - 1) == 0 ? 1 : $page) * PROFILES_PER_MINILIST;
/* Calculate total items */
$total_notes = $profile->noticeCount();
$total_pages = ceil($total_notes / PROFILES_PER_MINILIST);
$res = [
'@context' => [
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1",
],
'id' => common_local_url('apActorOutbox', ['id' => $profile_id]).(($page != 0) ? '?page='.$page : ''),
'type' => ($page == 0 ? 'OrderedCollection' : 'OrderedCollectionPage'),
'totalItems' => $total_notes
];
if ($page == 0) {
$res['first'] = common_local_url('apActorOutbox', ['id' => $profile_id]).'?page=1';
} else {
$res['orderedItems'] = $this->generate_outbox($profile);
$res['partOf'] = common_local_url('apActorOutbox', ['id' => $profile_id]);
if ($page+1 < $total_pages) {
$res['next'] = common_local_url('apActorOutbox', ['id' => $profile_id]).'page='.($page+1 == 1 ? 2 : $page+1);
}
if ($page > 1) {
$res['prev'] = common_local_url('apActorOutbox', ['id' => $profile_id]).'?page='.($page-1 <= 0 ? 1 : $page-1);
}
}
ActivityPubReturn::answer($res);
}
/**
* Generates a list of people following given profile.
*
* @author Daniel Supernault <danielsupernault@gmail.com>
* @param Profile $profile
* @return Array of Notices
*/
public function generate_outbox($profile)
{
/* Fetch Notices */
$notices = [];
$notice = $profile->getNotices();
while ($notice->fetch()) {
$note = $notice;
// TODO: Handle other types
if ($note->object_type == 'http://activitystrea.ms/schema/1.0/note') {
$notices[] = Activitypub_notice::notice_to_array($note);
}
}
return $notices;
}
}

View File

@ -67,7 +67,7 @@ class apActorProfileAction extends ManagedAction
}
if (!$profile->isLocal()) {
ActivityPubReturn::error("This is not a local user.");
ActivityPubReturn::error("This is not a local user.", 403);
}
$res = Activitypub_profile::profile_to_array($profile);

View File

@ -58,7 +58,7 @@ class apNoticeAction extends ManagedAction
}
if (!$notice->isLocal()) {
ActivityPubReturn::error("This is not a local notice.");
ActivityPubReturn::error("This is not a local notice.", 403);
}
$res = Activitypub_notice::notice_to_array($notice);

View File

@ -102,10 +102,11 @@ class Activitypub_profile extends Managed_DataObject
],
'id' => $uri,
'type' => 'Person',
'following' => common_local_url("apActorFollowing", array("id" => $id)),
'followers' => common_local_url("apActorFollowers", array("id" => $id)),
'liked' => common_local_url("apActorLiked", array("id" => $id)),
'inbox' => common_local_url("apInbox", array("id" => $id)),
'following' => common_local_url('apActorFollowing', ['id' => $id]),
'followers' => common_local_url('apActorFollowers', ['id' => $id]),
'liked' => common_local_url('apActorLiked', ['id' => $id]),
'inbox' => common_local_url('apInbox', ['id' => $id]),
'outbox' => common_local_url('apActorOutbox', ['id' => $id]),
'preferredUsername' => $profile->getNickname(),
'name' => $profile->getBestName(),
'summary' => ($desc = $profile->getDescription()) == null ? "" : $desc,