2018-07-14 10:08:36 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* Name: Mastodon Custom Emojis
|
|
|
|
* Description: Replace emojis shortcodes in Mastodon posts with their originating server custom emojis images.
|
|
|
|
* Version: 1.0
|
|
|
|
* Author: Hypolite Petovan
|
2018-07-17 05:12:52 +00:00
|
|
|
* Author: Roland Haeder
|
2018-11-08 21:40:52 +00:00
|
|
|
* Status: Unsupported
|
2018-07-14 10:08:36 +00:00
|
|
|
*/
|
|
|
|
|
2018-07-17 05:12:52 +00:00
|
|
|
use Friendica\Content\Smilies;
|
2021-10-23 08:49:26 +00:00
|
|
|
use Friendica\Core\Cache\Enum\Duration;
|
2018-12-26 07:28:16 +00:00
|
|
|
use Friendica\Core\Hook;
|
2018-07-17 05:12:52 +00:00
|
|
|
use Friendica\Core\Protocol;
|
2019-12-30 19:02:08 +00:00
|
|
|
use Friendica\DI;
|
2018-07-14 10:08:36 +00:00
|
|
|
|
|
|
|
function mastodoncustomemojis_install()
|
|
|
|
{
|
2018-12-26 07:28:16 +00:00
|
|
|
Hook::register('put_item_in_cache', __FILE__, 'mastodoncustomemojis_put_item_in_cache');
|
|
|
|
Hook::register('network_mod_init', __FILE__, 'mastodoncustomemojis_css_hook');
|
|
|
|
Hook::register('display_mod_init', __FILE__, 'mastodoncustomemojis_css_hook');
|
|
|
|
Hook::register('search_mod_init', __FILE__, 'mastodoncustomemojis_css_hook');
|
|
|
|
Hook::register('community_mod_init', __FILE__, 'mastodoncustomemojis_css_hook');
|
|
|
|
Hook::register('contacts_mod_init', __FILE__, 'mastodoncustomemojis_css_hook');
|
2018-07-14 10:08:36 +00:00
|
|
|
}
|
|
|
|
|
2023-01-14 02:16:09 +00:00
|
|
|
function mastodoncustomemojis_css_hook()
|
2018-07-14 10:08:36 +00:00
|
|
|
{
|
2019-12-30 19:02:08 +00:00
|
|
|
DI::page()['htmlhead'] .= <<<HTML
|
2018-07-14 10:08:36 +00:00
|
|
|
<!-- Style added by mastodoncustomemojis -->
|
|
|
|
<style type="text/css">
|
|
|
|
.emoji.mastodon {
|
|
|
|
height: 1.2em;
|
|
|
|
vertical-align: middle;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
|
|
|
|
|
|
|
|
HTML;
|
|
|
|
}
|
|
|
|
|
2023-01-14 02:16:09 +00:00
|
|
|
function mastodoncustomemojis_put_item_in_cache(array &$hook_data)
|
2018-07-14 10:08:36 +00:00
|
|
|
{
|
2018-10-02 15:28:31 +00:00
|
|
|
// Mastodon uses OStatus and ActivityPub, skipping other network protocols
|
|
|
|
if (empty($hook_data['item']['author-link']) || !in_array($hook_data['item']['network'], [Protocol::OSTATUS, Protocol::ACTIVITYPUB])) {
|
2018-07-14 10:08:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$emojis = mastodoncustomemojis_get_custom_emojis_for_author($hook_data['item']['author-link']);
|
|
|
|
|
2018-07-17 05:12:52 +00:00
|
|
|
$hook_data["rendered-html"] = Smilies::replaceFromArray($hook_data["rendered-html"], $emojis);
|
2018-07-14 10:08:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function mastodoncustomemojis_get_custom_emojis_for_author($author_link)
|
|
|
|
{
|
|
|
|
$url_parts = parse_url($author_link);
|
|
|
|
|
2018-07-16 23:40:09 +00:00
|
|
|
$api_base_url = $url_parts['scheme'] . '://' . $url_parts['host'] . (isset($url_parts['port']) ? ':' . $url_parts['port'] : '');
|
2018-07-14 10:08:36 +00:00
|
|
|
|
|
|
|
$cache_key = 'mastodoncustomemojis:' . $api_base_url;
|
|
|
|
|
2020-01-19 15:29:54 +00:00
|
|
|
$return = DI::cache()->get($cache_key);
|
2018-07-14 10:08:36 +00:00
|
|
|
|
2020-01-19 20:21:12 +00:00
|
|
|
if (empty($return) || DI::config()->get('system', 'ignore_cache')) {
|
2018-08-23 00:12:19 +00:00
|
|
|
$return = mastodoncustomemojis_fetch_custom_emojis_for_url($api_base_url);
|
2018-07-14 10:08:36 +00:00
|
|
|
|
2020-01-19 15:29:54 +00:00
|
|
|
DI::cache()->set($cache_key, $return, empty($return['texts']) ? Duration::QUARTER_HOUR : Duration::HOUR);
|
2018-08-23 00:12:19 +00:00
|
|
|
}
|
2018-07-14 10:08:36 +00:00
|
|
|
|
2018-08-23 00:12:19 +00:00
|
|
|
return $return;
|
|
|
|
}
|
2018-07-14 10:08:36 +00:00
|
|
|
|
2018-08-23 00:12:19 +00:00
|
|
|
function mastodoncustomemojis_fetch_custom_emojis_for_url($api_base_url)
|
|
|
|
{
|
|
|
|
$return = ['texts' => [], 'icons' => []];
|
2018-07-18 00:34:59 +00:00
|
|
|
|
2018-08-23 00:12:19 +00:00
|
|
|
$api_url = $api_base_url . '/api/v1/custom_emojis';
|
2018-07-14 10:08:36 +00:00
|
|
|
|
2024-09-04 11:46:33 +00:00
|
|
|
$fetchResult = DI::httpClient()->get($api_url);
|
2018-07-14 10:08:36 +00:00
|
|
|
|
2018-10-11 05:40:57 +00:00
|
|
|
if ($fetchResult->isSuccess()) {
|
2024-01-12 06:16:01 +00:00
|
|
|
$emojis_array = json_decode($fetchResult->getBodyString(), true);
|
2018-08-23 00:12:19 +00:00
|
|
|
|
|
|
|
if (is_array($emojis_array) && count($emojis_array)) {
|
|
|
|
foreach ($emojis_array as $emoji) {
|
2018-10-12 12:53:43 +00:00
|
|
|
if (!empty($emoji['shortcode']) && !empty($emoji['static_url'])) {
|
|
|
|
$return['texts'][] = ':' . $emoji['shortcode'] . ':';
|
2024-04-15 19:34:07 +00:00
|
|
|
$return['icons'][] = '<img class="emoji mastodon" src="' . $emoji['static_url'] . '" alt=":' . $emoji['shortcode'] . ':" title=":' . $emoji['shortcode'] . ':"/>';
|
2018-10-12 12:53:43 +00:00
|
|
|
}
|
2018-08-23 00:12:19 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-14 10:08:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|