[TESTS] Added unit tests

This commit is contained in:
Daniel
2020-10-16 01:07:01 +01:00
committed by Hugo Sales
parent d53fef09a8
commit 95f95d2dd8
29 changed files with 716 additions and 477 deletions

View File

@@ -18,12 +18,13 @@
* ActivityPub implementation for GNU social
*
* @package GNUsocial
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @link http://www.gnu.org/software/social/
*
* @see http://www.gnu.org/software/social/
*/
defined('GNUSOCIAL') || die();
/**
@@ -31,6 +32,7 @@ defined('GNUSOCIAL') || die();
*
* @category Plugin
* @package GNUsocial
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
@@ -40,17 +42,19 @@ class Activitypub_accept
* Generates an ActivityPub representation of a Accept
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
*
* @param array $object
*
* @return array pretty array to be used in a response
*/
public static function accept_to_array($object)
{
$res = [
'@context' => 'https://www.w3.org/ns/activitystreams',
'id' => common_root_url().'accept_follow_from_'.urlencode($object['actor']).'_to_'.urlencode($object['object']),
'type' => 'Accept',
'actor' => $object['object'],
'object' => $object
'id' => common_root_url() . 'accept_follow_from_' . urlencode($object['actor']) . '_to_' . urlencode($object['object']),
'type' => 'Accept',
'actor' => $object['object'],
'object' => $object,
];
return $res;
}
@@ -59,8 +63,11 @@ class Activitypub_accept
* Verifies if a given object is acceptable for an Accept Activity.
*
* @param array $object
* @return bool
*
* @throws Exception
*
* @return bool
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
*/
public static function validate_object($object)
@@ -75,7 +82,7 @@ class Activitypub_accept
case 'Follow':
// Validate data
if (!filter_var($object['object'], FILTER_VALIDATE_URL)) {
throw new Exception("Object is not a valid Object URI for Activity.");
throw new Exception('Object is not a valid Object URI for Activity.');
}
break;
default:

View File

