Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add/event objects #629

Merged
merged 39 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
564ea85
remove redundant property definitions
Menrath Jan 2, 2024
bef8ce5
Add redused context for actors.
Menrath Jan 2, 2024
700494a
Add classes to construct Moblizon compatible events
Menrath Jan 2, 2024
1f89d90
Bind the context to the activitypub object
Menrath Jan 2, 2024
847d4d0
fix phpcs
Menrath Jan 2, 2024
f1a25c3
Remove PostalAddress object: it's enough (at least atm) to directly w…
Menrath Jan 3, 2024
15c1f03
Remove _context property from ActivityPub objects in favour of getter…
Menrath Jan 3, 2024
0d3ff18
fix unit tests: ActivityPub Activity objects have a custom getter for…
Menrath Jan 3, 2024
aa1214d
fix phpcs
Menrath Jan 3, 2024
7f73253
fix unit-tests to also support php5.6
Menrath Jan 3, 2024
8ad599e
fix phpcs
Menrath Jan 3, 2024
e00b6b3
add param include_json_ld_context to to_array function
Menrath Jan 3, 2024
2e2e0d9
propagate the param include_json_ld_context to nested calls of to_array.
Menrath Jan 3, 2024
93265c3
fix phpcs
Menrath Jan 3, 2024
4f18d9a
Nested AcitivityPub objects: never build context in inner items in to…
Menrath Jan 3, 2024
b7a9045
Merge commit '4c297acfd454a13260900ff0f97a9e3f425e960c' into add/even…
Menrath Jan 5, 2024
9f25e0a
fix: param of set_address may also be an array
Menrath Jan 5, 2024
e76d2d5
fix typo in comment
Menrath Jan 13, 2024
b76820e
always prefix json-ld context with json-ld and move event class to su…
Menrath Jan 14, 2024
960ed53
fix usage of reserved object keyword
Menrath Jan 14, 2024
17765bd
Merge commit 'b2271cda6b857f879e0abd4f3c6683642d725267' into add/even…
Menrath Jan 14, 2024
4807c7d
Merge commit 'b2271cda6b857f879e0abd4f3c6683642d725267' into add/even…
Menrath Jan 15, 2024
7105c56
Fix calling non-static function as static
Menrath Jan 15, 2024
68a82cb
Partly fix Json-LD contexts in collections
Menrath Jan 15, 2024
9ea5ddf
Merge branch 'add/event-objects' of github.com:Menrath/wordpress-acti…
Menrath Jan 15, 2024
ae65b76
Update includes/activity/class-base-object.php
pfefferle Jan 17, 2024
e8db7e9
this is implicit
pfefferle Jan 17, 2024
7ed17c0
this change prevents the Activity to re-use Object vars
pfefferle Jan 17, 2024
76be832
add `$include_json_ld_context` support to `to_json`
pfefferle Jan 17, 2024
24bbd07
disable some more contexts
pfefferle Jan 17, 2024
14145fc
remove whitespace
pfefferle Jan 17, 2024
6728d74
Add php-comment for 7ed17c042a2651e08afc790adbdfc5ccf46c2708
Menrath Jan 17, 2024
4957294
Fix JSON-LD context for ActivityPub objects: child classes may overri…
Menrath Jan 17, 2024
278c8da
coding standards
pfefferle Jan 17, 2024
b97d79d
Merge branch 'master' into add/event-objects
pfefferle Jan 18, 2024
ddd1956
call folder/namespace `Extended_Object`
pfefferle Jan 18, 2024
403049b
fix: unnessesary nesting of extended-objects
Menrath Jan 18, 2024
5bebaf1
remove license
pfefferle Jan 18, 2024
0d090e7
Merge branch 'master' into add/event-objects
pfefferle Jan 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 16 additions & 62 deletions includes/activity/class-activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,75 +17,15 @@
* @see https://www.w3.org/TR/activitystreams-core/#intransitiveactivities
*/
class Activity extends Base_Object {
const CONTEXT = array(
const JSON_LD_CONTEXT = array(
'https://www.w3.org/ns/activitystreams',
'https://w3id.org/security/v1',
'https://purl.archive.org/socialweb/webfinger',
array(
'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
'PropertyValue' => 'schema:PropertyValue',
'schema' => 'http://schema.org#',
'pt' => 'https://joinpeertube.org/ns#',
'toot' => 'http://joinmastodon.org/ns#',
'litepub' => 'http://litepub.social/ns#',
'lemmy' => 'https://join-lemmy.org/ns#',
'value' => 'schema:value',
'Hashtag' => 'as:Hashtag',
'featured' => array(
'@id' => 'toot:featured',
'@type' => '@id',
),
'featuredTags' => array(
'@id' => 'toot:featuredTags',
'@type' => '@id',
),
'alsoKnownAs' => array(
'@id' => 'as:alsoKnownAs',
'@type' => '@id',
),
'moderators' => array(
'@id' => 'lemmy:moderators',
'@type' => '@id',
),
'postingRestrictedToMods' => 'lemmy:postingRestrictedToMods',
'discoverable' => 'toot:discoverable',
'indexable' => 'toot:indexable',
'sensitive' => 'as:sensitive',
),
);

/**
* 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
Expand All @@ -94,7 +34,7 @@ class Activity extends Base_Object {
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-object-term
*
* @var string
* | Base_Objectr
* | Base_Object
* | Link
* | null
*/
Expand Down Expand Up @@ -225,4 +165,18 @@ public function set_object( $object ) {
$this->set( 'id', $object->get_id() . '#activity' );
}
}

/**
* The context of an Activity is usually just the context of the object it contains.
*
* @return array $context A compacted JSON-LD context.
*/
public function get_json_ld_context() {
if ( $this->object instanceof Base_Object ) {
// Without php 5.6 support this could be just: 'return $this->object::JSON_LD_CONTEXT;'
return call_user_func( array( get_class( $this->object ), 'CONTEXT' ) );
} else {
return self::JSON_LD_CONTEXT;
}
}
}
35 changes: 35 additions & 0 deletions includes/activity/class-actor.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,39 @@
* @see https://www.w3.org/TR/activitystreams-vocabulary/#actor-types
*/
class Actor extends Base_Object {
// Reduced context for actors. TODO: still unused.
const JSON_LD_CONTEXT = array(
'https://www.w3.org/ns/activitystreams',
'https://w3id.org/security/v1',
'https://purl.archive.org/socialweb/webfinger',
array(
'schema' => 'http://schema.org#',
'toot' => 'http://joinmastodon.org/ns#',
'webfinger' => 'https://webfinger.net/#',
'lemmy' => 'https://join-lemmy.org/ns#',
'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
'PropertyValue' => 'schema:PropertyValue',
'value' => 'schema:value',
'Hashtag' => 'as:Hashtag',
'featured' => array(
'@id' => 'toot:featured',
'@type' => '@id',
),
'featuredTags' => array(
'@id' => 'toot:featuredTags',
'@type' => '@id',
),
'moderators' => array(
'@id' => 'lemmy:moderators',
'@type' => '@id',
),
'postingRestrictedToMods' => 'lemmy:postingRestrictedToMods',
'discoverable' => 'toot:discoverable',
'indexable' => 'toot:indexable',
'resource' => 'webfinger:resource',
),
);

/**
* @var string
*/
Expand Down Expand Up @@ -133,6 +166,8 @@ class Actor extends Base_Object {
*
* @see https://docs.joinmastodon.org/spec/activitypub/#as
*
* @context as:manuallyApprovesFollowers
*
* @var boolean
*/
protected $manually_approves_followers = false;
Expand Down
35 changes: 26 additions & 9 deletions includes/activity/class-base-object.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use WP_Error;
use ReflectionClass;
use DateTime;

use function Activitypub\camel_to_snake_case;
use function Activitypub\snake_to_camel_case;
Expand All @@ -26,6 +27,13 @@
* @see https://www.w3.org/TR/activitystreams-core/#object
*/
class Base_Object {
const JSON_LD_CONTEXT = array(
'https://www.w3.org/ns/activitystreams',
array(
'Hashtag' => 'as:Hashtag',
),
);

/**
* The object's unique global identifier
*
Expand Down Expand Up @@ -457,7 +465,7 @@ public function __call( $method, $params ) {
}

if ( \strncasecmp( $method, 'set', 3 ) === 0 ) {
$this->set( $var, $params[0] );
return $this->set( $var, $params[0] );
}

if ( \strncasecmp( $method, 'add', 3 ) === 0 ) {
Expand Down Expand Up @@ -524,7 +532,7 @@ public function set( $key, $value ) {

$this->$key = $value;

return $this->$key;
return $this;
pfefferle marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -622,9 +630,11 @@ public function from_array( $array ) {
* It tries to get the object attributes if they exist
* and falls back to the getters. Empty values are ignored.
*
* @param bool $include_json_ld_context
*
* @return array An array built from the Object.
*/
public function to_array() {
public function to_array( $include_json_ld_context = true ) {
pfefferle marked this conversation as resolved.
Show resolved Hide resolved
$array = array();
$vars = get_object_vars( $this );

Expand All @@ -640,7 +650,7 @@ public function to_array() {
}

if ( is_object( $value ) ) {
$value = $value->to_array();
$value = $value->to_array( false );
}

// if value is still empty, ignore it for the array and continue.
Expand All @@ -649,11 +659,9 @@ public function to_array() {
}
}

// replace 'context' key with '@context' and move it to the top.
if ( array_key_exists( 'context', $array ) ) {
$context = $array['context'];
unset( $array['context'] );
$array = array_merge( array( '@context' => $context ), $array );
if ( $include_json_ld_context ) {
// Get JsonLD context and move it to '@context' at the top.
$array = array_merge( array( '@context' => $this->get_json_ld_context() ), $array );
}

$class = new ReflectionClass( $this );
Expand Down Expand Up @@ -692,4 +700,13 @@ public function to_json() {
public function get_object_var_keys() {
return \array_keys( \get_object_vars( $this ) );
}

/**
* Returns the JSON-LD context of this object.
*
* @return array $context A compacted JSON-LD context for the ActivityPub object.
*/
public function get_json_ld_context() {
return $this::JSON_LD_CONTEXT;
pfefferle marked this conversation as resolved.
Show resolved Hide resolved
}
}
3 changes: 3 additions & 0 deletions includes/activity/objects/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This folder contains more complex ActivityPub objects.

Although they could be created using the Base_Object, they provide an easier structure for creating objects with maximum compatibility.
Loading
Loading