diff --git a/themes/hugo-mastodon-comments/static/comments/config.sample.php b/themes/hugo-mastodon-comments/static/comments/config.sample.php index 1b48359..daec92d 100644 --- a/themes/hugo-mastodon-comments/static/comments/config.sample.php +++ b/themes/hugo-mastodon-comments/static/comments/config.sample.php @@ -3,5 +3,6 @@ $config = [ 'mastodon-instance' => 'https://mastodon.social', 'user-id' => 379833, 'search-url' => 'https?://fsfe.org', // please use https?:// as schema - 'threshold' => 300 + 'cache_toots' => 300, + 'debug' => true ]; diff --git a/themes/hugo-mastodon-comments/static/comments/getcomments.php b/themes/hugo-mastodon-comments/static/comments/getcomments.php index 3a1b563..f1486d8 100644 --- a/themes/hugo-mastodon-comments/static/comments/getcomments.php +++ b/themes/hugo-mastodon-comments/static/comments/getcomments.php @@ -7,10 +7,9 @@ $searchurl = $config['search-url']; $search = isset($_GET['search']) ? $_GET['search'] : ''; $debug_on = $config['debug']; /* cache files */ +$ctt = $config['cache_toots'];; $dbt = "cache-toots.json"; -$timestamp = time(); - /* Exit if search empty */ if (empty($search)) { debug("No proper search given"); @@ -35,14 +34,25 @@ function write_db($db, $data) { file_put_contents($db, $encoded, LOCK_EX); } /* access data from file */ -function read_db($db, &$data) { +function read_db($db, &$data, &$cachebreak) { + global $ctt; // if DB does not exist, create it with empty array if (! file_exists($db)) { touch($db); write_db($db, array()); + $cachebreak = true; } $file = file_get_contents($db, true); - $data = json_decode($file, true)['toots']; + $data = json_decode($file, true); + + debug($data['timestamp']); + + // check if timestamp in cache file too old + if (empty($data['timestamp']) || ($data['timestamp'] + $ctt < time())) { + $cachebreak = true; + } + + $data = $data['toots']; } /* TOOT FUNCTIONS */ @@ -121,36 +131,44 @@ function tootStats($instance, $id, &$result) { * START PROGRAM ***************/ -/* Collect all the toots */ -/* get id of latest cached toot, and set as $min_id */ -read_db($dbt, $toots); -$toots_cached = $toots; -if (!empty($toots['0']['id'])) { - $min_id_cached = $toots['0']['id']; - $min_id = $min_id_cached; -} else { - /* if cached toots do not exist, start from oldest toot */ - $min_id = "0"; - $min_id_cached = "0"; -} +/* check whether the cached file containing all toots is older than max. cache time */ +// this at the same time loads the cached DB, either way +$cachebreak = false; +read_db($dbt, $toots, $cachebreak); -/* test whether there are new toots available */ -// Search for toots older than the cached latest toot ID ($min_id) -$uptodate = false; -while ($uptodate === false) { - $toots = array_merge(collectToots($instance, $uid, $min_id, $searchurl), $toots); - $min_id_new = $toots['0']['id']; // the latest ID of the recent search - - if ($min_id_new === $min_id) { - // min_id is the latest, let's write the new DB and end this loop - $uptodate = true; - debug("Toots up-to-date. Rewrite cache DB."); - write_db($dbt, $toots); +if ($cachebreak) { + /* Collect all the toots */ + /* get id of latest cached toot, and set as $min_id */ + debug("Toots cache oudated. Checking for new toots"); + if (!empty($toots['0']['id'])) { + $min_id_cached = $toots['0']['id']; + $min_id = $min_id_cached; } else { - // next round looks for toots newer than the newly found ID - debug("Newer toots than in cache found. Starting another search for new toots"); - $min_id = $min_id_new; + /* if cached toots do not exist, start from oldest toot */ + $min_id = "0"; + $min_id_cached = "0"; } + + /* test whether there are new toots available */ + // Search for toots older than the cached latest toot ID ($min_id) + $uptodate = false; + while ($uptodate === false) { + $toots = array_merge(collectToots($instance, $uid, $min_id, $searchurl), $toots); + $min_id_new = $toots['0']['id']; // the latest ID of the recent search + + if ($min_id_new === $min_id) { + // min_id is the latest, let's write the new DB and end this loop + $uptodate = true; + debug("Toots up-to-date. Rewrite cache DB."); + write_db($dbt, $toots); + } else { + // next round looks for toots newer than the newly found ID + debug("Newer toots than in cache found. Starting another search for new toots"); + $min_id = $min_id_new; + } + } +} else { + debug("Toots cache is OK"); } /* prepare $result array */