Skip to content

Commit

Permalink
fix: mastodon, cache tweaks, docs (#3661)
Browse files Browse the repository at this point in the history
* cache tweaks

* docs

* fix(mastodon): type bug
  • Loading branch information
dvikan authored Sep 10, 2023
1 parent 4b9f6f7 commit 3178deb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion bridges/ItakuBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ private function getData(string $url, bool $cache = false, bool $getJSON = false
// Debug::log($url);
if ($getJSON) { //get JSON object
if ($cache) {
$data = $this->loadCacheValue($url, 86400); // 24 hours
$data = $this->loadCacheValue($url);
if (is_null($data)) {
$data = getContents($url, $httpHeaders, $curlOptions) or returnServerError("Could not load $url");
$this->saveCacheValue($url, $data);
Expand Down
10 changes: 9 additions & 1 deletion bridges/MastodonBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ protected function parseItem($content)
// We fetch the boosted content.
try {
$rtContent = $this->fetchAP($content['object']);
if (!$rtContent) {
// Sometimes fetchAP returns null. Someone should figure out why. json_decode failure?
break;
}
$rtUser = $this->loadCacheValue($rtContent['attributedTo']);
if (!isset($rtUser)) {
// We fetch the author, since we cannot always assume the format of the URL.
Expand Down Expand Up @@ -277,6 +281,10 @@ protected function fetchAP($url)
array_push($headers, $sig);
}
}
return json_decode(getContents($url, $headers), true);
try {
return Json::decode(getContents($url, $headers));
} catch (\JsonException $e) {
return null;
}
}
}
8 changes: 5 additions & 3 deletions bridges/PixivBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,12 @@ private function checkCookie(array $headers)
private function getCookie()
{
// checks if cookie is set, if not initialise it with the cookie from the config
$value = $this->loadCacheValue('cookie', 2678400 /* 30 days + 1 day to let cookie chance to renew */);
$value = $this->loadCacheValue('cookie');
if (!isset($value)) {
$value = $this->getOption('cookie');
$this->saveCacheValue('cookie', $this->getOption('cookie'));

// 30 days + 1 day to let cookie chance to renew
$this->saveCacheValue('cookie', $this->getOption('cookie'), 2678400);
}
return $value;
}
Expand All @@ -370,7 +372,7 @@ private function getData(string $url, bool $cache = true, bool $getJSON = false,
}

if ($cache) {
$data = $this->loadCacheValue($url, 86400); // 24 hours
$data = $this->loadCacheValue($url);
if (!$data) {
$data = getContents($url, $httpHeaders, $curlOptions, true) or returnServerError("Could not load $url");
$this->saveCacheValue($url, $data);
Expand Down
4 changes: 4 additions & 0 deletions caches/SQLiteCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

declare(strict_types=1);

/**
* The storage table has a column `updated` which is incorrectly named.
* It should have been named `expiration` and the code treats it as an expiration date (in unix timestamp)
*/
class SQLiteCache implements CacheInterface
{
private \SQLite3 $db;
Expand Down

0 comments on commit 3178deb

Please sign in to comment.