New Entities added

Moved "hard coded" profile from action to an Entity class
Added the following entities:
- Profile
- Attachment
- Notice
- Tag
This commit is contained in:
Diogo Cordeiro 2018-05-05 00:54:41 +01:00
parent 2889b1ba7e
commit 9c4dc1a233
5 changed files with 224 additions and 59 deletions

View File

@ -41,43 +41,14 @@ class apActorProfileAction extends ManagedAction
try {
$user = User::getByNickname($nickname);
$profile = $user->getProfile();
$url = $profile->profileurl;
} catch (Exception $e) {
throw new \Exception('Invalid username');
}
$avatar_url = $profile->avatarUrl(AVATAR_PROFILE_SIZE);
$res = [
'@context' => [
"https://www.w3.org/ns/activitystreams",
[
"@language" => "en"
]
],
'id' => $user->getID(),
'type' => 'Person',
'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",
'following' => "{$url}/subscriptions",
'liked' => "{$url}/liked.json",
'liked_count' => Fave::countByProfile ($profile),
'summary' => $profile->bio,
'url' => $url,
'avatar' => [
'type' => 'Image',
'width' => 96,
'height' => 96,
'url' => $avatar_url
]
];
header('Content-Type: application/json');
echo json_encode($res, isset($_GET["pretty"]) ? JSON_PRETTY_PRINT : null);
$res = Activitypub_profile::profileToObject($profile);
echo json_encode($res, JSON_UNESCAPED_SLASHES | (isset($_GET["pretty"]) ? JSON_PRETTY_PRINT : null));
}
}

View File

@ -0,0 +1,65 @@
<?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 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
* @link https://gnu.io/social
*/
if (!defined('GNUSOCIAL')) { exit(1); }
class Activitypub_attachment extends Managed_DataObject
{
public static function attachmentToObject($attachment)
{
$res = [
'@context' => [
"https://www.w3.org/ns/activitystreams",
[
"@language" => "en"
]
],
'id' => $attachment->getID (),
'mimetype' => $attachment->mimetype,
'url' => $attachment->getUrl (),
'text_url' => $attachment->isLocal () ? common_shorten_links($attachment->getUrl (), true): null,
'size' => $attachment->getSize (),
'title' => $attachment->getTitle (),
'meta' => null
];
// Image
if (substr($res["mimetype"], 0, 5) == "image")
{
$res["meta"]= [
'width' => $attachment->width,
'height' => $attachment->height
];
}
return $res;
}
}

View File

@ -33,32 +33,44 @@ if (!defined('GNUSOCIAL')) { exit(1); }
class Activitypub_notice extends Managed_DataObject
{
public static function noticeToObject($notice)
public static function noticeToObject($notice)
{
$attachments = [];
foreach ($notice->attachments () as $attachment)
{
// 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;
$attachments[] = Activitypub_attachment::attachmentToObject ($attachment);
}
}
$tags = [];
foreach ($notice->getTags () as $tag)
{
$tags[] = Activitypub_tag::tagNameToObject ($tag);
}
// todo: fix timestamp formats
$item = [
'id' => $notice->getUrl(),
'type' => 'Notice', // TODO: handle other types
'actor' => $notice->getProfile()->getUrl(),
'published' => $notice->getCreated (),
'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->isLocal(),
'conversation' => $notice->conversation,
'attachment' => $attachments,
'tag' => $tags
];
return $item;
}
}

View File

@ -0,0 +1,67 @@
<?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 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
* @link https://gnu.io/social
*/
if (!defined('GNUSOCIAL')) { exit(1); }
class Activitypub_profile extends Managed_DataObject
{
public static function profileToObject($profile)
{
$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",
'outbox' => "{$url}/outbox.json",
'display_name' => $profile->getFullname(),
'followers' => "{$url}/subscribers",
'following' => "{$url}/subscriptions",
'liked' => "{$url}/liked.json",
'liked_count' => Fave::countByProfile ($profile),
'summary' => $profile->getDescription(),
'url' => $profile->getURL(),
'avatar' => [
'type' => 'Image',
'width' => 96,
'height' => 96,
'url' => $profile->avatarUrl(AVATAR_PROFILE_SIZE)
]
];
return $res;
}
}

View File

@ -0,0 +1,50 @@
<?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 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
* @link https://gnu.io/social
*/
if (!defined('GNUSOCIAL')) { exit(1); }
class Activitypub_tag extends Managed_DataObject
{
public static function tagNameToObject($tag)
{
$res = [
'@context' => [
"https://www.w3.org/ns/activitystreams",
[
"@language" => "en"
]
],
'name' => $tag,
'url' => common_local_url('tag', array('tag' => $tag))
];
return $res;
}
}