Skip to content

Commit 18b70a9

Browse files
fix: Make sure that links and feeds are saved in database
Usually, the fetch services save the models. Except when rate limiting is reached. In that case, the link may not be created in the database. It generated fatal errors on the API
1 parent 8deac9c commit 18b70a9

File tree

6 files changed

+35
-47
lines changed

6 files changed

+35
-47
lines changed

src/controllers/Links.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ public function create(Request $request): Response
213213
'ignore_rate_limit' => true,
214214
]);
215215
$link_fetcher_service->fetch($link);
216+
$link->save();
216217
}
217218

218219
$link_collections = $form->selectedCollections();

src/controllers/collections/Links.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public function create(Request $request): Response
101101
'ignore_rate_limit' => true,
102102
]);
103103
$link_fetcher_service->fetch($link);
104+
$link->save();
104105
}
105106

106107
$link->addCollection($collection);

src/controllers/links/Repairing.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ public function create(Request $request): Response
9999
'force_sync' => $form->force_sync,
100100
]);
101101
$link_fetcher_service->fetch($link);
102+
$link->save();
102103

103104
// Add the old link to the never list. It avoids to a link coming from
104105
// the news to reappear.

src/controllers/links/Searches.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,14 @@ public function create(Request $request): Response
7878
$link = $form->link();
7979
if (!$link->isPersisted()) {
8080
$link_fetcher_service->fetch($link);
81+
$link->save();
8182
}
8283

8384
$feeds = $form->feeds();
8485
foreach ($feeds as $feed) {
8586
if (!$feed->isPersisted()) {
8687
$feed_fetcher_service->fetch($feed);
88+
$feed->save();
8789
}
8890
}
8991

src/forms/api/Search.php

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\models;
66
use App\services;
7+
use App\utils;
78
use Minz\Form;
89
use Minz\Validable;
910

@@ -13,18 +14,15 @@
1314
*/
1415
class Search extends Form
1516
{
17+
use utils\Memoizer;
18+
1619
#[Form\Field(transform: '\SpiderBits\Url::sanitize')]
1720
#[Validable\Presence(message: 'The link is required.')]
1821
#[Validable\Url(message: 'The link is invalid.')]
1922
public string $url = '';
2023

2124
private models\User $user;
2225

23-
private ?models\Link $cached_link = null;
24-
25-
/** @var ?models\Collection[] */
26-
private ?array $cached_feeds = null;
27-
2826
public function __construct(models\User $user)
2927
{
3028
parent::__construct();
@@ -34,59 +32,43 @@ public function __construct(models\User $user)
3432

3533
public function link(): models\Link
3634
{
37-
if ($this->cached_link !== null) {
38-
return $this->cached_link;
39-
}
40-
41-
$link = models\Link::findBy([
42-
'user_id' => $this->user->id,
43-
'url_hash' => models\Link::hashUrl($this->url),
44-
]);
45-
46-
if (!$link) {
47-
$link = new models\Link($this->url, $this->user->id);
48-
49-
$link_fetcher_service = new services\LinkFetcher();
50-
$link_fetcher_service->fetch($link);
51-
}
35+
return $this->memoize('link', function (): models\Link {
36+
$link = $this->user->findOrBuildLink($this->url);
5237

53-
$this->cached_link = $link;
38+
if (!$link->isPersisted()) {
39+
$link_fetcher_service = new services\LinkFetcher();
40+
$link_fetcher_service->fetch($link);
41+
$link->save();
42+
}
5443

55-
return $link;
44+
return $link;
45+
});
5646
}
5747

5848
/**
5949
* @return models\Collection[]
6050
*/
6151
public function feeds(): array
6252
{
63-
if ($this->cached_feeds !== null) {
64-
return $this->cached_feeds;
65-
}
66-
67-
$link = $this->link();
68-
$feeds = [];
69-
70-
$support_user = models\User::supportUser();
71-
$feed_fetcher_service = new services\FeedFetcher();
72-
73-
foreach ($link->url_feeds as $feed_url) {
74-
$feed = models\Collection::findBy([
75-
'type' => 'feed',
76-
'feed_url' => $feed_url,
77-
'user_id' => $support_user->id,
78-
]);
79-
80-
if (!$feed) {
81-
$feed = models\Collection::initFeed($support_user->id, $feed_url);
82-
$feed_fetcher_service->fetch($feed);
83-
}
53+
return $this->memoize('feeds', function (): array {
54+
$link = $this->link();
55+
$feeds = [];
56+
57+
$support_user = models\User::supportUser();
58+
$feed_fetcher_service = new services\FeedFetcher();
8459

85-
$feeds[] = $feed;
86-
}
60+
foreach ($link->url_feeds as $feed_url) {
61+
$feed = models\Collection::findOrBuildFeed($feed_url);
8762

88-
$this->cached_feeds = $feeds;
63+
if (!$feed->isPersisted()) {
64+
$feed_fetcher_service->fetch($feed);
65+
$feed->save();
66+
}
67+
68+
$feeds[] = $feed;
69+
}
8970

90-
return $feeds;
71+
return $feeds;
72+
});
9173
}
9274
}

src/forms/collections/NewFeed.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public function setAutodiscovering(Request $request): void
5353
'ignore_rate_limit' => true,
5454
]);
5555
$link_fetcher_service->fetch($link);
56+
$link->save();
5657

5758
$this->autodiscovering_link = $link;
5859
}

0 commit comments

Comments
 (0)