-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathNews.php
128 lines (106 loc) · 3.57 KB
/
News.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
namespace App\controllers;
use Minz\Request;
use Minz\Response;
use App\auth;
use App\models;
use App\utils;
/**
* Handle the requests related to the news.
*
* @author Marien Fressinaud <[email protected]>
* @license http://www.gnu.org/licenses/agpl-3.0.en.html AGPL
*/
class News
{
/**
* Show the news page.
*
* @response 302 /login?redirect_to=/news
* if not connected
* @response 200
*/
public function index(): Response
{
$user = auth\CurrentUser::get();
if (!$user) {
return Response::redirect('login', [
'redirect_to' => \Minz\Url::for('news'),
]);
}
$news = $user->news();
$links = $news->links(['published_at', 'number_comments']);
$links_timeline = new utils\LinksTimeline($links);
return Response::ok('news/index.phtml', [
'news' => $news,
'links_timeline' => $links_timeline,
'no_news' => \Minz\Flash::pop('no_news'),
]);
}
/**
* Fill the news page with links to read (from bookmarks and followed
* collections)
*
* @request_param string csrf
*
* @response 302 /login?redirect_to=/news
* if not connected
* @response 400
* if csrf is invalid
* @response 302 /news
*/
public function create(Request $request): Response
{
$user = auth\CurrentUser::get();
if (!$user) {
return Response::redirect('login', [
'redirect_to' => \Minz\Url::for('news'),
]);
}
$csrf = $request->param('csrf', '');
$news = $user->news();
if (!\Minz\Csrf::validate($csrf)) {
return Response::badRequest('news/index.phtml', [
'news' => $news,
'links_timeline' => new utils\LinksTimeline([]),
'no_news' => false,
'error' => _('A security verification failed: you should retry to submit the form.'),
]);
}
$links = models\Link::listFromFollowedCollections($user->id, max:50);
$news = $user->news();
foreach ($links as $news_link) {
$link = $user->obtainLink($news_link);
// If the link has already a source info, we want to keep it (it
// might have been get via a followed collection, and put in the
// bookmarks then)
if (!$link->source_type && $news_link->source_news_type !== null) {
$link->source_type = $news_link->source_news_type;
$link->source_resource_id = $news_link->source_news_resource_id;
}
// Make sure to reset this value: it will be set to true later with
// Link::groupLinksBySources
$link->group_by_source = false;
$link->save();
// And don't forget to add the link to the news collection!
models\LinkToCollection::attach([$link->id], [$news->id], $news_link->published_at);
}
models\Link::groupLinksBySources($news->id);
if (!$links) {
\Minz\Flash::set('no_news', true);
}
return Response::redirect('news');
}
public function showAvailable(Request $request): Response
{
$user = auth\CurrentUser::get();
if (!$user) {
return Response::redirect('login', [
'redirect_to' => \Minz\Url::for('news'),
]);
}
return Response::json(200, [
'available' => models\Link::anyFromFollowedCollections($user->id),
]);
}
}