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 <hamzamahjoubi221@gmail.com>
  • Loading branch information
hamza221 committed Oct 18, 2024
1 parent 457c827 commit a2f78cb
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 2 deletions.
35 changes: 35 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,41 @@
'url' => '/api/follow-up/check-message-ids',
'verb' => 'POST',
],
[
'name' => 'snippet#getOwnSnippets',
'url' => '/api/snippets',
'verb' => 'GET',
],
[
'name' => 'snippet#getSharedSnippets',
'url' => '/api/snippets/shared',
'verb' => 'GET',
],
[
'name' => 'snippet#create',
'url' => '/api/snippets',
'verb' => 'POST',
],
[
'name' => 'snippet#update',
'url' => '/api/snippets',
'verb' => 'PUT',
],
[
'name' => 'snippet#delete',
'url' => '/api/snippets',
'verb' => 'DELETE',
],
[
'name' => 'snippet#share',
'url' => '/api/snippets',
'verb' => 'POST',
],
[
'name' => 'snippet#deleteShare',
'url' => '/api/snippets/share',
'verb' => 'DELETE',
],
],
'resources' => [
'accounts' => ['url' => '/api/accounts'],
Expand Down
38 changes: 38 additions & 0 deletions lib/Controller/SnippetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use OCA\Mail\Http\TrapError;
use OCA\Mail\Service\SnippetService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\IRequest;
Expand Down Expand Up @@ -96,7 +97,24 @@ public function update(int $snippetId, string $title, string $content): JsonResp
return JsonResponse::success($snippet, Http::STATUS_OK);
}

public function delete($snippetId): JsonResponse {
try {
$this->snippetService->delete($snippetId, $this->uid);

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

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

PossiblyNullArgument

lib/Controller/SnippetController.php:102:46: PossiblyNullArgument: Argument 2 of OCA\Mail\Service\SnippetService::delete cannot be null, possibly null value provided (see https://psalm.dev/078)
return JsonResponse::success();
} catch (DoesNotExistException $e) {
return JsonResponse::fail('Snippet not found', Http::STATUS_NOT_FOUND);
}
}

/**
* @NoAdminRequired
* @param int $snippetId
* @param string $shareWith
* @param string $type
*
* @return JsonResponse
*/
#[TrapError]
public function share(int $snippetId, string $shareWith, string $type): JsonResponse {
$snippet = $this->snippetService->find($snippetId, $this->uid);

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

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

PossiblyNullArgument

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

Expand All @@ -117,4 +135,24 @@ public function share(int $snippetId, string $shareWith, string $type): JsonResp

}

/**
* @NoAdminRequired
* @param int $snippetId
* @param string $shareWith
*
* @return JsonResponse
*/
#[TrapError]
public function deleteShare(int $snippetId, string $shareWith): JsonResponse {
$snippet = $this->snippetService->find($snippetId, $this->uid);

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

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

PossiblyNullArgument

lib/Controller/SnippetController.php:147: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 149 in lib/Controller/SnippetController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

TypeDoesNotContainNull

lib/Controller/SnippetController.php:149: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->unshare($snippetId, $shareWith);

return JsonResponse::success();
}

}
22 changes: 20 additions & 2 deletions lib/Service/SnippetService.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ public function findAll(string $userId): array {

/**
* @param string
* @return Snippet[]
*/
public function findAllSharedWithMe(string $userId): array {

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

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

InvalidDocblock

lib/Service/SnippetService.php:54:2: InvalidDocblock: Badly-formatted @param in docblock for OCA\Mail\Service\SnippetService::findAllSharedWithMe (see https://psalm.dev/008)
$groups = $this->groupManager->getUserGroupIds($userId);
return $this->snippetShareMapper->findSharedWithMe($userId, $groups);
return $this->snippetMapper->findSharedWithMe($userId, $groups);
}
/**
* @param int $snippetId
Expand Down Expand Up @@ -103,7 +104,13 @@ public function delete(int $snippetId, string $userId): void {
$this->snippetMapper->delete($snippet);
}

//TODO: run owner check on controller level

/**
* @param int $snippetId
* @param string $shareWith
* @throws DoesNotExistException
* @throws NotPermittedException
*/
public function share(int $snippetId, string $shareWith): void {

$sharee = $this->userManager->get($shareWith);
Expand All @@ -120,6 +127,12 @@ public function share(int $snippetId, string $shareWith): void {
$this->snippetShareMapper->insert($share);
}

/**
* @param int $snippetId
* @param string $groupId
* @throws DoesNotExistException
* @throws NotPermittedException
*/
public function shareWithGroup(int $snippetId, string $groupId): void {
if (!$this->groupManager->groupExists($groupId)) {
throw new DoesNotExistException('Group does not exist');
Expand All @@ -134,6 +147,11 @@ public function shareWithGroup(int $snippetId, string $groupId): void {
$this->snippetShareMapper->insert($share);
}

/**
* @param int $snippetId
* @param string $shareWith
* @throws DoesNotExistException
*/
public function unshare(int $snippetId, string $shareWith): void {
$share = $this->snippetShareMapper->find($snippetId, $shareWith);
$this->snippetShareMapper->delete($share);
Expand Down

0 comments on commit a2f78cb

Please sign in to comment.