2023-05-15 10:48:34 +02:00
|
|
|
<?php
|
|
|
|
namespace Activitypub\Model;
|
|
|
|
|
2023-05-24 16:32:00 +02:00
|
|
|
use WP_Query;
|
|
|
|
use WP_Error;
|
2023-05-24 17:27:46 +02:00
|
|
|
use Activitypub\Signature;
|
2023-07-03 11:20:44 +02:00
|
|
|
use Activitypub\Collection\Users;
|
2023-06-28 14:22:27 +02:00
|
|
|
use Activitypub\Activity\Actor;
|
2023-05-24 16:32:00 +02:00
|
|
|
|
2023-06-28 14:22:27 +02:00
|
|
|
use function Activitypub\is_user_disabled;
|
2023-05-24 16:32:00 +02:00
|
|
|
use function Activitypub\get_rest_url_by_path;
|
|
|
|
|
2023-06-28 14:22:27 +02:00
|
|
|
class User extends Actor {
|
2023-05-15 10:48:34 +02:00
|
|
|
/**
|
2023-07-03 11:20:44 +02:00
|
|
|
* The local User-ID (WP_User).
|
2023-05-15 10:48:34 +02:00
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
2023-06-28 14:22:27 +02:00
|
|
|
protected $_id; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
|
2023-05-15 10:48:34 +02:00
|
|
|
|
2023-08-09 13:07:30 +02:00
|
|
|
/**
|
|
|
|
* The Featured-Posts.
|
|
|
|
*
|
|
|
|
* @see https://docs.joinmastodon.org/spec/activitypub/#featured
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $featured;
|
|
|
|
|
2023-09-22 09:38:59 +02:00
|
|
|
/**
|
|
|
|
* Moderators endpoint.
|
|
|
|
*
|
|
|
|
* @see https://join-lemmy.org/docs/contributors/05-federation.html
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $moderators;
|
|
|
|
|
2023-08-30 21:14:57 +02:00
|
|
|
/**
|
2023-09-01 18:32:56 +02:00
|
|
|
* The User-Type
|
2023-08-30 21:14:57 +02:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2023-12-12 17:00:35 +01:00
|
|
|
protected $type = 'Group';
|
2023-08-30 21:14:57 +02:00
|
|
|
|
2023-05-15 10:48:34 +02:00
|
|
|
/**
|
2023-09-01 18:32:56 +02:00
|
|
|
* If the User is discoverable.
|
2023-05-15 10:48:34 +02:00
|
|
|
*
|
2023-09-22 09:38:59 +02:00
|
|
|
* @see https://docs.joinmastodon.org/spec/activitypub/#discoverable
|
|
|
|
*
|
2023-09-01 18:32:56 +02:00
|
|
|
* @var boolean
|
2023-05-15 10:48:34 +02:00
|
|
|
*/
|
2023-09-01 18:32:56 +02:00
|
|
|
protected $discoverable = true;
|
|
|
|
|
2023-09-22 09:38:59 +02:00
|
|
|
/**
|
|
|
|
* If the User is indexable.
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $indexable;
|
|
|
|
|
2023-09-01 18:32:56 +02:00
|
|
|
/**
|
|
|
|
* The WebFinger Resource.
|
|
|
|
*
|
|
|
|
* @var string<url>
|
|
|
|
*/
|
|
|
|
protected $resource;
|
2023-05-24 16:32:00 +02:00
|
|
|
|
2023-09-22 09:38:59 +02:00
|
|
|
/**
|
|
|
|
* Restrict posting to mods
|
|
|
|
*
|
|
|
|
* @see https://join-lemmy.org/docs/contributors/05-federation.html
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $posting_restricted_to_mods = null;
|
|
|
|
|
2023-06-28 14:22:27 +02:00
|
|
|
public static function from_wp_user( $user_id ) {
|
2023-07-11 09:09:37 +02:00
|
|
|
if ( is_user_disabled( $user_id ) ) {
|
2023-06-28 16:43:41 +02:00
|
|
|
return new WP_Error(
|
|
|
|
'activitypub_user_not_found',
|
|
|
|
\__( 'User not found', 'activitypub' ),
|
|
|
|
array( 'status' => 404 )
|
|
|
|
);
|
2023-06-28 14:22:27 +02:00
|
|
|
}
|
2023-05-24 16:32:00 +02:00
|
|
|
|
2023-06-28 14:22:27 +02:00
|
|
|
$object = new static();
|
|
|
|
$object->_id = $user_id;
|
|
|
|
|
|
|
|
return $object;
|
2023-05-24 16:32:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the User-ID.
|
|
|
|
*
|
|
|
|
* @return string The User-ID.
|
|
|
|
*/
|
|
|
|
public function get_id() {
|
|
|
|
return $this->get_url();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the User-Name.
|
|
|
|
*
|
|
|
|
* @return string The User-Name.
|
|
|
|
*/
|
|
|
|
public function get_name() {
|
2023-06-28 14:22:27 +02:00
|
|
|
return \esc_attr( \get_the_author_meta( 'display_name', $this->_id ) );
|
2023-05-24 16:32:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the User-Description.
|
|
|
|
*
|
|
|
|
* @return string The User-Description.
|
|
|
|
*/
|
|
|
|
public function get_summary() {
|
2023-06-28 14:22:27 +02:00
|
|
|
$description = get_user_meta( $this->_id, 'activitypub_user_description', true );
|
2023-05-24 16:32:00 +02:00
|
|
|
if ( empty( $description ) ) {
|
2023-06-28 14:22:27 +02:00
|
|
|
$description = get_user_meta( $this->_id, 'description', true );
|
2023-05-24 16:32:00 +02:00
|
|
|
}
|
|
|
|
return \wpautop( \wp_kses( $description, 'default' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the User-Url.
|
|
|
|
*
|
|
|
|
* @return string The User-Url.
|
|
|
|
*/
|
|
|
|
public function get_url() {
|
2023-06-28 14:22:27 +02:00
|
|
|
return \esc_url( \get_author_posts_url( $this->_id ) );
|
2023-05-30 10:22:01 +02:00
|
|
|
}
|
|
|
|
|
2023-08-16 01:22:58 +02:00
|
|
|
/**
|
|
|
|
* Returns the User-URL with @-Prefix for the username.
|
|
|
|
*
|
|
|
|
* @return string The User-URL with @-Prefix for the username.
|
|
|
|
*/
|
2023-12-21 10:04:15 +01:00
|
|
|
public function get_alternate_url() {
|
|
|
|
return \esc_url( \trailingslashit( get_home_url() ) . '@' . $this->get_preferred_username() );
|
2023-05-30 11:37:21 +02:00
|
|
|
}
|
|
|
|
|
2023-06-28 14:22:27 +02:00
|
|
|
public function get_preferred_username() {
|
|
|
|
return \esc_attr( \get_the_author_meta( 'login', $this->_id ) );
|
2023-05-24 16:32:00 +02:00
|
|
|
}
|
|
|
|
|
2023-06-28 14:22:27 +02:00
|
|
|
public function get_icon() {
|
|
|
|
$icon = \esc_url(
|
2023-05-24 16:32:00 +02:00
|
|
|
\get_avatar_url(
|
2023-06-28 14:22:27 +02:00
|
|
|
$this->_id,
|
2023-05-24 16:32:00 +02:00
|
|
|
array( 'size' => 120 )
|
|
|
|
)
|
|
|
|
);
|
2023-06-28 14:22:27 +02:00
|
|
|
|
|
|
|
return array(
|
|
|
|
'type' => 'Image',
|
|
|
|
'url' => $icon,
|
|
|
|
);
|
2023-05-24 16:32:00 +02:00
|
|
|
}
|
|
|
|
|
2023-06-28 14:22:27 +02:00
|
|
|
public function get_image() {
|
2023-05-24 16:32:00 +02:00
|
|
|
if ( \has_header_image() ) {
|
2023-06-28 14:22:27 +02:00
|
|
|
$image = \esc_url( \get_header_image() );
|
|
|
|
return array(
|
|
|
|
'type' => 'Image',
|
|
|
|
'url' => $image,
|
|
|
|
);
|
2023-05-24 16:32:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_published() {
|
2023-06-28 14:22:27 +02:00
|
|
|
return \gmdate( 'Y-m-d\TH:i:s\Z', \strtotime( \get_the_author_meta( 'registered', $this->_id ) ) );
|
2023-05-24 16:32:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function get_public_key() {
|
2023-06-28 18:02:14 +02:00
|
|
|
return array(
|
|
|
|
'id' => $this->get_id() . '#main-key',
|
|
|
|
'owner' => $this->get_id(),
|
2023-09-07 22:04:39 +02:00
|
|
|
'publicKeyPem' => Signature::get_public_key_for( $this->get__id() ),
|
2023-06-28 18:02:14 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-05-24 16:32:00 +02:00
|
|
|
/**
|
2023-06-28 16:43:41 +02:00
|
|
|
* Returns the Inbox-API-Endpoint.
|
2023-05-24 16:32:00 +02:00
|
|
|
*
|
2023-06-28 16:43:41 +02:00
|
|
|
* @return string The Inbox-Endpoint.
|
|
|
|
*/
|
|
|
|
public function get_inbox() {
|
|
|
|
return get_rest_url_by_path( sprintf( 'users/%d/inbox', $this->get__id() ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the Outbox-API-Endpoint.
|
2023-05-24 16:32:00 +02:00
|
|
|
*
|
2023-06-28 16:43:41 +02:00
|
|
|
* @return string The Outbox-Endpoint.
|
2023-05-24 16:32:00 +02:00
|
|
|
*/
|
2023-06-28 16:43:41 +02:00
|
|
|
public function get_outbox() {
|
|
|
|
return get_rest_url_by_path( sprintf( 'users/%d/outbox', $this->get__id() ) );
|
|
|
|
}
|
2023-05-24 16:32:00 +02:00
|
|
|
|
2023-06-28 16:43:41 +02:00
|
|
|
/**
|
|
|
|
* Returns the Followers-API-Endpoint.
|
|
|
|
*
|
|
|
|
* @return string The Followers-Endpoint.
|
|
|
|
*/
|
|
|
|
public function get_followers() {
|
|
|
|
return get_rest_url_by_path( sprintf( 'users/%d/followers', $this->get__id() ) );
|
2023-05-24 16:32:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-06-28 16:43:41 +02:00
|
|
|
* Returns the Following-API-Endpoint.
|
2023-05-24 16:32:00 +02:00
|
|
|
*
|
2023-06-28 16:43:41 +02:00
|
|
|
* @return string The Following-Endpoint.
|
|
|
|
*/
|
|
|
|
public function get_following() {
|
|
|
|
return get_rest_url_by_path( sprintf( 'users/%d/following', $this->get__id() ) );
|
|
|
|
}
|
|
|
|
|
2023-08-09 13:07:30 +02:00
|
|
|
/**
|
|
|
|
* Returns the Featured-API-Endpoint.
|
|
|
|
*
|
|
|
|
* @return string The Featured-Endpoint.
|
|
|
|
*/
|
|
|
|
public function get_featured() {
|
|
|
|
return get_rest_url_by_path( sprintf( 'users/%d/collections/featured', $this->get__id() ) );
|
|
|
|
}
|
|
|
|
|
2023-06-28 16:43:41 +02:00
|
|
|
/**
|
|
|
|
* Extend the User-Output with Attachments.
|
2023-05-24 16:32:00 +02:00
|
|
|
*
|
|
|
|
* @return array The extended User-Output.
|
|
|
|
*/
|
2023-06-28 16:43:41 +02:00
|
|
|
public function get_attachment() {
|
|
|
|
$array = array();
|
2023-05-24 16:32:00 +02:00
|
|
|
|
2023-06-28 16:43:41 +02:00
|
|
|
$array[] = array(
|
2023-05-24 16:32:00 +02:00
|
|
|
'type' => 'PropertyValue',
|
|
|
|
'name' => \__( 'Blog', 'activitypub' ),
|
|
|
|
'value' => \html_entity_decode(
|
|
|
|
'<a rel="me" title="' . \esc_attr( \home_url( '/' ) ) . '" target="_blank" href="' . \home_url( '/' ) . '">' . \wp_parse_url( \home_url( '/' ), \PHP_URL_HOST ) . '</a>',
|
|
|
|
\ENT_QUOTES,
|
|
|
|
'UTF-8'
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2023-06-28 16:43:41 +02:00
|
|
|
$array[] = array(
|
2023-05-24 16:32:00 +02:00
|
|
|
'type' => 'PropertyValue',
|
|
|
|
'name' => \__( 'Profile', 'activitypub' ),
|
|
|
|
'value' => \html_entity_decode(
|
2023-06-28 16:43:41 +02:00
|
|
|
'<a rel="me" title="' . \esc_attr( \get_author_posts_url( $this->get__id() ) ) . '" target="_blank" href="' . \get_author_posts_url( $this->get__id() ) . '">' . \wp_parse_url( \get_author_posts_url( $this->get__id() ), \PHP_URL_HOST ) . '</a>',
|
2023-05-24 16:32:00 +02:00
|
|
|
\ENT_QUOTES,
|
|
|
|
'UTF-8'
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2023-06-28 16:43:41 +02:00
|
|
|
if ( \get_the_author_meta( 'user_url', $this->get__id() ) ) {
|
|
|
|
$array[] = array(
|
2023-05-24 16:32:00 +02:00
|
|
|
'type' => 'PropertyValue',
|
|
|
|
'name' => \__( 'Website', 'activitypub' ),
|
|
|
|
'value' => \html_entity_decode(
|
2023-06-28 16:43:41 +02:00
|
|
|
'<a rel="me" title="' . \esc_attr( \get_the_author_meta( 'user_url', $this->get__id() ) ) . '" target="_blank" href="' . \get_the_author_meta( 'user_url', $this->get__id() ) . '">' . \wp_parse_url( \get_the_author_meta( 'user_url', $this->get__id() ), \PHP_URL_HOST ) . '</a>',
|
2023-05-24 16:32:00 +02:00
|
|
|
\ENT_QUOTES,
|
|
|
|
'UTF-8'
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $array;
|
|
|
|
}
|
2023-05-31 08:45:14 +02:00
|
|
|
|
2023-08-16 14:52:26 +02:00
|
|
|
/**
|
|
|
|
* Returns a user@domain type of identifier for the user.
|
|
|
|
*
|
|
|
|
* @return string The Webfinger-Identifier.
|
|
|
|
*/
|
2023-05-31 08:45:14 +02:00
|
|
|
public function get_resource() {
|
2023-06-28 14:22:27 +02:00
|
|
|
return $this->get_preferred_username() . '@' . \wp_parse_url( \home_url(), \PHP_URL_HOST );
|
2023-05-31 08:45:14 +02:00
|
|
|
}
|
2023-06-28 18:02:14 +02:00
|
|
|
|
|
|
|
public function get_canonical_url() {
|
|
|
|
return $this->get_url();
|
|
|
|
}
|
2023-09-22 09:38:59 +02:00
|
|
|
|
|
|
|
public function get_streams() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-09-28 14:38:48 +02:00
|
|
|
public function get_tag() {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2023-09-22 09:38:59 +02:00
|
|
|
public function get_indexable() {
|
|
|
|
if ( \get_option( 'blog_public', 1 ) ) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2023-05-15 10:48:34 +02:00
|
|
|
}
|