Skip to content

Commit d5affbb

Browse files
chore: Refactor the edition of links collections
1 parent de8d6e6 commit d5affbb

File tree

6 files changed

+412
-371
lines changed

6 files changed

+412
-371
lines changed

src/controllers/links/Collections.php

Lines changed: 40 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
namespace App\controllers\links;
44

5-
use Minz\Request;
6-
use Minz\Response;
75
use App\auth;
86
use App\controllers\BaseController;
7+
use App\forms;
98
use App\jobs;
109
use App\models;
1110
use App\utils;
11+
use Minz\Request;
12+
use Minz\Response;
1213

1314
/**
1415
* Handle the requests related to the links collections.
@@ -38,20 +39,18 @@ public function index(Request $request): Response
3839
$user = $this->requireCurrentUser(redirect_after_login: $from);
3940

4041
$link = models\Link::find($link_id);
41-
$notes = [];
42+
4243
if (!$link || !auth\LinksAccess::canView($user, $link)) {
4344
return Response::notFound('not_found.phtml');
4445
}
4546

46-
if ($link->user_id !== $user->id) {
47-
$existing_link = models\Link::findBy([
48-
'user_id' => $user->id,
49-
'url_hash' => models\Link::hashUrl($link->url),
50-
]);
51-
52-
if ($existing_link) {
53-
$link = $existing_link;
54-
}
47+
// Make sure that if the user has already saved the link's URL, we work
48+
// with this one instead of a link potentially owned by another user.
49+
// In particular, this allows to be sure that the collections are
50+
// correctly selected.
51+
$existing_link = $user->correspondingOwnedLink($link);
52+
if ($existing_link) {
53+
$link = $existing_link;
5554
}
5655

5756
if (auth\LinksAccess::canUpdate($user, $link)) {
@@ -60,40 +59,14 @@ public function index(Request $request): Response
6059
$collection_ids = [];
6160
}
6261

63-
$groups = models\Group::listBy(['user_id' => $user->id]);
64-
$groups = utils\Sorter::localeSort($groups, 'name');
65-
66-
$collections = $user->collections();
67-
$collections = utils\Sorter::localeSort($collections, 'name');
68-
$groups_to_collections = utils\Grouper::groupBy($collections, 'group_id');
69-
70-
$shared_collections = $user->sharedCollections([], [
71-
'access_type' => 'write',
72-
]);
73-
$shared_collections = utils\Sorter::localeSort($shared_collections, 'name');
74-
$collections_by_others = models\Collection::listWritableContainingNotOwnedLinkWithUrl(
75-
$user->id,
76-
$link->url_hash,
77-
);
78-
$collections_by_others = utils\Sorter::localeSort($collections_by_others, 'name');
79-
80-
$mastodon_configured = models\MastodonAccount::existsBy([
81-
'user_id' => $user->id,
82-
]);
62+
$form = new forms\links\EditLinkCollections([
63+
'collection_ids' => $collection_ids,
64+
'mark_as_read' => $mark_as_read,
65+
], $link);
8366

8467
return Response::ok('links/collections/index.phtml', [
8568
'link' => $link,
86-
'collection_ids' => $collection_ids,
87-
'new_collection_names' => [],
88-
'name_max_length' => models\Collection::NAME_MAX_LENGTH,
89-
'groups' => $groups,
90-
'groups_to_collections' => $groups_to_collections,
91-
'shared_collections' => $shared_collections,
92-
'collections_by_others' => $collections_by_others,
93-
'mark_as_read' => $mark_as_read,
94-
'content' => '',
95-
'share_on_mastodon' => false,
96-
'mastodon_configured' => $mastodon_configured,
69+
'form' => $form,
9770
'from' => $from,
9871
]);
9972
}
@@ -123,16 +96,7 @@ public function index(Request $request): Response
12396
public function update(Request $request): Response
12497
{
12598
$link_id = $request->parameters->getString('id', '');
126-
/** @var string[] */
127-
$new_collection_ids = $request->parameters->getArray('collection_ids', []);
128-
/** @var string[] */
129-
$new_collection_names = $request->parameters->getArray('new_collection_names', []);
130-
$is_hidden = $request->parameters->getBoolean('is_hidden');
131-
$mark_as_read = $request->parameters->getBoolean('mark_as_read');
132-
$content = trim($request->parameters->getString('content', ''));
133-
$share_on_mastodon = $request->parameters->getBoolean('share_on_mastodon');
13499
$from = $request->parameters->getString('from', '');
135-
$csrf = $request->parameters->getString('csrf', '');
136100

