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 Jan 9, 2025
1 parent 335832d commit 18fb5d3
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 9 deletions.
31 changes: 30 additions & 1 deletion lib/Controller/SnippetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public function __construct(
*/
#[TrapError]
public function getOwnSnippets(): JsonResponse {
if ($this->uid === null) {
return JsonResponse::error('User not found', Http::STATUS_UNAUTHORIZED);
}
$snippets = $this->snippetService->findAll($this->uid);

return JsonResponse::success($snippets);
Expand All @@ -51,7 +54,14 @@ public function getOwnSnippets(): JsonResponse {
*/
#[TrapError]
public function getSharedSnippets(): JsonResponse {
$snippets = $this->snippetService->findAllSharedWithMe($this->uid);
if ($this->uid === null) {
return JsonResponse::error('User not found', Http::STATUS_UNAUTHORIZED);
}
try {
$snippets = $this->snippetService->findAllSharedWithMe($this->uid);
} catch (DoesNotExistException $e) {
return JsonResponse::error('User not found', Http::STATUS_UNAUTHORIZED);
}

return JsonResponse::success($snippets);
}
Expand Down Expand Up @@ -84,6 +94,10 @@ public function create(string $title, string $content): JsonResponse {
#[TrapError]
public function update(int $id, string $title, string $content): JsonResponse {

if ($this->uid === null) {
return JsonResponse::error('User not found', Http::STATUS_UNAUTHORIZED);
}

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

if ($snippet === null) {
Expand All @@ -96,6 +110,9 @@ public function update(int $id, string $title, string $content): JsonResponse {
}

public function delete(int $id): JsonResponse {
if ($this->uid === null) {
return JsonResponse::error('User not found', Http::STATUS_UNAUTHORIZED);
}
try {
$this->snippetService->delete($id, $this->uid);
return JsonResponse::success();
Expand All @@ -114,6 +131,10 @@ public function delete(int $id): JsonResponse {
*/
#[TrapError]
public function share(int $snippetId, string $shareWith, string $type): JsonResponse {
if ($this->uid === null) {
return JsonResponse::error('User not found', Http::STATUS_UNAUTHORIZED);
}

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

if ($snippet === null) {
Expand All @@ -134,6 +155,10 @@ public function share(int $snippetId, string $shareWith, string $type): JsonResp
}

public function getShares(int $id): JsonResponse {
if ($this->uid === null) {
return JsonResponse::error('User not found', Http::STATUS_UNAUTHORIZED);
}

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

if ($snippet === null) {
Expand All @@ -154,6 +179,10 @@ public function getShares(int $id): JsonResponse {
*/
#[TrapError]
public function deleteShare(int $snippetId, string $shareWith): JsonResponse {
if ($this->uid === null) {
return JsonResponse::error('User not found', Http::STATUS_UNAUTHORIZED);
}

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

if ($snippet === null) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Db/SnippetShare.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class SnippetShare extends Entity implements JsonSerializable {
public function __construct() {
$this->addType('type', 'string');
$this->addType('shareWith', 'string');
$this->addType('snippetId', 'int');
$this->addType('snippetId', 'integer');
}

#[ReturnTypeWillChange]
Expand Down
3 changes: 1 addition & 2 deletions lib/Db/SnippetShareMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ public function findAllShares(string $owner): array {
}

/**
* @param string $owner
* @param string $snippetId
* @param int $snippetId
*
* @return SnippetShare[]
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/Migration/Version4001Date20241017154801.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
if ($schema->hasTable('mail_snippets')) {
return;
return null;
}
$table = $schema->createTable('mail_snippets');

Expand Down
2 changes: 1 addition & 1 deletion lib/Migration/Version4001Date20241017155914.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
if ($schema->hasTable('mail_snippets_shares')) {
return;
return null;
}
$table = $schema->createTable('mail_snippets_shares');

Expand Down
6 changes: 5 additions & 1 deletion lib/Service/SnippetService.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ public function findAll(string $userId): array {
/**
* @param string
* @return Snippet[]
* @throws DoesNotExistException
*/
public function findAllSharedWithMe(string $userId): array {
$user = $this->userManager->get($userId);
if ($user === null) {
throw new DoesNotExistException('User does not exist');
}
$groups = $this->groupManager->getUserGroupIds($user);
return $this->snippetMapper->findSharedWithMe($userId, $groups);
}
Expand Down Expand Up @@ -132,7 +136,7 @@ public function share(int $snippetId, string $shareWith): void {
$this->snippetShareMapper->insert($share);
}

public function getShares(string $uid, int $snippetId): array {
public function getShares(int $snippetId): array {
return $this->snippetShareMapper->findSnippetShares($snippetId);
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/Composer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@ export default {
},

snippets() {
return this.$store.getters.getSharedSnippets.map(snippet => ({ title: snippet.title, content: snippet.content }))
return this.$store.getters.getSharedSnippets?.map(snippet => ({ title: snippet.title, content: snippet.content }))
.concat(this.$store.getters.getMySnippets.map(snippet => ({ title: snippet.title, content: snippet.content })))
},
},
Expand Down
8 changes: 7 additions & 1 deletion tests/Unit/Controller/SnippetControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/


namespace OCA\Mail\Tests\Unit\Service;

use OCA\Mail\Db\Snippet;
Expand All @@ -16,7 +22,7 @@
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class SnippetServiceTest extends TestCase {
class SnippetControllerTest extends TestCase {
/** @var SnippetMapper|MockObject */
private $snippetMapper;

Expand Down
5 changes: 5 additions & 0 deletions tests/Unit/Service/SnippetServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/

namespace OCA\Mail\Tests\Unit\Service;

use OCA\Mail\Db\Snippet;
Expand Down

0 comments on commit 18fb5d3

Please sign in to comment.