. // }}} /** * 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; use App\Core\DB\DB; use App\Entity\Activity as GSActivity; use Component\Subscription\Entity\Subscription; use DateTime; use InvalidArgumentException; use Plugin\ActivityPub\Entity\ActivitypubActivity; /** * 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 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) { $subscribed = Actor::fromJson($type_object); } elseif ($type_object instanceof \App\Entity\Actor) { $subscribed = $type_object; } else { throw new InvalidArgumentException('Follow{:Object} should be either an AbstractObject or an Actor.'); } // Store Subscription DB::persist(Subscription::create([ 'subscriber_id' => $actor->getId(), 'subscribed_id' => $subscribed->getActorId(), 'created' => new DateTime($type_activity->get('published') ?? 'now'), ])); // Store Activity $act = GSActivity::create([ 'actor_id' => $actor->getId(), 'verb' => 'subscribe', 'object_type' => 'actor', 'object_id' => $subscribed->getActorId(), 'created' => new DateTime($type_activity->get('published') ?? 'now'), 'source' => 'ActivityPub', ]); DB::persist($act); // 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; } public static function handle_undo(\App\Entity\Actor $actor, AbstractObject $type_activity, GSActivity $type_object, ?ActivitypubActivity &$ap_act): ActivitypubActivity { // Remove Subscription DB::removeBy(Subscription::class, [ 'subscriber_id' => $type_object->getActorId(), 'subscribed_id' => $type_object->getObjectId(), ]); // Store Activity $act = GSActivity::create([ 'actor_id' => $actor->getId(), 'verb' => 'undo', 'object_type' => 'activity', 'object_id' => $type_object->getId(), 'created' => new DateTime($type_activity->get('published') ?? 'now'), 'source' => 'ActivityPub', ]); DB::persist($act); // 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; } }