fix bug with cached comment stats

This commit is contained in:
2019-10-19 00:44:49 +02:00
parent f6c7576519
commit 16e44ab3f4

View File

@@ -5,7 +5,7 @@ require_once 'config.php';
$instance = $config['mastodon-instance'];
$uid = $config['user-id'];
$searchurl = $config['search-url'];
$search = isset($_GET['search']) ? $_GET['search'] : '';
$search = isset($_GET['search']) ? strtolower($_GET['search']) : '';
$debug_on = $config['debug'];
/* cache files */
$ctt = $config['cache_toots'];
@@ -40,6 +40,14 @@ function write_db($db, $data, $id) {
$encoded = json_encode($file, JSON_PRETTY_PRINT);
file_put_contents($db, $encoded, LOCK_EX);
}
/* delete file */
function delete_db($db, $id) {
// if $id is given, it's a comments file. Replace placeholder in filename
if ($id) {
$db = str_replace('%id', $id, $db);
}
unlink($db);
}
/* access data from file */
function read_db($db, &$data, $cachetime, &$cachebreak, $id) {
// if $id is given, it's a comments file. Replace placeholder in filename
@@ -185,11 +193,19 @@ if ($cachebreak) {
debug("Toots cache is up-to-date");
}
/* prepare empty $result array with structure */
$result = ['comments' => [], 'stats' => ['reblogs' => 0, 'favs' => 0, 'replies' => 0, 'url' => '', 'root' => 0]];
// create empty $result
$result_empty = ['comments' => [], 'stats' => ['reblogs' => 0, 'favs' => 0, 'replies' => 0, 'url' => '', 'root' => 0]];
$result = $result_empty;
/* check if URL from $search exists in $toots */
$id = array_keys(array_column($toots, 'url'), strtolower($search));
$id = array_keys(
array_filter(
array_column($toots, 'url'),
function ($value) use ($search) {
return (strpos($value, $search) !== false);
}
)
);
if (empty($id)) {
debug("Blog URL \"$search\" has not been found");
} else {
@@ -202,6 +218,11 @@ if (empty($id)) {
if ($cachebreak) {
debug("Comments cache for $id outdated. Checking for new comments");
// delete old cache file, otherwise the stats would add up
delete_db($dbc, $id);
// re-create empty $result and new cache file
$result = $result_empty;
read_db($dbc, $result, $ctc, $cachebreak, $id);
/* Extract comments and stats from toot */
tootContext($instance, $id, $result);
tootStats($instance, $id, $result);