From 907e7647493ee10a6a41d0a8e39b6214ce2752cd Mon Sep 17 00:00:00 2001 From: Marco Nassabain Date: Tue, 23 Feb 2021 22:59:14 +0100 Subject: =?UTF-8?q?=F0=9F=9A=A7=20ItemService,=20Controller:=20added=20bas?= =?UTF-8?q?ic=20sharing=20-=20TODO:=20check=20if=20relation=20already=20ex?= =?UTF-8?q?ists?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marco Nassabain --- lib/Controller/ItemController.php | 17 ++++++++------- lib/Service/ItemServiceV2.php | 44 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/lib/Controller/ItemController.php b/lib/Controller/ItemController.php index 757f2f115..7eeaa26ed 100644 --- a/lib/Controller/ItemController.php +++ b/lib/Controller/ItemController.php @@ -311,21 +311,22 @@ class ItemController extends Controller /** * @NoAdminRequired * - * @param int $itemId - * @param string $shareWithId - * @return array + * @param int $itemId Item to share + * @param string $shareWithId User to share with */ public function share($itemId, $shareWithId) { try { - $exists = $this->itemService->checkSharing($itemId, $shareWithId, $this->userId); - - if($exists==0){ - $this->itemService->shareItem($itemId, $shareWithId, $this->userId); - } + $this->itemService->share( + $this->getUserId(), + $itemId, + $shareWithId + ); } catch (ServiceNotFoundException $ex) { return $this->error($ex, Http::STATUS_NOT_FOUND); } + + return []; } } diff --git a/lib/Service/ItemServiceV2.php b/lib/Service/ItemServiceV2.php index 57804a8c3..a32eef314 100644 --- a/lib/Service/ItemServiceV2.php +++ b/lib/Service/ItemServiceV2.php @@ -392,4 +392,48 @@ class ItemServiceV2 extends Service ): array { return $this->mapper->findAllItems($userId, $type, $limit, $offset, $oldestFirst, $search); } + + + /** + * Share an item with a user + * + * @param string $userId Item owner + * @param int $id Item ID + * @param bool $shareWithId User to share with + * + * Sharing by copying - the item is duplicated, and the 'sharedBy' and + * 'sharedWith' fields are filled accordingly. + * We copy the 'feedId', because the article will still be owned by + * $userId, and it'll be stored in his feed + * + * @return Item + * @throws ServiceNotFoundException|ServiceConflictException + */ + public function share(string $userId, int $id, string $shareWithId): Entity + { + // TODO: check if item is already shared with the user + + /** @var Item $item */ + $item = $this->find($userId, $id); + + // duplicate the item + $sharedItem = Item::fromImport($item->jsonSerialize()); + + // copy and initialize fields + $sharedItem->setUnread(true); + $sharedItem->setStarred(false); + $sharedItem->setFeedId($item->getFeedId()); + $sharedItem->setFingerprint($item->getFingerprint()); + $sharedItem->setContentHash($item->getContentHash()); + $sharedItem->setSearchIndex($item->getSearchIndex()); + + // set share data + $sharedItem->setSharedBy($userId); + $sharedItem->setSharedWith($shareWithId); + + // return $this->mapper->update($item); + return $this->mapper->insert($sharedItem); + } + + // TODO: implement shared() -> return all items shared with user } -- cgit v1.2.3