From 287ebeb52143287115fb0e463bc04c8cc7053e65 Mon Sep 17 00:00:00 2001 From: Diogo Cordeiro Date: Mon, 6 Aug 2018 08:09:25 +0100 Subject: [PATCH] Beginning of OStatus support, various bug fixes --- ActivityPubPlugin.php | 69 ++++++++++++++++----------------- actions/apactorfollowers.php | 3 +- classes/Activitypub_notice.php | 4 +- classes/Activitypub_profile.php | 6 ++- utils/inbox_handler.php | 2 +- utils/postman.php | 18 +++++---- 6 files changed, 55 insertions(+), 47 deletions(-) diff --git a/ActivityPubPlugin.php b/ActivityPubPlugin.php index 45ee3ce..399fb6c 100755 --- a/ActivityPubPlugin.php +++ b/ActivityPubPlugin.php @@ -151,49 +151,48 @@ class ActivityPubPlugin extends Plugin ['nickname' => Nickname::DISPLAY_FMT], 'apActorProfile' ); - - ActivityPubURLMapperOverwrite::variable( - $m, - 'notice/:id', - ['id' => '[0-9]+'], - 'apNotice' - ); } + $m->connect( + 'note/:id.json', + ['action' => 'apNotice'], + ['id' => '[0-9]+'] + ); + $m->connect( 'user/:id/liked.json', - ['action' => 'apActorLiked'], - ['id' => '[0-9]+'] - ); + ['action' => 'apActorLiked'], + ['id' => '[0-9]+'] + ); $m->connect( 'user/:id/followers.json', - ['action' => 'apActorFollowers'], - ['id' => '[0-9]+'] - ); + ['action' => 'apActorFollowers'], + ['id' => '[0-9]+'] + ); $m->connect( 'user/:id/following.json', - ['action' => 'apActorFollowing'], - ['id' => '[0-9]+'] - ); + ['action' => 'apActorFollowing'], + ['id' => '[0-9]+'] + ); $m->connect( 'user/:id/inbox.json', - ['action' => 'apInbox'], - ['id' => '[0-9]+'] - ); + ['action' => 'apInbox'], + ['id' => '[0-9]+'] + ); $m->connect( 'user/:id/outbox.json', - ['action' => 'apActorOutbox'], - ['id' => '[0-9]+'] - ); + ['action' => 'apActorOutbox'], + ['id' => '[0-9]+'] + ); $m->connect( 'inbox.json', - ['action' => 'apInbox'] - ); + ['action' => 'apInbox'] + ); } /** @@ -278,7 +277,7 @@ class ActivityPubPlugin extends Plugin } // Look for profile URLs, with or without scheme: - $urls = array(); + $urls = []; if (preg_match('!^https?://((?:\w+\.)*\w+(?:\w+\-\w+)*\.\w+(?:/\w+)+)$!', $arg)) { $urls[] = $arg; } @@ -336,7 +335,7 @@ class ActivityPubPlugin extends Plugin */ public static function extractUrlMentions($text, $preMention='@') { - $wmatches = array(); + $wmatches = []; // In the regexp below we need to match / _before_ URL_REGEX_VALID_PATH_CHARS because it otherwise gets merged // with the TLD before (but / is in URL_REGEX_VALID_PATH_CHARS anyway, it's just its positioning that is important) $result = preg_match_all( @@ -386,7 +385,7 @@ class ActivityPubPlugin extends Plugin */ public function onEndFindMentions(Profile $sender, $text, &$mentions) { - $matches = array(); + $matches = []; foreach (self::extractWebfingerIds($text, '@') as $wmatch) { list($target, $pos) = $wmatch; @@ -547,7 +546,7 @@ class ActivityPubPlugin extends Plugin try { $other = Activitypub_profile::from_profile($other); } catch (Exception $e) { - return true; + return true; // Let other plugin handle this instead } $postman = new Activitypub_postman($profile, array($other)); @@ -574,7 +573,7 @@ class ActivityPubPlugin extends Plugin try { $other = Activitypub_profile::from_profile($other); } catch (Exception $e) { - return true; + return true; // Let other plugin handle this instead } $postman = new Activitypub_postman($profile, array($other)); @@ -585,7 +584,7 @@ class ActivityPubPlugin extends Plugin } /** - * Notify remote users when their notices get favorited. + * Notify remote users when their notices get favourited. * * @author Diogo Cordeiro * @param Profile $profile of local user doing the faving @@ -600,7 +599,7 @@ class ActivityPubPlugin extends Plugin return true; } - $other = array(); + $other = []; try { $other[] = Activitypub_profile::from_profile($notice->getProfile()); } catch (Exception $e) { @@ -644,7 +643,7 @@ class ActivityPubPlugin extends Plugin } /** - * Notify remote users when their notices get de-favorited. + * Notify remote users when their notices get de-favourited. * * @author Diogo Cordeiro * @param Profile $profile of local user doing the de-faving @@ -659,7 +658,7 @@ class ActivityPubPlugin extends Plugin return true; } - $other = array(); + $other = []; try { $other[] = Activitypub_profile::from_profile($notice->getProfile()); } catch (Exception $e) { @@ -718,7 +717,7 @@ class ActivityPubPlugin extends Plugin return true; } - $other = array(); + $other = []; foreach ($notice->getAttentionProfiles() as $to_profile) { try { @@ -771,7 +770,7 @@ class ActivityPubPlugin extends Plugin return true; } - $other = array(); + $other = []; try { $other[] = Activitypub_profile::from_profile($notice->getProfile()); } catch (Exception $e) { diff --git a/actions/apactorfollowers.php b/actions/apactorfollowers.php index 007e8ff..dd7c796 100755 --- a/actions/apactorfollowers.php +++ b/actions/apactorfollowers.php @@ -116,7 +116,7 @@ class apActorFollowersAction extends ManagedAction * @param int32 $limit * @return Array of URIs */ - public function generate_followers($profile, $since, $limit) + public static function generate_followers($profile, $since, $limit) { /* Fetch Followers */ try { @@ -130,6 +130,7 @@ class apActorFollowersAction extends ManagedAction while ($sub->fetch()) { $subs[] = ActivityPubPlugin::actor_uri($sub); } + return $subs; } } diff --git a/classes/Activitypub_notice.php b/classes/Activitypub_notice.php index 824fa07..0f515cb 100755 --- a/classes/Activitypub_notice.php +++ b/classes/Activitypub_notice.php @@ -73,7 +73,7 @@ class Activitypub_notice extends Managed_DataObject $item = [ '@context' => 'https://www.w3.org/ns/activitystreams', - 'id' => $notice->getUrl(), + 'id' => common_local_url('apNotice', ['id' => $notice->getID()]), 'type' => 'Note', 'published' => str_replace(' ', 'T', $notice->getCreated()).'Z', 'url' => $notice->getUrl(), @@ -210,7 +210,7 @@ class Activitypub_notice extends Managed_DataObject $discovery = new Activitypub_explorer; foreach ($mentions as $mention) { try { - $mentions_profiles[] = array_merge($mentions_profiles, $discovery->lookup($mention)); + $mentions_profiles[] = $discovery->lookup($mention)[0]; } catch (Exception $e) { // Invalid actor found, just let it go. // TODO: Fallback to OStatus } diff --git a/classes/Activitypub_profile.php b/classes/Activitypub_profile.php index ecd7da6..35cd0b4 100755 --- a/classes/Activitypub_profile.php +++ b/classes/Activitypub_profile.php @@ -239,9 +239,13 @@ class Activitypub_profile extends Managed_DataObject */ private static function create_from_local_profile(Profile $profile) { - $url = $profile->getURL(); + $url = $profile->getUri(); $inboxes = Activitypub_explorer::get_actor_inboxes_uri($url); + if ($inboxes == null) { + throw new Exception ('This is not an ActivityPub user thus AProfile is politely refusing to proceed.'); + } + $aprofile->created = $aprofile->modified = common_sql_now(); $aprofile = new Activitypub_profile; diff --git a/utils/inbox_handler.php b/utils/inbox_handler.php index c0680dc..ce009bf 100644 --- a/utils/inbox_handler.php +++ b/utils/inbox_handler.php @@ -103,7 +103,7 @@ class Activitypub_inbox_handler case 'Like': case 'Announce': if (!filter_var($this->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; case 'Undo': diff --git a/utils/postman.php b/utils/postman.php index 03f6509..d60e158 100755 --- a/utils/postman.php +++ b/utils/postman.php @@ -63,15 +63,19 @@ class Activitypub_postman public function __construct($from, $to = []) { $this->actor = $from; + $discovery = new Activitypub_explorer(); - $this->to = array_merge( - $to, - $discovery->lookup(common_local_url( - 'apActorFollowers', - ['id' => $from->getID()] - )) - ); + $this->to = $to; + $followers = apActorFollowersAction::generate_followers($this->actor, 0, null); + foreach ($followers as $sub) { + try { + $to[]= Activitypub_profile::from_profile($discovery->lookup($sub)[0]); + } catch (Exception $e) { + // Not an ActivityPub Remote Follower, let it go + } + } unset($discovery); + $this->actor_uri = ActivityPubPlugin::actor_uri($this->actor); $actor_private_key = new Activitypub_rsa();