aboutsummaryrefslogtreecommitdiff
path: root/app/Controllers/feedController.php
diff options
context:
space:
mode:
Diffstat (limited to 'app/Controllers/feedController.php')
-rw-r--r--app/Controllers/feedController.php86
1 files changed, 69 insertions, 17 deletions
diff --git a/app/Controllers/feedController.php b/app/Controllers/feedController.php
index be86afb2d..aa1b30182 100644
--- a/app/Controllers/feedController.php
+++ b/app/Controllers/feedController.php
@@ -679,29 +679,81 @@ class FreshRSS_feed_Controller extends FreshRSS_ActionController {
return [$updated_feeds, reset($feeds) ?: null, $nb_new_articles];
}
- public static function commitNewEntries(): bool {
- $entryDAO = FreshRSS_Factory::createEntryDao();
- if (!$entryDAO->inTransaction()) {
- $entryDAO->beginTransaction();
+ /**
+ * @param array<int,int> $newUnreadEntriesPerFeed
+ * @return int|false The number of articles marked as read, of false if error
+ */
+ private static function keepMaxUnreads(array $newUnreadEntriesPerFeed) {
+ $affected = 0;
+ $feedDAO = FreshRSS_Factory::createFeedDao();
+ $feeds = $feedDAO->listFeedsOrderUpdate(-1);
+ foreach ($feeds as $feed) {
+ if (!empty($newUnreadEntriesPerFeed[$feed->id()]) && $feed->keepMaxUnread() !== null &&
+ ($feed->nbNotRead() + $newUnreadEntriesPerFeed[$feed->id()] > $feed->keepMaxUnread())) {
+ Minz_Log::debug('New unread entries (' . ($feed->nbNotRead() + $newUnreadEntriesPerFeed[$feed->id()]) . ') exceeding max number of ' .
+ $feed->keepMaxUnread() . ' for [' . $feed->url(false) . ']');
+ $n = $feed->markAsReadMaxUnread();
+ if ($n === false) {
+ $affected = false;
+ break;
+ } else {
+ $affected += $n;
+ }
+ }
+ }
+ if ($feedDAO->updateCachedValues() === false) {
+ $affected = false;
}
+ return $affected;
+ }
- $newUnreadEntriesPerFeed = $entryDAO->newUnreadEntriesPerFeed();
- if ($entryDAO->commitNewEntries()) {
- $feedDAO = FreshRSS_Factory::createFeedDao();
- $feeds = $feedDAO->listFeedsOrderUpdate(-1);
- foreach ($feeds as $feed) {
- if (!empty($newUnreadEntriesPerFeed[$feed->id()]) && $feed->keepMaxUnread() !== null &&
- ($feed->nbNotRead() + $newUnreadEntriesPerFeed[$feed->id()] > $feed->keepMaxUnread())) {
- Minz_Log::debug('New unread entries (' . ($feed->nbNotRead() + $newUnreadEntriesPerFeed[$feed->id()]) . ') exceeding max number of ' .
- $feed->keepMaxUnread() . ' for [' . $feed->url(false) . ']');
- $feed->markAsReadMaxUnread();
+ /**
+ * Auto-add labels to new articles.
+ * @param int $nbNewEntries The number of top recent entries to process.
+ * @return int|false The number of new labels added, or false in case of error.
+ */
+ private static function applyLabelActions(int $nbNewEntries) {
+ $tagDAO = FreshRSS_Factory::createTagDao();
+ $labels = $tagDAO->listTags() ?: [];
+ $labels = array_filter($labels, static function (FreshRSS_Tag $label) {
+ return !empty($label->filtersAction('label'));
+ });
+ if (count($labels) <= 0) {
+ return 0;
+ }
+
+ $entryDAO = FreshRSS_Factory::createEntryDao();
+ /** @var array<array{id_tag:int,id_entry:string}> $applyLabels */
+ $applyLabels = [];
+ foreach (FreshRSS_Entry::fromTraversable($entryDAO->selectAll($nbNewEntries)) as $entry) {
+ foreach ($labels as $label) {
+ $label->applyFilterActions($entry, $applyLabel);
+ if ($applyLabel) {
+ $applyLabels[] = [
+ 'id_tag' => $label->id(),
+ 'id_entry' => $entry->id(),
+ ];
}
}
- $feedDAO->updateCachedValues();
}
+ return $tagDAO->tagEntries($applyLabels);
+ }
- if ($entryDAO->inTransaction()) {
- $entryDAO->commit();
+ public static function commitNewEntries(): bool {
+ $entryDAO = FreshRSS_Factory::createEntryDao();
+ $newUnreadEntriesPerFeed = $entryDAO->newUnreadEntriesPerFeed();
+ $nbNewEntries = array_sum($newUnreadEntriesPerFeed);
+ if ($nbNewEntries > 0) {
+ if (!$entryDAO->inTransaction()) {
+ $entryDAO->beginTransaction();
+ }
+ if ($entryDAO->commitNewEntries()) {
+ self::keepMaxUnreads($newUnreadEntriesPerFeed);
+ self::applyLabelActions($nbNewEntries);
+ }
+ if ($entryDAO->inTransaction()) {
+ $entryDAO->commit();
+ }
}
$databaseDAO = FreshRSS_Factory::createDatabaseDAO();