Skip to content

Commit ade7adc

Browse files
committed
better casting on Activity->set_object
1 parent 24e2de7 commit ade7adc

File tree

5 files changed

+132
-62
lines changed

5 files changed

+132
-62
lines changed

includes/activity/class-activity.php

Lines changed: 90 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
namespace Activitypub\Activity;
1111

12-
use Activitypub\Link;
12+
use Activitypub\Activity\Extended_Object\Event;
13+
use Activitypub\Activity\Extended_Object\Place;
1314

1415
/**
1516
* \Activitypub\Activity\Activity implements the common
@@ -23,6 +24,43 @@ class Activity extends Base_Object {
2324
'https://www.w3.org/ns/activitystreams',
2425
);
2526

27+
/**
28+
* The default types for Activities.
29+
*
30+
* @see https://www.w3.org/TR/activitystreams-vocabulary/#activity-types
31+
*
32+
* @var array
33+
*/
34+
const DEFAULT_TYPES = array(
35+
'Accept',
36+
'Add',
37+
'Announce',
38+
'Arrive',
39+
'Block',
40+
'Create',
41+
'Delete',
42+
'Dislike',
43+
'Follow',
44+
'Flag',
45+
'Ignore',
46+
'Invite',
47+
'Join',
48+
'Leave',
49+
'Like',
50+
'Listen',
51+
'Move',
52+
'Offer',
53+
'Read',
54+
'Reject',
55+
'Remove',
56+
'TentativeAccept',
57+
'TentativeReject',
58+
'Travel',
59+
'Undo',
60+
'Update',
61+
'View',
62+
);
63+
2664
/**
2765
* The type of the object.
2866
*
@@ -124,58 +162,85 @@ class Activity extends Base_Object {
124162
*
125163
* @see https://www.w3.org/TR/activitypub/#object-without-create
126164
*
127-
* @param array|string|Base_Object|Link|null $data Activity object.
165+
* @param array|string|Base_Object|Activity|Actor|null $data Activity object.
166+
* @param bool $pre_fill Whether to pre-fill the activity with object properties.
128167
*/
129-
public function set_object( $data ) {
130-
// Convert array to object.
168+
public function set_object( $data, $pre_fill = true ) {
169+
$object = $data;
170+
171+
// Convert array to appropriate object type.
131172
if ( is_array( $data ) ) {
132-
$data = Generic_Object::init_from_array( $data );
173+
$type = $data['type'] ?? null;
174+
175+
if ( in_array( $type, self::DEFAULT_TYPES, true ) ) {
176+
$object = self::init_from_array( $data );
177+
} elseif ( in_array( $type, Actor::DEFAULT_TYPES, true ) ) {
178+
$object = Actor::init_from_array( $data );
179+
} elseif ( in_array( $type, Base_Object::DEFAULT_TYPES, true ) ) {
180+
$object = match ( $type ) {
181+
'Event' => Event::init_from_array( $data ),
182+
'Place' => Place::init_from_array( $data ),
183+
default => Base_Object::init_from_array( $data ),
184+
};
185+
} else {
186+
$object = Generic_Object::init_from_array( $data );
187+
}
188+
}
189+
190+
$this->set( 'object', $object );
191+
192+
if ( $pre_fill ) {
193+
$this->pre_fill_activity_from_object();
133194
}
195+
}
134196

135-
// Set object.
136-
$this->set( 'object', $data );
197+
/**
198+
* Fills the Activity with the specified activity object.
199+
*/
200+
public function pre_fill_activity_from_object() {
201+
$object = $this->get_object();
137202

138203
// Check if `$data` is a URL and use it to generate an ID then.
139-
if ( is_string( $data ) && filter_var( $data, FILTER_VALIDATE_URL ) && ! $this->get_id() ) {
140-
$this->set( 'id', $data . '#activity-' . strtolower( $this->get_type() ) . '-' . time() );
204+
if ( is_string( $object ) && filter_var( $object, FILTER_VALIDATE_URL ) && ! $this->get_id() ) {
205+
$this->set( 'id', $object . '#activity-' . strtolower( $this->get_type() ) . '-' . time() );
141206

142207
return;
143208
}
144209

145210
// Check if `$data` is an object and copy some properties otherwise do nothing.
146-
if ( ! is_object( $data ) ) {
211+
if ( ! is_object( $object ) ) {
147212
return;
148213
}
149214

150215
foreach ( array( 'to', 'bto', 'cc', 'bcc', 'audience' ) as $i ) {
151-
$value = $data->get( $i );
216+
$value = $object->get( $i );
152217
if ( $value && ! $this->get( $i ) ) {
153218
$this->set( $i, $value );
154219
}
155220
}
156221

157-
if ( $data->get_published() && ! $this->get_published() ) {
158-
$this->set( 'published', $data->get_published() );
222+
if ( $object->get_published() && ! $this->get_published() ) {
223+
$this->set( 'published', $object->get_published() );
159224
}
160225

161-
if ( $data->get_updated() && ! $this->get_updated() ) {
162-
$this->set( 'updated', $data->get_updated() );
226+
if ( $object->get_updated() && ! $this->get_updated() ) {
227+
$this->set( 'updated', $object->get_updated() );
163228
}
164229

165-
if ( $data->get_attributed_to() && ! $this->get_actor() ) {
166-
$this->set( 'actor', $data->get_attributed_to() );
230+
if ( $object->get_attributed_to() && ! $this->get_actor() ) {
231+
$this->set( 'actor', $object->get_attributed_to() );
167232
}
168233

169-
if ( $data->get_in_reply_to() && ! $this->get_in_reply_to() ) {
170-
$this->set( 'in_reply_to', $data->get_in_reply_to() );
234+
if ( $object->get_in_reply_to() && ! $this->get_in_reply_to() ) {
235+
$this->set( 'in_reply_to', $object->get_in_reply_to() );
171236
}
172237

173-
if ( $data->get_id() && ! $this->get_id() ) {
174-
$id = strtok( $data->get_id(), '#' );
175-
if ( $data->get_updated() ) {
176-
$updated = $data->get_updated();
177-
} elseif ( $data->get_published() ) {
178-
$updated = $data->get_published();
238+
if ( $object->get_id() && ! $this->get_id() ) {
239+
$id = strtok( $object->get_id(), '#' );
240+
if ( $object->get_updated() ) {
241+
$updated = $object->get_updated();
242+
} elseif ( $object->get_published() ) {
243+
$updated = $object->get_published();
179244
} else {
180245
$updated = time();
181246
}

includes/activity/class-actor.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,21 @@ class Actor extends Base_Object {
6161
),
6262
);
6363

64+
/**
65+
* The default types for Actors.
66+
*
67+
* @see https://www.w3.org/TR/activitystreams-vocabulary/#actor-types
68+
*
69+
* @var array
70+
*/
71+
const DEFAULT_TYPES = array(
72+
'Application',
73+
'Group',
74+
'Organization',
75+
'Person',
76+
'Service',
77+
);
78+
6479
/**
6580
* The type of the object.
6681
*

includes/activity/class-base-object.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,28 @@ class Base_Object extends Generic_Object {
7878
),
7979
);
8080

81+
/**
82+
* The default types for Objects.
83+
*
84+
* @see https://www.w3.org/TR/activitystreams-vocabulary/#object-types
85+
*
86+
* @var array
87+
*/
88+
const DEFAULT_TYPES = array(
89+
'Article',
90+
'Audio',
91+
'Document',
92+
'Event',
93+
'Image',
94+
'Note',
95+
'Page',
96+
'Place',
97+
'Profile',
98+
'Relationship',
99+
'Tombstone',
100+
'Video',
101+
);
102+
81103
/**
82104
* The type of the object.
83105
*

includes/collection/class-outbox.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77

88
namespace Activitypub\Collection;
99

10-
use Activitypub\Activity\Base_Object;
1110
use Activitypub\Dispatcher;
1211
use Activitypub\Scheduler;
1312
use Activitypub\Activity\Activity;
13+
use Activitypub\Activity\Base_Object;
14+
1415
use function Activitypub\add_to_outbox;
1516

1617
/**

includes/functions.php

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use WP_Error;
1111
use Activitypub\Activity\Activity;
12+
use Activitypub\Activity\Actor;
1213
use Activitypub\Activity\Base_Object;
1314
use Activitypub\Collection\Actors;
1415
use Activitypub\Collection\Outbox;
@@ -1537,35 +1538,7 @@ function is_activity( $data ) {
15371538
*/
15381539
$types = apply_filters(
15391540
'activitypub_activity_types',
1540-
array(
1541-
'Accept',
1542-
'Add',
1543-
'Announce',
1544-
'Arrive',
1545-
'Block',
1546-
'Create',
1547-
'Delete',
1548-
'Dislike',
1549-
'Follow',
1550-
'Flag',
1551-
'Ignore',
1552-
'Invite',
1553-
'Join',
1554-
'Leave',
1555-
'Like',
1556-
'Listen',
1557-
'Move',
1558-
'Offer',
1559-
'Read',
1560-
'Reject',
1561-
'Remove',
1562-
'TentativeAccept',
1563-
'TentativeReject',
1564-
'Travel',
1565-
'Undo',
1566-
'Update',
1567-
'View',
1568-
)
1541+
Activity::DEFAULT_TYPES
15691542
);
15701543

15711544
if ( is_string( $data ) ) {
@@ -1600,13 +1573,7 @@ function is_actor( $data ) {
16001573
*/
16011574
$types = apply_filters(
16021575
'activitypub_actor_types',
1603-
array(
1604-
'Application',
1605-
'Group',
1606-
'Organization',
1607-
'Person',
1608-
'Service',
1609-
)
1576+
Actor::DEFAULT_TYPES
16101577
);
16111578

16121579
if ( is_string( $data ) ) {

0 commit comments

Comments
 (0)