1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ownCloud - News |
4
|
|
|
* |
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
6
|
|
|
* later. See the COPYING file. |
7
|
|
|
* |
8
|
|
|
* @author Alessandro Cosentino <[email protected]> |
9
|
|
|
* @author Bernhard Posselt <[email protected]> |
10
|
|
|
* @copyright Alessandro Cosentino 2012 |
11
|
|
|
* @copyright Bernhard Posselt 2012, 2014 |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace OCA\News\Controller; |
15
|
|
|
|
16
|
|
|
use \OCP\IRequest; |
17
|
|
|
use \OCP\IConfig; |
18
|
|
|
use \OCP\AppFramework\Controller; |
19
|
|
|
use \OCP\AppFramework\Http; |
20
|
|
|
|
21
|
|
|
use \OCA\News\Service\ServiceException; |
22
|
|
|
use \OCA\News\Service\ServiceNotFoundException; |
23
|
|
|
use \OCA\News\Service\ItemService; |
24
|
|
|
use \OCA\News\Service\FeedService; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
class ItemController extends Controller { |
28
|
|
|
|
29
|
|
|
use JSONHttpError; |
30
|
|
|
|
31
|
|
|
private $itemService; |
32
|
|
|
private $feedService; |
33
|
|
|
private $userId; |
34
|
|
|
private $settings; |
35
|
|
|
|
36
|
|
|
public function __construct($AppName, |
37
|
|
|
IRequest $request, |
38
|
|
|
FeedService $feedService, |
39
|
|
|
ItemService $itemService, |
40
|
|
|
IConfig $settings, |
41
|
|
|
$UserId){ |
42
|
|
|
parent::__construct($AppName, $request); |
43
|
|
|
$this->itemService = $itemService; |
44
|
|
|
$this->feedService = $feedService; |
45
|
|
|
$this->userId = $UserId; |
46
|
|
|
$this->settings = $settings; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @NoAdminRequired |
52
|
|
|
* |
53
|
|
|
* @param int $type |
54
|
|
|
* @param int $id |
55
|
|
|
* @param int $limit |
56
|
|
|
* @param int $offset |
57
|
|
|
* @param bool $showAll |
58
|
|
|
* @param bool $oldestFirst |
59
|
|
|
* @param string $search |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
|
public function index($type=3, $id=0, $limit=50, $offset=0, $showAll=null, |
63
|
|
|
$oldestFirst=null, $search='') { |
64
|
|
|
|
65
|
|
|
// in case this is called directly and not from the website use the |
66
|
|
|
// internal state |
67
|
|
|
if ($showAll === null) { |
68
|
|
|
$showAll = $this->settings->getUserValue( |
69
|
|
|
$this->userId, $this->appName,'showAll' |
70
|
|
|
) === '1'; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($oldestFirst === null) { |
74
|
|
|
$oldestFirst = $this->settings->getUserValue( |
75
|
|
|
$this->userId, $this->appName, 'oldestFirst' |
76
|
|
|
) === '1'; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$this->settings->setUserValue($this->userId, $this->appName, |
80
|
|
|
'lastViewedFeedId', $id); |
81
|
|
|
$this->settings->setUserValue($this->userId, $this->appName, |
82
|
|
|
'lastViewedFeedType', $type); |
83
|
|
|
|
84
|
|
|
$params = []; |
85
|
|
|
|
86
|
|
|
// split search parameter on url space |
87
|
|
|
$search = trim(urldecode($search)); |
88
|
|
|
$search = preg_replace('/\s+/', ' ', $search); // remove multiple ws |
89
|
|
|
if ($search === '') { |
90
|
|
|
$search = []; |
91
|
|
|
} else { |
92
|
|
|
$search = explode(' ', $search); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
try { |
96
|
|
|
|
97
|
|
|
// the offset is 0 if the user clicks on a new feed |
98
|
|
|
// we need to pass the newest feeds to not let the unread count get |
99
|
|
|
// out of sync |
100
|
|
|
if($offset === 0) { |
101
|
|
|
$params['newestItemId'] = |
102
|
|
|
$this->itemService->getNewestItemId($this->userId); |
103
|
|
|
$params['feeds'] = $this->feedService->findAll($this->userId); |
104
|
|
|
$params['starred'] = |
105
|
|
|
$this->itemService->starredCount($this->userId); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$params['items'] = $this->itemService->findAll( |
109
|
|
|
$id, $type, $limit, $offset, $showAll, $oldestFirst, |
110
|
|
|
$this->userId, $search |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
// this gets thrown if there are no items |
114
|
|
|
// in that case just return an empty array |
115
|
|
|
} catch(ServiceException $ex) {} |
116
|
|
|
|
117
|
|
|
return $params; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @NoAdminRequired |
123
|
|
|
* |
124
|
|
|
* @param int $type |
125
|
|
|
* @param int $id |
126
|
|
|
* @param int $lastModified |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
|
|
public function newItems($type, $id, $lastModified=0) { |
130
|
|
|
$showAll = $this->settings->getUserValue($this->userId, $this->appName, |
131
|
|
|
'showAll') === '1'; |
132
|
|
|
|
133
|
|
|
$params = []; |
134
|
|
|
|
135
|
|
|
try { |
136
|
|
|
$params['newestItemId'] = |
137
|
|
|
$this->itemService->getNewestItemId($this->userId); |
138
|
|
|
$params['feeds'] = $this->feedService->findAll($this->userId); |
139
|
|
|
$params['starred'] = |
140
|
|
|
$this->itemService->starredCount($this->userId); |
141
|
|
|
$params['items'] = $this->itemService->findAllNew($id, $type, |
142
|
|
|
$lastModified, $showAll, $this->userId); |
143
|
|
|
|
144
|
|
|
// this gets thrown if there are no items |
145
|
|
|
// in that case just return an empty array |
146
|
|
|
} catch(ServiceException $ex) {} |
147
|
|
|
|
148
|
|
|
return $params; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @NoAdminRequired |
154
|
|
|
* |
155
|
|
|
* @param int $feedId |
156
|
|
|
* @param string $guidHash |
157
|
|
|
* @param bool $isStarred |
158
|
|
|
* @return array|\OCP\AppFramework\Http\JSONResponse |
159
|
|
|
*/ |
160
|
|
|
public function star($feedId, $guidHash, $isStarred){ |
161
|
|
|
try { |
162
|
|
|
$this->itemService->star($feedId, $guidHash, $isStarred, |
163
|
|
|
$this->userId); |
164
|
|
|
} catch(ServiceException $ex) { |
165
|
|
|
return $this->error($ex, Http::STATUS_NOT_FOUND); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return []; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @NoAdminRequired |
174
|
|
|
* |
175
|
|
|
* @param int $itemId |
176
|
|
|
* @param bool $isRead |
177
|
|
|
* @return array|\OCP\AppFramework\Http\JSONResponse |
178
|
|
|
*/ |
179
|
|
|
public function read($itemId, $isRead=true){ |
180
|
|
|
try { |
181
|
|
|
$this->itemService->read($itemId, $isRead, $this->userId); |
182
|
|
|
} catch(ServiceException $ex) { |
183
|
|
|
return $this->error($ex, Http::STATUS_NOT_FOUND); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return []; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @NoAdminRequired |
192
|
|
|
* |
193
|
|
|
* @param int $highestItemId |
194
|
|
|
* @return array |
195
|
|
|
*/ |
196
|
|
|
public function readAll($highestItemId){ |
197
|
|
|
$this->itemService->readAll($highestItemId, $this->userId); |
198
|
|
|
return ['feeds' => $this->feedService->findAll($this->userId)]; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @NoAdminRequired |
204
|
|
|
* |
205
|
|
|
* @param int[] item ids |
206
|
|
|
*/ |
207
|
|
|
public function readMultiple($itemIds) { |
208
|
|
|
foreach($itemIds as $id) { |
209
|
|
|
try { |
210
|
|
|
$this->itemService->read($id, true, $this->userId); |
211
|
|
|
} catch(ServiceNotFoundException $ex) { |
212
|
|
|
continue; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
|
218
|
|
|
} |
219
|
|
|
|