use collection instead of factory

This commit is contained in:
Matthias Pfefferle 2023-07-03 11:20:44 +02:00
parent f207089269
commit 359eabf671
28 changed files with 295 additions and 111 deletions

View file

@ -0,0 +1,165 @@
<?php
/**
* Inspired by the PHP ActivityPub Library by @Landrok
*
* @link https://github.com/landrok/activitypub
*/
namespace Activitypub\Activity;
/**
* \Activitypub\Activity\Activity implements the common
* attributes of an Activity.
*
* @see https://www.w3.org/TR/activitystreams-core/#activities
* @see https://www.w3.org/TR/activitystreams-core/#intransitiveactivities
*/
class Activity extends Base_Object {
const 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',
),
),
);
/**
* The object's unique global identifier
*
* @see https://www.w3.org/TR/activitypub/#obj-id
*
* @var string
*/
protected $id;
/**
* @var string
*/
protected $type = 'Activity';
/**
* The context within which the object exists or an activity was
* performed.
* The notion of "context" used is intentionally vague.
* The intended function is to serve as a means of grouping objects
* and activities that share a common originating context or
* purpose. An example could be all activities relating to a common
* project or event.
*
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-context
*
* @var string
* | ObjectType
* | Link
* | null
*/
protected $context = self::CONTEXT;
/**
* Describes the direct object of the activity.
* For instance, in the activity "John added a movie to his
* wishlist", the object of the activity is the movie added.
*
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-object-term
*
* @var string
* | ObjectType
* | Link
* | null
*/
protected $object;
/**
* Describes one or more entities that either performed or are
* expected to perform the activity.
* Any single activity can have multiple actors.
* The actor MAY be specified using an indirect Link.
*
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-actor
*
* @var string
* | \ActivityPhp\Type\Extended\AbstractActor
* | array<Actor>
* | array<Link>
* | Link
*/
protected $actor;
/**
* The indirect object, or target, of the activity.
* The precise meaning of the target is largely dependent on the
* type of action being described but will often be the object of
* the English preposition "to".
* For instance, in the activity "John added a movie to his
* wishlist", the target of the activity is John's wishlist.
* An activity can have more than one target.
*
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-target
*
* @var string
* | ObjectType
* | array<ObjectType>
* | Link
* | array<Link>
*/
protected $target;
/**
* Describes the result of the activity.
* For instance, if a particular action results in the creation of
* a new resource, the result property can be used to describe
* that new resource.
*
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-result
*
* @var string
* | ObjectType
* | Link
* | null
*/
protected $result;
/**
* An indirect object of the activity from which the
* activity is directed.
* The precise meaning of the origin is the object of the English
* preposition "from".
* For instance, in the activity "John moved an item to List B
* from List A", the origin of the activity is "List A".
*
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-origin
*
* @var string
* | ObjectType
* | Link
* | null
*/
protected $origin;
/**
* One or more objects used (or to be used) in the completion of an
* Activity.
*
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-instrument
*
* @var string
* | ObjectType
* | Link
* | null
*/
protected $instrument;
}

View file

