summaryrefslogtreecommitdiffstats
path: root/controller/feedcontroller.php
blob: 2b95794a390239f17617b318bf460b1409697221 (plain)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
/**
 * ownCloud - News
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Alessandro Cosentino <cosenal@gmail.com>
 * @author Bernhard Posselt <dev@bernhard-posselt.com>
 * @copyright Alessandro Cosentino 2012
 * @copyright Bernhard Posselt 2012, 2014
 */

namespace OCA\News\Controller;

use OCP\IRequest;
use OCP\IConfig;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;

use OCA\News\Service\ItemService;
use OCA\News\Service\FeedService;
use OCA\News\Service\FolderService;
use OCA\News\Service\ServiceNotFoundException;
use OCA\News\Service\ServiceConflictException;
use OCA\News\Db\FeedType;


class FeedController extends Controller {

    use JSONHttpError;

    private $feedService;
    private $folderService;
    private $itemService;
    private $userId;
    private $settings;

    public function __construct($AppName,
                                IRequest $request,
                                FolderService $folderService,
                                FeedService $feedService,
                                ItemService $itemService,
                                IConfig $settings,
                                $UserId){
        parent::__construct($AppName, $request);
        $this->feedService = $feedService;
        $this->folderService = $folderService;
        $this->itemService = $itemService;
        $this->userId = $UserId;
        $this->settings = $settings;
    }


    /**
     * @NoAdminRequired
     */
    public function index(){

        // this method is also used to update the interface
        // because of this we also pass the starred count and the newest
        // item id which will be used for marking feeds read
        $params = [
            'feeds' => $this->feedService->findAll($this->userId),
            'starred' => $this->itemService->starredCount($this->userId)
        ];

        try {
            $params['newestItemId'] =
                $this->itemService->getNewestItemId($this->userId);

        // An exception occurs if there is a newest item. If there is none,
        // simply ignore it and do not add the newestItemId
        } catch (ServiceNotFoundException $ex) {}

        return $params;
    }


    /**
     * @NoAdminRequired
     */
    public function active(){
        $feedId = (int) $this->settings->getUserValue($this->userId,
            $this->appName,'lastViewedFeedId');
        $feedType = $this->settings->getUserValue($this->userId, $this->appName,
            'lastViewedFeedType');

        // cast from null to int is 0
        if($feedType !== null){
            $feedType = (int) $feedType;
        }

        // check if feed or folder exists
        try {
            if($feedType === FeedType::FOLDER){
                $this->folderService->find($feedId, $this->userId);

            } elseif ($feedType === FeedType::FEED){
                $this->feedService->find($feedId, $this->userId);

            // if its the first launch, those values will be null
            } elseif($feedType === null){
                throw new ServiceNotFoundException('');
            }

        } catch (ServiceNotFoundException $ex){
            $feedId = 0;
            $feedType = FeedType::SUBSCRIPTIONS;
        }

        return [
            'activeFeed' => [
                'id' => $feedId,
                'type' => $feedType
            ]
        ];
    }


    /**
     * @NoAdminRequired
     *
     * @param string $url
     * @param int $parentFolderId
     * @param string $title
     * @param string $user
     * @param string $password
     * @return array|\OCP\AppFramework\Http\JSONResponse
     */
    public function create($url, $parentFolderId, $title=null,
                           $user=null, $password=null){
        try {
            // we need to purge deleted feeds if a feed is created to
            // prevent already exists exceptions
            $this->feedService->purgeDeleted($this->userId, false);

            $feed = $this->feedService->create($url, $parentFolderId,
                                               $this->userId, $title,
                                               $user, $password);
            $params = ['feeds' => [$feed]];

            try {
                $params['newestItemId'] =
                    $this->itemService->getNewestItemId($this->userId);

            // An exception occurs if there is a newest item. If there is none,
            // simply ignore it and do not add the newestItemId
            } catch (ServiceNotFoundException $ex) {}

            return $params;

        } catch(ServiceConflictException $ex) {
            return $this->error($ex, Http::STATUS_CONFLICT);
        } catch(ServiceNotFoundException $ex) {
            return $this->error($ex, Http::STATUS_UNPROCESSABLE_ENTITY);
        }

    }


    /**
     * @NoAdminRequired
     *
     * @param int $feedId
     * @return array|\OCP\AppFramework\Http\JSONResponse
     */
    public function delete($feedId){
        try {
            $this->feedService->markDeleted($feedId, $this->userId);
        } catch(ServiceNotFoundException $ex) {
            return $this->error($ex, Http::STATUS_NOT_FOUND);
        }

        return [];
    }


    /**
     * @NoAdminRequired
     *
     * @param int $feedId
     * @return array|\OCP\AppFramework\Http\JSONResponse
     */
    public function update($feedId){
        try {
            $feed = $this->feedService->update($feedId, $this->userId);

            return [
                'feeds' => [
                    // only pass unread count to not accidentally readd
                    // the feed again
                    [
                        'id' => $feed->getId(),
                        'unreadCount' => $feed->getUnreadCount()
                    ]
                ]
            ];

        } catch(ServiceNotFoundException $ex) {
            return $this->error($ex, Http::STATUS_NOT_FOUND);
        }

    }


    /**
     * @NoAdminRequired
     *
     * @param array $json
     * @return array
     */
    public function import($json) {
        $feed = $this->feedService->importArticles($json, $this->userId);

        $params = [
            'starred' => $this->itemService->starredCount($this->userId)
        ];

        if($feed) {
            $params['feeds'] = [$feed];
        }

        return $params;
    }


    /**
     * @NoAdminRequired
     *
     * @param int $feedId
     * @param int $highestItemId
     * @return array
     */
    public function read($feedId, $highestItemId){
        $this->itemService->readFeed($feedId, $highestItemId, $this->userId);

        return [
            'feeds' => [
                [
                    'id' => $feedId,
                    'unreadCount' => 0
                ]
            ]
        ];
    }


    /**
     * @NoAdminRequired
     *
     * @param int $feedId
     * @return array|\OCP\AppFramework\Http\JSONResponse
     */
    public function restore($feedId){
        try {
            $this->feedService->unmarkDeleted($feedId, $this->userId);
        } catch(ServiceNotFoundException $ex) {
            return $this->error($ex, Http::STATUS_NOT_FOUND);
        }

        return [];
    }

    /**
     * @NoAdminRequired
     *
     * @param int $feedId
     * @param bool $pinned
     * @param bool $fullTextEnabled
     * @param int $updateMode
     * @param int $ordering
     * @param int $folderId
     * @param string $title
     */
    public function patch($feedId, $pinned=null, $fullTextEnabled=null,
                          $updateMode=null, $ordering=null, $title=null,
                          $folderId=null) {
        $attributes = [
            'pinned' => $pinned,
            'fullTextEnabled' => $fullTextEnabled,
            'updateMode' => $updateMode,
            'ordering' => $ordering,
            'title' => $title,
            'folderId' => $folderId
        ];

        $diff = array_filter($attributes, function ($value) {
            return $value !== null;
        });

        try {
            $this->feedService->patch($feedId, $this->userId, $diff);
        } catch(ServiceNotFoundException $ex) {
            return $this->error($ex, Http::STATUS_NOT_FOUND);
        }

        return [];
    }

}