Completed
Push — master ( 8c2cb3...b136e5 )
by korelstar
02:38
created

NotesApiController::getUID()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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