Skip to content

Commit 0d635d5

Browse files
authored
More Group meta-data to play nicely with existing platforms (#441)
* more group friendly settings * change http code * Fix Actor-Type * fix check if value is set * only ignore null * better posting_restricted_to_mods handling * remove user namespace from moderators endpoint thanks for the feedback @mattwiebe
1 parent dd29775 commit 0d635d5

File tree

6 files changed

+122
-18
lines changed

6 files changed

+122
-18
lines changed

includes/activity/class-activity.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Activity extends Base_Object {
2828
'toot' => 'http://joinmastodon.org/ns#',
2929
'webfinger' => 'https://webfinger.net/#',
3030
'litepub' => 'http://litepub.social/ns#',
31+
'lemmy' => 'https://join-lemmy.org/ns#',
3132
'value' => 'schema:value',
3233
'Hashtag' => 'as:Hashtag',
3334
'featured' => array(
@@ -42,7 +43,13 @@ class Activity extends Base_Object {
4243
'@id' => 'as:alsoKnownAs',
4344
'@type' => '@id',
4445
),
46+
'moderators' => array(
47+
'@id' => 'lemmy:moderators',
48+
'@type' => '@id',
49+
),
50+
'postingRestrictedToMods' => 'lemmy:postingRestrictedToMods',
4551
'discoverable' => 'toot:discoverable',
52+
'indexable' => 'toot:indexable',
4653
'sensitive' => 'as:sensitive',
4754
'resource' => 'webfinger:resource',
4855
),

includes/activity/class-base-object.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ public function to_array() {
644644
}
645645

646646
// if value is still empty, ignore it for the array and continue.
647-
if ( $value ) {
647+
if ( isset( $value ) ) {
648648
$array[ snake_to_camel_case( $key ) ] = $value;
649649
}
650650
}

includes/model/class-application-user.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,18 @@ public function get_following() {
5555
}
5656

5757
public function get_attachment() {
58-
return array();
58+
return null;
5959
}
6060

6161
public function get_featured_tags() {
62-
return array();
62+
return null;
6363
}
6464

6565
public function get_featured() {
66-
return array();
66+
return null;
67+
}
68+
69+
public function get_moderators() {
70+
return null;
6771
}
6872
}

includes/model/class-blog-user.php

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use function Activitypub\is_single_user;
99
use function Activitypub\is_user_disabled;
10+
use function Activitypub\get_rest_url_by_path;
1011

1112
class Blog_User extends User {
1213
/**
@@ -21,7 +22,7 @@ class Blog_User extends User {
2122
*
2223
* @var string
2324
*/
24-
protected $type = 'Group';
25+
protected $type = null;
2526

2627
/**
2728
* Is Account discoverable?
@@ -45,6 +46,21 @@ public static function from_wp_user( $user_id ) {
4546
return $object;
4647
}
4748

49+
/**
50+
* Get the type of the object.
51+
*
52+
* If the Blog is in "single user" mode, return "Person" insted of "Group".
53+
*
54+
* @return string The type of the object.
55+
*/
56+
public function get_type() {
57+
if ( is_single_user() ) {
58+
return 'Person';
59+
} else {
60+
return 'Group';
61+
}
62+
}
63+
4864
/**
4965
* Get the User-Name.
5066
*
@@ -188,25 +204,26 @@ public function get_published() {
188204
}
189205

190206
public function get_attachment() {
191-
return array();
207+
return null;
192208
}
193209

194210
public function get_canonical_url() {
195211
return \home_url();
196212
}
197213

198-
/**
199-
* Get the type of the object.
200-
*
201-
* If the Blog is in "single user" mode, return "Person" insted of "Group".
202-
*
203-
* @return string The type of the object.
204-
*/
205-
public function get_type() {
206-
if ( is_single_user() ) {
207-
return 'Person';
208-
} else {
209-
return $this->type;
214+
public function get_moderators() {
215+
if ( is_single_user() || 'Group' !== $this->get_type() ) {
216+
return null;
217+
}
218+
219+
return get_rest_url_by_path( 'collections/moderators' );
220+
}
221+
222+
public function get_posting_restricted_to_mods() {
223+
if ( 'Group' === $this->get_type() ) {
224+
return true;
210225
}
226+
227+
return null;
211228
}
212229
}

includes/model/class-user.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ class User extends Actor {
3636
*/
3737
protected $featured;
3838

39+
/**
40+
* Moderators endpoint.
41+
*
42+
* @see https://join-lemmy.org/docs/contributors/05-federation.html
43+
*
44+
* @var string
45+
*/
46+
protected $moderators;
47+
3948
/**
4049
* The User-Type
4150
*
@@ -46,17 +55,35 @@ class User extends Actor {
4655
/**
4756
* If the User is discoverable.
4857
*
58+
* @see https://docs.joinmastodon.org/spec/activitypub/#discoverable
59+
*
4960
* @var boolean
5061
*/
5162
protected $discoverable = true;
5263

64+
/**
65+
* If the User is indexable.
66+
*
67+
* @var boolean
68+
*/
69+
protected $indexable;
70+
5371
/**
5472
* The WebFinger Resource.
5573
*
5674
* @var string<url>
5775
*/
5876
protected $resource;
5977

78+
/**
79+
* Restrict posting to mods
80+
*
81+
* @see https://join-lemmy.org/docs/contributors/05-federation.html
82+
*
83+
* @var boolean
84+
*/
85+
protected $posting_restricted_to_mods = null;
86+
6087
public static function from_wp_user( $user_id ) {
6188
if ( is_user_disabled( $user_id ) ) {
6289
return new WP_Error(
@@ -272,4 +299,16 @@ public function get_resource() {
272299
public function get_canonical_url() {
273300
return $this->get_url();
274301
}
302+
303+
public function get_streams() {
304+
return null;
305+
}
306+
307+
public function get_indexable() {
308+
if ( \get_option( 'blog_public', 1 ) ) {
309+
return true;
310+
} else {
311+
return false;
312+
}
313+
}
275314
}

includes/rest/class-collection.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Activitypub\Rest;
33

4+
use WP_Error;
45
use WP_REST_Server;
56
use WP_REST_Response;
67
use Activitypub\Transformer\Post;
@@ -56,6 +57,18 @@ public static function register_routes() {
5657
),
5758
)
5859
);
60+
61+
\register_rest_route(
62+
ACTIVITYPUB_REST_NAMESPACE,
63+
'/collections/moderators',
64+
array(
65+
array(
66+
'methods' => WP_REST_Server::READABLE,
67+
'callback' => array( self::class, 'moderators_get' ),
68+
'permission_callback' => '__return_true',
69+
),
70+
)
71+
);
5972
}
6073

6174
/**
@@ -158,6 +171,30 @@ public static function featured_get( $request ) {
158171
return new WP_REST_Response( $response, 200 );
159172
}
160173

174+
/**
175+
* Moderators endpoint
176+
*
177+
* @param WP_REST_Request $request The request object.
178+
*
179+
* @return WP_REST_Response The response object.
180+
*/
181+
public static function moderators_get( $request ) {
182+
$response = array(
183+
'@context' => Activity::CONTEXT,
184+
'id' => get_rest_url_by_path( 'collections/moderators' ),
185+
'type' => 'OrderedCollection',
186+
'orderedItems' => array(),
187+
);
188+
189+
$users = User_Collection::get_collection();
190+
191+
foreach ( $users as $user ) {
192+
$response['orderedItems'][] = $user->get_url();
193+
}
194+
195+
return new WP_REST_Response( $response, 200 );
196+
}
197+
161198
/**
162199
* The supported parameters
163200
*

0 commit comments

Comments
 (0)