Completed
Pull Request — master (#107)
by Sander
01:31
created

NoteApiController::formatApiResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 2
eloc 10
nc 2
nop 1
1
<?php
2
/**
3
 * Nextcloud - NextNote
4
 *
5
 *
6
 * @copyright Copyright (c) 2017, Sander Brand ([email protected])
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\NextNote\Controller;
25
26
use OCA\NextNote\Db\Notebook;
27
use OCA\NextNote\Db\Note;
28
use OCA\NextNote\Service\NotebookService;
29
use OCA\NextNote\Service\NoteService;
30
use OCA\NextNote\Utility\NotFoundJSONResponse;
31
use OCA\NextNote\Utility\Utils;
32
use \OCP\AppFramework\ApiController;
33
use OCP\AppFramework\Http\JSONResponse;
34
use OCP\Constants;
35
use OCP\IConfig;
36
use OCP\ILogger;
37
use \OCP\IRequest;
38
use OCP\IUserManager;
39
40
41
42
class NoteApiController extends ApiController {
43
44
	private $config;
45
	private $noteService;
46
	private $shareBackend;
0 ignored issues
show
Unused Code introduced by
The property $shareBackend is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
47
	private $userManager;
48
	private $shareManager;
0 ignored issues
show
Unused Code introduced by
The property $shareManager is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
49
	private $notebookService;
50
51 View Code Duplication
	public function __construct($appName, IRequest $request,
52
								ILogger $logger, IConfig $config, NoteService $noteService, NotebookService $groupService,IUserManager $userManager) {
53
		parent::__construct($appName, $request);
54
		$this->config = $config;
55
		$this->noteService = $noteService;
56
		$this->notebookService = $groupService;
57
		$this->userManager = $userManager;
58
	}
59
60
	/**
61
	 * @NoAdminRequired
62
	 * @NoCSRFRequired
63
	 * @TODO Add etag / lastmodified
64
	 * @param int|bool $deleted
65
	 * @param string|bool $notebook_id
66
	 * @return JSONResponse
67
	 */
68
	public function index($deleted = false, $notebook_id = false) {
69
		$uid = \OC::$server->getUserSession()->getUser()->getUID();
70
71
		if(!empty($notebook_id)){
72
			$notebook_id = $this->notebookService->find($notebook_id)->getId();
0 ignored issues
show
Documentation introduced by
$notebook_id is of type string|boolean, but the function expects a null|integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
73
		}
74
		$result = $this->noteService->findNotesFromUser($uid, $deleted, $notebook_id);
75
		foreach ($result as &$note) {
76
			if (is_array($note)) {
77
				$note = $this->noteService->find($note['id']);
78
			}
79
			$note = $note->jsonSerialize();
80
			$note = $this->formatApiResponse($note);
81
82
		}
83
84
		$results = $result;
85
		if($results instanceof Note){
86
			$results = [];
87
			/**
88
			 * @var $result Note
89
			 */
90
			$results[$result->getId()] = $result;
91
		}
92
		return new JSONResponse($results);
93
	}
94
95
	/**
96
	 * @NoAdminRequired
97
	 * @NoCSRFRequired
98
	 * @TODO Add etag / lastmodified
99
	 */
100
	public function get($id) {
101
		$result = $this->noteService->find($id);
102
		if (!$result) {
103
			return new NotFoundJSONResponse();
104
		}
105
		//@todo Check access
106
		$result = $result->jsonSerialize();
107
		return new JSONResponse($this->formatApiResponse($result));
108
	}
109
110
111
	/**
112
	 * @NoAdminRequired
113
	 * @NoCSRFRequired
114
	 */
115
	public function create($title, $notebook_id, $content) {
116
		if ($title == "" || !$title) {
117
			return new JSONResponse(['error' => 'title is missing']);
118
		}
119
120
		$uid = \OC::$server->getUserSession()->getUser()->getUID();
121
		$note = new Note();
122
		$note->setName($title);
123
		$note->setUid($uid);
124
		$note->setGuid(Utils::GUID());
125
		$note->setNote($content);
126
		$note->setMtime(time());
127
		$note->setDeleted(0);
128
129 View Code Duplication
		if(!empty($notebook_id)){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
			$notebook = $this->notebookService->find($notebook_id);
131
			if($notebook instanceof Notebook) {
132
				$note->setNotebook($notebook->getId());
133
			} else {
134
				return new JSONResponse(['error' => 'Notebook not found']);
135
			}
136
		}
137
138
		$result = $this->noteService->create($note)->jsonSerialize();
139
		\OC_Hook::emit('OCA\NextNote', 'post_create_note', ['note' => $note]);
140
		return new JSONResponse($this->formatApiResponse($result));
141
	}
142
143
	/**
144
	 * @NoAdminRequired
145
	 * @NoCSRFRequired
146
	 */
147
	public function update($id, $title, $content, $deleted, $notebook_id) {
148
		if ($title == "" || !$title) {
149
			return new JSONResponse(['error' => 'title is missing']);
150
		}
151
152
		$note = $this->noteService->find($id);
153
		if (!$note) {
154
			return new NotFoundJSONResponse();
155
		}
156
157
		if(!$note->getGuid()){
158
			$note->setGuid(Utils::GUID());
159
		}
160
161
162 View Code Duplication
		if(!empty($notebook_id)){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
163
			$notebook = $this->notebookService->find($notebook_id);
164
			if($notebook instanceof Notebook) {
165
				$note->setNotebook($notebook->getId());
166
			} else {
167
				return new JSONResponse(['error' => 'Notebook not found']);
168
			}
169
		}
170
		$note->setName($title);
171
		$note->setNote($content);
172
		$note->setDeleted($deleted);
173
174
		$results = $this->noteService->update($note)->jsonSerialize();
175
		\OC_Hook::emit('OCA\NextNote', 'post_update_note', ['note' => $note]);
176
		return new JSONResponse($this->formatApiResponse($results));
177
	}
178
179
	/**
180
	 * @NoAdminRequired
181
	 * @NoCSRFRequired
182
	 */
183 View Code Duplication
	public function delete($id) {
184
		$entity = $this->noteService->find($id);
185
		if (!$entity) {
186
			return new NotFoundJSONResponse();
187
		}
188
189
		$this->noteService->delete($id);
190
		$result = (object)['success' => true];
191
		\OC_Hook::emit('OCA\NextNote', 'post_delete_note', ['note_id' => $id]);
192
		return new JSONResponse($result);
193
	}
194
195
	/**
196
	 * @param $note array
197
	 * @return array
198
	 */
199
	private function formatApiResponse($note) {
200
		$uid = \OC::$server->getUserSession()->getUser()->getUID();
201
		$acl = [
202
			'permissions' => Constants::PERMISSION_ALL
203
		];
204
205
		$note['owner'] = Utils::getUserInfo($note['uid']);
206
		$note['permissions'] = $acl['permissions'];
207
208
		$shared_with = [];
209
210
		$note['shared_with'] = ($note['uid'] == $uid) ? $shared_with : [$uid];
211
		unset($note['uid']);
212
		return $note;
213
	}
214
}
215