@@ -18,12 +18,13 @@
* ActivityPub implementation for GNU social
*
* @package GNUsocial
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @link http://www.gnu.org/software/social/
*
* @see http://www.gnu.org/software/social/
*/
defined('GNUSOCIAL') || die();
/**
@@ -31,6 +32,7 @@ defined('GNUSOCIAL') || die();
*
* @category Plugin
* @package GNUsocial
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
@@ -40,31 +42,30 @@ class Activitypub_announce
* Generates an ActivityPub representation of a Announce
*
* @param Profile $actor
* @param Notice $notice
* @param Notice $repeat_of
* @param Notice $notice
*
* @return array pretty array to be used in a response
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
*/
public static function announce_to_array(
Profile $actor,
Notice $notice,
Notice $repeat_of
): array {
$actor_uri = $actor->getUri();
public static function announce_to_array(Profile $actor, Notice $notice): array
{
$actor_uri = $actor->getUri();
$notice_url = Activitypub_notice::getUrl($notice);
$to = [common_local_url('apActorFollowers', ['id' => $actor->getID()])];
foreach ($notice->getAttentionProfiles() as $to_profile) {
$to[] = $to_profile->getUri();
}
$cc[]= 'https://www.w3.org/ns/activitystreams#Public';
$cc[] = 'https://www.w3.org/ns/activitystreams#Public';
$res = [
'@context' => 'https://www.w3.org/ns/activitystreams',
'id' => Activitypub_notice::getUri($notice),
'id' => common_root_url() . 'share_from_' . urlencode($actor_uri) . '_to_' . urlencode($notice_url),
'type' => 'Announce',
'actor' => $actor_uri,
'object' => Activitypub_notice::getUri($repeat_of),
'object' => $notice_url,
'to' => $to,
'cc' => $cc,
];

View File

@@ -18,12 +18,13 @@
* ActivityPub implementation for GNU social
*
* @package GNUsocial
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @link http://www.gnu.org/software/social/
*
* @see http://www.gnu.org/software/social/
*/
defined('GNUSOCIAL') || die();
/**
@@ -31,6 +32,7 @@ defined('GNUSOCIAL') || die();
*
* @category Plugin
* @package GNUsocial
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
@@ -40,13 +42,15 @@ class Activitypub_attachment
* Generates a pretty array from an Attachment object
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
*
* @param Attachment $attachment
*
* @return array pretty array to be used in a response
*/
public static function attachment_to_array($attachment)
{
$res = [
'@context' => 'https://www.w3.org/ns/activitystreams',
'@context' => 'https://www.w3.org/ns/activitystreams',
'type' => 'Document',
'mediaType' => $attachment->mimetype,
'url' => $attachment->getUrl(),
@@ -55,10 +59,10 @@ class Activitypub_attachment
];
// Image
if (substr($res["mediaType"], 0, 5) == "image") {
$res["meta"]= [
if (substr($res['mediaType'], 0, 5) == 'image') {
$res['meta'] = [
'width' => $attachment->width,
'height' => $attachment->height
'height' => $attachment->height,
];
}

View File

@@ -18,12 +18,13 @@
* ActivityPub implementation for GNU social
*
* @package GNUsocial
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @link http://www.gnu.org/software/social/
*
* @see http://www.gnu.org/software/social/
*/
defined('GNUSOCIAL') || die();
/**
@@ -31,6 +32,7 @@ defined('GNUSOCIAL') || die();
*
* @category Plugin
* @package GNUsocial
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
@@ -40,23 +42,24 @@ class Activitypub_create
* Generates an ActivityPub representation of a Create
*
* @param string $actor
* @param string $uri
* @param mixed $object
* @param bool $directMessage whether it is a private Create activity or not
* @param array $object
* @param bool $directMessage whether it is a private Create activity or not
*
* @return array pretty array to be used in a response
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
*/
public static function create_to_array(string $actor, string $uri, $object, bool $directMessage = false): array
{
$res = [
'@context' => 'https://www.w3.org/ns/activitystreams',
'id' => $uri,
'type' => 'Create',
'@context' => 'https://www.w3.org/ns/activitystreams',
'id' => $object['id'] . '/create',
'type' => 'Create',
'directMessage' => $directMessage,
'to' => $object['to'],
'cc' => $object['cc'],
'actor' => $actor,
'object' => $object
'to' => $object['to'],
'cc' => $object['cc'],
'actor' => $actor,
'object' => $object,
];
return $res;
}
@@ -65,8 +68,11 @@ class Activitypub_create
* Verifies if a given object is acceptable for a Create Activity.
*
* @param array $object
* @return bool True if acceptable, false if valid but unsupported
*
* @throws Exception if invalid
*
* @return bool True if acceptable, false if valid but unsupported
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
*/
public static function validate_object($object): bool
@@ -100,7 +106,9 @@ class Activitypub_create
* https://github.com/w3c/activitypub/issues/196#issuecomment-304958984
*
* @param array $activity received Create-Note activity
*
* @return bool true if note is private, false otherwise
*
* @author Bruno casteleiro <brunoccast@fc.up.pt>
*/
public static function isPrivateNote(array $activity): bool

View File

@@ -18,12 +18,13 @@
* ActivityPub implementation for GNU social
*
* @package GNUsocial
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @link http://www.gnu.org/software/social/
*
* @see http://www.gnu.org/software/social/
*/
defined('GNUSOCIAL') || die();
/**
@@ -31,6 +32,7 @@ defined('GNUSOCIAL') || die();
*
* @category Plugin
* @package GNUsocial
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
@@ -39,35 +41,35 @@ class Activitypub_delete
/**
* Generates an ActivityStreams 2.0 representation of a Delete
*
* @param Notice $object
* @param string $actor actor URI
* @param string $object object URI
*
* @return array pretty array to be used in a response
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
*/
public static function delete_to_array($object): array
{
if ($object instanceof Notice) {
return Activitypub_notice::notice_to_array($object);
} else if ($object instanceof Profile) {
$actor_uri = $object->getUri();
return [
'@context' => 'https://www.w3.org/ns/activitystreams',
'id' => $actor_uri . '#delete',
'type' => 'Delete',
'to' => ['https://www.w3.org/ns/activitystreams#Public'],
'actor' => $actor_uri,
'object' => $object
];
} else {
throw new InvalidArgumentException();
}
$res = [
'@context' => 'https://www.w3.org/ns/activitystreams',
'id' => $object . '/delete',
'type' => 'Delete',
'to' => ['https://www.w3.org/ns/activitystreams#Public'],
'actor' => $actor,
'object' => $object,
];
return $res;
}
/**
* Verifies if a given object is acceptable for a Delete Activity.
*
* @param array|string $object
* @return bool
*
* @throws Exception
*
* @return bool
*
* @author Bruno Casteleiro <brunoccast@fc.up.pt>
*/
public static function validate_object($object): bool
@@ -80,7 +82,7 @@ class Activitypub_delete
if (!isset($object['type'])) {
throw new Exception('Object type was not specified for Delete Activity.');
}
if ($object['type'] !== "Tombstone" && $object['type'] !== "Person") {
if ($object['type'] !== 'Tombstone' && $object['type'] !== 'Person') {
throw new Exception('Invalid Object type for Delete Activity.');
}
if (!isset($object['id'])) {

View File

@@ -18,12 +18,13 @@
* ActivityPub implementation for GNU social
*
* @package GNUsocial
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @link http://www.gnu.org/software/social/
*
* @see http://www.gnu.org/software/social/
*/
defined('GNUSOCIAL') || die();
/**
@@ -31,6 +32,7 @@ defined('GNUSOCIAL') || die();
*
* @category Plugin
* @package GNUsocial
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
@@ -40,13 +42,15 @@ class Activitypub_error
* Generates a pretty error from a string
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
*
* @param string $m
*
* @return array pretty array to be used in a response
*/
public static function error_message_to_array(string $m): array
{
return [
'error'=> $m
$res = [
'error' => $m,
];
}
}

View File

@@ -18,12 +18,13 @@
* ActivityPub implementation for GNU social
*
* @package GNUsocial
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @link http://www.gnu.org/software/social/
*
* @see http://www.gnu.org/software/social/
*/
defined('GNUSOCIAL') || die();
/**
@@ -31,6 +32,7 @@ defined('GNUSOCIAL') || die();
*
* @category Plugin
* @package GNUsocial
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
@@ -40,24 +42,26 @@ class Activitypub_follow
* Generates an ActivityPub representation of a subscription
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @param string $actor
* @param string $object
* @param string|null $id Activity id, to be used when generating for an Accept Activity
*
* @param string $actor
* @param string $object
* @param null|string $id Activity id, to be used when generating for an Accept Activity
*
* @return array pretty array to be used in a response
*/
public static function follow_to_array(string $actor, string $object, ?string $id = null): array
{
if ($id === null) {
$id = common_root_url().'follow_from_'.urlencode($actor).'_to_'.urlencode($object);
$id = common_root_url() . 'follow_from_' . urlencode($actor) . '_to_' . urlencode($object);
}
$res = [
'@context' => 'https://www.w3.org/ns/activitystreams',
'id' => $id,
'type' => 'Follow',
'actor' => $actor,
'object' => $object
];
'id' => $id,
'type' => 'Follow',
'actor' => $actor,
'object' => $object,
];
return $res;
}
@@ -65,12 +69,14 @@ class Activitypub_follow
* Handles a Follow Activity received by our inbox.
*
* @param Profile $actor_profile Remote Actor
* @param string $object Local Actor
* @param string $id Activity id
* @param string $object Local Actor
* @param string $id Activity id
*
* @throws AlreadyFulfilledException
* @throws HTTP_Request2_Exception
* @throws NoProfileException
* @throws ServerException
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
*/
public static function follow(Profile $actor_profile, string $object, string $id)
@@ -85,13 +91,13 @@ class Activitypub_follow
if (!Subscription::exists($actor_profile, $object_profile)) {
Subscription::start($actor_profile, $object_profile);
Activitypub_profile::subscribeCacheUpdate($actor_profile, $object_profile);
common_debug('ActivityPubPlugin: Accepted Follow request from '.$actor_profile->getUri().' to '.$object);
common_debug('ActivityPubPlugin: Accepted Follow request from ' . $actor_profile->getUri() . ' to ' . $object);
} else {
common_debug('ActivityPubPlugin: Received a repeated Follow request from '.$actor_profile->getUri().' to '.$object);
common_debug('ActivityPubPlugin: Received a repeated Follow request from ' . $actor_profile->getUri() . ' to ' . $object);
}
// Notify remote instance that we have accepted their request
common_debug('ActivityPubPlugin: Notifying remote instance that we have accepted their Follow request request from '.$actor_profile->getUri().' to '.$object);
common_debug('ActivityPubPlugin: Notifying remote instance that we have accepted their Follow request request from ' . $actor_profile->getUri() . ' to ' . $object);
$postman = new Activitypub_postman($object_profile, [$actor_aprofile]);
$postman->accept_follow($id);
}

View File

@@ -18,12 +18,13 @@
* ActivityPub implementation for GNU social
*
* @package GNUsocial
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @link http://www.gnu.org/software/social/
*
* @see http://www.gnu.org/software/social/
*/
defined('GNUSOCIAL') || die();
/**
@@ -31,6 +32,7 @@ defined('GNUSOCIAL') || die();
*
* @category Plugin
* @package GNUsocial
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
@@ -39,8 +41,11 @@ class Activitypub_like
/**
* Generates an ActivityPub representation of a Like
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
*
* @param string $actor Actor URI
* @param Notice $notice Notice URI
* @param string $object Notice URI
*
* @return array pretty array to be used in a response
* @author Diogo Cordeiro <diogo@fc.up.pt>
*/
@@ -48,10 +53,10 @@ class Activitypub_like
{
$res = [
'@context' => 'https://www.w3.org/ns/activitystreams',
'id' => Activitypub_notice::getUri($notice),
'id' => common_root_url() . 'like_from_' . urlencode($actor) . '_to_' . urlencode($object),
'type' => 'Like',
'actor' => $actor,
'object' => Activitypub_notice::getUri($notice->getParent()),
'object' => $object,
];
return $res;
}

View File

@@ -18,12 +18,13 @@
* ActivityPub implementation for GNU social
*
* @package GNUsocial
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @link http://www.gnu.org/software/social/
*
* @see http://www.gnu.org/software/social/
*/
defined('GNUSOCIAL') || die();
/**
@@ -31,6 +32,7 @@ defined('GNUSOCIAL') || die();
*
* @category Plugin
* @package GNUsocial
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
@@ -40,18 +42,20 @@ class Activitypub_mention_tag
* Generates an ActivityPub representation of a Mention Tag
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
*
* @param string $href Actor Uri
* @param string $name Mention name
*
* @return array pretty array to be used in a response
*/
public static function mention_tag_to_array_from_values(string $href, string $name): array
{
$res = [
'@context' => 'https://www.w3.org/ns/activitystreams',
"type" => "Mention",
"href" => $href,
"name" => $name
];
'type' => 'Mention',
'href' => $href,
'name' => $name,
];
return $res;
}
}

View File

@@ -18,11 +18,11 @@
* ActivityPub implementation for GNU social
*
* @package GNUsocial
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
defined('GNUSOCIAL') || die();
/**
@@ -38,7 +38,9 @@ class Activitypub_message
* Generates a pretty message from a Notice object
*
* @param Notice $message
*
* @return array array to be used in a response
*
* @author Bruno Casteleiro <brunoccast@fc.up.pt>
*/
public static function message_to_array(Notice $message): array
@@ -54,21 +56,21 @@ class Activitypub_message
$to = [];
foreach ($message->getAttentionProfiles() as $to_profile) {
$to[] = $href = $to_profile->getUri();
$tags[] = Activitypub_mention_tag::mention_tag_to_array_from_values($href, $to_profile->getNickname().'@'.parse_url($href, PHP_URL_HOST));
$to[] = $href = $to_profile->getUri();
$tags[] = Activitypub_mention_tag::mention_tag_to_array_from_values($href, $to_profile->getNickname() . '@' . parse_url($href, PHP_URL_HOST));
}
$item = [
'@context' => 'https://www.w3.org/ns/activitystreams',
'id' => common_local_url('showmessage', ['message' => $message->getID()]),
'type' => 'Note',
'published' => str_replace(' ', 'T', $message->created).'Z',
'attributedTo' => $from->getUri(),
'to' => $to,
'cc' => [],
'content' => $message->getRendered(),
'attachment' => [],
'tag' => $tags
'@context' => 'https://www.w3.org/ns/activitystreams',
'id' => common_local_url('showmessage', ['message' => $message->getID()]),
'type' => 'Note',
'published' => str_replace(' ', 'T', $message->created) . 'Z',
'attributedTo' => $from->getUri(),
'to' => $to,
'cc' => [],
'content' => $message->getRendered(),
'attachment' => [],
'tag' => $tags,
];
return $item;
@@ -79,10 +81,13 @@ class Activitypub_message
* Returns created Notice.
*
* @author Bruno Casteleiro <brunoccast@fc.up.pt>
* @param array $object
*
* @param array $object
* @param Profile $actor_profile
* @return Notice
*
* @throws Exception
*
* @return Notice
*/
public static function create_message(array $object, Profile $actor_profile = null): Notice
{

View File

@@ -18,12 +18,13 @@
* ActivityPub implementation for GNU social
*
* @package GNUsocial
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @link http://www.gnu.org/software/social/
*
* @see http://www.gnu.org/software/social/
*/
defined('GNUSOCIAL') || die();
/**
@@ -31,6 +32,7 @@ defined('GNUSOCIAL') || die();
*
* @category Plugin
* @package GNUsocial
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
@@ -40,16 +42,19 @@ class Activitypub_notice
* Generates a pretty notice from a Notice object
*
* @param Notice $notice
* @return array array to be used in a response
*
* @throws EmptyPkeyValueException
* @throws InvalidUrlException
* @throws ServerException
* @throws Exception
*
* @return array array to be used in a response
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
*/
public static function notice_to_array(Notice $notice): array
{
$profile = $notice->getProfile();
$profile = $notice->getProfile();
$attachments = [];
foreach ($notice->attachments() as $attachment) {
$attachments[] = Activitypub_attachment::attachment_to_array($attachment);
@@ -57,7 +62,7 @@ class Activitypub_notice
$tags = [];
foreach ($notice->getTags() as $tag) {
if ($tag != "") { // Hacky workaround to avoid stupid outputs
if ($tag != '') { // Hacky workaround to avoid stupid outputs
$tags[] = Activitypub_tag::tag_to_array($tag);
}
}
@@ -75,46 +80,25 @@ class Activitypub_notice
}
foreach ($notice->getAttentionProfiles() as $to_profile) {
$to[] = $href = $to_profile->getUri();
$to[] = $href = $to_profile->getUri();
$tags[] = Activitypub_mention_tag::mention_tag_to_array_from_values($href, $to_profile->getNickname() . '@' . parse_url($href, PHP_URL_HOST));
}
if (ActivityUtils::compareVerbs($notice->getVerb(), ActivityVerb::DELETE)) {
$item = [
'@context' => 'https://www.w3.org/ns/activitystreams',
'id' => self::getUri($notice),
'type' => 'Delete',
// XXX: A bit of ugly code here
'object' => array_merge(Activitypub_tombstone::tombstone_to_array((int)substr(explode(':', $notice->getUri())[2], 9)), ['deleted' => str_replace(' ', 'T', $notice->getCreated()) . 'Z']),
'url' => $notice->getUrl(),
'actor' => $profile->getUri(),
'to' => $to,
'cc' => $cc,
'conversationId' => $notice->getConversationUrl(false),
'conversationUrl' => $notice->getConversationUrl(),
'content' => $notice->getRendered(),
'isLocal' => $notice->isLocal(),
'attachment' => $attachments,
'tag' => $tags
];
} else { // Note
$item = [
'@context' => 'https://www.w3.org/ns/activitystreams',
'id' => self::note_uri($notice->getID()),
'type' => 'Note',
'published' => str_replace(' ', 'T', $notice->getCreated()) . 'Z',
'url' => $notice->getUrl(),
'attributedTo' => $profile->getUri(),
'to' => $to,
'cc' => $cc,
'conversationId' => $notice->getConversationUrl(false),
'conversationUrl' => $notice->getConversationUrl(),
'content' => $notice->getRendered(),
'isLocal' => $notice->isLocal(),
'attachment' => $attachments,
'tag' => $tags
];
}
$item = [
'@context' => 'https://www.w3.org/ns/activitystreams',
'id' => self::getUrl($notice),
'type' => 'Note',
'published' => str_replace(' ', 'T', $notice->getCreated()) . 'Z',
'url' => self::getUrl($notice),
'attributedTo' => $profile->getUri(),
'to' => $to,
'cc' => $cc,
'conversation' => $notice->getConversationUrl(),
'content' => $notice->getRendered(),
'isLocal' => $notice->isLocal(),
'attachment' => $attachments,
'tag' => $tags,
];
// Is this a reply?
if (!empty($notice->reply_to)) {
@@ -123,8 +107,8 @@ class Activitypub_notice
// Do we have a location for this notice?
try {
$location = Notice_location::locFromStored($notice);
$item['latitude'] = $location->lat;
$location = Notice_location::locFromStored($notice);
$item['latitude'] = $location->lat;
$item['longitude'] = $location->lon;
} catch (Exception $e) {
// Apparently no.
@@ -137,17 +121,20 @@ class Activitypub_notice
* Create a Notice via ActivityPub Note Object.
* Returns created Notice.
*
* @param array $object
* @param array $object
* @param Profile $actor_profile
* @param bool $directMessage
* @return Notice
* @param bool $directMessage
*
* @throws Exception
*
* @return Notice
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
*/
public static function create_notice(array $object, Profile $actor_profile, bool $directMessage = false): Notice
{
$id = $object['id']; // int
$url = isset($object['url']) ? $object['url'] : $id; // string
$id = $object['id']; // int
$url = isset($object['url']) ? $object['url'] : $id; // string
$content = $object['content']; // string
// possible keys: ['inReplyTo', 'latitude', 'longitude']
@@ -162,15 +149,15 @@ class Activitypub_notice
$settings['longitude'] = $object['longitude'];
}
$act = new Activity();
$act->verb = ActivityVerb::POST;
$act->time = time();
$act->actor = $actor_profile->asActivityObject();
$act = new Activity();
$act->verb = ActivityVerb::POST;
$act->time = time();
$act->actor = $actor_profile->asActivityObject();
$act->context = new ActivityContext();
$options = ['source' => 'ActivityPub',
'uri' => $id,
'url' => $url,
'is_local' => self::getNotePolicyType($object, $actor_profile)];
$options = ['source' => 'ActivityPub',
'uri' => $id,
'url' => $url,
'is_local' => self::getNotePolicyType($object, $actor_profile), ];
if ($directMessage) {
$options['scope'] = Notice::MESSAGE_SCOPE;
@@ -179,8 +166,8 @@ class Activitypub_notice
// Is this a reply?
if (isset($settings['inReplyTo'])) {
try {
$inReplyTo = ActivityPubPlugin::grab_notice_from_url($settings['inReplyTo']);
$act->context->replyToID = $inReplyTo->getUri();
$inReplyTo = ActivityPubPlugin::grab_notice_from_url($settings['inReplyTo']);
$act->context->replyToID = $inReplyTo->getUri();
$act->context->replyToUrl = $inReplyTo->getUrl();
} catch (Exception $e) {
// It failed to grab, maybe we got this note from another source
@@ -203,7 +190,7 @@ class Activitypub_notice
}
}
$mentions_profiles = [];
$discovery = new Activitypub_explorer;
$discovery = new Activitypub_explorer;
foreach ($mentions as $mention) {
try {
$mentioned_profile = $discovery->lookup($mention);
@@ -240,32 +227,10 @@ class Activitypub_notice
&& $attachment['type'] === 'Document'
&& array_key_exists('url', $attachment)) {
try {
$file = new File();
$file->url = $attachment['url'];
$file->title = array_key_exists('type', $attachment) ? $attachment['name'] : null;
if (array_key_exists('type', $attachment)) {
$file->mimetype = $attachment['mediaType'];
} else {
$http = new HTTPClient();
common_debug(
'Performing HEAD request for incoming activity '
. 'to avoid unnecessarily downloading too '
. 'large files. URL: ' . $file->url
);
$head = $http->head($file->url);
$headers = $head->getHeader();
$headers = array_change_key_case($headers, CASE_LOWER);
if (array_key_exists('content-type', $headers)) {
$file->mimetype = $headers['content-type'];
} else {
continue;
}
if (array_key_exists('content-length', $headers)) {
$file->size = $headers['content-length'];
}
}
$file->saveFile();
$attachments[] = $file;
// throws exception on failure
$attachment = MediaFile::fromUrl($attachment['url'], $actor_profile, $attachment['name']);
$act->enclosures[] = $attachment->getEnclosure();
$attachments[] = $attachment;
} catch (Exception $e) {
// Whatever.
continue;
@@ -274,9 +239,9 @@ class Activitypub_notice
}
}
$actobj = new ActivityObject();
$actobj->type = ActivityObject::NOTE;
$actobj->content = strip_tags($content, '<p><b><i><u><a><ul><ol><li><br>');
$actobj = new ActivityObject();
$actobj->type = ActivityObject::NOTE;
$actobj->content = strip_tags($content, '<p><b><i><u><a><ul><ol><li>');
// Finally add the activity object to our activity
$act->objects[] = $actobj;
@@ -284,8 +249,8 @@ class Activitypub_notice
$note = Notice::saveActivity($act, $actor_profile, $options);
// Attachments (last part)
foreach ($attachments as $file) {
File_to_post::processNew($file, $note);
foreach ($attachments as $attachment) {
$attachment->attachToNotice($note);
}
return $note;
@@ -295,8 +260,11 @@ class Activitypub_notice
* Validates a note.
*
* @param array $object
* @return bool false if unacceptable for GS but valid ActivityPub object
*
* @throws Exception if invalid ActivityPub object
*
* @return bool false if unacceptable for GS but valid ActivityPub object
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
*/
public static function validate_note(array $object): bool
@@ -316,7 +284,7 @@ class Activitypub_notice
common_debug('ActivityPub Notice Validator: Rejected because Object URL is invalid.');
throw new Exception('Invalid Object URL.');
}
if (!(isset($object['to']) && isset($object['cc']))) {
if (!(isset($object['to'], $object['cc']))) {
common_debug('ActivityPub Notice Validator: Rejected because either Object CC or TO wasn\'t specified.');
throw new Exception('Either Object CC or TO wasn\'t specified.');
}
@@ -331,9 +299,12 @@ class Activitypub_notice
* Get the original representation URL of a given notice.
*
* @param Notice $notice notice from which to retrieve the URL
* @return string URL
*
* @throws InvalidUrlException
* @throws Exception
*
* @return string URL
*
* @author Bruno Casteleiro <brunoccast@fc.up.pt>
* @see note_uri when it's not a generic activity but a object type note
*/
@@ -362,9 +333,11 @@ class Activitypub_notice
/**
* Extract note policy type from note targets.
*
* @param array $note received Note
* @param array $note received Note
* @param Profile $actor_profile Note author
*
* @return int Notice policy type
*
* @author Bruno Casteleiro <brunoccast@fc.up.pt>
*/
public static function getNotePolicyType(array $note, Profile $actor_profile): int

View File

@@ -18,12 +18,13 @@
* ActivityPub implementation for GNU social
*
* @package GNUsocial
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @link http://www.gnu.org/software/social/
*
* @see http://www.gnu.org/software/social/
*/
defined('GNUSOCIAL') || die();
/**
@@ -31,6 +32,7 @@ defined('GNUSOCIAL') || die();
*
* @category Plugin
* @package GNUsocial
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
@@ -40,15 +42,17 @@ class Activitypub_reject
* Generates an ActivityPub representation of a Reject
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
*
* @param array $object
*
* @return array pretty array to be used in a response
*/
public static function reject_to_array(array $object): array
{
$res = [
'@context' => 'https://www.w3.org/ns/activitystreams',
'type' => 'Reject',
'object' => $object
'@context' => 'https://www.w3.org/ns/activitystreams',
'type' => 'Reject',
'object' => $object,
];
return $res;
}

View File

@@ -18,12 +18,13 @@
* ActivityPub implementation for GNU social
*
* @package GNUsocial
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @link http://www.gnu.org/software/social/
*
* @see http://www.gnu.org/software/social/
*/
defined('GNUSOCIAL') || die();
/**
@@ -31,6 +32,7 @@ defined('GNUSOCIAL') || die();
*
* @category Plugin
* @package GNUsocial
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
@@ -40,15 +42,17 @@ class Activitypub_tag
* Generates a pretty tag from a Tag object
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @param string $tag
*
* @param array Tag $tag
*
* @return array pretty array to be used in a response
*/
public static function tag_to_array(string $tag): array
{
$res = [
'@context' => 'https://www.w3.org/ns/activitystreams',
'name' => $tag,
'url' => common_local_url('tag', ['tag' => $tag]),
'name' => $tag,
'url' => common_local_url('tag', ['tag' => $tag]),
];
return $res;
}

View File

@@ -18,12 +18,13 @@
* ActivityPub implementation for GNU social
*
* @package GNUsocial
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @link http://www.gnu.org/software/social/
*
* @see http://www.gnu.org/software/social/
*/
defined('GNUSOCIAL') || die();
/**
@@ -31,6 +32,7 @@ defined('GNUSOCIAL') || die();
*
* @category Plugin
* @package GNUsocial
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
@@ -40,17 +42,19 @@ class Activitypub_undo
* Generates an ActivityPub representation of a Undo
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
*
* @param array $object
*
* @return array pretty array to be used in a response
*/
public static function undo_to_array(array $object): array
{
$res = [
'@context' => 'https://www.w3.org/ns/activitystreams',
'id' => $object['id'] . '#undo',
'type' => 'Undo',
'actor' => $object['actor'],
'object' => $object
'id' => $object['id'] . '/undo',
'type' => 'Undo',
'actor' => $object['actor'],
'object' => $object,
];
return $res;
}
@@ -59,8 +63,11 @@ class Activitypub_undo
* Verifies if a given object is acceptable for a Undo Activity.
*
* @param array $object
* @return bool
*
* @throws Exception
*
* @return bool
*
* @author Diogo Cordeiro <diogo@fc.up.pt>
*/
public static function validate_object(array $object): bool