Skip to content

Commit

Permalink
Automatically detect Mastodon instances for submitted Mastodon bots.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanbohacek committed Feb 20, 2023
1 parent e441b67 commit 1258750
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
8 changes: 4 additions & 4 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,11 @@ function load_mastodon_js(){
global $helpers;

$bot_tweets_html = get_post_meta( $post->ID, 'bot_tweets_html', true );
$known_mastodon_instances = $helpers->get_known_mastodon_instances();
$urls = wp_extract_urls($bot_tweets_html);

foreach ( $known_mastodon_instances as $instance ){
if ( strpos( $bot_tweets_html, $instance ) !== false ){
echo $helpers->get_mastodon_embed_script_tag($instance);
foreach ( $urls as $url ){
if ( $helpers->is_mastodon_instance($url) ){
echo $helpers->get_mastodon_embed_script_tag($helpers->get_domain_from_url($url));
break;
}
}
Expand Down
16 changes: 16 additions & 0 deletions includes/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,22 @@ function get_fediverse_url( $handle ){
return $fediverse_url;
}

function is_mastodon_instance($url){
$domain = $this->get_domain_from_url( $url );
if (in_array($domain, $this->get_known_mastodon_instances()) ){
return true;
}

$response = wp_remote_get( "https://$domain/api/v2/instance" );
$response_code = wp_remote_retrieve_response_code( $response );

if ($response_code === 200){
return true;
}

return false;
}

function get_known_mastodon_instances(){
// TODO: Move these to a settings page for easier maintanance.

Expand Down
16 changes: 5 additions & 11 deletions includes/post-types/bots.php
Original file line number Diff line number Diff line change
Expand Up @@ -699,17 +699,11 @@ function save_meta( $post_id ) {
// $bot_tweets_html .= $data->html;
}
else {

global $helpers;
$known_mastodon_instances = $helpers->get_known_mastodon_instances();

foreach ( $known_mastodon_instances as $instance ){
if ( strpos( $tweet_url, $instance ) !== false ){
$bot_tweets_html .= '<blockquote><iframe src="' . $tweet_url . '/embed" class="mastodon-embed" style="max-width: 100%; border: 0" width="400"></iframe></blockquote>';
break;
}
}

global $helpers;
if ( $helpers->is_mastodon_instance($tweet_url) ){
$bot_tweets_html .= '<blockquote><iframe src="' . $tweet_url . '/embed" class="mastodon-embed" style="max-width: 100%; border: 0" width="400"></iframe></blockquote>';
break;
}
}
}

Expand Down

0 comments on commit 1258750

Please sign in to comment.