wordpress-activitypub/includes/handler/class-follow.php
Matthias Pfefferle 6e7f82bf42
Activity-Type based handlers (#551)
* init

* save source id

* fix delete and add improve undo

* test new functions

* add support for threaded comments

* some formatting

* check if URL is no longer available

...and returns either status 410 or 404.

* improve delete handler

* improve update handler

* `object` and `actor` are already required by the inbox endpoint

* fix typo

* simplify queries

* cosmetics

* fix unit tests

* schedule delete comments of deleted actor (#575)

* schedule delete comments of deleted actor

* phpcs

---------

Co-authored-by: Django Doucet <django.doucet@webdevstudios.com>

* move `get_comments_by_actor` to interactions collection

* consistent wording

* implement Tombstone

* fix follow issue

* fix inbox-create

* added missing namespace

* check if field is set

* Fix namespacing issue

* update profile and update interaction

* fields are already validated by inbox

* optimize avatar handling

---------

Co-authored-by: Django <mediaformat.ux@gmail.com>
Co-authored-by: Django Doucet <django.doucet@webdevstudios.com>
2023-11-30 11:43:48 +01:00

81 lines
2.1 KiB
PHP

<?php
namespace Activitypub\Handler;
use Activitypub\Http;
use Activitypub\Activity\Activity;
use Activitypub\Collection\Users;
use Activitypub\Collection\Followers;
/**
* Handle Follow requests
*/
class Follow {
/**
* Initialize the class, registering WordPress hooks
*/
public static function init() {
\add_action( 'activitypub_inbox_follow', array( self::class, 'handle_follow' ), 10, 2 );
\add_action( 'activitypub_followers_post_follow', array( self::class, 'send_follow_response' ), 10, 4 );
}
/**
* Handle "Follow" requests
*
* @param array $activity The activity object
* @param int $user_id The user ID
*/
public static function handle_follow( $activity, $user_id ) {
// save follower
$follower = Followers::add_follower( $user_id, $activity['actor'] );
do_action( 'activitypub_followers_post_follow', $activity['actor'], $activity, $user_id, $follower );
}
/**
* Send Accept response
*
* @param string $actor The Actor URL
* @param array $object The Activity object
* @param int $user_id The ID of the WordPress User
* @param Activitypub\Model\Follower $follower The Follower object
*
* @return void
*/
public static function send_follow_response( $actor, $object, $user_id, $follower ) {
if ( \is_wp_error( $follower ) ) {
// it is not even possible to send a "Reject" because
// we can not get the Remote-Inbox
return;
}
// only send minimal data
$object = array_intersect_key(
$object,
array_flip(
array(
'id',
'type',
'actor',
'object',
)
)
);
$user = Users::get_by_id( $user_id );
// get inbox
$inbox = $follower->get_shared_inbox();
// send "Accept" activity
$activity = new Activity();
$activity->set_type( 'Accept' );
$activity->set_object( $object );
$activity->set_actor( $user->get_id() );
$activity->set_to( $actor );
$activity->set_id( $user->get_id() . '#follow-' . \preg_replace( '~^https?://~', '', $actor ) . '-' . \time() );
$activity = $activity->to_json();
Http::post( $inbox, $activity, $user_id );
}
}