137101
$user = $this->requireCurrentUser(redirect_after_login: $from);
138102

@@ -141,66 +105,48 @@ public function update(Request $request): Response
141105
return Response::notFound('not_found.phtml');
142106
}
143107

144-
if (!$user->canWriteCollections($new_collection_ids)) {
145-
\Minz\Flash::set('error', _('One of the associated collection doesn’t exist.'));
146-
return Response::found($from);
108+
if (!auth\LinksAccess::canUpdate($user, $link)) {
109+
$link = $user->obtainLink($link);
110+
utils\SourceHelper::setLinkSource($link, $from);
147111
}
148112

149-
if (!\App\Csrf::validate($csrf)) {
150-
\Minz\Flash::set('error', _('A security verification failed.'));
151-
return Response::found($from);
152-
}
113+
$form = new forms\links\EditLinkCollections(model: $link);
153114

154-
$link_collections = [];
115+
$form->handleRequest($request);
155116

156-
foreach ($new_collection_ids as $collection_id) {
157-
$collection = models\Collection::find($collection_id);
158-
if ($collection) {
159-
$link_collections[] = $collection;
160-
}
117+
if (!$form->validate()) {
118+
return Response::badRequest('links/collections/index.phtml', [
119+
'link' => $link,
120+
'form' => $form,
121+
'from' => $from,
122+
]);
161123
}
162124

163-
foreach ($new_collection_names as $name) {
164-
$new_collection = models\Collection::init($user->id, $name, '', false);
165-
166-
if (!$new_collection->validate()) {
167-
\Minz\Flash::set('errors', $new_collection->errors());
168-
return Response::found($from);
169-
}
170-
171-
$new_collection->save();
172-
$link_collections[] = $new_collection;
173-
}
125+
$link = $form->model();
126+
$link->save();
174127

175-
if (!auth\LinksAccess::canUpdate($user, $link)) {
176-
$link = $user->obtainLink($link);
177-
utils\SourceHelper::setLinkSource($link, $from);
128+
$link_collections = $form->selectedCollections();
129+
foreach ($form->newCollections() as $collection) {
130+
$collection->save();
131+
$link_collections[] = $collection;
178132
}
179133

180-
$link->is_hidden = $is_hidden;
181-
$link->save();
182-
183134
$link->setCollections($link_collections);
184135

185-
if ($content) {
186-
$note = new models\Note($user->id, $link->id, $content);
136+
$note = $form->note();
137+
if ($note) {
187138
$note->save();
188139
}
189140

190-
if ($mark_as_read) {
191-
$user->markAsRead($link);
192-
}
193-
194141
$link->refreshTags();
195142

196-
$mastodon_configured = models\MastodonAccount::existsBy([
197-
'user_id' => $user->id,
198-
]);
143+
if ($form->mark_as_read) {
144+
$user->markAsRead($link);
145+
}
199146

200-
if ($mastodon_configured && $share_on_mastodon) {
201-
$note_id = isset($note) ? $note->id : null;
147+
if ($form->shouldShareOnMastodon()) {
202148
$share_on_mastodon_job = new jobs\ShareOnMastodon();
203-
$share_on_mastodon_job->performAsap($user->id, $link->id, $note_id);
149+
$share_on_mastodon_job->performAsap($user->id, $link->id, $note?->id);
204150
}
205151

206152
return Response::found($from);

0 commit comments

Comments
 (0)