aboutsummaryrefslogtreecommitdiff
path: root/app/Models
diff options
context:
space:
mode:
authorGravatar Alexandre Alapetite <alexandre@alapetite.fr> 2023-06-16 16:11:16 +0200
committerGravatar GitHub <noreply@github.com> 2023-06-16 16:11:16 +0200
commit723f7577d0a388a90779930754c5aacb9f66b168 (patch)
treeaa863f67592d0795be83b533de981082e0713601 /app/Models
parent228d7adfdb90c3fdd179f80fbfde565eb06e0cec (diff)
Refactor lastSeen and markReadAsGone (#5470)
* Refactor lastSeen and markReadAsGone Make the logic a bit more robust and explicit * Remove forgotten SQL param * Add test inTransaction * More robust transaction * Add a debug log * Add max timestamp to markAsReadUponGone * Reduce number of debug lines * typing * Better detection of when feed is empty * More explicit case for push
Diffstat (limited to 'app/Models')
-rw-r--r--app/Models/EntryDAO.php13
-rw-r--r--app/Models/Feed.php24
-rw-r--r--app/Models/FeedDAO.php4
3 files changed, 27 insertions, 14 deletions
diff --git a/app/Models/EntryDAO.php b/app/Models/EntryDAO.php
index 9ea7bd261..f9bdd7be2 100644
--- a/app/Models/EntryDAO.php
+++ b/app/Models/EntryDAO.php
@@ -120,7 +120,7 @@ SQL;
*/
private $addEntryPrepared = false;
- /** @param array{'id':string,'guid':string,'title':string,'author':string,'content':string,'link':string,'date':int,'hash':string,
+ /** @param array{'id':string,'guid':string,'title':string,'author':string,'content':string,'link':string,'date':int,'lastSeen':int,'hash':string,
* 'is_read':bool|int|null,'is_favorite':bool|int|null,'id_feed':int,'tags':string,'attributes':array<string,mixed>} $valuesTmp */
public function addEntry(array $valuesTmp, bool $useTmpTable = true): bool {
if ($this->addEntryPrepared == null) {
@@ -556,7 +556,10 @@ SQL;
$idMax = time() . '000000';
Minz_Log::debug('Calling markReadFeed(0) is deprecated!');
}
- $this->pdo->beginTransaction();
+ $hadTransaction = $this->pdo->inTransaction();
+ if (!$hadTransaction) {
+ $this->pdo->beginTransaction();
+ }
$sql = 'UPDATE `_entry` '
. 'SET is_read=? '
@@ -589,7 +592,9 @@ SQL;
}
}
- $this->pdo->commit();
+ if (!$hadTransaction) {
+ $this->pdo->commit();
+ }
return $affected;
}
@@ -698,7 +703,7 @@ SQL;
}
}
- /** @return Traversable<array{'id':string,'guid':string,'title':string,'author':string,'content':string,'link':string,'date':int,
+ /** @return Traversable<array{'id':string,'guid':string,'title':string,'author':string,'content':string,'link':string,'date':int,'lastSeen':int,
* 'hash':string,'is_read':?bool,'is_favorite':?bool,'id_feed':int,'tags':string,'attributes':array<string,mixed>}> */
public function selectAll(): Traversable {
$content = static::isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content';
diff --git a/app/Models/Feed.php b/app/Models/Feed.php
index 09f0ef068..a27259978 100644
--- a/app/Models/Feed.php
+++ b/app/Models/Feed.php
@@ -764,23 +764,35 @@ class FreshRSS_Feed extends Minz_Model {
/**
* Applies the *mark as read upon gone* policy, if enabled.
- * Remember to call updateCachedValue($id_feed) or updateCachedValues() just after.
+ * Remember to call `updateCachedValue($id_feed)` or `updateCachedValues()` just after.
* @return int|false the number of lines affected, or false if not applicable
*/
- public function markAsReadUponGone() {
+ public function markAsReadUponGone(bool $upstreamIsEmpty, int $maxTimestamp = 0) {
$readUponGone = $this->attributes('read_upon_gone');
if ($readUponGone === null) {
$readUponGone = FreshRSS_Context::$user_conf->mark_when['gone'];
}
- if ($readUponGone) {
+ if (!$readUponGone) {
+ return false;
+ }
+ if ($upstreamIsEmpty) {
+ if ($maxTimestamp <= 0) {
+ $maxTimestamp = time();
+ }
+ $entryDAO = FreshRSS_Factory::createEntryDao();
+ $affected = $entryDAO->markReadFeed($this->id(), $maxTimestamp . '000000');
+ } else {
$feedDAO = FreshRSS_Factory::createFeedDao();
- return $feedDAO->markAsReadUponGone($this->id());
+ $affected = $feedDAO->markAsReadUponGone($this->id());
}
- return false;
+ if ($affected > 0) {
+ Minz_Log::debug(__METHOD__ . " $affected items" . ($upstreamIsEmpty ? ' (all)' : '') . ' [' . $this->url(false) . ']');
+ }
+ return $affected;
}
/**
- * Remember to call updateCachedValue($id_feed) or updateCachedValues() just after
+ * Remember to call `updateCachedValue($id_feed)` or `updateCachedValues()` just after
* @return int|false
*/
public function cleanOldEntries() {
diff --git a/app/Models/FeedDAO.php b/app/Models/FeedDAO.php
index ec51486a6..b9295abe0 100644
--- a/app/Models/FeedDAO.php
+++ b/app/Models/FeedDAO.php
@@ -503,16 +503,12 @@ WHERE id_feed=:id_feed1 AND is_read=0 AND (
`lastSeen` + 60 < (SELECT s1.maxlastseen FROM (
SELECT MAX(e2.`lastSeen`) AS maxlastseen FROM `_entry` e2 WHERE e2.id_feed = :id_feed2
) s1)
- OR `lastSeen` + 60 < (SELECT s2.lastcorrectupdate FROM (
- SELECT f2.`lastUpdate` AS lastcorrectupdate FROM `_feed` f2 WHERE f2.id = :id_feed3 AND f2.error = 0
- ) s2)
)
SQL;
if (($stm = $this->pdo->prepare($sql)) &&
$stm->bindParam(':id_feed1', $id, PDO::PARAM_INT) &&
$stm->bindParam(':id_feed2', $id, PDO::PARAM_INT) &&
- $stm->bindParam(':id_feed3', $id, PDO::PARAM_INT) &&
$stm->execute()) {
return $stm->rowCount();
} else {