Completed
Pull Request — master (#95)
by korelstar
05:25
created

NotesApiController::index()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 23
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.5906
c 0
b 0
f 0
cc 5
eloc 19
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 int $modified
131
     * @param boolean $favorite
132
     * @return DataResponse
133
     */
134
    public function create($content, $modified=0, $favorite=null) {
135
        return $this->respond(function () use ($content, $modified, $favorite) {
136
            $note = $this->service->create($this->userId);
137
            return $this->updateData($note->getId(), $content, $modified, $favorite);
138
        });
139
    }
140
141
142
    /**
143
     * @NoAdminRequired
144
     * @CORS
145
     * @NoCSRFRequired
146
     *
147
     * @param int $id
148
     * @param string $content
149
     * @param int $modified
150
     * @param boolean $favorite
151
     * @return DataResponse
152
     */
153
    public function update($id, $content=null, $modified=0, $favorite=null) {
154
        return $this->respond(function () use ($id, $content, $modified, $favorite) {
155
            return $this->updateData($id, $content, $modified, $favorite);
156
        });
157
    }
158
159
    /**
160
     * Updates a note, used by create and update
161
     * @param int $id
162
     * @param string $content
163
     * @param int $modified
164
     * @param boolean $favorite
165
     * @return Note
166
     */
167
    private function updateData($id, $content, $modified, $favorite) {
168
        if($favorite!==null) {
169
            $this->service->favorite($id, $favorite, $this->userId);
170
        }
171
        if($content===null) {
172
            return $this->service->get($id, $this->userId);
173
        } else {
174
            return $this->service->update($id, $content, $this->userId, $modified);
175
        }
176
    }
177
178
    /**
179
     * @NoAdminRequired
180
     * @CORS
181
     * @NoCSRFRequired
182
     *
183
     * @param int $id
184
     * @return DataResponse
185
     */
186
    public function destroy($id) {
187
        return $this->respond(function () use ($id) {
188
            $this->service->delete($id, $this->userId);
189
            return [];
190
        });
191
    }
192
193
194
}
195