@ -7,6 +7,14 @@
namespace Activitypub\Activity;
/**
* \Activitypub\Activity\Actor is an implementation of
* one an Activity Streams Actor.
*
* Represents an individual actor.
*
* @see https://www.w3.org/TR/activitystreams-vocabulary/#actor-types
*/
class Actor extends Base_Object {
/**
* @var string

View file

@ -3,7 +3,7 @@ namespace Activitypub;
use Activitypub\Model\Post;
use Activitypub\Model\Activity;
use Activitypub\User_Factory;
use Activitypub\Collection\Users;
use Activitypub\Collection\Followers;
use function Activitypub\safe_remote_post;

View file

@ -82,7 +82,7 @@ class Activitypub {
$json_template = false;
// check if user can publish posts
if ( \is_author() && ! User_Factory::get_by_id( \get_the_author_meta( 'ID' ) ) ) {
if ( \is_author() && ! Users::get_by_id( \get_the_author_meta( 'ID' ) ) ) {
return $template;
}

View file

@ -2,7 +2,7 @@
namespace Activitypub;
use WP_Error;
use Activitypub\User_Factory;
use Activitypub\Collection\Users;
/**
* ActivityPub HTTP Class
@ -63,7 +63,7 @@ class Http {
*/
public static function get( $url ) {
$date = \gmdate( 'D, d M Y H:i:s T' );
$signature = Signature::generate_signature( User_Factory::APPLICATION_USER_ID, 'get', $url, $date );
$signature = Signature::generate_signature( Users::APPLICATION_USER_ID, 'get', $url, $date );
$wp_version = \get_bloginfo( 'version' );
$user_agent = \apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . \get_bloginfo( 'url' ) );

View file

@ -3,6 +3,7 @@
namespace Activitypub;
use Activitypub\Model\Post;
use Activitypub\Collection\Users;
use Activitypub\Collection\Followers;
/**
@ -82,7 +83,7 @@ class Scheduler {
return;
}
$activitypub_post = new Post( $post, Users::BLOG_USER_ID );
$activitypub_post = new Post( $post );
\wp_schedule_single_event(
\time(),

View file

@ -5,7 +5,7 @@ use WP_Error;
use DateTime;
use DateTimeZone;
use Activitypub\Model\User;
use Activitypub\User_Factory;
use Activitypub\Collection\Users;
/**
* ActivityPub Signature Class
@ -106,7 +106,7 @@ class Signature {
* @return string The signature.
*/
public static function generate_signature( $user_id, $http_method, $url, $date, $digest = null ) {
$user = User_Factory::get_by_id( $user_id );
$user = Users::get_by_id( $user_id );
$key = $user->get__private_key();
$url_parts = \wp_parse_url( $url );
@ -136,7 +136,7 @@ class Signature {
\openssl_sign( $signed_string, $signature, $key, \OPENSSL_ALGO_SHA256 );
$signature = \base64_encode( $signature ); // phpcs:ignore
$user = User_Factory::get_by_id( $user_id );
$user = Users::get_by_id( $user_id );
$key_id = $user->get_url() . '#main-key';
if ( ! empty( $digest ) ) {

View file

@ -24,7 +24,7 @@ class Webfinger {
return \get_webfinger_resource( $user_id, false );
}
$user = User_Factory::get_by_id( $user_id );
$user = Users::get_by_id( $user_id );
if ( ! $user ) {
return '';
}

View file

@ -1,5 +1,5 @@
<?php
namespace Activitypub;
namespace Activitypub\Collection;
use WP_Error;
use WP_User_Query;
@ -7,7 +7,7 @@ use Activitypub\Model\User;
use Activitypub\Model\Blog_User;
use Activitypub\Model\Application_User;
class User_Factory {
class Users {
/**
* The ID of the Blog User
*

View file

@ -279,10 +279,10 @@ function is_activitypub_request() {
function is_user_disabled( $user_id ) {
switch ( $user_id ) {
// if the user is the application user, it's always enabled.
case \Activitypub\User_Factory::APPLICATION_USER_ID:
case \Activitypub\Collection\Users::APPLICATION_USER_ID:
return false;
// if the user is the blog user, it's only enabled in single-user mode.
case \Activitypub\User_Factory::BLOG_USER_ID:
case \Activitypub\Collection\Users::BLOG_USER_ID:
if ( defined( 'ACTIVITYPUB_DISABLE_BLOG_USER' ) ) {
return ACTIVITYPUB_DISABLE_BLOG_USER;
}

View file

@ -3,7 +3,7 @@ namespace Activitypub\Model;
use WP_Query;
use Activitypub\Signature;
use Activitypub\User_Factory;
use Activitypub\Collection\Users;
use function Activitypub\get_rest_url_by_path;
@ -13,7 +13,7 @@ class Application_User extends Blog_User {
*
* @var int
*/
protected $_id = User_Factory::APPLICATION_USER_ID; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
protected $_id = Users::APPLICATION_USER_ID; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
/**
* The User-Type

View file

@ -3,7 +3,7 @@ namespace Activitypub\Model;
use WP_Query;
use Activitypub\Signature;
use Activitypub\User_Factory;
use Activitypub\Collection\Users;
use function Activitypub\is_user_disabled;
@ -13,7 +13,7 @@ class Blog_User extends User {
*
* @var int
*/
protected $_id = User_Factory::BLOG_USER_ID; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
protected $_id = Users::BLOG_USER_ID; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
/**
* The User-Type

View file

@ -1,7 +1,7 @@
<?php
namespace Activitypub\Model;
use Activitypub\User_Factory;
use Activitypub\Collection\Users;
use function Activitypub\get_rest_url_by_path;
/**
@ -243,7 +243,7 @@ class Post {
* @return string The URL of the Actor.
*/
public function get_actor() {
$user = User_Factory::get_by_id( $this->get_user_id() );
$user = Users::get_by_id( $this->get_user_id() );
return $user->get_url();
}

View file

@ -5,7 +5,7 @@ use WP_Query;
use WP_Error;
use Activitypub\Signature;
use Activitypub\Model\User;
use Activitypub\User_Factory;
use Activitypub\Collection\Users;
use Activitypub\Activity\Actor;
use function Activitypub\is_user_disabled;
@ -13,7 +13,7 @@ use function Activitypub\get_rest_url_by_path;
class User extends Actor {
/**
* The User-ID
* The local User-ID (WP_User).
*
* @var int
*/

View file

@ -1,67 +0,0 @@
<?php
namespace Activitypub\Peer;
/**
* ActivityPub Users DB-Class
*
* @author Matthias Pfefferle
*/
class Users {
/**
* Undocumented function
*
* @return void
*/
public static function get_user_by_various( $data ) {
}
/**
* Examine a url and try to determine the author ID it represents.
*
* Checks are supposedly from the hosted site blog.
*
* @param string $url Permalink to check.
*
* @return int User ID, or 0 on failure.
*/
public static function url_to_authorid( $url ) {
global $wp_rewrite;
// check if url hase the same host
if ( \wp_parse_url( \site_url(), \PHP_URL_HOST ) !== \wp_parse_url( $url, \PHP_URL_HOST ) ) {
return 0;
}
// first, check to see if there is a 'author=N' to match against
if ( \preg_match( '/[?&]author=(\d+)/i', $url, $values ) ) {
$id = \absint( $values[1] );
if ( $id ) {
return $id;
}
}
// check to see if we are using rewrite rules
$rewrite = $wp_rewrite->wp_rewrite_rules();
// not using rewrite rules, and 'author=N' method failed, so we're out of options
if ( empty( $rewrite ) ) {
return 0;
}
// generate rewrite rule for the author url
$author_rewrite = $wp_rewrite->get_author_permastruct();
$author_regexp = \str_replace( '%author%', '', $author_rewrite );
// match the rewrite rule with the passed url
if ( \preg_match( '/https?:\/\/(.+)' . \preg_quote( $author_regexp, '/' ) . '([^\/]+)/i', $url, $match ) ) {
$user = \get_user_by( 'slug', $match[2] );
if ( $user ) {
return $user->ID;
}
}
return 0;
}
}

View file

@ -5,7 +5,7 @@ use WP_Error;
use stdClass;
use WP_REST_Server;
use WP_REST_Response;
use Activitypub\User_Factory;
use Activitypub\Collection\Users;
use Activitypub\Collection\Followers as FollowerCollection;
use function Activitypub\get_rest_url_by_path;
@ -52,7 +52,7 @@ class Followers {
*/
public static function get( $request ) {
$user_id = $request->get_param( 'user_id' );
$user = User_Factory::get_by_various( $user_id );
$user = Users::get_by_various( $user_id );
if ( is_wp_error( $user ) ) {
return $user;

View file

@ -1,7 +1,7 @@
<?php
namespace Activitypub\Rest;
use Activitypub\User_Factory;
use Activitypub\Collection\Users;
use function Activitypub\get_rest_url_by_path;
@ -47,7 +47,7 @@ class Following {
*/
public static function get( $request ) {
$user_id = $request->get_param( 'user_id' );
$user = User_Factory::get_by_various( $user_id );
$user = Users::get_by_various( $user_id );
if ( is_wp_error( $user ) ) {
return $user;

View file

@ -4,7 +4,7 @@ namespace Activitypub\Rest;
use WP_Error;
use WP_REST_Server;
use WP_REST_Response;
use Activitypub\User_Factory;
use Activitypub\Collection\Users;
use Activitypub\Model\Activity;
use function Activitypub\get_context;
@ -74,7 +74,7 @@ class Inbox {
*/
public static function user_inbox_get( $request ) {
$user_id = $request->get_param( 'user_id' );
$user = User_Factory::get_by_various( $user_id );
$user = Users::get_by_various( $user_id );
if ( is_wp_error( $user ) ) {
return $user;
@ -126,7 +126,7 @@ class Inbox {
public static function user_inbox_post( $request ) {
$user_id = $request->get_param( 'user_id' );
$user = User_Factory::get_by_various( $user_id );
$user = Users::get_by_various( $user_id );
if ( is_wp_error( $user ) ) {
return $user;

View file

@ -5,7 +5,7 @@ use stdClass;
use WP_Error;
use WP_REST_Server;
use WP_REST_Response;
use Activitypub\User_Factory;
use Activitypub\Collection\Users;
use Activitypub\Model\Post;
use Activitypub\Model\Activity;
@ -53,7 +53,7 @@ class Outbox {
*/
public static function user_outbox_get( $request ) {
$user_id = $request->get_param( 'user_id' );
$user = User_Factory::get_by_various( $user_id );
$user = Users::get_by_various( $user_id );
if ( is_wp_error( $user ) ) {
return $user;

View file

@ -4,7 +4,8 @@ namespace Activitypub\Rest;
use WP_Error;
use WP_REST_Server;
use WP_REST_Response;
use Activitypub\User_Factory;
use \Activitypub\Model\Activity;
use Activitypub\Collection\User_Collection;
use function Activitypub\is_activitypub_request;
@ -50,7 +51,7 @@ class Users {
*/
public static function get( $request ) {
$user_id = $request->get_param( 'user_id' );
$user = User_Factory::get_by_various( $user_id );
$user = User_Collection::get_by_various( $user_id );
if ( is_wp_error( $user ) ) {
return $user;
@ -68,7 +69,7 @@ class Users {
\do_action( 'activitypub_outbox_pre' );
$user->set_context(
\Activitypub\Model\Activity::CONTEXT
Activity::CONTEXT
);
$json = $user->to_array();

View file

@ -3,7 +3,7 @@ namespace Activitypub\Rest;
use WP_Error;
use WP_REST_Response;
use Activitypub\User_Factory;
use Activitypub\Collection\Users;
/**
* ActivityPub WebFinger REST-Class
@ -47,7 +47,7 @@ class Webfinger {
*/
public static function webfinger( $request ) {
$resource = $request->get_param( 'resource' );
$user = User_Factory::get_by_resource( $resource );
$user = Users::get_by_resource( $resource );
if ( is_wp_error( $user ) ) {
return $user;

View file

@ -2,7 +2,7 @@
namespace Activitypub\Table;
use WP_List_Table;
use Activitypub\User_Factory;
use Activitypub\Collection\Users;
use Activitypub\Collection\Followers as FollowerCollection;
if ( ! \class_exists( '\WP_List_Table' ) ) {
@ -14,7 +14,7 @@ class Followers extends WP_List_Table {
public function __construct() {
if ( get_current_screen()->id === 'settings_page_activitypub' ) {
$this->user_id = User_Factory::BLOG_USER_ID;
$this->user_id = Users::BLOG_USER_ID;
} else {
$this->user_id = \get_current_user_id();
}

View file

@ -0,0 +1,76 @@
<?php
namespace Activitypub\Transformer;
use \Activitypub\Model\User;
use function Activitypub\is_user_disabled;
use function Activitypub\get_rest_url_by_path;
class Wp_User {
protected $wp_user;
public function construct( WP_User $wp_user ) {
$this->wp_user = $wp_user;
}
public function to_user() {
$wp_user = $this->wp_user;
if (
is_user_disabled( $user->ID ) ||
! get_user_by( 'id', $user->ID )
) {
return new WP_Error(
'activitypub_user_not_found',
\__( 'User not found', 'activitypub' ),
array( 'status' => 404 )
);
}
$user = new User();
$user->setwp_user->ID( \esc_url( \get_author_posts_url( $wp_user->ID ) ) );
$user->set_url( \esc_url( \get_author_posts_url( $wp_user->ID ) ) );
$user->set_summary( $this->get_summary() );
$user->set_name( \esc_attr( $wp_user->display_name ) );
$user->set_preferred_username( \esc_attr( $wp_user->login ) );
$user->set_icon( $this->get_icon() );
$user->set_image( $this->get_image() );
return $user;
}
public function get_summary() {
$description = get_user_meta( $this->wp_user->ID, 'activitypub_user_description', true );
if ( empty( $description ) ) {
$description = $this->wp_user->description;
}
return \wpautop( \wp_kses( $description, 'default' ) );
}
public function get_icon() {
$icon = \esc_url(
\get_avatar_url(
$this->wp_user->ID,
array( 'size' => 120 )
)
);
return array(
'type' => 'Image',
'url' => $icon,
);
}
public function get_image() {
if ( \has_header_image() ) {
$image = \esc_url( \get_header_image() );
return array(
'type' => 'Image',
'url' => $image,
);
}
return null;
}
}

View file

@ -12,7 +12,7 @@
<?php \esc_html_e( 'Settings', 'activitypub' ); ?>
</a>
<?php if ( ! \Activitypub\is_user_disabled( \ActivityPub\User_Factory::BLOG_USER_ID ) ) : ?>
<?php if ( ! \Activitypub\is_user_disabled( \Activitypub\Collection\Users::BLOG_USER_ID ) ) : ?>
<a href="<?php echo \esc_url_raw( admin_url( 'options-general.php?page=activitypub&tab=followers' ) ); ?>" class="activitypub-settings-tab <?php echo \esc_attr( $args['followers'] ); ?>">
<?php \esc_html_e( 'Followers', 'activitypub' ); ?>

View file

@ -1,5 +1,5 @@
<?php
$user = \Activitypub\User_Factory::get_by_id( \get_the_author_meta( 'ID' ) );
$user = \Activitypub\Collection\Users::get_by_id( \get_the_author_meta( 'ID' ) );
$user->set_context(
\Activitypub\Model\Activity::CONTEXT

View file

@ -31,7 +31,7 @@
<form method="post" action="options.php">
<?php \settings_fields( 'activitypub' ); ?>
<?php if ( ! \Activitypub\is_user_disabled( \ActivityPub\User_Factory::BLOG_USER_ID ) ) : ?>
<?php if ( ! \Activitypub\is_user_disabled( \Activitypub\Collection\Users::BLOG_USER_ID ) ) : ?>
<h3><?php \esc_html_e( 'Blog-User', 'activitypub' ); ?></h3>

View file

@ -15,7 +15,7 @@
<p><?php \esc_html_e( 'With ActivityPub your blog becomes part of a federated social network. This means you can share and talk to everyone using the ActivityPub protocol, including users of Friendica, Pleroma and Mastodon.', 'activitypub' ); ?></p>
<?php if ( ! \Activitypub\is_user_disabled( \ActivityPub\User_Factory::BLOG_USER_ID ) ) : ?>
<?php if ( ! \Activitypub\is_user_disabled( \Activitypub\Collection\Users::BLOG_USER_ID ) ) : ?>
<h3><?php \esc_html_e( 'Blog Account', 'activitypub' ); ?></h3>
<p>
@ -44,7 +44,7 @@
<h3><?php \esc_html_e( 'Personal Account', 'activitypub' ); ?></h3>
<p>
<?php
$user = \Activitypub\User_Factory::get_by_id( wp_get_current_user()->ID );
$user = \Activitypub\Collection\Users::get_by_id( wp_get_current_user()->ID );
echo wp_kses(
\sprintf(
// translators:

View file

@ -42,7 +42,7 @@ class Test_Activitypub_Signature_Verification extends WP_UnitTestCase {
$signed_headers = $signature_block['headers'];
$signed_data = Activitypub\Signature::get_signed_data( $signed_headers, $signature_block, $headers );
$user = Activitypub\User_Factory::get_by_id( 1 );
$user = Activitypub\Collection\Users::get_by_id( 1 );
$public_key = $user->get__public_key();
@ -55,7 +55,7 @@ class Test_Activitypub_Signature_Verification extends WP_UnitTestCase {
add_filter(
'pre_get_remote_metadata_by_actor',
function( $json, $actor ) {
$user = Activitypub\User_Factory::get_by_id( 1 );
$user = Activitypub\Collection\Users::get_by_id( 1 );
$public_key = $user->get__public_key();
// return ActivityPub Profile with signature
return array(