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 17, 2024
1 parent 24e8b38 commit 5a90354
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 5 deletions.
9 changes: 6 additions & 3 deletions lib/Db/SnippetShare.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
22 changes: 20 additions & 2 deletions lib/Db/SnippetShareMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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[]
Expand Down
152 changes: 152 additions & 0 deletions lib/Service/SnippetService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php

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
*/

namespace OCA\Mail\Service;

use OCA\Mail\Db\Snippet;
use OCA\Mail\Db\SnippetMapper;
use OCA\Mail\Db\SnippetShare;
use OCA\Mail\Db\SnippetShareMapper;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\Files\NotPermittedException;
use OCP\IGroupManager;
use OCP\IUserManager;

class SnippetService {

/** @var SnippetMapper */
private $snippetMapper;

/** @var SnippetShareMapper */
private $snippetShareMapper;

/** @var IUserManager */
private $userManager;

/** @var IGroupManager */
private $groupManager;

public function __construct(SnippetMapper $snippetMapper, SnippetShareMapper $snippetShareMapper, IUserManager $userManager, IGroupManager $groupManager) {
$this->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);
}



}

0 comments on commit 5a90354

Please sign in to comment.