| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-26 09:48:16 +00:00
										 |  |  | declare(strict_types = 1); | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // {{{ License
 | 
					
						
							|  |  |  | // This file is part of GNU social - https://www.gnu.org/software/social
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // GNU social 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.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // GNU social 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 GNU social.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							|  |  |  | // }}}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * ActivityPub implementation for GNU social | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package   GNUsocial | 
					
						
							|  |  |  |  * @category  ActivityPub | 
					
						
							| 
									
										
										
										
											2021-12-26 09:48:16 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  |  * @author    Diogo Peralta Cordeiro <@diogo.site> | 
					
						
							|  |  |  |  * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org | 
					
						
							|  |  |  |  * @license   https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Plugin\ActivityPub\Util\Model; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  | use ActivityPhp\Type; | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  | use ActivityPhp\Type\AbstractObject; | 
					
						
							| 
									
										
										
										
											2022-02-18 17:48:06 +00:00
										 |  |  | use App\Core\DB\DB; | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  | use App\Core\Event; | 
					
						
							| 
									
										
										
										
											2022-03-23 13:17:35 +00:00
										 |  |  | use App\Core\Log; | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  | use App\Core\Router\Router; | 
					
						
							|  |  |  | use App\Entity\Activity as GSActivity; | 
					
						
							| 
									
										
										
										
											2022-03-28 20:58:48 +01:00
										 |  |  | use App\Util\Common; | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  | use App\Util\Exception\ClientException; | 
					
						
							|  |  |  | use App\Util\Exception\NoSuchActorException; | 
					
						
							| 
									
										
										
										
											2021-12-28 16:44:38 +00:00
										 |  |  | use App\Util\Exception\NotFoundException; | 
					
						
							| 
									
										
										
										
											2022-01-03 00:57:37 +00:00
										 |  |  | use App\Util\Exception\NotImplementedException; | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  | use DateTimeInterface; | 
					
						
							| 
									
										
										
										
											2022-03-23 13:17:35 +00:00
										 |  |  | use Exception; | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  | use InvalidArgumentException; | 
					
						
							|  |  |  | use Plugin\ActivityPub\ActivityPub; | 
					
						
							|  |  |  | use Plugin\ActivityPub\Entity\ActivitypubActivity; | 
					
						
							| 
									
										
										
										
											2022-02-25 01:05:28 +00:00
										 |  |  | use Plugin\ActivityPub\Util\Explorer; | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  | use Plugin\ActivityPub\Util\Model; | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  | use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; | 
					
						
							|  |  |  | use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; | 
					
						
							|  |  |  | use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; | 
					
						
							|  |  |  | use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * This class handles translation between JSON and ActivityPub Activities | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org | 
					
						
							|  |  |  |  * @license   https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class Activity extends Model | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Create an Entity from an ActivityStreams 2.0 JSON string | 
					
						
							|  |  |  |      * This will persist new GSActivities, GSObjects, and APActivity | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  |      * @throws ClientExceptionInterface | 
					
						
							| 
									
										
										
										
											2021-12-26 09:48:16 +00:00
										 |  |  |      * @throws NoSuchActorException | 
					
						
							| 
									
										
										
										
											2022-02-12 04:34:10 +00:00
										 |  |  |      * @throws NotImplementedException | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  |      * @throws RedirectionExceptionInterface | 
					
						
							|  |  |  |      * @throws ServerExceptionInterface | 
					
						
							|  |  |  |      * @throws TransportExceptionInterface | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public static function fromJson(string|AbstractObject $json, array $options = []): ActivitypubActivity | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-12-26 09:48:16 +00:00
										 |  |  |         $type_activity = \is_string($json) ? self::jsonToType($json) : $json; | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  |         // Ditch known activities
 | 
					
						
							| 
									
										
										
										
											2022-02-12 04:34:10 +00:00
										 |  |  |         if ($type_activity->has('id')) { // We can't dereference a transient activity
 | 
					
						
							| 
									
										
										
										
											2022-02-18 17:48:06 +00:00
										 |  |  |             $ap_act = DB::findOneBy(ActivitypubActivity::class, ['activity_uri' => $type_activity->get('id')], return_null: true); | 
					
						
							| 
									
										
										
										
											2022-02-12 04:34:10 +00:00
										 |  |  |             if (!\is_null($ap_act)) { | 
					
						
							|  |  |  |                 return $ap_act; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  |         // Find Actor and Object
 | 
					
						
							| 
									
										
										
										
											2022-02-25 01:05:28 +00:00
										 |  |  |         $actor       = Explorer::getOneFromUri($type_activity->get('actor')); | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  |         $type_object = $type_activity->get('object'); | 
					
						
							| 
									
										
										
										
											2022-03-28 20:58:48 +01:00
										 |  |  |         if (\is_string($type_object)) { | 
					
						
							|  |  |  |             if (Common::isValidHttpUrl($type_object)) { // Retrieve it
 | 
					
						
							|  |  |  |                 $type_object = ActivityPub::getObjectByUri($type_object, try_online: true); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 $type_object = Type::fromJson($type_object); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ($type_object instanceof AbstractObject) { // Encapsulated, if we have it locally, prefer it
 | 
					
						
							| 
									
										
										
										
											2022-02-12 04:34:10 +00:00
										 |  |  |             // TODO: Test authority of activity over object
 | 
					
						
							| 
									
										
										
										
											2022-03-23 13:17:35 +00:00
										 |  |  |             try { | 
					
						
							|  |  |  |                 $type_object = ActivityPub::getObjectByUri($type_object->get('id'), try_online: false); | 
					
						
							|  |  |  |             } catch (Exception $e) { | 
					
						
							|  |  |  |                 // Use the encapsulated then
 | 
					
						
							|  |  |  |                 Log::debug('Failed to find a local activity, will continue with encapsulated.', [$e]); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  |         if (($type_object instanceof Type\AbstractObject)) { // It's a new object apparently
 | 
					
						
							|  |  |  |             if (Event::handle('NewActivityPubActivity', [$actor, $type_activity, $type_object, &$ap_act]) !== Event::stop) { | 
					
						
							|  |  |  |                 return self::handle_core_activity($actor, $type_activity, $type_object, $ap_act); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } else { // Object was already stored locally then
 | 
					
						
							|  |  |  |             if (Event::handle('NewActivityPubActivityWithObject', [$actor, $type_activity, $type_object, &$ap_act]) !== Event::stop) { | 
					
						
							|  |  |  |                 return self::handle_core_activity($actor, $type_activity, $type_object, $ap_act); | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $ap_act; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private static function handle_core_activity(\App\Entity\Actor $actor, AbstractObject $type_activity, mixed $type_object, ?ActivitypubActivity &$ap_act): ActivitypubActivity | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-01-02 20:37:15 +00:00
										 |  |  |         switch ($type_activity->get('type')) { | 
					
						
							|  |  |  |             case 'Create': | 
					
						
							|  |  |  |                 ActivityCreate::handle_core_activity($actor, $type_activity, $type_object, $ap_act); | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 'Follow': | 
					
						
							|  |  |  |                 ActivityFollow::handle_core_activity($actor, $type_activity, $type_object, $ap_act); | 
					
						
							| 
									
										
										
										
											2022-01-02 22:24:04 +00:00
										 |  |  |                 break; | 
					
						
							|  |  |  |             case 'Undo': | 
					
						
							|  |  |  |                 $object_type = $type_object instanceof AbstractObject ? match ($type_object->get('type')) { | 
					
						
							|  |  |  |                     'Note' => \App\Entity\Note::class, | 
					
						
							| 
									
										
										
										
											2022-02-23 17:39:11 +00:00
										 |  |  |                     // no break
 | 
					
						
							| 
									
										
										
										
											2022-01-02 22:24:04 +00:00
										 |  |  |                     default => throw new NotImplementedException('Unsupported Undo of Object Activity.'), | 
					
						
							|  |  |  |                 } : $type_object::class; | 
					
						
							| 
									
										
										
										
											2022-02-12 04:34:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-02 22:24:04 +00:00
										 |  |  |                 switch ($object_type) { | 
					
						
							|  |  |  |                     case GSActivity::class: | 
					
						
							|  |  |  |                         switch ($type_object->getVerb()) { | 
					
						
							|  |  |  |                             case 'subscribe': | 
					
						
							|  |  |  |                                 ActivityFollow::handle_undo($actor, $type_activity, $type_object, $ap_act); | 
					
						
							|  |  |  |                                 break; | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                         break; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2022-02-12 04:34:10 +00:00
										 |  |  |             case 'Announce': | 
					
						
							|  |  |  |                 ActivityAnnounce::handle_core_activity($actor, $type_activity, $type_object, $ap_act); | 
					
						
							|  |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |         return $ap_act; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get a JSON | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @throws ClientException | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-03-28 20:58:48 +01:00
										 |  |  |     public static function toType(mixed $object): AbstractObject | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-12-28 16:07:35 +00:00
										 |  |  |         if ($object::class !== GSActivity::class) { | 
					
						
							| 
									
										
										
										
											2022-01-02 20:37:15 +00:00
										 |  |  |             throw new InvalidArgumentException('First argument type must be an Activity.'); | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-12-26 09:48:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-02 20:37:15 +00:00
										 |  |  |         $gs_verb_to_activity_streams_two_verb = null; | 
					
						
							|  |  |  |         if (Event::handle('GSVerbToActivityStreamsTwoActivityType', [($verb = $object->getVerb()), &$gs_verb_to_activity_streams_two_verb]) === Event::next) { | 
					
						
							|  |  |  |             $gs_verb_to_activity_streams_two_verb = match ($verb) { | 
					
						
							|  |  |  |                 'undo'      => 'Undo', | 
					
						
							|  |  |  |                 'create'    => 'Create', | 
					
						
							|  |  |  |                 'subscribe' => 'Follow', | 
					
						
							|  |  |  |                 default     => throw new ClientException('Invalid verb'), | 
					
						
							| 
									
										
										
										
											2021-12-26 09:48:16 +00:00
										 |  |  |             }; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $attr = [ | 
					
						
							| 
									
										
										
										
											2022-01-02 20:37:15 +00:00
										 |  |  |             'type'      => $gs_verb_to_activity_streams_two_verb, | 
					
						
							| 
									
										
										
										
											2022-02-23 17:39:11 +00:00
										 |  |  |             '@context'  => ActivityPub::$activity_streams_two_context, | 
					
						
							| 
									
										
										
										
											2021-12-26 09:48:16 +00:00
										 |  |  |             'id'        => Router::url('activity_view', ['id' => $object->getId()], Router::ABSOLUTE_URL), | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  |             'published' => $object->getCreated()->format(DateTimeInterface::RFC3339), | 
					
						
							| 
									
										
										
										
											2021-12-26 09:48:16 +00:00
										 |  |  |             'actor'     => $object->getActor()->getUri(Router::ABSOLUTE_URL), | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2022-01-02 20:37:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-19 22:20:17 +00:00
										 |  |  |         $attr['to'] = ['https://www.w3.org/ns/activitystreams#Public']; | 
					
						
							|  |  |  |         $attr['cc'] = []; | 
					
						
							| 
									
										
										
										
											2022-03-13 18:23:19 +00:00
										 |  |  |         foreach ($object->getAttentionTargets() as $target) { | 
					
						
							|  |  |  |             $attr['cc'][] = $target->getUri(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-02 20:37:15 +00:00
										 |  |  |         // Get object or Tombstone
 | 
					
						
							| 
									
										
										
										
											2021-12-28 16:44:38 +00:00
										 |  |  |         try { | 
					
						
							| 
									
										
										
										
											2022-03-13 18:23:19 +00:00
										 |  |  |             $child          = $object->getObject(); // Throws NotFoundException
 | 
					
						
							| 
									
										
										
										
											2022-03-28 20:58:48 +01:00
										 |  |  |             $prefer_embed   = ['Create', 'Undo']; | 
					
						
							|  |  |  |             $attr['object'] = \in_array($attr['type'], $prefer_embed) ? self::jsonToType(Model::toJson($child)) : ActivityPub::getUriByObject($child); | 
					
						
							| 
									
										
										
										
											2021-12-28 16:44:38 +00:00
										 |  |  |         } catch (NotFoundException) { | 
					
						
							|  |  |  |             // It seems this object was deleted, refer to it as a Tombstone
 | 
					
						
							|  |  |  |             $uri = match ($object->getObjectType()) { | 
					
						
							|  |  |  |                 'note'  => Router::url('note_view', ['id' => $object->getObjectId()], type: Router::ABSOLUTE_URL), | 
					
						
							|  |  |  |                 'actor' => Router::url('actor_view_id', ['id' => $object->getObjectId()], type: Router::ABSOLUTE_URL), | 
					
						
							| 
									
										
										
										
											2022-01-03 00:57:37 +00:00
										 |  |  |                 default => throw new NotImplementedException(), | 
					
						
							| 
									
										
										
										
											2021-12-28 16:44:38 +00:00
										 |  |  |             }; | 
					
						
							| 
									
										
										
										
											2022-03-13 18:23:19 +00:00
										 |  |  |             $attr['object'] = Type::create('Tombstone', ['id' => $uri]); | 
					
						
							| 
									
										
										
										
											2021-12-26 05:51:59 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-01 17:59:53 +00:00
										 |  |  |         if (!\is_string($attr['object'])) { | 
					
						
							| 
									
										
										
										
											2022-03-19 22:20:17 +00:00
										 |  |  |             $attr['@context'] = $attr['object']->get('@context'); | 
					
						
							| 
									
										
										
										
											2022-03-01 17:59:53 +00:00
										 |  |  |             $attr['object']->set('@context', null); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  |         $type = self::jsonToType($attr); | 
					
						
							|  |  |  |         Event::handle('ActivityPubAddActivityStreamsTwoData', [$type->get('type'), &$type]); | 
					
						
							| 
									
										
										
										
											2022-03-28 20:58:48 +01:00
										 |  |  |         return $type; | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-12-12 06:40:13 +00:00
										 |  |  | } |