| 
									
										
										
										
											2022-01-02 20:37:15 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types = 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // {{{ 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 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @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; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use ActivityPhp\Type\AbstractObject; | 
					
						
							| 
									
										
										
										
											2022-03-27 15:19:09 +01:00
										 |  |  | use App\Core\DB; | 
					
						
							| 
									
										
										
										
											2022-01-02 20:37:15 +00:00
										 |  |  | use App\Entity\Activity as GSActivity; | 
					
						
							| 
									
										
										
										
											2022-01-03 00:57:37 +00:00
										 |  |  | use App\Util\Exception\ClientException; | 
					
						
							|  |  |  | use Component\Subscription\Subscription; | 
					
						
							| 
									
										
										
										
											2022-03-28 20:58:48 +01:00
										 |  |  | use Component\Subscription\Subscription as SubscriptionComponent; | 
					
						
							| 
									
										
										
										
											2022-01-02 20:37:15 +00:00
										 |  |  | use DateTime; | 
					
						
							|  |  |  | use InvalidArgumentException; | 
					
						
							|  |  |  | use Plugin\ActivityPub\Entity\ActivitypubActivity; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2022-02-13 22:55:54 +00:00
										 |  |  |  * This class handles translation between JSON and ActivityPub Follows | 
					
						
							| 
									
										
										
										
											2022-01-02 20:37:15 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org | 
					
						
							|  |  |  |  * @license   https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class ActivityFollow extends Activity | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     protected static function handle_core_activity(\App\Entity\Actor $actor, AbstractObject $type_activity, mixed $type_object, ?ActivitypubActivity &$ap_act): ActivitypubActivity | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if ($type_object instanceof AbstractObject) { | 
					
						
							| 
									
										
										
										
											2022-01-03 00:57:37 +00:00
										 |  |  |             $subscribed = Actor::fromJson($type_object)->getActorId(); | 
					
						
							| 
									
										
										
										
											2022-01-02 20:37:15 +00:00
										 |  |  |         } elseif ($type_object instanceof \App\Entity\Actor) { | 
					
						
							|  |  |  |             $subscribed = $type_object; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             throw new InvalidArgumentException('Follow{:Object} should be either an AbstractObject or an Actor.'); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-01-03 00:57:37 +00:00
										 |  |  |         // Execute Subscribe
 | 
					
						
							|  |  |  |         $act = Subscription::subscribe($actor, $subscribed, 'ActivityPub'); | 
					
						
							|  |  |  |         if (\is_null($act)) { | 
					
						
							|  |  |  |             throw new ClientException('You are already subscribed to this actor.'); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-03-28 20:58:48 +01:00
										 |  |  |         SubscriptionComponent::refreshSubscriptionCount($actor, $subscribed); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-02 20:37:15 +00:00
										 |  |  |         // Store ActivityPub Activity
 | 
					
						
							|  |  |  |         $ap_act = ActivitypubActivity::create([ | 
					
						
							|  |  |  |             'activity_id'  => $act->getId(), | 
					
						
							|  |  |  |             'activity_uri' => $type_activity->get('id'), | 
					
						
							|  |  |  |             'created'      => new DateTime($type_activity->get('published') ?? 'now'), | 
					
						
							|  |  |  |             'modified'     => new DateTime(), | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  |         DB::persist($ap_act); | 
					
						
							|  |  |  |         return $ap_act; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-01-02 22:24:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public static function handle_undo(\App\Entity\Actor $actor, AbstractObject $type_activity, GSActivity $type_object, ?ActivitypubActivity &$ap_act): ActivitypubActivity | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-01-03 00:57:37 +00:00
										 |  |  |         // Execute Unsubscribe
 | 
					
						
							|  |  |  |         $act = Subscription::unsubscribe($actor, $type_object->getObjectId(), 'ActivityPub'); | 
					
						
							|  |  |  |         if (\is_null($act)) { | 
					
						
							|  |  |  |             throw new ClientException('You are already unsubscribed of this actor.'); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-01-02 22:24:04 +00:00
										 |  |  |         // Store ActivityPub Activity
 | 
					
						
							|  |  |  |         $ap_act = ActivitypubActivity::create([ | 
					
						
							|  |  |  |             'activity_id'  => $act->getId(), | 
					
						
							|  |  |  |             'activity_uri' => $type_activity->get('id'), | 
					
						
							|  |  |  |             'created'      => new DateTime($type_activity->get('published') ?? 'now'), | 
					
						
							|  |  |  |             'modified'     => new DateTime(), | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  |         DB::persist($ap_act); | 
					
						
							|  |  |  |         return $ap_act; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-01-02 20:37:15 +00:00
										 |  |  | } |