summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorMarco Nassabain <marco.nassabain@hotmail.com>2021-02-23 22:59:14 +0100
committerSean Molenaar <SMillerDev@users.noreply.github.com>2021-04-08 22:31:21 +0200
commit907e7647493ee10a6a41d0a8e39b6214ce2752cd (patch)
tree7ab42d6120d7e39f31441bb3adf3ad5de9601002 /lib
parent237363e087c629f8902828575908d3c54e5aa348 (diff)
🚧 ItemService, Controller: added basic sharing
- TODO: check if relation already exists Signed-off-by: Marco Nassabain <marco.nassabain@hotmail.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/Controller/ItemController.php17
-rw-r--r--lib/Service/ItemServiceV2.php44
2 files changed, 53 insertions, 8 deletions
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
}