| 1 |
<?php |
| 2 |
namespace Activitypub\Handler; |
| 3 |
|
| 4 |
use WP_Error; |
| 5 |
use Activitypub\Collection\Interactions; |
| 6 |
|
| 7 |
use function Activitypub\get_remote_metadata_by_actor; |
| 8 |
|
| 9 |
/** |
| 10 |
* Handle Update requests. |
| 11 |
*/ |
| 12 |
class Update { |
| 13 |
/** |
| 14 |
* Initialize the class, registering WordPress hooks |
| 15 |
*/ |
| 16 |
public static function init() { |
| 17 |
\add_action( 'activitypub_inbox_update', array( self::class, 'handle_update' ), 10, 2 ); |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Handle "Update" requests |
| 22 |
* |
| 23 |
* @param array $array The activity-object |
| 24 |
* @param int $user_id The id of the local blog-user |
| 25 |
*/ |
| 26 |
public static function handle_update( $array, $user_id ) { |
| 27 |
$object_type = isset( $array['object']['type'] ) ? $array['object']['type'] : ''; |
| 28 |
|
| 29 |
switch ( $object_type ) { |
| 30 |
// Actor Types |
| 31 |
// @see https://www.w3.org/TR/activitystreams-vocabulary/#actor-types |
| 32 |
case 'Person': |
| 33 |
case 'Group': |
| 34 |
case 'Organization': |
| 35 |
case 'Service': |
| 36 |
case 'Application': |
| 37 |
self::update_actor( $array ); |
| 38 |
break; |
| 39 |
// Object and Link Types |
| 40 |
// @see https://www.w3.org/TR/activitystreams-vocabulary/#object-types |
| 41 |
case 'Note': |
| 42 |
case 'Article': |
| 43 |
case 'Image': |
| 44 |
case 'Audio': |
| 45 |
case 'Video': |
| 46 |
case 'Event': |
| 47 |
case 'Document': |
| 48 |
self::update_interaction( $array, $user_id ); |
| 49 |
break; |
| 50 |
// Minimal Activity |
| 51 |
// @see https://www.w3.org/TR/activitystreams-core/#example-1 |
| 52 |
default: |
| 53 |
break; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Update an Interaction |
| 59 |
* |
| 60 |
* @param array $activity The activity-object |
| 61 |
* @param int $user_id The id of the local blog-user |
| 62 |
* |
| 63 |
* @return void |
| 64 |
*/ |
| 65 |
public static function update_interaction( $activity, $user_id ) { |
| 66 |
$state = Interactions::update_comment( $activity ); |
| 67 |
$reaction = null; |
| 68 |
|
| 69 |
if ( $state && ! \is_wp_error( $reaction ) ) { |
| 70 |
$reaction = \get_comment( $state ); |
| 71 |
} |
| 72 |
|
| 73 |
\do_action( 'activitypub_handled_update', $activity, $user_id, $state, $reaction ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Update an Actor |
| 78 |
* |
| 79 |
* @param array $activity The activity-object |
| 80 |
* |
| 81 |
* @return void |
| 82 |
*/ |
| 83 |
public static function update_actor( $activity ) { |
| 84 |
// update cache |
| 85 |
get_remote_metadata_by_actor( $activity['actor'], false ); |
| 86 |
|
| 87 |
// @todo maybe also update all interactions |
| 88 |
} |
| 89 |
} |
| 90 |
|