Skip to content

Commit bbcc201

Browse files
committed
noteapicontroller: migrated use of Nextcloud API to Nextcloud 29+ standards
Signed-off-by: Patrizio Bekerle <[email protected]>
1 parent 0ebfdc2 commit bbcc201

File tree

3 files changed

+48
-24
lines changed

3 files changed

+48
-24
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
## Next
44

55
- Migrated to from PHP7 to PHP8 annotations (for [#53](https://github.com/pbek/qownnotesapi/issues/53))
6+
- Migrated the use of the Nextcloud API to Nextcloud 29+ standards
7+
- Increased min-version to Nextcloud 29
68

79
## 25.8.0
810

appinfo/info.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ The only purpose of this App is to provide API access to your Nextcloud/ownCloud
3030
<!-- dependencies -->
3131
<dependencies>
3232
<owncloud min-version="8.2" max-version="10" />
33-
<nextcloud min-version="22" max-version="32" />
33+
<nextcloud min-version="29" max-version="32" />
3434
</dependencies>
3535
</info>

lib/Controller/NoteApiController.php

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,46 @@
2121
use OCA\Files_Trashbin\Helper;
2222
use OCA\Files_Trashbin\Trashbin;
2323
use OCA\Files_Versions\Storage;
24+
use OCP\App\IAppManager;
2425
use OCP\AppFramework\ApiController;
26+
use OCP\AppFramework\Http\Attribute\CORS;
27+
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
28+
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
2529
use OCP\Files\NotFoundException;
30+
use OCP\Files\NotPermittedException;
31+
use OCP\IAppConfig;
32+
use OCP\IConfig;
2633
use OCP\IRequest;
2734
use OCP\IUserManager;
35+
use OCP\Lock\LockedException;
2836

2937
class NoteApiController extends ApiController {
3038
protected $user;
39+
private IUserManager $userManager;
40+
private IAppManager $appManager;
41+
private IConfig $config;
42+
private IAppConfig $appConfig;
3143

3244
/**
3345
* @param string $appName
3446
* @param string $userId
3547
*/
36-
public function __construct($appName,
48+
public function __construct(
49+
$appName,
3750
$userId,
38-
IRequest $request) {
51+
IRequest $request,
52+
IUserManager $userManager,
53+
IAppManager $appManager,
54+
IConfig $config,
55+
IAppConfig $appConfig
56+
) {
3957
// For some reason $userId is null on ownCloud 10.3+ anymore
4058
// https://github.com/pbek/QOwnNotes/issues/1725
4159
$this->user = $userId ? $userId : $_SERVER['PHP_AUTH_USER'];
60+
$this->userManager = $userManager;
61+
$this->appManager = $appManager;
62+
$this->config = $config;
63+
$this->appConfig = $appConfig;
4264
parent::__construct($appName, $request);
4365
}
4466

@@ -47,8 +69,8 @@ public function __construct($appName,
4769
*
4870
* @return array
4971
*
50-
* @throws \OCP\Lock\LockedException
51-
* @throws \OC\User\NoUserException
72+
* @throws LockedException
73+
* @throws NoUserException
5274
*/
5375
#[NoAdminRequired]
5476
#[NoCSRFRequired]
@@ -94,7 +116,7 @@ public function getAllVersions() {
94116
];
95117
}
96118
}
97-
} catch (\OCP\Files\NotFoundException $exception) {
119+
} catch (NotFoundException $exception) {
98120
// Requested file was not found, silently fail (for now)
99121
$errorMessages[] = 'Requested file was not found!';
100122
} catch (Exception $exception) {
@@ -117,13 +139,12 @@ public function getAllVersions() {
117139
* @return array
118140
* @throws NoUserException
119141
*/
120-
protected static function getUidAndFilename($filename) {
142+
protected function getUidAndFilename($filename) {
121143
$uid = Filesystem::getOwner($filename);
122-
$userManager = \OC::$server->get(IUserManager::class);
123-
// if the user with the UID doesn't exists, e.g. because the UID points
144+
// if the user with the UID doesn't exist, e.g. because the UID points
124145
// to a remote user with a federated cloud ID we use the current logged-in
125146
// user. We need a valid local user to create the versions
126-
if (!$userManager->userExists($uid)) {
147+
if (!$this->userManager->userExists($uid)) {
127148
$uid = OC_User::getUser();
128149
}
129150
Filesystem::initMountPoints($uid);
@@ -153,16 +174,15 @@ protected static function getUidAndFilename($filename) {
153174
#[NoCSRFRequired]
154175
#[CORS]
155176
public function getAppInfo() {
156-
$appManager = \OC::$server->getAppManager();
157-
$versionsAppEnabled = $appManager->isEnabledForUser('files_versions');
158-
$trashAppEnabled = $appManager->isEnabledForUser('files_trashbin');
177+
$versionsAppEnabled = $this->appManager->isEnabledForUser('files_versions');
178+
$trashAppEnabled = $this->appManager->isEnabledForUser('files_trashbin');
159179
$notesPathExists = false;
160180
$notesPath = $this->request->getParam('notes_path', '');
161181

162182
// check if notes path exists
163183
if ($notesPath !== '') {
164184
$notesPath = '/files'.(string) $notesPath;
165-
$view = new \OC\Files\View('/'.$this->user);
185+
$view = new View('/'.$this->user);
166186
$notesPathExists = $view->is_dir($notesPath);
167187
}
168188

@@ -171,8 +191,8 @@ public function getAppInfo() {
171191
'versions_app' => $versionsAppEnabled,
172192
'trash_app' => $trashAppEnabled,
173193
'versioning' => true,
174-
'app_version' => \OC::$server->getConfig()->getAppValue('qownnotesapi', 'installed_version'),
175-
'server_version' => \OC::$server->getSystemConfig()->getValue('version'),
194+
'app_version' => $this->appConfig->getValueString('qownnotesapi', 'installed_version', ''),
195+
'server_version' => $this->config->getSystemValue('version'),
176196
'notes_path_exists' => $notesPathExists,
177197
];
178198
}
@@ -182,7 +202,7 @@ public function getAppInfo() {
182202
*
183203
* @return string|array
184204
*
185-
* @throws \OCP\Lock\LockedException
205+
* @throws LockedException
186206
*/
187207
#[NoAdminRequired]
188208
#[NoCSRFRequired]
@@ -198,18 +218,18 @@ public function getTrashedNotes() {
198218
$noteFileExtensions = array_merge(['md', 'txt'], $customFileExtensions);
199219

200220
// remove leading "/"
201-
if (substr($dir, 0, 1) === '/') {
221+
if (str_starts_with($dir, '/')) {
202222
$dir = substr($dir, 1);
203223
}
204224

205225
// remove trailing "/"
206-
if (substr($dir, -1) === '/') {
226+
if (str_ends_with($dir, '/')) {
207227
$dir = substr($dir, 0, -1);
208228
}
209229

210230
$sortAttribute = $this->request->getParam('sort', 'mtime');
211231
$sortDirectionParam = $this->request->getParam('sortdirection', '');
212-
$sortDirection = ($sortDirectionParam !== '') ? ($sortDirectionParam === 'desc') : true;
232+
$sortDirection = !($sortDirectionParam !== '') || $sortDirectionParam === 'desc';
213233
$filesInfo = [];
214234

215235
// generate the file list
@@ -226,15 +246,17 @@ public function getTrashedNotes() {
226246
$extension = $pathParts['extension'] ?? '';
227247

228248
// if $fileInfo["extraData"] is not set we will have to show the note files from all folders in QOwnNotes
229-
$isInDir = isset($fileInfo['extraData']) ?
230-
(strpos($fileInfo['extraData'], $dir.'/'.$fileInfo['name']) === 0) : true;
249+
$isInDir = !isset($fileInfo['extraData']) || str_starts_with(
250+
$fileInfo['extraData'],
251+
$dir.'/'.$fileInfo['name']
252+
);
231253
$isNoteFile = in_array($extension, $noteFileExtensions, true);
232254

233255
if ($isInDir && $isNoteFile) {
234256
$timestamp = (int) ($fileInfo['mtime'] / 1000);
235257
$fileName = '/files_trashbin/files/'.$fileInfo['name'].".d$timestamp";
236258

237-
$view = new \OC\Files\View('/'.$this->user);
259+
$view = new View('/'.$this->user);
238260
$data = '';
239261

240262
// load the file data
@@ -272,7 +294,7 @@ public function getTrashedNotes() {
272294
*
273295
* @return string|array
274296
*
275-
* @throws \OCP\Files\NotPermittedException
297+
* @throws NotPermittedException
276298
*
277299
* @see owncloud/core/apps/files_trashbin/ajax/undelete.php
278300
*/

0 commit comments

Comments
 (0)