Skip to content

Commit

Permalink
fixup! feat: mail snippets
Browse files Browse the repository at this point in the history
Signed-off-by: Hamza Mahjoubi <[email protected]>
  • Loading branch information
hamza221 committed Oct 18, 2024
1 parent 5a90354 commit 457c827
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 28 deletions.
120 changes: 120 additions & 0 deletions lib/Controller/SnippetController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Mail\Controller;

use OCA\Mail\AppInfo\Application;
use OCA\Mail\Db\SnippetShare;
use OCA\Mail\Http\JsonResponse;
use OCA\Mail\Http\TrapError;
use OCA\Mail\Service\SnippetService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\IRequest;

class SnippetController extends Controller {
private ?string $uid;

public function __construct(
IRequest $request,
?string $userId,
private SnippetService $snippetService,
) {
parent::__construct(Application::APP_ID, $request);

$this->snippetService = $snippetService;
$this->uid = $userId;
}

/**
* @NoAdminRequired
*
* @return JsonResponse
*/
#[TrapError]
public function getOwnSnippets(): JsonResponse {
$snippets = $this->snippetService->findAll($this->uid);

Check failure on line 43 in lib/Controller/SnippetController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

PossiblyNullArgument

lib/Controller/SnippetController.php:43:46: PossiblyNullArgument: Argument 1 of OCA\Mail\Service\SnippetService::findAll cannot be null, possibly null value provided (see https://psalm.dev/078)

return JsonResponse::success($snippets);
}

/**
* @NoAdminRequired
*
* @return JsonResponse
*/
#[TrapError]
public function getSharedSnippets(): JsonResponse {
$snippets = $this->snippetService->findAllSharedWithMe($this->uid);

Check failure on line 55 in lib/Controller/SnippetController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

PossiblyNullArgument

lib/Controller/SnippetController.php:55:58: PossiblyNullArgument: Argument 1 of OCA\Mail\Service\SnippetService::findAllSharedWithMe cannot be null, possibly null value provided (see https://psalm.dev/078)

return JsonResponse::success($snippets);
}

/**
* @NoAdminRequired
* @param string $title
* @param string $content
*
* @return JsonResponse
*/
#[TrapError]
public function create(string $title, string $content): JsonResponse {
if ($this->uid === null) {
return JsonResponse::error('User not found', Http::STATUS_UNAUTHORIZED);
}
$snippet = $this->snippetService->create($this->uid, $title, $content);

return JsonResponse::success($snippet, Http::STATUS_CREATED);
}

/**
* @NoAdminRequired
* @param int $snippetId
* @param string $title
* @param string $content
*
* @return JsonResponse
*/
#[TrapError]
public function update(int $snippetId, string $title, string $content): JsonResponse {

$snippet = $this->snippetService->find($snippetId, $this->uid);

Check failure on line 88 in lib/Controller/SnippetController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

PossiblyNullArgument

lib/Controller/SnippetController.php:88:54: PossiblyNullArgument: Argument 2 of OCA\Mail\Service\SnippetService::find cannot be null, possibly null value provided (see https://psalm.dev/078)

if ($snippet === null) {

Check failure on line 90 in lib/Controller/SnippetController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

TypeDoesNotContainNull

lib/Controller/SnippetController.php:90:7: TypeDoesNotContainNull: OCA\Mail\Db\Snippet does not contain null (see https://psalm.dev/090)
return JsonResponse::error('Snippet not found', Http::STATUS_NOT_FOUND);
}

$this->snippetService->update($snippetId, $this->uid, $title, $content);

return JsonResponse::success($snippet, Http::STATUS_OK);
}


public function share(int $snippetId, string $shareWith, string $type): JsonResponse {
$snippet = $this->snippetService->find($snippetId, $this->uid);

Check failure on line 101 in lib/Controller/SnippetController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

PossiblyNullArgument

lib/Controller/SnippetController.php:101:54: PossiblyNullArgument: Argument 2 of OCA\Mail\Service\SnippetService::find cannot be null, possibly null value provided (see https://psalm.dev/078)

if ($snippet === null) {

Check failure on line 103 in lib/Controller/SnippetController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

TypeDoesNotContainNull

lib/Controller/SnippetController.php:103:7: TypeDoesNotContainNull: OCA\Mail\Db\Snippet does not contain null (see https://psalm.dev/090)
return JsonResponse::error('Snippet not found', Http::STATUS_NOT_FOUND);
}

switch ($type) {
case SnippetShare::TYPE_USER:
$this->snippetService->share($snippetId, $shareWith);
return JsonResponse::success();
case SnippetShare::TYPE_GROUP:
$this->snippetService->shareWithGroup($snippetId, $shareWith);
return JsonResponse::success();
default:
return JsonResponse::fail('Invalid share type', Http::STATUS_BAD_REQUEST);
}

}

}
2 changes: 1 addition & 1 deletion lib/Db/Snippet.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

Expand Down
2 changes: 1 addition & 1 deletion lib/Db/SnippetMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

Expand Down
2 changes: 1 addition & 1 deletion lib/Db/SnippetShare.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

Expand Down
4 changes: 2 additions & 2 deletions lib/Db/SnippetShareMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

Expand Down Expand Up @@ -42,7 +42,7 @@ public function find(int $id, string $owner): SnippetShare {
return $this->findEntity($qb);
}

public function shareExists(string $snippetId, string $shareWith): bool {
public function shareExists(int $snippetId, string $shareWith): bool {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
Expand Down
37 changes: 14 additions & 23 deletions lib/Service/SnippetService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Mail\Service;
Expand Down Expand Up @@ -52,8 +51,8 @@ public function findAll(string $userId): array {
* @param string
*/
public function findAllSharedWithMe(string $userId): array {

Check failure on line 53 in lib/Service/SnippetService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

InvalidDocblock

lib/Service/SnippetService.php:53:2: InvalidDocblock: Badly-formatted @param in docblock for OCA\Mail\Service\SnippetService::findAllSharedWithMe (see https://psalm.dev/008)
// TODO: add groupshares
return $this->snippetShareMapper->findAllSharedWith($userId);
$groups = $this->groupManager->getUserGroupIds($userId);

Check failure on line 54 in lib/Service/SnippetService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

InvalidArgument

lib/Service/SnippetService.php:54:50: InvalidArgument: Argument 1 of OCP\IGroupManager::getUserGroupIds expects OCP\IUser, but string provided (see https://psalm.dev/004)
return $this->snippetShareMapper->findSharedWithMe($userId, $groups);

Check failure on line 55 in lib/Service/SnippetService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

UndefinedMethod

lib/Service/SnippetService.php:55:37: UndefinedMethod: Method OCA\Mail\Db\SnippetShareMapper::findSharedWithMe does not exist (see https://psalm.dev/022)
}
/**
* @param int $snippetId
Expand Down Expand Up @@ -103,17 +102,14 @@ public function delete(int $snippetId, string $userId): void {
$snippet = $this->snippetMapper->find($snippetId, $userId);
$this->snippetMapper->delete($snippet);
}

//TODO: run owner check on controller level
public function share(int $snippetId, string $userId, string $shareWith): void {
public function share(int $snippetId, string $shareWith): void {

$sharee = $this->userManager->get($shareWith);
if ($sharee === null) {
throw new DoesNotExistException('Sharee does not exist');
}
$snippet = $this->snippetMapper->find($snippetId, $userId);
if ($snippet === null) {
throw new DoesNotExistException('Snippet does not exist');
}
if ($this->snippetShareMapper->shareExists($snippetId, $shareWith)) {
throw new NotPermittedException('Share already exists');
}
Expand All @@ -124,29 +120,24 @@ public function share(int $snippetId, string $userId, string $shareWith): void {
$this->snippetShareMapper->insert($share);
}

//TODO: run owner check on controller level

public function unshare(int $snippetId, string $shareWith): void {
$share = $this->snippetShareMapper->find($snippetId, $shareWith);
$this->snippetShareMapper->delete($share);
}

//TODO: run owner check on controller level
public function shareWithGroup(int $snippetId, string $userId, string $groupId): void {
$snippet = $this->snippetMapper->find($snippetId, $userId);
if ($snippet === null) {
throw new DoesNotExistException('Snippet does not exist');
}
public function shareWithGroup(int $snippetId, string $groupId): void {
if (!$this->groupManager->groupExists($groupId)) {
throw new DoesNotExistException('Group does not exist');
}
if ($this->snippetShareMapper->shareExists($snippetId, $groupId)) {
throw new NotPermittedException('Share already exists');
}
$share = new SnippetShare();
$share->setShareWith($groupId);
$share->setSnippetId($snippetId);
$share->setType(SnippetShare::TYPE_GROUP);
$this->snippetShareMapper->insert($share);
}

public function unshare(int $snippetId, string $shareWith): void {
$share = $this->snippetShareMapper->find($snippetId, $shareWith);
$this->snippetShareMapper->delete($share);
}


}

0 comments on commit 457c827

Please sign in to comment.