summaryrefslogtreecommitdiffstats
path: root/lib/Service/FeedServiceV2.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Service/FeedServiceV2.php')
-rw-r--r--lib/Service/FeedServiceV2.php122
1 files changed, 62 insertions, 60 deletions
diff --git a/lib/Service/FeedServiceV2.php b/lib/Service/FeedServiceV2.php
index 16402d0da..88f18a3bf 100644
--- a/lib/Service/FeedServiceV2.php
+++ b/lib/Service/FeedServiceV2.php
@@ -29,7 +29,6 @@ use OCP\AppFramework\Db\DoesNotExistException;
use OCA\News\Db\Feed;
use OCA\News\Db\Item;
-use OCA\News\Db\FeedMapper;
use OCA\News\Db\ItemMapper;
use OCA\News\Fetcher\Fetcher;
use OCA\News\Config\Config;
@@ -105,22 +104,6 @@ class FeedServiceV2 extends Service
}
/**
- * Finds a feed of a user
- *
- * @param string $userId the name of the user
- * @param string $id the id of the feed
- *
- * @return Feed
- *
- * @throws DoesNotExistException
- * @throws MultipleObjectsReturnedException
- */
- public function findForUser(string $userId, string $id): Feed
- {
- return $this->mapper->findFromUser($userId, $id);
- }
-
- /**
* @param int|null $id
*
* @return Feed[]
@@ -139,6 +122,7 @@ class FeedServiceV2 extends Service
*/
public function findAllForUserRecursive(string $userId): array
{
+ /** @var Feed[] $feeds */
$feeds = $this->mapper->findAllFromUser($userId);
foreach ($feeds as &$feed) {
@@ -168,11 +152,23 @@ class FeedServiceV2 extends Service
*/
public function existsForUser(string $userID, string $url): bool
{
+ return $this->findByURL($userID, $url) !== null;
+ }
+
+ /**
+ * Check if a feed exists for a user
+ *
+ * @param string $userID the name of the user
+ * @param string $url the feed URL
+ *
+ * @return Entity|Feed|null
+ */
+ public function findByURL(string $userID, string $url): ?Entity
+ {
try {
- $this->mapper->findByURL($userID, $url);
- return true;
+ return $this->mapper->findByURL($userID, $url);
} catch (DoesNotExistException $e) {
- return false;
+ return null;
}
}
@@ -274,58 +270,59 @@ class FeedServiceV2 extends Service
$feed->getBasicAuthUser(),
$feed->getBasicAuthPassword()
);
+ } catch (ReadErrorException $ex) {
+ $feed->setUpdateErrorCount($feed->getUpdateErrorCount() + 1);
+ $feed->setLastUpdateError($ex->getMessage());
- // if there is no feed it means that no update took place
- if (!$fetchedFeed) {
- return $feed;
- }
+ return $this->mapper->update($feed);
+ }
- // update number of articles on every feed update
- $itemCount = count($items);
+ // if there is no feed it means that no update took place
+ if (!$fetchedFeed) {
+ return $feed;
+ }
- // this is needed to adjust to updates that add more items
- // than when the feed was created. You can't update the count
- // if it's lower because it may be due to the caching headers
- // that were sent as the request and it might cause unwanted
- // deletion and reappearing of feeds
- if ($itemCount > $feed->getArticlesPerUpdate()) {
- $feed->setArticlesPerUpdate($itemCount);
- }
+ // update number of articles on every feed update
+ $itemCount = count($items);
- $feed->setHttpLastModified($fetchedFeed->getHttpLastModified())
- ->setHttpEtag($fetchedFeed->getHttpEtag())
- ->setLocation($fetchedFeed->getLocation());
+ // this is needed to adjust to updates that add more items
+ // than when the feed was created. You can't update the count
+ // if it's lower because it may be due to the caching headers
+ // that were sent as the request and it might cause unwanted
+ // deletion and reappearing of feeds
+ if ($itemCount > $feed->getArticlesPerUpdate()) {
+ $feed->setArticlesPerUpdate($itemCount);
+ }
- // insert items in reverse order because the first one is
- // usually the newest item
- for ($i = $itemCount - 1; $i >= 0; $i--) {
- $item = $items[$i];
- $item->setFeedId($feed->getId())
- ->setBody($this->purifier->purify($item->getBody()));
+ $feed->setHttpLastModified($fetchedFeed->getHttpLastModified())
+ ->setHttpEtag($fetchedFeed->getHttpEtag())
+ ->setLocation($fetchedFeed->getLocation());
- // update modes: 0 nothing, 1 set unread
- if ($feed->getUpdateMode() === 1) {
- $item->setUnread(true);
- }
+ foreach (array_reverse($items) as &$item) {
+ $item->setFeedId($feed->getId())
+ ->setBody($this->purifier->purify($item->getBody()));
- $this->itemService->insertOrUpdate($item);
+ // update modes: 0 nothing, 1 set unread
+ if ($feed->getUpdateMode() === Feed::UPDATE_MODE_NORMAL) {
+ $item->setUnread(true);
}
- // mark feed as successfully updated
- $feed->setUpdateErrorCount(0);
- $feed->setLastUpdateError(null);
- } catch (ReadErrorException $ex) {
- $feed->setUpdateErrorCount($feed->getUpdateErrorCount() + 1);
- $feed->setLastUpdateError($ex->getMessage());
+ $item = $this->itemService->insertOrUpdate($item);
}
- return $this->mapper->update($feed);
- }
- public function delete(string $user, int $id): void
- {
- $feed = $this->mapper->findFromUser($user, $id);
- $this->mapper->delete($feed);
+ // mark feed as successfully updated
+ $feed->setUpdateErrorCount(0);
+ $feed->setLastUpdateError(null);
+
+ $unreadCount = 0;
+ array_map(function (Item $item) use (&$unreadCount) {
+ if ($item->isUnread()) {
+ $unreadCount++;
+ }
+ }, $items);
+
+ return $this->mapper->update($feed)->setUnreadCount($unreadCount);
}
/**
@@ -341,6 +338,11 @@ class FeedServiceV2 extends Service
$this->mapper->purgeDeleted($userID, $minTimestamp);
}
+ /**
+ * Fetch all feeds.
+ *
+ * @see FeedServiceV2::fetch()
+ */
public function fetchAll(): void
{
foreach ($this->findAll() as $feed) {