-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
Signed-off-by: Hamza Mahjoubi <[email protected]>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
<?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\Db\DoesNotExistException; | ||
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->uid = $userId; | ||
} | ||
|
||
/** | ||
* @NoAdminRequired | ||
* | ||
* @return JsonResponse | ||
*/ | ||
#[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); | ||
} | ||
|
||
/** | ||
* @NoAdminRequired | ||
* | ||
* @return JsonResponse | ||
*/ | ||
#[TrapError] | ||
public function getSharedSnippets(): JsonResponse { | ||
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); | ||
} | ||
|
||
/** | ||
* @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 $id | ||
* @param string $title | ||
* @param string $content | ||
* | ||
* @return 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) { | ||
Check failure on line 103 in lib/Controller/SnippetController.php GitHub Actions / static-psalm-analysis dev-masterTypeDoesNotContainNull
|
||
return JsonResponse::error('Snippet not found', Http::STATUS_NOT_FOUND); | ||
} | ||
|
||
$this->snippetService->update($id, $this->uid, $title, $content); | ||
|
||
return JsonResponse::success($snippet, Http::STATUS_OK); | ||
} | ||
|
||
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(); | ||
} 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 { | ||
if ($this->uid === null) { | ||
return JsonResponse::error('User not found', Http::STATUS_UNAUTHORIZED); | ||
} | ||
|
||
$snippet = $this->snippetService->find($snippetId, $this->uid); | ||
|
||
if ($snippet === null) { | ||
Check failure on line 140 in lib/Controller/SnippetController.php GitHub Actions / static-psalm-analysis dev-masterTypeDoesNotContainNull
|
||
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); | ||
} | ||
|
||
} | ||
|
||
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) { | ||
Check failure on line 164 in lib/Controller/SnippetController.php GitHub Actions / static-psalm-analysis dev-masterTypeDoesNotContainNull
|
||
return JsonResponse::error('Snippet not found', Http::STATUS_NOT_FOUND); | ||
} | ||
|
||
$shares = $this->snippetService->getShares($this->uid, $id); | ||
Check failure on line 168 in lib/Controller/SnippetController.php GitHub Actions / static-psalm-analysis dev-masterTooManyArguments
Check failure on line 168 in lib/Controller/SnippetController.php GitHub Actions / static-psalm-analysis dev-masterInvalidArgument
|
||
|
||
return JsonResponse::success($shares); | ||
} | ||
|
||
/** | ||
* @NoAdminRequired | ||
* @param int $snippetId | ||
* @param string $shareWith | ||
* | ||
* @return 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) { | ||
Check failure on line 188 in lib/Controller/SnippetController.php GitHub Actions / static-psalm-analysis dev-masterTypeDoesNotContainNull
|
||
return JsonResponse::error('Snippet not found', Http::STATUS_NOT_FOUND); | ||
} | ||
|
||
$this->snippetService->unshare($snippetId, $shareWith); | ||
|
||
return JsonResponse::success(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Mail\Db; | ||
|
||
use JsonSerializable; | ||
use OCP\AppFramework\Db\Entity; | ||
use ReturnTypeWillChange; | ||
|
||
/** | ||
* @method string getOwner() | ||
* @method void setOwner(string $owner) | ||
* @method string getTitle() | ||
* @method void setTitle(string $title) | ||
* @method string getContent() | ||
* @method void setContent(string $content) | ||
* @method string getPreview() | ||
* @method void setPreview(string $preview) | ||
*/ | ||
class Snippet extends Entity implements JsonSerializable { | ||
protected $owner; | ||
protected $title; | ||
protected $content; | ||
protected $preview; | ||
|
||
public function __construct() { | ||
$this->addType('owner', 'string'); | ||
$this->addType('title', 'string'); | ||
$this->addType('content', 'string'); | ||
$this->addType('preview', 'string'); | ||
} | ||
|
||
#[ReturnTypeWillChange] | ||
public function jsonSerialize() { | ||
return [ | ||
'id' => $this->getId(), | ||
'owner' => $this->getOwner(), | ||
'title' => $this->getTitle(), | ||
'content' => $this->getContent(), | ||
'preview' => $this->getPreview(), | ||
]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Mail\Db; | ||
|
||
use OCP\AppFramework\Db\DoesNotExistException; | ||
use OCP\AppFramework\Db\QBMapper; | ||
use OCP\DB\QueryBuilder\IQueryBuilder; | ||
use OCP\IDBConnection; | ||
|
||
/** | ||
* @template-extends QBMapper<Snippet> | ||
*/ | ||
class SnippetMapper extends QBMapper { | ||
/** | ||
* @param IDBConnection $db | ||
*/ | ||
public function __construct(IDBConnection $db) { | ||
parent::__construct($db, 'mail_snippets'); | ||
} | ||
|
||
/** | ||
* @param int $id | ||
* @param string $owner | ||
* @return Snippet | ||
* | ||
* @throws DoesNotExistException | ||
*/ | ||
public function find(int $id, string $owner): Snippet { | ||
$qb = $this->db->getQueryBuilder(); | ||
$qb->select('*') | ||
->from($this->getTableName()) | ||
->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) | ||
->andWhere($qb->expr()->eq('owner', $qb->createNamedParameter($owner))); | ||
|
||
return $this->findEntity($qb); | ||
} | ||
|
||
/** | ||
* @param string $owner | ||
* @return Snippet[] | ||
*/ | ||
public function findAll(string $owner): array { | ||
$qb = $this->db->getQueryBuilder(); | ||
$qb->select('*') | ||
->from($this->getTableName()) | ||
->where( | ||
$qb->expr()->eq('owner', $qb->createNamedParameter($owner, IQueryBuilder::PARAM_STR)) | ||
); | ||
|
||
return $this->findEntities($qb); | ||
} | ||
|
||
/** | ||
* @param string $userId | ||
* @param array $groups | ||
* @return Snippet[] | ||
*/ | ||
public function findSharedWithMe(string $userId, array $groups): array { | ||
$qb = $this->db->getQueryBuilder(); | ||
$qb->select('s.*') | ||
->from($this->getTableName(), 's') | ||
->join('s', 'mail_snippets_shares', 'share', $qb->expr()->eq('s.id', 'share.snippet_id')) | ||
->where($qb->expr()->andX( | ||
$qb->expr()->eq('share.share_with', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)), | ||
$qb->expr()->eq('share.type', $qb->createNamedParameter('user', IQueryBuilder::PARAM_STR)) | ||
)) | ||
->orWhere( | ||
$qb->expr()->andX( | ||
$qb->expr()->in('share.share_with', $qb->createNamedParameter($groups, IQueryBuilder::PARAM_STR_ARRAY)), | ||
$qb->expr()->eq('share.type', $qb->createNamedParameter('group', IQueryBuilder::PARAM_STR)) | ||
) | ||
); | ||
return $this->findEntities($qb); | ||
} | ||
|
||
} |