page['htmlhead'] .= <<
HTML;
}
function mastodoncustomemojis_put_item_in_cache(App $a, array &$hook_data)
{
// Mastodon uses OStatus, skipping other network protocols
if ($hook_data['item']['network'] != Protocol::OSTATUS) {
return;
}
$emojis = mastodoncustomemojis_get_custom_emojis_for_author($hook_data['item']['author-link']);
$hook_data["rendered-html"] = Smilies::replaceFromArray($hook_data["rendered-html"], $emojis);
}
function mastodoncustomemojis_get_custom_emojis_for_author($author_link)
{
$return = ['texts' => [], 'icons' => []];
$url_parts = parse_url($author_link);
$api_base_url = $url_parts['scheme'] . '://' . $url_parts['host'] . (isset($url_parts['port']) ? ':' . $url_parts['port'] : '');
$cache_key = 'mastodoncustomemojis:' . $api_base_url;
$emojis = Cache::get($cache_key);
if (empty($emojis['texts']) || Config::get('system', 'ignore_cache')) {
// Reset the emojis array
$emojis = $return;
$api_url = $api_base_url . '/api/v1/custom_emojis';
$ret = Network::fetchUrlFull($api_url);
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'][] = '';
}
}
$ttl = Cache::WEEK;
} else {
$ttl = Cache::HALF_HOUR;
}
Cache::set($cache_key, $emojis, $ttl);
$return = $emojis;
}
return $return;
}