From 9c4dc1a233ce8ec6f741f0341af8f691fdc5d173 Mon Sep 17 00:00:00 2001 From: Diogo Cordeiro Date: Sat, 5 May 2018 00:54:41 +0100 Subject: [PATCH] New Entities added Moved "hard coded" profile from action to an Entity class Added the following entities: - Profile - Attachment - Notice - Tag --- actions/apactorprofile.php | 35 ++-------------- classes/Activitypub_attachment.php | 65 +++++++++++++++++++++++++++++ classes/Activitypub_notice.php | 66 +++++++++++++++++------------ classes/Activitypub_profile.php | 67 ++++++++++++++++++++++++++++++ classes/Activitypub_tag.php | 50 ++++++++++++++++++++++ 5 files changed, 224 insertions(+), 59 deletions(-) create mode 100644 classes/Activitypub_attachment.php create mode 100644 classes/Activitypub_profile.php create mode 100644 classes/Activitypub_tag.php diff --git a/actions/apactorprofile.php b/actions/apactorprofile.php index 28abcd6..b89de7c 100644 --- a/actions/apactorprofile.php +++ b/actions/apactorprofile.php @@ -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)); } } diff --git a/classes/Activitypub_attachment.php b/classes/Activitypub_attachment.php new file mode 100644 index 0000000..1473ff1 --- /dev/null +++ b/classes/Activitypub_attachment.php @@ -0,0 +1,65 @@ +. + * + * @category Plugin + * @package GNUsocial + * @author Diogo Cordeiro + * @author Daniel Supernault + * @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; + } +} diff --git a/classes/Activitypub_notice.php b/classes/Activitypub_notice.php index eb25b7d..7e75108 100644 --- a/classes/Activitypub_notice.php +++ b/classes/Activitypub_notice.php @@ -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); } -} \ No newline at end of file + + $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; + } +} diff --git a/classes/Activitypub_profile.php b/classes/Activitypub_profile.php new file mode 100644 index 0000000..bbd560d --- /dev/null +++ b/classes/Activitypub_profile.php @@ -0,0 +1,67 @@ +. + * + * @category Plugin + * @package GNUsocial + * @author Diogo Cordeiro + * @author Daniel Supernault + * @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; + } +} diff --git a/classes/Activitypub_tag.php b/classes/Activitypub_tag.php new file mode 100644 index 0000000..24255c0 --- /dev/null +++ b/classes/Activitypub_tag.php @@ -0,0 +1,50 @@ +. + * + * @category Plugin + * @package GNUsocial + * @author Diogo Cordeiro + * @author Daniel Supernault + * @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; + } +}