Fix wish cache being generated twice

This commit is contained in:
Jay Trees 2022-03-23 12:03:31 +01:00
parent a2611faf49
commit 42ae1e63bd
3 changed files with 24 additions and 13 deletions

View file

@ -104,7 +104,7 @@ $(function () {
var cards = $('.ui.card[data-cache="false"]'); var cards = $('.ui.card[data-cache="false"]');
cards.each(function (index, card) { cards.each(function (index, card) {
generateCacheCard(card); generateCacheCard($(card));
if (index >= 0) { if (index >= 0) {
return false; return false;
@ -130,9 +130,7 @@ $(function () {
}); });
function generateCacheCard(card) { function generateCacheCard(card) {
card = $(card); var href = card.find('.content [href]').prop('href');
var href = card.find('.content [href]').prop('href');
if (!href) { if (!href) {
return; return;
@ -144,17 +142,16 @@ $(function () {
var wishlistIndex = $('.ui.dropdown.wishlists select').prop('selectedIndex') - 1; var wishlistIndex = $('.ui.dropdown.wishlists select').prop('selectedIndex') - 1;
var wishlist_user = wishlists[wishlistIndex].user; var wishlist_user = wishlists[wishlistIndex].user;
fetch('/src/api/wishes.php?wish_id=' + card.data('id') + '&wishlist_user=' + wishlist_user, { fetch('/src/api/wishes.php?wish_id=' + card.attr('data-id') + '&wishlist_user=' + wishlist_user, {
method: 'GET' method: 'GET'
}) })
.then(handleFetchError) .then(handleFetchError)
.then(handleFetchResponse) .then(handleFetchResponse)
.then(function(response) { .then(function(response) {
card.replaceWith(response.html); card.replaceWith(response.html.replace('data-cache="false"', 'data-cache="true"'));
}) })
.catch(handleFetchCatch) .catch(handleFetchCatch)
.finally(function() { .finally(function() {
card.attr('data-cache', 'true');
card.removeClass('loading'); card.removeClass('loading');
progress.progress('increment'); progress.progress('increment');

View file

@ -38,10 +38,8 @@ class EmbedCache
public function get(bool $generateCache = false): \stdClass public function get(bool $generateCache = false): \stdClass
{ {
$info = null; $info = null;
$maxAge = 2592000; // 30 days
$age = file_exists($this->getFilepath()) ? time() - filemtime($this->getFilepath()) : $maxAge;
if ($this->exists() && $age <= $maxAge && false === $generateCache) { if ($this->generateCache() && false === $generateCache) {
$info = json_decode(file_get_contents($this->getFilepath())); $info = json_decode(file_get_contents($this->getFilepath()));
} else { } else {
/** /**
@ -115,4 +113,20 @@ class EmbedCache
{ {
return file_exists($this->getFilepath()); return file_exists($this->getFilepath());
} }
public function age(): int
{
return time() - filemtime($this->getFilepath());
}
public function maxAge(): int
{
return 2592000; // 30 days
}
public function generateCache(): bool
{
return !$this->exists()
|| ($this->exists() && $this->age() <= $this->maxAge());
}
} }

View file

@ -101,13 +101,13 @@ class Wish
$userIsCurrent = isset($_SESSION['user']['id']) && intval($_SESSION['user']['id']) === $ofUser; $userIsCurrent = isset($_SESSION['user']['id']) && intval($_SESSION['user']['id']) === $ofUser;
if ($this->url) { if ($this->url) {
$exists = $this->cache->exists() || !$this->url ? 'true' : 'false'; $generateCache = $this->cache->generateCache() || !$this->url ? 'true' : 'false';
} else { } else {
$exists = 'true'; $generateCache = 'true';
} }
?> ?>
<div class="ui fluid card stretch" data-id="<?= $this->id ?>" data-cache="<?= $exists ?>"> <div class="ui fluid card stretch" data-id="<?= $this->id ?>" data-cache="<?= $generateCache ?>">
<div class="image"> <div class="image">
<?php if ($this->priority && isset(Wish::$priorities[$this->priority])) { ?> <?php if ($this->priority && isset(Wish::$priorities[$this->priority])) { ?>
<div class="ui small <?= Wish::$priorities[$this->priority]['color'] ?> right ribbon label"> <div class="ui small <?= Wish::$priorities[$this->priority]['color'] ?> right ribbon label">