[mastodoncustomemojis] Fix caching

This commit is contained in:
Hypolite Petovan 2018-08-22 20:12:19 -04:00
parent 16dfd15cb2
commit 0c73b90f04

View file

@ -66,41 +66,40 @@ function mastodoncustomemojis_put_item_in_cache(App $a, array &$hook_data)
function mastodoncustomemojis_get_custom_emojis_for_author($author_link) function mastodoncustomemojis_get_custom_emojis_for_author($author_link)
{ {
$return = ['texts' => [], 'icons' => []];
$url_parts = parse_url($author_link); $url_parts = parse_url($author_link);
$api_base_url = $url_parts['scheme'] . '://' . $url_parts['host'] . (isset($url_parts['port']) ? ':' . $url_parts['port'] : ''); $api_base_url = $url_parts['scheme'] . '://' . $url_parts['host'] . (isset($url_parts['port']) ? ':' . $url_parts['port'] : '');
$cache_key = 'mastodoncustomemojis:' . $api_base_url; $cache_key = 'mastodoncustomemojis:' . $api_base_url;
$emojis = Cache::get($cache_key); $return = 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'; if (empty($return) || Config::get('system', 'ignore_cache')) {
$return = mastodoncustomemojis_fetch_custom_emojis_for_url($api_base_url);
$ret = Network::fetchUrlFull($api_url); Cache::set($cache_key, $return, empty($return['texts']) ? Cache::HALF_HOUR : Cache::WEEK);
}
if ($ret['success']) {
$emojis_array = json_decode($ret['body'], true); return $return;
}
if (is_array($emojis_array)) {
foreach ($emojis_array as $emoji) { function mastodoncustomemojis_fetch_custom_emojis_for_url($api_base_url)
$emojis['texts'][] = ':' . $emoji['shortcode'] . ':'; {
$emojis['icons'][] = '<img class="emoji mastodon" src="' . ProxyUtils::proxifyUrl($emoji['static_url']) . '" alt=":' . $emoji['shortcode'] . ':" title=":' . $emoji['shortcode'] . ':"/>'; $return = ['texts' => [], 'icons' => []];
}
} $api_url = $api_base_url . '/api/v1/custom_emojis';
$ttl = Cache::WEEK; $ret = Network::fetchUrlFull($api_url);
} else {
$ttl = Cache::HALF_HOUR; if ($ret['success']) {
} $emojis_array = json_decode($ret['body'], true);
Cache::set($cache_key, $emojis, $ttl); if (is_array($emojis_array) && count($emojis_array)) {
foreach ($emojis_array as $emoji) {
$return = $emojis; $return['texts'][] = ':' . $emoji['shortcode'] . ':';
$return['icons'][] = '<img class="emoji mastodon" src="' . ProxyUtils::proxifyUrl($emoji['static_url']) . '" alt=":' . $emoji['shortcode'] . ':" title=":' . $emoji['shortcode'] . ':"/>';
}
}
} }
return $return; return $return;