[PLUGIN][ActivityPub] Accept Undo Follow

This commit is contained in:
Diogo Peralta Cordeiro 2022-01-02 22:24:04 +00:00
parent a9ea49d34c
commit fe087b2217
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
2 changed files with 46 additions and 0 deletions

View File

@ -32,6 +32,7 @@ declare(strict_types = 1);
namespace Plugin\ActivityPub\Util\Model;
use _PHPStan_4a258568e\Nette\NotImplementedException;
use ActivityPhp\Type;
use ActivityPhp\Type\AbstractObject;
use App\Core\Event;
@ -108,6 +109,23 @@ class Activity extends Model
break;
case 'Follow':
ActivityFollow::handle_core_activity($actor, $type_activity, $type_object, $ap_act);
break;
case 'Undo':
$object_type = $type_object instanceof AbstractObject ? match ($type_object->get('type')) {
'Note' => \App\Entity\Note::class,
// no break
default => throw new NotImplementedException('Unsupported Undo of Object Activity.'),
} : $type_object::class;
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;
}
return $ap_act;
}

View File

@ -83,4 +83,32 @@ class ActivityFollow extends Activity
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;
}
}