janboddez / import-from-mastodon

Automatically turn toots—short messages on Mastodon—into WordPress posts.
GNU General Public License v3.0
13 stars 2 forks source link

Better indieweb support #18

Closed diniscorreia closed 1 year ago

diniscorreia commented 1 year ago

Got everything up and running on my site, but was wondering if this could be improved to support more indieweb ideas/plugins.

For instance, would love to have a syndication link to the original toot. I'm using the Syndication Links plugin and it looks like the only thing needed would be to create an mf2_syndication meta field.

I'm also using Post Kinds and in the past I've used the 'note' kind for microblogging/tweets. Apparently post kinds are just another taxonomy, would it be a case of just listing taxonomies on the settings page and choose one (or multiple) to apply to the imported toots?

janboddez commented 1 year ago

I see. There's already another meta field you could use for that, but you'd have to script your own front-end hook. I do the same on my site (not for this plugin, but its "POSSE cousin"). I was looking into a syndication links block or something, even considering merging Share on Mastodon/Pixelfed to make this easier.

Back in topic: for this plugin, maybe we could copy metadata over into the other field or otherwise extend Syndication Links. Gotta admit I haven't really looked into that plugin, a lot of my IndieWeb stuff is, uh, "homegrown."

janboddez commented 1 year ago

Some context: I created this plugin a long time ago and had more or less abandoned it when I switched to POSSE. I then brought it back because some people let me know they were looking for a something similar. But I purposely don't have it up on WP.org, for instance. There originally was no settings page or nothing either.

janboddez commented 1 year ago

Re: post kinds, I think you could apply a taxonomy/kind after a post was imported. There's hooks for that sort of thing. Would have to also check what custom fields they expect/require to render things OK. For notes, likely nothing, yeah, I think that'd work.

diniscorreia commented 1 year ago

Yeah, POSSE was my first approach, but I soon realised it would be harder to post toots/microposts in general if I needed to open WP for that 🙂 Also, that would mean losing some of Mastodon's features like content warnings, etc.

diniscorreia commented 1 year ago

Got it, just saw the hook. It would totally work for the syndication link as well, copying the value from _import_from_mastodon_url, right?

janboddez commented 1 year ago

I noticed in Syndication Links there's this filter: syn_add_links. Lets you add extra links.

If you have some PHP knowledge, you could try to add something like (untested!) this to either your child (!) theme's functions.php or an mu-plugin:

add_filter( 'syn_add_links', function( $urls, $object_id ) {
  $mastodon_url = get_post_meta( $object_id, '_import_from_mastodon_url', true );

  if ( ! empty( $mastodon_url ) ) {
    $urls[] = $mastodon_url;
  }

  return $urls;
}, 10, 2 );

This should try and look for the custom field and, if it finds a value, add that to $urls before it gets returned. If it can't (because $object_id is not a post, or simply because it wasn't a Mastodon post, then nothing should happen).

janboddez commented 1 year ago

Re: Post Kinds, I think you can do something like this:

add_action( 'import_from_mastodon_after_import', function( $post_id ) {
  if ( ! function_exists( 'set_post_kind' ) ) {
    return;
  }

  set_post_kind( $post_id, 'note' );
} );

Again, untested. But it should be something like that.

janboddez commented 1 year ago

If you want, and they work, you could add those to snippets to a new mu-plugin. Just create a PHP file (like mastodon-import-tweaks.php, it really doesn't matter) in wp-content/mu-plugins (create the folder if it doesn't exist), and just add:

<?php

// Add "note" post kind.
add_action( 'import_from_mastodon_after_import', function( $post_id ) {
  if ( ! function_exists( 'set_post_kind' ) ) {
    return;
  }

  set_post_kind( $post_id, 'note' );
} );

// Add Mastodon URL to syndication links.
add_filter( 'syn_add_links', function( $urls, $object_id ) {
  $mastodon_url = get_post_meta( $object_id, '_import_from_mastodon_url', true );

  if ( ! empty( $mastodon_url ) ) {
    $urls[] = $mastodon_url;
  }

  return $urls;
}, 10, 2 );

I know I should really test these before posting instructions like that, but I don't currently have these plugins active. If it doesn't work, feel free to drop me an email with whatever errors you encounter.

diniscorreia commented 1 year ago

So this totally works – might just wrap the set_post_kind in a plugin activation check. And will try to add some custom fields to replies as well.

janboddez commented 1 year ago

That's what the function_exists was for. Nothing will happen if Post Kinds is not installed (unless of course another plugin were to define the same function). Easier than checking which plugins are active or not.

janboddez commented 1 year ago

For replies, you can actually use the same …after_import hook with a second argument, which would be the complete status object from the Mastodon API. If that has a parent property, it's a reply, and you could look for its URL inside. If you browse the code, you'll see I actually do something similar to add the "In reply to …" to these posts.

diniscorreia commented 1 year ago

Ah yes, didn't realise the complete status was available for that hook. For replies I might add those custom fields and eventually leave the post body with just the actual reply (the reply-to link would be on the custom field the way Post Kinds expects).

Might also try to find a way to exclude replies to my own toots — I'm using Bridgy to backfeed likes and replies so those would be comments to the original post/toot and not a post itself.

janboddez commented 1 year ago

For replies I might add those custom fields and eventually leave the post body with just the actual reply[.]

Exactly. You should be able to recognize them from the $status object.

I didn't even know I added microformats to them! :-) https://github.com/janboddez/import-from-mastodon/blob/master/includes/class-import-handler.php#L202

Anyhow, you could totally implement a callback for the import_from_mastodon_post_content hook, which again takes both the $content as generated by the plugin as an argument, as well as the Mastodon $status. So you could "regenerate" that content with only the bits that you want.

And add the meta fields in the "post import" hook.

I thought for a moment you might even be able to use the core wp_insert_post_data hook, to insert metadata, but that is in fact too late to still modify meta_input. But import_from_mastodon_after_import should work nicely (and has the benefit it won't do anything for "normal" posts).

diniscorreia commented 1 year ago

Here's what I came up with: Import From Mastodon IndieWeb Addons

Ended up not using the syn_add_links hook, as that would only display the links, not stored them in the post meta. Anyway, this seems to do the job!

janboddez commented 1 year ago

Ended up not using the syn_add_links hook, as that would only display the links, not stored them in the post meta. Anyway, this seems to do the job!

Absolutely, the choice is yours.

I might one day cook up a syndication links blocks, good to know that hook exists