Skip to content

Commit 120d571

Browse files
committed
basic outbox implementation
1 parent ac9022f commit 120d571

File tree

7 files changed

+219
-17
lines changed

7 files changed

+219
-17
lines changed

README.md

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,64 @@
44
**Tags:** OStatus, fediverse, activitypub, activitystream
55
**Requires at least:** 4.7
66
**Tested up to:** 4.9.8
7-
**Stable tag:** 1.0.0
7+
**Stable tag:** 0.0.1
88
**Requires PHP:** 5.3
99
**License:** MIT
1010
**License URI:** http://opensource.org/licenses/MIT
1111

1212
The ActivityPub protocol is a decentralized social networking protocol based upon the ActivityStreams 2.0 data format.
13+
14+
## Description ##
15+
16+
This plugin enables ActivityPub for your Blog. Your readers will be able to follow your Blogposts on Mastodon and other Federated Plattforms that support ActivityPub.
17+
18+
## Frequently Asked Questions ##
19+
20+
### Why does the plugin not support following and other social network stuff ###
21+
22+
If you want to have a **decentralized social network**, please use [Mastodon](https://joinmastodon.org/) or [GNU.social](https://gnu.io/social/).
23+
24+
25+
## Changelog ##
26+
27+
### 0.0.1 ###
28+
29+
* initial
30+
31+
## Installation ##
32+
33+
Follow the normal instructions for [installing WordPress plugins](https://codex.wordpress.org/Managing_Plugins#Installing_Plugins).
34+
35+
### Automatic Plugin Installation ###
36+
37+
To add a WordPress Plugin using the [built-in plugin installer](https://codex.wordpress.org/Administration_Screens#Add_New_Plugins):
38+
39+
1. Go to [Plugins](https://codex.wordpress.org/Administration_Screens#Plugins) > [Add New](https://codex.wordpress.org/Plugins_Add_New_Screen).
40+
1. Type "`activitypub`" into the **Search Plugins** box.
41+
1. Find the WordPress Plugin you wish to install.
42+
1. Click **Details** for more information about the Plugin and instructions you may wish to print or save to help setup the Plugin.
43+
1. Click **Install Now** to install the WordPress Plugin.
44+
1. The resulting installation screen will list the installation as successful or note any problems during the install.
45+
1. If successful, click **Activate Plugin** to activate it, or **Return to Plugin Installer** for further actions.
46+
47+
### Manual Plugin Installation ###
48+
49+
There are a few cases when manually installing a WordPress Plugin is appropriate.
50+
51+
* If you wish to control the placement and the process of installing a WordPress Plugin.
52+
* If your server does not permit automatic installation of a WordPress Plugin.
53+
* If you want to try the [latest development version](https://github.com/pfefferle/wordpress-activitypub).
54+
55+
Installation of a WordPress Plugin manually requires FTP familiarity and the awareness that you may put your site at risk if you install a WordPress Plugin incompatible with the current version or from an unreliable source.
56+
57+
Backup your site completely before proceeding.
58+
59+
To install a WordPress Plugin manually:
60+
61+
* Download your WordPress Plugin to your desktop.
62+
* Download from [the WordPress directory](https://wordpress.org/plugins/activitypub/)
63+
* Download from [GitHub](https://github.com/pfefferle/wordpress-activitypub/releases)
64+
* If downloaded as a zip archive, extract the Plugin folder to your desktop.
65+
* With your FTP program, upload the Plugin folder to the `wp-content/plugins` folder in your WordPress directory online.
66+
* Go to [Plugins screen](https://codex.wordpress.org/Administration_Screens#Plugins) and find the newly uploaded Plugin in the list.
67+
* Click **Activate** to activate it.

includes/class-activitypub-outbox.php

Lines changed: 97 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,120 @@
66
*/
77
class Activitypub_Outbox {
88
/**
9-
* Register the Route.
9+
* Register routes
1010
*/
1111
public static function register_routes() {
1212
register_rest_route(
13-
'activitypub/1.0', '/outbox', array(
13+
'activitypub/1.0', '/users/(?P<id>\d+)/outbox', array(
1414
array(
1515
'methods' => WP_REST_Server::READABLE,
1616
'callback' => array( 'Activitypub_Outbox', 'get' ),
17+
'args' => self::request_parameters(),
1718
),
1819
)
1920
);
2021
}
2122

2223
public static function get( $request ) {
23-
$outbox = new stdClass();
24+
$author_id = $request->get_param( 'id' );
25+
$author = get_user_by( 'ID', $author_id );
2426

25-
$outbox->{'@context'} = array(
27+
if ( ! $author ) {
28+
return new WP_Error( 'rest_invalid_param', __( 'User not found', 'activitypub' ), array(
29+
'status' => 404, 'params' => array(
30+
'user_id' => __( 'User not found', 'activitypub' )
31+
)
32+
) );
33+
}
34+
35+
$page = $request->get_param( 'page', 0 );
36+
37+
/*
38+
* Action triggerd prior to the ActivityPub profile being created and sent to the client
39+
*/
40+
do_action( 'activitypub_outbox_pre' );
41+
42+
$json = new stdClass();
43+
44+
$json->{'@context'} = array(
2645
'https://www.w3.org/ns/activitystreams',
2746
'https://w3id.org/security/v1',
2847
);
2948

30-
//var_dump($request->get_param('page'));
49+
$json->generator = 'http://wordpress.org/?v=' . get_bloginfo_rss( 'version' );
50+
$json->actor = get_author_posts_url( $author_id );
51+
$json->type = 'OrderedCollectionPage';
52+
$json->partOf = get_rest_url( null, "/activitypub/1.0/users/$author_id/outbox" ); // phpcs:ignore
53+
54+
$count_posts = wp_count_posts();
55+
$json->totalItems = $count_posts->publish;
56+
57+
$posts = get_posts( array(
58+
'author' => $author_id,
59+
'offset' => $page,
60+
) );
61+
62+
$json->first = add_query_arg( 'page', 0, $json->partOf );
63+
$json->last = add_query_arg( 'page', ( $json->totalItems%10 )-1, $json->partOf );
64+
if ( $json->last < $page ) {
65+
$json->next = add_query_arg( 'page', ++$page, $json->partOf );
66+
}
67+
68+
foreach ( $posts as $post ) {
69+
$json->orderedItems[] = self::post_to_json( $post ); // phpcs:ignore
70+
}
71+
72+
// filter output
73+
$json = apply_filters( 'activitypub_outbox_array', $json );
74+
75+
/*
76+
* Action triggerd after the ActivityPub profile has been created and sent to the client
77+
*/
78+
do_action( 'activitypub_outbox_post' );
79+
80+
$response = new WP_REST_Response( $json, 200 );
81+
82+
$response->header( 'Content-Type', 'application/activity-json' );
83+
84+
return $response;
85+
}
86+
87+
public static function post_to_json( $post ) {
88+
$json = new stdClass();
89+
90+
$json->published = $post->post_date;
91+
$json->id = $post->guid;
92+
$json->type = 'Create';
93+
$json->actor = 'https://mastodon.social/users/pfefferle';
94+
$json->to = array( 'https://www.w3.org/ns/activitystreams#Public' );
95+
96+
$json->object = array(
97+
'id' => $post->guid,
98+
'type' => 'Note',
99+
'published' => $post->post_date,
100+
'to' => array( 'https://www.w3.org/ns/activitystreams#Public' ),
101+
'content' => $post->post_content,
102+
'contentMap' => array(
103+
strstr( get_locale(), '_', true ) => $post->post_content,
104+
),
105+
);
106+
107+
return $json;
108+
}
109+
110+
111+
public static function request_parameters() {
112+
$params = array();
113+
114+
$params['page'] = array(
115+
'type' => 'integer',
116+
);
117+
118+
$params['id'] = array(
119+
'required' => true,
120+
'type' => 'integer',
121+
);
31122

32-
return new WP_REST_Response( $outbox, 200 );
123+
return $params;
33124
}
34125
}

includes/class-activitypub.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public static function render_profile( $template ) {
66
return $template;
77
}
88

9-
$json_template = dirname( __FILE__ ) . '/../templates/author-profile.php';
9+
$json_template = dirname( __FILE__ ) . '/../templates/profile.php';
1010

1111
global $wp_query;
1212

@@ -61,6 +61,7 @@ public static function add_webfinger_discovery( $array, $resource, $user ) {
6161
*/
6262
public static function add_query_vars( $vars ) {
6363
$vars[] = 'activitypub';
64+
6465
return $vars;
6566
}
6667

includes/functions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php

languages/activitypub.pot

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ msgid ""
44
msgstr ""
55
"Project-Id-Version: ActivityPub 1.0.0\n"
66
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/activitypub\n"
7-
"POT-Creation-Date: 2018-08-18 10:35:26+00:00\n"
7+
"POT-Creation-Date: 2018-09-24 18:46:07+00:00\n"
88
"MIME-Version: 1.0\n"
99
"Content-Type: text/plain; charset=utf-8\n"
1010
"Content-Transfer-Encoding: 8bit\n"
@@ -13,6 +13,11 @@ msgstr ""
1313
"Language-Team: LANGUAGE <[email protected]>\n"
1414
"X-Generator: grunt-wp-i18n1.0.2\n"
1515

16+
#: includes/class-activitypub-outbox.php:28
17+
#: includes/class-activitypub-outbox.php:30
18+
msgid "User not found"
19+
msgstr ""
20+
1621
#. Plugin Name of the plugin/theme
1722
msgid "ActivityPub"
1823
msgstr ""

readme.txt

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,64 @@ Donate link: https://notiz.blog/donate/
44
Tags: OStatus, fediverse, activitypub, activitystream
55
Requires at least: 4.7
66
Tested up to: 4.9.8
7-
Stable tag: 1.0.0
7+
Stable tag: 0.0.1
88
Requires PHP: 5.3
99
License: MIT
1010
License URI: http://opensource.org/licenses/MIT
1111

1212
The ActivityPub protocol is a decentralized social networking protocol based upon the ActivityStreams 2.0 data format.
13+
14+
== Description ==
15+
16+
This plugin enables ActivityPub for your Blog. Your readers will be able to follow your Blogposts on Mastodon and other Federated Plattforms that support ActivityPub.
17+
18+
== Frequently Asked Questions ==
19+
20+
= Why does the plugin not support following and other social network stuff =
21+
22+
If you want to have a **decentralized social network**, please use [Mastodon](https://joinmastodon.org/) or [GNU.social](https://gnu.io/social/).
23+
24+
25+
== Changelog ==
26+
27+
= 0.0.1 =
28+
29+
* initial
30+
31+
== Installation ==
32+
33+
Follow the normal instructions for [installing WordPress plugins](https://codex.wordpress.org/Managing_Plugins#Installing_Plugins).
34+
35+
= Automatic Plugin Installation =
36+
37+
To add a WordPress Plugin using the [built-in plugin installer](https://codex.wordpress.org/Administration_Screens#Add_New_Plugins):
38+
39+
1. Go to [Plugins](https://codex.wordpress.org/Administration_Screens#Plugins) > [Add New](https://codex.wordpress.org/Plugins_Add_New_Screen).
40+
1. Type "`activitypub`" into the **Search Plugins** box.
41+
1. Find the WordPress Plugin you wish to install.
42+
1. Click **Details** for more information about the Plugin and instructions you may wish to print or save to help setup the Plugin.
43+
1. Click **Install Now** to install the WordPress Plugin.
44+
1. The resulting installation screen will list the installation as successful or note any problems during the install.
45+
1. If successful, click **Activate Plugin** to activate it, or **Return to Plugin Installer** for further actions.
46+
47+
= Manual Plugin Installation =
48+
49+
There are a few cases when manually installing a WordPress Plugin is appropriate.
50+
51+
* If you wish to control the placement and the process of installing a WordPress Plugin.
52+
* If your server does not permit automatic installation of a WordPress Plugin.
53+
* If you want to try the [latest development version](https://github.com/pfefferle/wordpress-activitypub).
54+
55+
Installation of a WordPress Plugin manually requires FTP familiarity and the awareness that you may put your site at risk if you install a WordPress Plugin incompatible with the current version or from an unreliable source.
56+
57+
Backup your site completely before proceeding.
58+
59+
To install a WordPress Plugin manually:
60+
61+
* Download your WordPress Plugin to your desktop.
62+
* Download from [the WordPress directory](https://wordpress.org/plugins/activitypub/)
63+
* Download from [GitHub](https://github.com/pfefferle/wordpress-activitypub/releases)
64+
* If downloaded as a zip archive, extract the Plugin folder to your desktop.
65+
* With your FTP program, upload the Plugin folder to the `wp-content/plugins` folder in your WordPress directory online.
66+
* Go to [Plugins screen](https://codex.wordpress.org/Administration_Screens#Plugins) and find the newly uploaded Plugin in the list.
67+
* Click **Activate** to activate it.
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
<?php
2-
/**
3-
* Activity Streams 1 Feed Template for displaying AS1 Posts feed.
4-
*
5-
* @link https://github.com/pento/7B a lot of changes made by @pento
6-
*/
7-
82
$author_id = get_the_author_meta( 'ID' );
93

104
$json = new stdClass();
@@ -32,7 +26,7 @@
3226
);
3327
}
3428

35-
$json->outbox = get_rest_url( null, '/activitypub/1.0/outbox' );
29+
$json->outbox = get_rest_url( null, "/activitypub/1.0/users/$author_id/outbox" );
3630

3731
if ( method_exists( 'Magic_Sig', 'get_public_key' ) ) {
3832
// phpcs:ignore

0 commit comments

Comments
 (0)