Completed
Pull Request — master (#210)
by korelstar
35:08
created

NotesApiController::index()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.2408
c 0
b 0
f 0
cc 5
nc 6
nop 2
1
<?php
2
/**
3
 * Nextcloud - Notes
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Bernhard Posselt <[email protected]>
9
 * @copyright Bernhard Posselt 2012, 2014
10
 */
11
12
namespace OCA\Notes\Controller;
13
14
use OCP\AppFramework\ApiController;
15
use OCP\AppFramework\Http;
16
use OCP\AppFramework\Http\DataResponse;
17
use OCP\IRequest;
18
19
use OCA\Notes\Service\NotesService;
20
use OCA\Notes\Service\MetaService;
21
use OCA\Notes\Db\Note;
22
23
/**
24
 * Class NotesApiController
25
 *
26
 * @package OCA\Notes\Controller
27
 */
28
class NotesApiController extends ApiController {
29
30
    use Errors;
31
32
    /** @var NotesService */
33
    private $service;
34
    /** @var MetaService */
35
    private $metaService;
36
    /** @var string */
37
    private $userId;
38
39
    /**
40
     * @param string $AppName
41
     * @param IRequest $request
42
     * @param NotesService $service
43
     * @param string $UserId
44
     */
45
    public function __construct($AppName, IRequest $request, NotesService $service, MetaService $metaService, $UserId) {
46
        parent::__construct($AppName, $request);
47
        $this->service = $service;
48
        $this->metaService = $metaService;
49
        $this->userId = $UserId;
50
    }
51
52
53
    /**
54
     * @param Note $note
55
     * @param string[] $exclude the fields that should be removed from the
56
     * notes
57
     * @return Note
58
     */
59
    private function excludeFields(Note &$note, array $exclude) {
60
        if(count($exclude) > 0) {
61
            foreach ($exclude as $field) {
62
                if(property_exists($note, $field)) {
63
                    unset($note->$field);
64
                }
65
            }
66
        }
67
        return $note;
68
    }
69
70
71
    /**
72
     * @NoAdminRequired
73
     * @CORS
74
     * @NoCSRFRequired
75
     *
76
     * @param string $exclude
77
     * @return DataResponse
78
     */
79
    public function index($exclude='', $pruneBefore=0) {
80
        $exclude = explode(',', $exclude);
81
        $now = new \DateTime(); // this must be before loading notes if there are concurrent changes possible
82
        $notes = $this->service->getAll($this->userId);
83
        $metas = $this->metaService->updateAll($this->userId, $notes);
84
        foreach ($notes as $note) {
85
            $lastUpdate = $metas[$note->getId()]->getLastUpdate();
86
            if($pruneBefore && $lastUpdate<$pruneBefore) {
87
                $vars = get_object_vars($note);
88
                unset($vars['id']);
89
                $this->excludeFields($note, array_keys($vars));
90
            } else {
91
                $this->excludeFields($note, $exclude);
92
            }
93
        }
94
        $etag = md5(json_encode($notes));
95
        if ($this->request->getHeader('If-None-Match') === '"'.$etag.'"') {
96
            return new DataResponse([], Http::STATUS_NOT_MODIFIED);
97
        }
98
        return (new DataResponse($notes))
99
            ->setLastModified($now)
100
            ->setETag($etag);
101
    }
102
103
104
    /**
105
     * @NoAdminRequired
106
     * @CORS
107
     * @NoCSRFRequired
108
     *
109
     * @param int $id
110
     * @param string $exclude
111
     * @return DataResponse
112
     */
113
    public function get($id, $exclude='') {
114
        $exclude = explode(',', $exclude);
115
116
        return $this->respond(function () use ($id, $exclude) {
117
            $note = $this->service->get($id, $this->userId);
118
            $note = $this->excludeFields($note, $exclude);
119
            return $note;
120
        });
121
    }
122
123
124
    /**
125
     * @NoAdminRequired
126
     * @CORS
127
     * @NoCSRFRequired
128
     *
129
     * @param string $content
130
     * @param string $category
131
     * @param int $modified
132
     * @param boolean $favorite
133
     * @return DataResponse
134
     */
135
    public function create($content, $category=null, $modified=0, $favorite=null) {
136
        return $this->respond(function () use ($content, $category, $modified, $favorite) {
137
            $note = $this->service->create($this->userId);
138
            return $this->updateData($note->getId(), $content, $category, $modified, $favorite);
139
        });
140
    }
141
142
143
    /**
144
     * @NoAdminRequired
145
     * @CORS
146
     * @NoCSRFRequired
147
     *
148
     * @param int $id
149
     * @param string $content
150
     * @param string $category
151
     * @param int $modified
152
     * @param boolean $favorite
153
     * @return DataResponse
154
     */
155
    public function update($id, $content=null, $category=null, $modified=0, $favorite=null) {
156
        return $this->respond(function () use ($id, $content, $category, $modified, $favorite) {
157
            return $this->updateData($id, $content, $category, $modified, $favorite);
158
        });
159
    }
160
161
    /**
162
     * Updates a note, used by create and update
163
     * @param int $id
164
     * @param string $content
165
     * @param int $modified
166
     * @param boolean $favorite
167
     * @return Note
168
     */
169
    private function updateData($id, $content, $category, $modified, $favorite) {
170
        if($favorite!==null) {
171
            $this->service->favorite($id, $favorite, $this->userId);
172
        }
173
        if($content===null) {
174
            return $this->service->get($id, $this->userId);
175
        } else {
176
            return $this->service->update($id, $content, $this->userId, $category, $modified);
177
        }
178
    }
179
180
    /**
181
     * @NoAdminRequired
182
     * @CORS
183
     * @NoCSRFRequired
184
     *
185
     * @param int $id
186
     * @return DataResponse
187
     */
188
    public function destroy($id) {
189
        return $this->respond(function () use ($id) {
190
            $this->service->delete($id, $this->userId);
191
            return [];
192
        });
193
    }
194
195
196
}
197