| 
									
										
										
										
											2021-08-24 20:29:26 +01:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-01 20:53:32 +00:00
										 |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2021-10-10 09:26:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-24 20:29:26 +01:00
										 |  |  | namespace Plugin\ActivityPub; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use App\Core\Event; | 
					
						
							| 
									
										
										
										
											2021-11-29 23:58:42 +00:00
										 |  |  | use App\Core\HTTPClient; | 
					
						
							| 
									
										
										
										
											2021-11-01 12:16:46 +00:00
										 |  |  | use App\Core\Log; | 
					
						
							| 
									
										
										
										
											2021-08-24 20:29:26 +01:00
										 |  |  | use App\Core\Modules\Plugin; | 
					
						
							|  |  |  | use App\Core\Router\RouteLoader; | 
					
						
							| 
									
										
										
										
											2021-10-04 17:00:58 +01:00
										 |  |  | use App\Core\Router\Router; | 
					
						
							| 
									
										
										
										
											2021-11-29 23:58:42 +00:00
										 |  |  | use App\Entity\Activity; | 
					
						
							| 
									
										
										
										
											2021-10-18 13:22:02 +01:00
										 |  |  | use App\Entity\Actor; | 
					
						
							| 
									
										
										
										
											2021-10-27 04:14:01 +01:00
										 |  |  | use App\Entity\LocalUser; | 
					
						
							| 
									
										
										
										
											2021-11-01 12:16:46 +00:00
										 |  |  | use App\Util\Common; | 
					
						
							| 
									
										
										
										
											2021-10-27 04:14:01 +01:00
										 |  |  | use App\Util\Exception\NoSuchActorException; | 
					
						
							|  |  |  | use App\Util\Nickname; | 
					
						
							| 
									
										
										
										
											2021-12-02 04:25:58 +00:00
										 |  |  | use Component\FreeNetwork\Entity\FreeNetworkActorProtocol; | 
					
						
							|  |  |  | use Component\FreeNetwork\Util\Discovery; | 
					
						
							| 
									
										
										
										
											2021-08-24 20:29:26 +01:00
										 |  |  | use Exception; | 
					
						
							|  |  |  | use Plugin\ActivityPub\Controller\Inbox; | 
					
						
							| 
									
										
										
										
											2021-10-27 04:14:01 +01:00
										 |  |  | use Plugin\ActivityPub\Entity\ActivitypubActor; | 
					
						
							| 
									
										
										
										
											2021-11-30 16:47:31 +00:00
										 |  |  | use Plugin\ActivityPub\Util\HTTPSignature; | 
					
						
							| 
									
										
										
										
											2021-11-29 23:58:42 +00:00
										 |  |  | use Plugin\ActivityPub\Util\Model\EntityToType\EntityToType; | 
					
						
							| 
									
										
										
										
											2021-10-04 17:00:58 +01:00
										 |  |  | use Plugin\ActivityPub\Util\Response\ActorResponse; | 
					
						
							|  |  |  | use Plugin\ActivityPub\Util\Response\NoteResponse; | 
					
						
							|  |  |  | use Plugin\ActivityPub\Util\Response\TypeResponse; | 
					
						
							| 
									
										
										
										
											2021-11-29 23:58:42 +00:00
										 |  |  | use Plugin\ActivityPub\Util\Type; | 
					
						
							|  |  |  | use Symfony\Contracts\HttpClient\ResponseInterface; | 
					
						
							| 
									
										
										
										
											2021-10-18 13:22:02 +01:00
										 |  |  | use XML_XRD; | 
					
						
							|  |  |  | use XML_XRD_Element_Link; | 
					
						
							| 
									
										
										
										
											2021-11-01 12:16:46 +00:00
										 |  |  | use function count; | 
					
						
							| 
									
										
										
										
											2021-12-02 04:25:58 +00:00
										 |  |  | use function Psy\debug; | 
					
						
							| 
									
										
										
										
											2021-11-01 12:16:46 +00:00
										 |  |  | use const PREG_SET_ORDER; | 
					
						
							| 
									
										
										
										
											2021-08-24 20:29:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | class ActivityPub extends Plugin | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-10-27 04:14:01 +01:00
										 |  |  |     // ActivityStreams 2.0 Accept Headers
 | 
					
						
							| 
									
										
										
										
											2021-10-18 13:22:02 +01:00
										 |  |  |     public static array $accept_headers = [ | 
					
						
							|  |  |  |         'application/ld+json; profile="https://www.w3.org/ns/activitystreams"', | 
					
						
							|  |  |  |         'application/activity+json', | 
					
						
							|  |  |  |         'application/json', | 
					
						
							|  |  |  |         'application/ld+json', | 
					
						
							|  |  |  |     ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-27 04:14:01 +01:00
										 |  |  |     // So that this isn't hardcoded everywhere
 | 
					
						
							| 
									
										
										
										
											2021-11-27 15:06:46 +00:00
										 |  |  |     public const PUBLIC_TO = [ | 
					
						
							|  |  |  |         'https://www.w3.org/ns/activitystreams#Public', | 
					
						
							| 
									
										
										
										
											2021-10-27 04:14:01 +01:00
										 |  |  |         'Public', | 
					
						
							|  |  |  |         'as:Public', | 
					
						
							|  |  |  |     ]; | 
					
						
							|  |  |  |     public const HTTP_CLIENT_HEADERS = [ | 
					
						
							| 
									
										
										
										
											2021-12-01 20:53:32 +00:00
										 |  |  |         'Accept' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"', | 
					
						
							| 
									
										
										
										
											2021-10-27 04:14:01 +01:00
										 |  |  |         'User-Agent' => 'GNUsocialBot ' . GNUSOCIAL_VERSION . ' - ' . GNUSOCIAL_PROJECT_URL, | 
					
						
							|  |  |  |     ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-24 20:29:26 +01:00
										 |  |  |     public function version(): string | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return '3.0.0'; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * This code executes when GNU social creates the page routing, and we hook | 
					
						
							| 
									
										
										
										
											2021-10-04 17:00:58 +01:00
										 |  |  |      * on this event to add our Inbox and Outbox handler for ActivityPub. | 
					
						
							| 
									
										
										
										
											2021-08-24 20:29:26 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-10-10 09:26:18 +01:00
										 |  |  |      * @param RouteLoader $r the router that was initialized | 
					
						
							| 
									
										
										
										
											2021-08-24 20:29:26 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function onAddRoute(RouteLoader $r): bool | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-09-16 17:04:05 +01:00
										 |  |  |         $r->connect( | 
					
						
							| 
									
										
										
										
											2021-10-19 23:46:01 +01:00
										 |  |  |             'activitypub_inbox', | 
					
						
							|  |  |  |             '/inbox.json', | 
					
						
							| 
									
										
										
										
											2021-09-16 17:04:05 +01:00
										 |  |  |             [Inbox::class, 'handle'], | 
					
						
							| 
									
										
										
										
											2021-10-27 04:14:01 +01:00
										 |  |  |             options: ['accept' => self::$accept_headers, 'format' => self::$accept_headers[0]], | 
					
						
							| 
									
										
										
										
											2021-09-16 17:04:05 +01:00
										 |  |  |         ); | 
					
						
							|  |  |  |         $r->connect( | 
					
						
							| 
									
										
										
										
											2021-10-19 23:46:01 +01:00
										 |  |  |             'activitypub_actor_inbox', | 
					
						
							|  |  |  |             '/actor/{gsactor_id<\d+>}/inbox.json', | 
					
						
							| 
									
										
										
										
											2021-09-16 17:04:05 +01:00
										 |  |  |             [Inbox::class, 'handle'], | 
					
						
							| 
									
										
										
										
											2021-10-27 04:14:01 +01:00
										 |  |  |             options: ['accept' => self::$accept_headers, 'format' => self::$accept_headers[0]], | 
					
						
							| 
									
										
										
										
											2021-09-16 17:04:05 +01:00
										 |  |  |         ); | 
					
						
							| 
									
										
										
										
											2021-08-24 20:29:26 +01:00
										 |  |  |         $r->connect( | 
					
						
							| 
									
										
										
										
											2021-10-19 23:46:01 +01:00
										 |  |  |             'activitypub_actor_outbox', | 
					
						
							|  |  |  |             '/actor/{gsactor_id<\d+>}/outbox.json', | 
					
						
							| 
									
										
										
										
											2021-08-24 20:29:26 +01:00
										 |  |  |             [Inbox::class, 'handle'], | 
					
						
							| 
									
										
										
										
											2021-10-27 04:14:01 +01:00
										 |  |  |             options: ['accept' => self::$accept_headers, 'format' => self::$accept_headers[0]], | 
					
						
							| 
									
										
										
										
											2021-08-24 20:29:26 +01:00
										 |  |  |         ); | 
					
						
							|  |  |  |         return Event::next; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-01 20:53:32 +00:00
										 |  |  |     public function onStartGetActorUrl(Actor $actor, int $type, ?string &$url): bool | 
					
						
							| 
									
										
										
										
											2021-11-27 15:06:46 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         if ( | 
					
						
							|  |  |  |             // Is remote?
 | 
					
						
							|  |  |  |             !$actor->getIsLocal() | 
					
						
							|  |  |  |             // Is in ActivityPub?
 | 
					
						
							|  |  |  |             && !is_null($ap_actor = ActivitypubActor::getWithPK(['actor_id' => $actor->getId()])) | 
					
						
							|  |  |  |             // We can only provide a full URL (anything else wouldn't make sense)
 | 
					
						
							|  |  |  |             && $type === Router::ABSOLUTE_URL | 
					
						
							|  |  |  |         ) { | 
					
						
							| 
									
										
										
										
											2021-11-29 23:58:42 +00:00
										 |  |  |             $url = $ap_actor->getUri(); | 
					
						
							| 
									
										
										
										
											2021-11-27 15:06:46 +00:00
										 |  |  |             return Event::stop; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return Event::next; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-01 20:53:32 +00:00
										 |  |  |     public function onAddFreeNetworkProtocol(array &$protocols): bool | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-11-29 23:58:42 +00:00
										 |  |  |         $protocols[] = '\Plugin\ActivityPub\ActivityPub'; | 
					
						
							|  |  |  |         return Event::next; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 04:25:58 +00:00
										 |  |  |     public static function freeNetworkDistribute(Actor $sender, Activity $activity, array $targets, ?string $reason = null, array &$delivered = []): bool | 
					
						
							| 
									
										
										
										
											2021-11-27 15:06:46 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-11-29 23:58:42 +00:00
										 |  |  |         $to_addr = []; | 
					
						
							| 
									
										
										
										
											2021-12-02 04:25:58 +00:00
										 |  |  |         foreach ($targets as $actor) { | 
					
						
							|  |  |  |             if (FreeNetworkActorProtocol::canIActor('activitypub', $actor->getId())) { | 
					
						
							|  |  |  |                 if (is_null($ap_target = ActivitypubActor::getWithPK(['actor_id' => $actor->getId()]))) { | 
					
						
							|  |  |  |                     continue; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 $to_addr[$ap_target->getInboxSharedUri() ?? $ap_target->getInboxUri()][] = $actor; | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 return Event::next; | 
					
						
							| 
									
										
										
										
											2021-11-29 23:58:42 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $errors = []; | 
					
						
							| 
									
										
										
										
											2021-12-02 04:25:58 +00:00
										 |  |  |         //$to_failed = [];
 | 
					
						
							| 
									
										
										
										
											2021-11-29 23:58:42 +00:00
										 |  |  |         foreach ($to_addr as $inbox => $dummy) { | 
					
						
							|  |  |  |             try { | 
					
						
							| 
									
										
										
										
											2021-11-30 16:47:31 +00:00
										 |  |  |                 $res = self::postman($sender, EntityToType::translate($activity), $inbox); | 
					
						
							| 
									
										
										
										
											2021-11-29 23:58:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 04:25:58 +00:00
										 |  |  |                 // accumulate errors for later use, if needed
 | 
					
						
							| 
									
										
										
										
											2021-11-29 23:58:42 +00:00
										 |  |  |                 $status_code = $res->getStatusCode(); | 
					
						
							|  |  |  |                 if (!($status_code === 200 || $status_code === 202 || $status_code === 409)) { | 
					
						
							|  |  |  |                     $res_body = json_decode($res->getContent(), true); | 
					
						
							|  |  |  |                     $errors[] = isset($res_body['error']) ? | 
					
						
							|  |  |  |                         $res_body['error'] : "An unknown error occurred."; | 
					
						
							| 
									
										
										
										
											2021-12-02 04:25:58 +00:00
										 |  |  |                     //$to_failed[$inbox] = $activity;
 | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     array_push($delivered, ...$dummy); | 
					
						
							|  |  |  |                     foreach ($dummy as $actor) { | 
					
						
							|  |  |  |                         FreeNetworkActorProtocol::protocolSucceeded( | 
					
						
							|  |  |  |                             'activitypub', | 
					
						
							|  |  |  |                             $actor, | 
					
						
							|  |  |  |                             Discovery::normalize($actor->getNickname() . '@' . parse_url($inbox, PHP_URL_HOST)) | 
					
						
							|  |  |  |                         ); | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2021-11-29 23:58:42 +00:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } catch (Exception $e) { | 
					
						
							|  |  |  |                 Log::error('ActivityPub @ freeNetworkDistribute: ' . $e->getMessage()); | 
					
						
							| 
									
										
										
										
											2021-12-02 04:25:58 +00:00
										 |  |  |                 //$to_failed[$inbox] = $activity;
 | 
					
						
							| 
									
										
										
										
											2021-11-29 23:58:42 +00:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!empty($errors)) { | 
					
						
							|  |  |  |             Log::error(sizeof($errors) . ' instance/s failed to handle the delete_profile activity!'); | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-11-30 16:47:31 +00:00
										 |  |  |      * @param Actor $sender | 
					
						
							| 
									
										
										
										
											2021-11-29 23:58:42 +00:00
										 |  |  |      * @param Type $activity | 
					
						
							|  |  |  |      * @param string $inbox | 
					
						
							|  |  |  |      * @param string $method | 
					
						
							|  |  |  |      * @return ResponseInterface | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-11-30 16:47:31 +00:00
										 |  |  |     public static function postman(Actor $sender, mixed $activity, string $inbox, string $method = 'post'): ResponseInterface | 
					
						
							| 
									
										
										
										
											2021-11-29 23:58:42 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         $data = $activity->toJson(); | 
					
						
							|  |  |  |         Log::debug('ActivityPub Postman: Delivering ' . $data . ' to ' . $inbox); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-30 16:47:31 +00:00
										 |  |  |         $headers = HTTPSignature::sign($sender, $inbox, $data); | 
					
						
							|  |  |  |         Log::debug('ActivityPub Postman: Delivery headers were: ' . print_r($headers, true)); | 
					
						
							| 
									
										
										
										
											2021-11-29 23:58:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $response = HTTPClient::$method($inbox, ['headers' => $headers, 'body' => $data]); | 
					
						
							| 
									
										
										
										
											2021-12-01 20:53:32 +00:00
										 |  |  |         Log::debug('ActivityPub Postman: Delivery result with status code ' . $response->getStatusCode() . ': ' . $response->getContent()); | 
					
						
							| 
									
										
										
										
											2021-11-29 23:58:42 +00:00
										 |  |  |         return $response; | 
					
						
							| 
									
										
										
										
											2021-11-27 15:06:46 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 23:58:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-27 04:14:01 +01:00
										 |  |  |     public static function getActorByUri(string $resource, ?bool $attempt_fetch = true): Actor | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // Try local
 | 
					
						
							| 
									
										
										
										
											2021-11-01 12:16:46 +00:00
										 |  |  |         if (Common::isValidHttpUrl($resource)) { | 
					
						
							| 
									
										
										
										
											2021-10-27 04:14:01 +01:00
										 |  |  |             // This means $resource is a valid url
 | 
					
						
							|  |  |  |             $resource_parts = parse_url($resource); | 
					
						
							|  |  |  |             // TODO: Use URLMatcher
 | 
					
						
							|  |  |  |             if ($resource_parts['host'] === $_ENV['SOCIAL_DOMAIN']) { // XXX: Common::config('site', 'server')) {
 | 
					
						
							|  |  |  |                 $str = $resource_parts['path']; | 
					
						
							|  |  |  |                 // actor_view_nickname
 | 
					
						
							|  |  |  |                 $renick = '/\/@(' . Nickname::DISPLAY_FMT . ')\/?/m'; | 
					
						
							|  |  |  |                 // actor_view_id
 | 
					
						
							|  |  |  |                 $reuri = '/\/actor\/(\d+)\/?/m'; | 
					
						
							| 
									
										
										
										
											2021-11-01 12:16:46 +00:00
										 |  |  |                 if (preg_match_all($renick, $str, $matches, PREG_SET_ORDER, 0) === 1) { | 
					
						
							| 
									
										
										
										
											2021-10-27 04:14:01 +01:00
										 |  |  |                     return LocalUser::getWithPK(['nickname' => $matches[0][1]])->getActor(); | 
					
						
							| 
									
										
										
										
											2021-11-01 12:16:46 +00:00
										 |  |  |                 } elseif (preg_match_all($reuri, $str, $matches, PREG_SET_ORDER, 0) === 1) { | 
					
						
							| 
									
										
										
										
											2021-12-01 20:53:32 +00:00
										 |  |  |                     return Actor::getById((int)$matches[0][1]); | 
					
						
							| 
									
										
										
										
											2021-10-27 04:14:01 +01:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // Try remote
 | 
					
						
							|  |  |  |         $aprofile = ActivitypubActor::getByAddr($resource); | 
					
						
							|  |  |  |         if ($aprofile instanceof ActivitypubActor) { | 
					
						
							|  |  |  |             return Actor::getById($aprofile->getActorId()); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             throw new NoSuchActorException("From URI: {$resource}"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-04 17:00:58 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @throws Exception | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function onControllerResponseInFormat(string $route, array $accept_header, array $vars, ?TypeResponse &$response = null): bool | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-11-01 12:16:46 +00:00
										 |  |  |         if (count(array_intersect(self::$accept_headers, $accept_header)) === 0) { | 
					
						
							| 
									
										
										
										
											2021-10-04 17:00:58 +01:00
										 |  |  |             return Event::next; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         switch ($route) { | 
					
						
							|  |  |  |             case 'actor_view_id': | 
					
						
							|  |  |  |             case 'actor_view_nickname': | 
					
						
							|  |  |  |                 $response = ActorResponse::handle($vars['actor']); | 
					
						
							|  |  |  |                 return Event::stop; | 
					
						
							|  |  |  |             case 'note_view': | 
					
						
							|  |  |  |                 $response = NoteResponse::handle($vars['note']); | 
					
						
							|  |  |  |                 return Event::stop; | 
					
						
							|  |  |  |             /*case 'actor_favourites_id': | 
					
						
							|  |  |  |             case 'actor_favourites_nickname': | 
					
						
							|  |  |  |                 $response = LikeResponse::handle($vars['actor']); | 
					
						
							|  |  |  |                 return Event::stop; | 
					
						
							|  |  |  |             case 'actor_subscriptions_id': | 
					
						
							|  |  |  |             case 'actor_subscriptions_nickname': | 
					
						
							|  |  |  |                 $response = FollowingResponse::handle($vars['actor']); | 
					
						
							|  |  |  |                 return Event::stop; | 
					
						
							|  |  |  |             case 'actor_subscribers_id': | 
					
						
							|  |  |  |             case 'actor_subscribers_nickname': | 
					
						
							|  |  |  |                 $response = FollowersResponse::handle($vars['actor']); | 
					
						
							|  |  |  |                 return Event::stop;*/ | 
					
						
							|  |  |  |             default: | 
					
						
							| 
									
										
										
										
											2021-10-28 17:28:02 +01:00
										 |  |  |                 if (Event::handle('ActivityStreamsTwoResponse', [$route, &$response]) == Event::stop) { | 
					
						
							| 
									
										
										
										
											2021-10-04 17:00:58 +01:00
										 |  |  |                     return Event::stop; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 return Event::next; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-01 12:16:46 +00:00
										 |  |  |     /******************************************************** | 
					
						
							|  |  |  |      *                   WebFinger Events                   * | 
					
						
							|  |  |  |      ********************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-18 13:22:02 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Add activity+json mimetype on WebFinger | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @throws Exception | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-10-27 04:14:01 +01:00
										 |  |  |     public function onEndWebFingerProfileLinks(XML_XRD $xrd, Actor $object): bool | 
					
						
							| 
									
										
										
										
											2021-10-04 17:00:58 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-10-18 13:22:02 +01:00
										 |  |  |         if ($object->isPerson()) { | 
					
						
							|  |  |  |             $link = new XML_XRD_Element_Link( | 
					
						
							| 
									
										
										
										
											2021-10-27 04:14:01 +01:00
										 |  |  |                 rel: 'self', | 
					
						
							|  |  |  |                 href: $object->getUri(Router::ABSOLUTE_URL),//Router::url('actor_view_id', ['id' => $object->getId()], Router::ABSOLUTE_URL),
 | 
					
						
							|  |  |  |                 type: 'application/activity+json', | 
					
						
							| 
									
										
										
										
											2021-10-18 13:22:02 +01:00
										 |  |  |             ); | 
					
						
							|  |  |  |             $xrd->links[] = clone $link; | 
					
						
							| 
									
										
										
										
											2021-10-04 17:00:58 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-10-27 04:14:01 +01:00
										 |  |  |         return Event::next; | 
					
						
							| 
									
										
										
										
											2021-10-18 13:22:02 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 03:34:31 +00:00
										 |  |  |     public function onFreeNetworkFindMentions(string $target, ?Actor &$actor = null): bool | 
					
						
							| 
									
										
										
										
											2021-10-18 13:22:02 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-12-02 03:34:31 +00:00
										 |  |  |         try { | 
					
						
							| 
									
										
										
										
											2021-12-02 04:25:58 +00:00
										 |  |  |             if (FreeNetworkActorProtocol::canIAddr('activitypub', $addr = Discovery::normalize($target))) { | 
					
						
							|  |  |  |                 $ap_actor = ActivitypubActor::getByAddr($addr); | 
					
						
							|  |  |  |                 $actor = Actor::getById($ap_actor->getActorId()); | 
					
						
							|  |  |  |                 FreeNetworkActorProtocol::protocolSucceeded('activitypub', $actor->getId(), $addr); | 
					
						
							|  |  |  |                 return Event::stop; | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 return Event::next; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-12-02 03:34:31 +00:00
										 |  |  |         } catch (Exception $e) { | 
					
						
							|  |  |  |             Log::error("ActivityPub Webfinger Mention check failed: " . $e->getMessage()); | 
					
						
							|  |  |  |             return Event::next; | 
					
						
							| 
									
										
										
										
											2021-11-01 12:16:46 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 04:25:58 +00:00
										 |  |  |     public function onFreeNetworkFoundXrd(XML_XRD $xrd, ?Actor &$actor = null): bool | 
					
						
							| 
									
										
										
										
											2021-11-01 12:16:46 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-12-02 04:25:58 +00:00
										 |  |  |         $addr = null; | 
					
						
							|  |  |  |         foreach ($xrd->aliases as $alias) { | 
					
						
							|  |  |  |             if (Discovery::isAcct($alias)) { | 
					
						
							|  |  |  |                 $addr = Discovery::normalize($alias); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (is_null($addr)) { | 
					
						
							|  |  |  |             return Event::next; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             if (!FreeNetworkActorProtocol::canIAddr('activitypub', $addr)) { | 
					
						
							|  |  |  |                 return Event::next; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-12-02 03:34:31 +00:00
										 |  |  |         try { | 
					
						
							| 
									
										
										
										
											2021-12-02 04:25:58 +00:00
										 |  |  |             $ap_actor = ActivitypubActor::fromXrd($addr, $xrd); | 
					
						
							| 
									
										
										
										
											2021-12-02 03:34:31 +00:00
										 |  |  |             $actor = Actor::getById($ap_actor->getActorId()); | 
					
						
							| 
									
										
										
										
											2021-12-02 04:25:58 +00:00
										 |  |  |             FreeNetworkActorProtocol::protocolSucceeded('activitypub', $actor, $addr); | 
					
						
							| 
									
										
										
										
											2021-12-02 03:34:31 +00:00
										 |  |  |             return Event::stop; | 
					
						
							|  |  |  |         } catch (Exception $e) { | 
					
						
							|  |  |  |             Log::error("ActivityPub Actor from URL Mention check failed: " . $e->getMessage()); | 
					
						
							|  |  |  |             return Event::next; | 
					
						
							| 
									
										
										
										
											2021-11-01 12:16:46 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-10-04 17:00:58 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-01 12:16:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Allow remote profile references to be used in commands: | 
					
						
							|  |  |  |      *   sub update@status.net | 
					
						
							|  |  |  |      *   whois evan@identi.ca | 
					
						
							|  |  |  |      *   reply http://identi.ca/evan hey what's up | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Command $command | 
					
						
							|  |  |  |      * @param string $arg | 
					
						
							|  |  |  |      * @param Actor &$profile | 
					
						
							|  |  |  |      * @return bool hook return code | 
					
						
							|  |  |  |      * @author GNU social | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     //public function onStartCommandGetProfile($command, $arg, &$profile)
 | 
					
						
							|  |  |  |     //{
 | 
					
						
							|  |  |  |     //    $aprofile = ActivitypubActor::fromUri($arg);
 | 
					
						
							|  |  |  |     //    if (!($aprofile instanceof ActivitypubActor)) {
 | 
					
						
							|  |  |  |     //        // No remote ActivityPub profile found
 | 
					
						
							|  |  |  |     //        return Event::next;
 | 
					
						
							|  |  |  |     //    }
 | 
					
						
							|  |  |  |     //
 | 
					
						
							|  |  |  |     //    return Event::stop;
 | 
					
						
							|  |  |  |     //}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /******************************************************** | 
					
						
							|  |  |  |      *                   Discovery Events                   * | 
					
						
							|  |  |  |      ********************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Profile from URI. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param string $uri | 
					
						
							|  |  |  |      * @param Actor &$profile in/out param: Profile got from URI | 
					
						
							|  |  |  |      * @return mixed hook return code | 
					
						
							| 
									
										
										
										
											2021-12-01 20:53:32 +00:00
										 |  |  |      * @author GNU social | 
					
						
							| 
									
										
										
										
											2021-11-01 12:16:46 +00:00
										 |  |  |      */ | 
					
						
							|  |  |  |     //public function onStartGetProfileFromURI($uri, &$profile)
 | 
					
						
							|  |  |  |     //{
 | 
					
						
							|  |  |  |     //    try {
 | 
					
						
							|  |  |  |     //        $profile = Explorer::get_profile_from_url($uri);
 | 
					
						
							|  |  |  |     //        return Event::stop;
 | 
					
						
							|  |  |  |     //    } catch (Exception) {
 | 
					
						
							|  |  |  |     //        return Event::next; // It's not an ActivityPub profile as far as we know, continue event handling
 | 
					
						
							|  |  |  |     //    }
 | 
					
						
							|  |  |  |     //}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Try to grab and store the remote profile by the given uri | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param string $uri | 
					
						
							|  |  |  |      * @param Actor|null &$profile | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     //public function onRemoteFollowPullProfile(string $uri, ?Actor &$profile): bool
 | 
					
						
							|  |  |  |     //{
 | 
					
						
							|  |  |  |     //    $aprofile = ActivitypubActor::fromUri($uri);
 | 
					
						
							|  |  |  |     //    if (!($aprofile instanceof ActivitypubActor)) {
 | 
					
						
							|  |  |  |     //        // No remote ActivityPub profile found
 | 
					
						
							|  |  |  |     //        return Event::next;
 | 
					
						
							|  |  |  |     //    }
 | 
					
						
							|  |  |  |     //
 | 
					
						
							|  |  |  |     //    return is_null($profile) ? Event::next : Event::stop;
 | 
					
						
							|  |  |  |     //}
 | 
					
						
							| 
									
										
										
										
											2021-09-06 23:47:28 +01:00
										 |  |  | } |