-
Notifications
You must be signed in to change notification settings - Fork 342
Expand file tree
/
Copy pathItems.php
More file actions
159 lines (132 loc) · 3.89 KB
/
Items.php
File metadata and controls
159 lines (132 loc) · 3.89 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
149
150
151
152
153
154
155
156
157
158
159
<?php
declare(strict_types=1);
namespace Selfoss\controllers;
use DateTimeInterface;
use InvalidArgumentException;
use Selfoss\daos\ItemOptions;
use Selfoss\helpers\Authentication;
use Selfoss\helpers\Misc;
use Selfoss\helpers\Request;
use Selfoss\helpers\View;
/**
* Controller for item 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 Items {
public function __construct(
private Authentication $authentication,
private \Selfoss\daos\Items $itemsDao,
private Request $request,
private View $view
) {
}
/**
* mark items as read. Allows one id or an array of ids
* json
*
* @param ?string $itemId ID of item to mark as read
*/
public function mark(?string $itemId = null): void {
$this->authentication->ensureIsPrivileged();
$ids = null;
if ($itemId !== null) {
$ids = [$itemId];
} else {
$ids = $this->request->getData();
if (!is_array($ids)) {
$this->view->error('The request body needs to contain a list of numbers.');
}
// Legacy format $_POST['ids'].
if (isset($ids['ids'])) {
$ids = $ids['ids'];
}
}
// validate id or ids
try {
$ids = Misc::forceIds($ids);
} catch (InvalidArgumentException) {
$this->view->error('invalid id');
}
$this->itemsDao->mark($ids);
$return = [
'success' => true,
];
$this->view->jsonSuccess($return);
}
/**
* mark item as unread
* json
*
* @param string $itemId id of an item to mark as unread
*/
public function unmark(string $itemId): void {
$this->authentication->ensureIsPrivileged();
try {
$itemId = Misc::forceId($itemId);
} catch (InvalidArgumentException) {
$this->view->error('invalid id');
}
$this->itemsDao->unmark([(int) $itemId]);
$this->view->jsonSuccess([
'success' => true,
]);
}
/**
* starr item
* json
*
* @param string $itemId id of an item to starr
*/
public function starr(string $itemId): void {
$this->authentication->ensureIsPrivileged();
try {
$itemId = Misc::forceId($itemId);
} catch (InvalidArgumentException) {
$this->view->error('invalid id');
}
$this->itemsDao->starr($itemId);
$this->view->jsonSuccess([
'success' => true,
]);
}
/**
* unstarr item
* json
*
* @param string $itemId id of an item to unstarr
*/
public function unstarr(string $itemId): void {
$this->authentication->ensureIsPrivileged();
try {
$itemId = Misc::forceId($itemId);
} catch (InvalidArgumentException) {
$this->view->error('invalid id');
}
$this->itemsDao->unstarr($itemId);
$this->view->jsonSuccess([
'success' => true,
]);
}
/**
* returns items as json string
* json
*/
public function listItems(): never {
$this->authentication->ensureCanRead();
// parse params
$options = new ItemOptions($_GET);
// get items
$items = $this->itemsDao->get($options);
$items = array_map(function(array $item): array {
$stringifiedDates = [
'datetime' => $item['datetime']->format(DateTimeInterface::ATOM),
'updatetime' => $item['updatetime']->format(DateTimeInterface::ATOM),
];
return array_merge($item, $stringifiedDates);
}, $items);
$this->view->jsonSuccess($items);
}
}