From 5a903540f67e7c2acd4334aadc449e779271852d Mon Sep 17 00:00:00 2001 From: Hamza Mahjoubi Date: Thu, 17 Oct 2024 22:35:32 +0200 Subject: [PATCH] fixup! feat: mail snippets Signed-off-by: Hamza Mahjoubi --- lib/Db/SnippetShare.php | 9 +- lib/Db/SnippetShareMapper.php | 22 ++++- lib/Service/SnippetService.php | 152 +++++++++++++++++++++++++++++++++ 3 files changed, 178 insertions(+), 5 deletions(-) create mode 100644 lib/Service/SnippetService.php diff --git a/lib/Db/SnippetShare.php b/lib/Db/SnippetShare.php index 1aa1c099d8..8db8cb66ec 100644 --- a/lib/Db/SnippetShare.php +++ b/lib/Db/SnippetShare.php @@ -17,19 +17,22 @@ * @method string getType() * @method void setType(string $type) * @method string getShareWith() - * @method void setShareWith((string $shareWith) + * @method void setShareWith(string $shareWith) * @method string getSnippetId() - * @method void setSnippetId(string $snippetId) + * @method void setSnippetId(int $snippetId) */ class SnippetShare extends Entity implements JsonSerializable { protected $owner; protected $title; protected $content; + public const TYPE_USER = 'user'; + public const TYPE_GROUP = 'group'; + public function __construct() { $this->addType('type', 'string'); $this->addType('shareWith', 'string'); - $this->addType('snippetId', 'string'); + $this->addType('snippetId', 'int'); } #[ReturnTypeWillChange] diff --git a/lib/Db/SnippetShareMapper.php b/lib/Db/SnippetShareMapper.php index b26b87170c..da11c0c8ee 100644 --- a/lib/Db/SnippetShareMapper.php +++ b/lib/Db/SnippetShareMapper.php @@ -28,11 +28,11 @@ public function __construct(IDBConnection $db) { /** * @param int $id * @param string $owner - * @return Snippet + * @return SnippetShare * * @throws DoesNotExistException */ - public function find(int $id, string $owner): Snippet { + public function find(int $id, string $owner): SnippetShare { $qb = $this->db->getQueryBuilder(); $qb->select('*') ->from($this->getTableName()) @@ -42,6 +42,24 @@ public function find(int $id, string $owner): Snippet { return $this->findEntity($qb); } + public function shareExists(string $snippetId, string $shareWith): bool { + $qb = $this->db->getQueryBuilder(); + $qb->select('*') + ->from($this->getTableName()) + ->where($qb->expr()->eq('snippet_id', $qb->createNamedParameter($snippetId))) + ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($shareWith))); + + try { + $share = $this->findEntity($qb); + if ($share !== null) { + return true; + } + } catch (DoesNotExistException $e) { + return false; + } + return false; + } + /** * @param string $owner * @return SnippetShare[] diff --git a/lib/Service/SnippetService.php b/lib/Service/SnippetService.php new file mode 100644 index 0000000000..3967d5128b --- /dev/null +++ b/lib/Service/SnippetService.php @@ -0,0 +1,152 @@ +snippetMapper = $snippetMapper; + $this->snippetShareMapper = $snippetShareMapper; + $this->userManager = $userManager; + $this->$groupManager = $groupManager; + } + + /** + * @param string $userId + * @return Snippet[] + */ + public function findAll(string $userId): array { + return $this->snippetMapper->findAll($userId); + } + + /** + * @param string + */ + public function findAllSharedWithMe(string $userId): array { + // TODO: add groupshares + return $this->snippetShareMapper->findAllSharedWith($userId); + } + /** + * @param int $snippetId + * @param string $userId + * @return Snippet + * @throws DoesNotExistException + */ + public function find(int $snippetId, string $userId): Snippet { + return $this->snippetMapper->find($snippetId, $userId); + } + + /** + * @param string $userId + * @param string $title + * @param string $content + * @return Snippet + */ + public function create(string $userId, string $title, string $content): Snippet { + $snippet = new Snippet(); + $snippet->setContent($content); + $snippet->setOwner($userId); + $snippet->setTitle($title); + return $this->snippetMapper->insert($snippet); + } + + /** + * @param int $snippetId + * @param string $userId + * @param string $title + * @param string $content + * @return Snippet + * @throws DoesNotExistException + */ + public function update(int $snippetId, string $userId, string $title, string $content): Snippet { + $snippet = $this->snippetMapper->find($snippetId, $userId); + $snippet->setContent($content); + $snippet->setTitle($title); + return $this->snippetMapper->update($snippet); + } + + /** + * @param int $snippetId + * @param string $userId + * @throws DoesNotExistException + */ + 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 { + + $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'); + } + $share = new SnippetShare(); + $share->setShareWith($shareWith); + $share->setSnippetId($snippetId); + $share->setType(SnippetShare::TYPE_USER); + $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'); + } + if (!$this->groupManager->groupExists($groupId)) { + throw new DoesNotExistException('Group does not exist'); + } + $share = new SnippetShare(); + $share->setShareWith($groupId); + $share->setSnippetId($snippetId); + $share->setType(SnippetShare::TYPE_GROUP); + $this->snippetShareMapper->insert($share); + } + + + +}