2018-07-14 12:08:36 +02: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 07:12:52 +02:00
|
|
|
* Author: Roland Haeder
|
2018-07-14 12:08:36 +02:00
|
|
|
*/
|
|
|
|
|
2018-07-17 07:12:52 +02:00
|
|
|
use Friendica\App;
|
|
|
|
use Friendica\Content\Smilies;
|
2018-07-14 12:08:36 +02:00
|
|
|
use Friendica\Core\Addon;
|
2018-07-17 07:12:52 +02:00
|
|
|
use Friendica\Core\Cache;
|
2018-07-18 02:34:59 +02:00
|
|
|
use Friendica\Core\Config;
|
2018-07-17 07:12:52 +02:00
|
|
|
use Friendica\Core\Protocol;
|
|
|
|
use Friendica\Util\Network;
|
2018-07-14 12:08:36 +02:00
|
|
|
|
|
|
|
function mastodoncustomemojis_install()
|
|
|
|
{
|
|
|
|
Addon::registerHook('put_item_in_cache', __FILE__, 'mastodoncustomemojis_put_item_in_cache');
|
|
|
|
Addon::registerHook('network_mod_init', __FILE__, 'mastodoncustomemojis_css_hook');
|
|
|
|
Addon::registerHook('display_mod_init', __FILE__, 'mastodoncustomemojis_css_hook');
|
|
|
|
Addon::registerHook('search_mod_init', __FILE__, 'mastodoncustomemojis_css_hook');
|
|
|
|
Addon::registerHook('community_mod_init', __FILE__, 'mastodoncustomemojis_css_hook');
|
|
|
|
Addon::registerHook('contacts_mod_init', __FILE__, 'mastodoncustomemojis_css_hook');
|
|
|
|
}
|
|
|
|
|
|
|
|
function mastodoncustomemojis_uninstall()
|
|
|
|
{
|
|
|
|
Addon::unregisterHook('put_item_in_cache', __FILE__, 'mastodoncustomemojis_put_item_in_cache');
|
|
|
|
Addon::unregisterHook('network_mod_init', __FILE__, 'mastodoncustomemojis_css_hook');
|
|
|
|
Addon::unregisterHook('display_mod_init', __FILE__, 'mastodoncustomemojis_css_hook');
|
|
|
|
Addon::unregisterHook('search_mod_init', __FILE__, 'mastodoncustomemojis_css_hook');
|
|
|
|
Addon::unregisterHook('community_mod_init', __FILE__, 'mastodoncustomemojis_css_hook');
|
|
|
|
Addon::unregisterHook('contacts_mod_init', __FILE__, 'mastodoncustomemojis_css_hook');
|
|
|
|
}
|
|
|
|
|
2018-07-17 07:12:52 +02:00
|
|
|
function mastodoncustomemojis_css_hook(App $a)
|
2018-07-14 12:08:36 +02:00
|
|
|
{
|
|
|
|
$a->page['htmlhead'] .= <<<HTML
|
|
|
|
<!-- Style added by mastodoncustomemojis -->
|
|
|
|
<style type="text/css">
|
|
|
|
.emoji.mastodon {
|
|
|
|
height: 1.2em;
|
|
|
|
vertical-align: middle;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
|
|
|
|
|
|
|
|
HTML;
|
|
|
|
}
|
|
|
|
|
2018-07-17 07:12:52 +02:00
|
|
|
function mastodoncustomemojis_put_item_in_cache(App $a, array &$hook_data)
|
2018-07-14 12:08:36 +02:00
|
|
|
{
|
|
|
|
// Mastodon uses OStatus, skipping other network protocols
|
2018-07-20 07:36:55 +02:00
|
|
|
if (empty($hook_data['item']['author-link']) || ($hook_data['item']['network'] != Protocol::OSTATUS)) {
|
2018-07-14 12:08:36 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$emojis = mastodoncustomemojis_get_custom_emojis_for_author($hook_data['item']['author-link']);
|
|
|
|
|
2018-07-17 07:12:52 +02:00
|
|
|
$hook_data["rendered-html"] = Smilies::replaceFromArray($hook_data["rendered-html"], $emojis);
|
2018-07-14 12:08:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function mastodoncustomemojis_get_custom_emojis_for_author($author_link)
|
|
|
|
{
|
|
|
|
$return = ['texts' => [], 'icons' => []];
|
|
|
|
|
|
|
|
$url_parts = parse_url($author_link);
|
|
|
|
|
2018-07-17 01:40:09 +02:00
|
|
|
$api_base_url = $url_parts['scheme'] . '://' . $url_parts['host'] . (isset($url_parts['port']) ? ':' . $url_parts['port'] : '');
|
2018-07-14 12:08:36 +02:00
|
|
|
|
|
|
|
$cache_key = 'mastodoncustomemojis:' . $api_base_url;
|
|
|
|
|
2018-07-17 07:12:52 +02:00
|
|
|
$emojis = Cache::get($cache_key);
|
2018-07-18 02:34:59 +02:00
|
|
|
if (empty($emojis['texts']) || Config::get('system', 'ignore_cache')) {
|
2018-07-14 12:08:36 +02:00
|
|
|
// Reset the emojis array
|
|
|
|
$emojis = $return;
|
|
|
|
|
|
|
|
$api_url = $api_base_url . '/api/v1/custom_emojis';
|
|
|
|
|
2018-07-17 07:12:52 +02:00
|
|
|
$ret = Network::fetchUrlFull($api_url);
|
2018-07-14 12:08:36 +02:00
|
|
|
|
|
|
|
if ($ret['success']) {
|
|
|
|
$emojis_array = json_decode($ret['body'], true);
|
|
|
|
|
|
|
|
if (is_array($emojis_array)) {
|
|
|
|
foreach ($emojis_array as $emoji) {
|
|
|
|
$emojis['texts'][] = ':' . $emoji['shortcode'] . ':';
|
|
|
|
$emojis['icons'][] = '<img class="emoji mastodon" src="' . proxy_url($emoji['static_url']) . '" alt=":' . $emoji['shortcode'] . ':" title=":' . $emoji['shortcode'] . ':"/>';
|
|
|
|
}
|
|
|
|
}
|
2018-07-18 02:34:59 +02:00
|
|
|
|
|
|
|
$ttl = Cache::WEEK;
|
|
|
|
} else {
|
|
|
|
$ttl = Cache::HALF_HOUR;
|
2018-07-14 12:08:36 +02:00
|
|
|
}
|
|
|
|
|
2018-07-18 02:34:59 +02:00
|
|
|
Cache::set($cache_key, $emojis, $ttl);
|
2018-07-14 12:08:36 +02:00
|
|
|
|
|
|
|
$return = $emojis;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|