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

Hooks for customizing ActivityPub output? #904

Closed
DeadSuperHero opened this issue Sep 26, 2024 · 2 comments
Closed

Hooks for customizing ActivityPub output? #904

DeadSuperHero opened this issue Sep 26, 2024 · 2 comments

Comments

@DeadSuperHero
Copy link

What

We're currently experimenting with different types of content on our site, across a variety of different WordPress post types.

Right now, there's an option to customize all output in the ActivityPub plugin settings page. Unfortunately, this tool applies uniform rules to every post type provided by WordPress, which may not always be the best experience.

We want to be able to set different templates for different WordPress post types. Falling short of a dedicated UI tool, such as in #822, it would be great to have a way for our WordPress theme to set those parameters in a theme function.

Why

We are interested in experimenting with hosting our entire Fediverse presence from our news publication. This includes the brand accounts we have on our Akkoma instance.

Screenshot_20240926_092052_Moshidon

Above is an example of what a WordPress microblog post currently looks like on the Fediverse. It has some extra elements that we don't want, such as whitespace and a URL, and doesn't support hashtags. Including a URL for our articles makes sense, but it looks weird for status updates.

Having the ability to customize this to look more natural to the Fediverse would be really good, even if we had to do it with some custom code. However, we don't really know if the plugin provides any hooks or documentation on how to do this.

How

It would be great if we could use a function in our theme to specify how ActivityPub output should look. Something like:

function ap_output(postType) {
if $postType == "Note" {
    $postType.activity = [
     "Type": "Note",
     "Body": Content,
     "Hashtags": true
     ]
   }
else if $postType == "Article" {
if $postType == "Note" {
    $postType.activity = [
     "Type": "Note",
     "Body": Excerpt & Link,
     "Hashtags": false
     ]
   }
}

I apologize for my terrible psuedocode, but this is my general thinking on how we can make customizations on our site.

@pfefferle
Copy link
Member

Hey @DeadSuperHero 👋

So, there are some answers to your question :)

Use Shortcodes

You can change the shortcodes that are used to generate the content. The function that does generate that is her:

protected function get_post_content_template() {

To change it, you can use the filter: activitypub_object_content_template.

Change the output before JSON is rendered

The Activity-Object has two functions where you can modify the Activity-Object-Array short before the JSON is generated:

$array = \apply_filters( 'activitypub_activity_object_array', $array, $class, $this->id, $this );

The filters are:

  • $array = \apply_filters( 'activitypub_activity_object_array', $array, $class, $this->id, $this );
  • $array = \apply_filters( "activitypub_activity_{$class}_object_array", $array, $this->id, $this );

Create a custom Transformer

The plugin has some Transformers to transform between WordPress-Objects and ActivityPub objects. This is the most powerful and flexible way to change the output.

You can see an example in the integrations folder. I used overloading of a transformer to implement podcasting support.

Loading your own transformer:

// hook into filter
add_filter(
	'activitypub_transformer',
	function ( $transformer, $object, $object_class ) {
		// check when to load custom transformer, you can change this to always use yours and inherit the classic post transformer
		if (
			'WP_Post' === $object_class &&
			\get_post_meta( $object->ID, 'audio_file', true )
		) {
			// require your class and return an instance of your transformer
			require_once __DIR__ . '/class-seriously-simple-podcasting.php';
			return new Seriously_Simple_Podcasting( $object );
		}
		return $transformer;
	},
	10,
	3
);

And here is the example of a custom transformer, that inherits the default post transformer to replace some of the functions. In this case it is to prioritise the podcast audio in the attachments.

https://github.com/Automattic/wordpress-activitypub/blob/trunk/integration/class-seriously-simple-podcasting.php

Let me know if you need some more informations or if I can help with the implementation.

@pfefferle
Copy link
Member

I will close this issue, so that it no longer shows up in the queue, but I am happy to discuss this further here!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants