2018-12-20 11:33:08 +01:00
|
|
|
<?php
|
2019-11-18 20:57:00 +01:00
|
|
|
namespace Activitypub\Model;
|
2019-02-24 12:07:41 +01:00
|
|
|
|
2023-05-12 22:31:53 +02:00
|
|
|
use function Activitypub\get_rest_url_by_path;
|
|
|
|
|
2018-12-20 11:33:08 +01:00
|
|
|
/**
|
|
|
|
* ActivityPub Post Class
|
|
|
|
*
|
|
|
|
* @author Matthias Pfefferle
|
2019-02-24 13:01:28 +01:00
|
|
|
*
|
|
|
|
* @see https://www.w3.org/TR/activitypub/
|
2018-12-20 11:33:08 +01:00
|
|
|
*/
|
2019-02-24 12:07:41 +01:00
|
|
|
class Activity {
|
2023-05-10 15:36:45 +02:00
|
|
|
/**
|
|
|
|
* The JSON-LD context.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2023-02-02 02:36:29 +01:00
|
|
|
private $context = array(
|
|
|
|
'https://www.w3.org/ns/activitystreams',
|
|
|
|
'https://w3id.org/security/v1',
|
|
|
|
array(
|
|
|
|
'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
|
|
|
|
'PropertyValue' => 'schema:PropertyValue',
|
|
|
|
'schema' => 'http://schema.org#',
|
|
|
|
'pt' => 'https://joinpeertube.org/ns#',
|
|
|
|
'toot' => 'http://joinmastodon.org/ns#',
|
|
|
|
'value' => 'schema:value',
|
|
|
|
'Hashtag' => 'as:Hashtag',
|
|
|
|
'featured' => array(
|
|
|
|
'@id' => 'toot:featured',
|
|
|
|
'@type' => '@id',
|
|
|
|
),
|
|
|
|
'featuredTags' => array(
|
|
|
|
'@id' => 'toot:featuredTags',
|
|
|
|
'@type' => '@id',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2023-05-10 15:36:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The published date.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2018-12-20 11:33:08 +01:00
|
|
|
private $published = '';
|
2023-05-10 15:36:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Activity-ID.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2018-12-20 11:33:08 +01:00
|
|
|
private $id = '';
|
2023-05-10 15:36:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Activity-Type.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2018-12-20 11:33:08 +01:00
|
|
|
private $type = 'Create';
|
2023-05-10 15:36:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Activity-Actor.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2018-12-20 11:33:08 +01:00
|
|
|
private $actor = '';
|
2023-05-10 15:36:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Audience.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2018-12-20 11:33:08 +01:00
|
|
|
private $to = array( 'https://www.w3.org/ns/activitystreams#Public' );
|
2023-05-10 15:36:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The CC.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2023-02-02 02:04:06 +01:00
|
|
|
private $cc = array();
|
2023-05-10 15:36:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Activity-Object.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2018-12-20 11:33:08 +01:00
|
|
|
private $object = null;
|
|
|
|
|
2023-05-10 15:36:45 +02:00
|
|
|
/**
|
|
|
|
* The Class-Constructor.
|
|
|
|
*
|
|
|
|
* @param string $type The Activity-Type.
|
|
|
|
* @param boolean $context The JSON-LD context.
|
|
|
|
*/
|
2023-02-02 02:36:29 +01:00
|
|
|
public function __construct( $type = 'Create', $context = true ) {
|
|
|
|
if ( true !== $context ) {
|
2018-12-20 11:33:08 +01:00
|
|
|
$this->context = null;
|
|
|
|
}
|
|
|
|
|
2019-09-27 10:12:59 +02:00
|
|
|
$this->type = \ucfirst( $type );
|
2023-05-10 18:52:13 +02:00
|
|
|
$this->published = \gmdate( 'Y-m-d\TH:i:s\Z', \time() );
|
2018-12-20 11:33:08 +01:00
|
|
|
}
|
|
|
|
|
2023-05-10 15:36:45 +02:00
|
|
|
/**
|
|
|
|
* Magic Getter/Setter
|
|
|
|
*
|
|
|
|
* @param string $method The method name.
|
|
|
|
* @param string $params The method params.
|
|
|
|
*
|
|
|
|
* @return mixed The value.
|
|
|
|
*/
|
2018-12-20 11:33:08 +01:00
|
|
|
public function __call( $method, $params ) {
|
2019-09-27 10:12:59 +02:00
|
|
|
$var = \strtolower( \substr( $method, 4 ) );
|
2018-12-20 11:33:08 +01:00
|
|
|
|
2019-09-27 10:12:59 +02:00
|
|
|
if ( \strncasecmp( $method, 'get', 3 ) === 0 ) {
|
2018-12-20 11:33:08 +01:00
|
|
|
return $this->$var;
|
|
|
|
}
|
|
|
|
|
2019-09-27 10:12:59 +02:00
|
|
|
if ( \strncasecmp( $method, 'set', 3 ) === 0 ) {
|
2018-12-20 11:33:08 +01:00
|
|
|
$this->$var = $params[0];
|
|
|
|
}
|
2023-02-02 01:42:15 +01:00
|
|
|
|
|
|
|
if ( \strncasecmp( $method, 'add', 3 ) === 0 ) {
|
|
|
|
if ( ! is_array( $this->$var ) ) {
|
|
|
|
$this->$var = $params[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( is_array( $params[0] ) ) {
|
|
|
|
$this->$var = array_merge( $this->$var, $params[0] );
|
|
|
|
} else {
|
|
|
|
array_push( $this->$var, $params[0] );
|
|
|
|
}
|
2023-02-02 02:04:06 +01:00
|
|
|
|
|
|
|
$this->$var = array_unique( $this->$var );
|
2023-02-02 01:42:15 +01:00
|
|
|
}
|
2018-12-20 11:33:08 +01:00
|
|
|
}
|
|
|
|
|
2023-05-10 15:36:45 +02:00
|
|
|
/**
|
|
|
|
* Convert from a Post-Object.
|
|
|
|
*
|
|
|
|
* @param Post $post The Post-Object.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-12-08 21:23:19 +01:00
|
|
|
public function from_post( Post $post ) {
|
2022-12-09 11:59:24 +01:00
|
|
|
$this->object = $post->to_array();
|
2022-12-08 21:23:19 +01:00
|
|
|
|
2020-05-23 12:34:11 +02:00
|
|
|
if ( isset( $object['published'] ) ) {
|
|
|
|
$this->published = $object['published'];
|
|
|
|
}
|
2023-02-02 01:42:15 +01:00
|
|
|
|
2023-05-12 21:58:50 +02:00
|
|
|
$path = sprintf( 'users/%d/followers', intval( $post->get_post_author() ) );
|
|
|
|
$this->add_to( get_rest_url_by_path( $path ) );
|
2020-05-23 12:34:11 +02:00
|
|
|
|
2022-12-09 18:44:46 +01:00
|
|
|
if ( isset( $this->object['attributedTo'] ) ) {
|
|
|
|
$this->actor = $this->object['attributedTo'];
|
2020-05-23 12:34:11 +02:00
|
|
|
}
|
|
|
|
|
2022-12-09 11:59:24 +01:00
|
|
|
foreach ( $post->get_tags() as $tag ) {
|
|
|
|
if ( 'Mention' === $tag['type'] ) {
|
2023-02-02 01:42:15 +01:00
|
|
|
$this->add_cc( $tag['href'] );
|
2022-12-09 11:59:24 +01:00
|
|
|
}
|
2022-12-08 21:23:19 +01:00
|
|
|
}
|
|
|
|
|
2022-01-17 11:03:30 +01:00
|
|
|
$type = \strtolower( $this->type );
|
|
|
|
|
2022-12-09 18:44:46 +01:00
|
|
|
if ( isset( $this->object['id'] ) ) {
|
|
|
|
$this->id = add_query_arg( 'activity', $type, $this->object['id'] );
|
2020-05-23 12:34:11 +02:00
|
|
|
}
|
2018-12-20 11:33:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function from_comment( $object ) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-07-21 09:23:35 +02:00
|
|
|
public function to_comment() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function from_remote_array( $array ) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-05-10 15:36:45 +02:00
|
|
|
/**
|
|
|
|
* Convert to an Array.
|
|
|
|
*
|
|
|
|
* @return array The Array.
|
|
|
|
*/
|
2018-12-20 11:33:08 +01:00
|
|
|
public function to_array() {
|
2022-11-09 15:08:32 +01:00
|
|
|
$array = array_filter( \get_object_vars( $this ) );
|
2018-12-20 11:33:08 +01:00
|
|
|
|
|
|
|
if ( $this->context ) {
|
|
|
|
$array = array( '@context' => $this->context ) + $array;
|
|
|
|
}
|
|
|
|
|
|
|
|
unset( $array['context'] );
|
|
|
|
|
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
2020-07-21 09:23:35 +02:00
|
|
|
/**
|
|
|
|
* Convert to JSON
|
|
|
|
*
|
2023-05-10 15:36:45 +02:00
|
|
|
* @return string The JSON.
|
2020-07-21 09:23:35 +02:00
|
|
|
*/
|
2018-12-20 11:33:08 +01:00
|
|
|
public function to_json() {
|
2020-05-12 19:42:09 +02:00
|
|
|
return \wp_json_encode( $this->to_array(), \JSON_HEX_TAG | \JSON_HEX_AMP | \JSON_HEX_QUOT );
|
2018-12-20 11:33:08 +01:00
|
|
|
}
|
|
|
|
|
2023-05-10 15:36:45 +02:00
|
|
|
/**
|
|
|
|
* Convert to a Simple Array.
|
|
|
|
*
|
|
|
|
* @return string The array.
|
|
|
|
*/
|
2018-12-20 11:33:08 +01:00
|
|
|
public function to_simple_array() {
|
2019-03-09 01:22:49 +01:00
|
|
|
$activity = array(
|
2018-12-20 11:33:08 +01:00
|
|
|
'@context' => $this->context,
|
|
|
|
'type' => $this->type,
|
|
|
|
'actor' => $this->actor,
|
|
|
|
'object' => $this->object,
|
|
|
|
'to' => $this->to,
|
2019-03-14 23:10:11 +01:00
|
|
|
'cc' => $this->cc,
|
2018-12-20 11:33:08 +01:00
|
|
|
);
|
2019-03-09 01:22:49 +01:00
|
|
|
|
|
|
|
if ( $this->id ) {
|
|
|
|
$activity['id'] = $this->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $activity;
|
2018-12-20 11:33:08 +01:00
|
|
|
}
|
|
|
|
|
2023-05-10 15:36:45 +02:00
|
|
|
/**
|
|
|
|
* Convert to a Simple JSON.
|
|
|
|
*
|
|
|
|
* @return string The JSON.
|
|
|
|
*/
|
2018-12-20 11:33:08 +01:00
|
|
|
public function to_simple_json() {
|
2020-05-12 19:42:09 +02:00
|
|
|
return \wp_json_encode( $this->to_simple_array(), \JSON_HEX_TAG | \JSON_HEX_AMP | \JSON_HEX_QUOT );
|
2018-12-20 11:33:08 +01:00
|
|
|
}
|
|
|
|
}
|