-
Notifications
You must be signed in to change notification settings - Fork 342
Expand file tree
/
Copy pathSources.php
More file actions
148 lines (121 loc) · 3.57 KB
/
Sources.php
File metadata and controls
148 lines (121 loc) · 3.57 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
declare(strict_types=1);
namespace Selfoss\controllers;
use InvalidArgumentException;
use Selfoss\daos;
use Selfoss\helpers\Authentication;
use Selfoss\helpers\Misc;
use Selfoss\helpers\SpoutLoader;
use Selfoss\helpers\View;
/**
* Controller for sources handling
*
* @copyright Copyright (c) Tobias Zeising (http://www.aditu.de)
* @license GPLv3 (https://www.gnu.org/licenses/gpl-3.0.html)
* @author Tobias Zeising <tobias.zeising@aditu.de>
*/
final readonly class Sources {
public function __construct(
private Authentication $authentication,
private daos\Sources $sourcesDao,
private SpoutLoader $spoutLoader,
private daos\Tags $tagsDao,
private View $view
) {
}
/**
* list all available sources
* json
*/
public function show(): void {
$this->authentication->ensureIsPrivileged();
// get available spouts
$spouts = $this->spoutLoader->all();
$sources = [];
// load sources
foreach ($this->sourcesDao->getWithIcon() as $source) {
// decode params
$source['params'] = json_decode(html_entity_decode($source['params']), true);
$sources[] = $source;
}
$this->view->jsonSuccess([
'spouts' => $spouts,
'sources' => $sources,
]);
}
/**
* render spouts params
* json
*/
public function params(): void {
$this->authentication->ensureIsPrivileged();
if (!isset($_GET['spout'])) {
$this->view->error('no spout type given');
}
$spoutClass = str_replace('_', '\\', $_GET['spout']);
$spout = $this->spoutLoader->get($spoutClass);
if ($spout === null) {
$this->view->error('invalid spout type given');
}
$id = 'new-' . random_int(0, mt_getrandmax());
$this->view->jsonSuccess([
'id' => $id,
'spout' => $spout,
]);
}
/**
* delete source
* json
*
* @param string $id ID of source to remove
*/
public function remove(string $id): void {
$this->authentication->ensureIsPrivileged();
try {
$id = Misc::forceId($id);
} catch (InvalidArgumentException) {
$this->view->error('invalid id given');
}
$this->sourcesDao->delete($id);
// cleanup tags
$allTags = $this->sourcesDao->getAllTags();
$this->tagsDao->cleanup($allTags);
$this->view->jsonSuccess([
'success' => true,
]);
}
/**
* returns all available sources
* json
*/
public function listSources(): void {
$this->authentication->ensureIsPrivileged();
// load sources
$sources = $this->sourcesDao->getWithIcon();
// get last icon
foreach ($sources as &$source) {
$source['params'] = json_decode(html_entity_decode($source['params']), true);
$source['error'] ??= '';
}
$this->view->jsonSuccess($sources);
}
/**
* returns all available spouts
* json
*/
public function spouts(): never {
$this->authentication->ensureIsPrivileged();
$spouts = $this->spoutLoader->all();
$this->view->jsonSuccess($spouts);
}
/**
* returns all sources with unread items
* json
*/
public function stats(): never {
$this->authentication->ensureCanRead();
// load sources
$sources = $this->sourcesDao->getWithUnread();
$this->view->jsonSuccess($sources);
}
}