diff options
| author | 2024-01-03 11:23:06 +0100 | |
|---|---|---|
| committer | 2024-01-03 11:23:06 +0100 | |
| commit | 70e71b8364c1317af04f92fd86df4541fa269e0c (patch) | |
| tree | 193f4db62a85a218e227ee79ce7554a32eade390 /app/Controllers/feedController.php | |
| parent | 1e5f5078ed029640f69bdcc5ba51dd4dc69574ca (diff) | |
Auto-label (#5954)
Add labels automatically to incoming articles
fix https://github.com/FreshRSS/FreshRSS/issues/2380
fix https://github.com/FreshRSS/FreshRSS/issues/2420
fix https://github.com/FreshRSS/FreshRSS/issues/3279
fix https://github.com/FreshRSS/FreshRSS/discussions/4947
fix https://github.com/FreshRSS/FreshRSS/issues/5728
fix https://github.com/FreshRSS/FreshRSS/issues/5599
Diffstat (limited to 'app/Controllers/feedController.php')
| -rw-r--r-- | app/Controllers/feedController.php | 86 |
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(); |
