Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/polls.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authordartcafe <github@dartcafe.de>2020-06-16 14:12:02 +0300
committerdartcafe <github@dartcafe.de>2020-06-16 14:12:02 +0300
commit076f8b3785f5728d7e61e5b9ac1233bda7e9fe3d (patch)
tree5b1ae4ce90891c565b20542a76a877625dbd4d7a /lib
parentfe8bc1b4aa307e0d956994c0fdfef51a93f549b2 (diff)
Added subscription to API
Diffstat (limited to 'lib')
-rw-r--r--lib/Controller/SubscriptionApiController.php116
-rw-r--r--lib/Controller/SubscriptionController.php28
-rw-r--r--lib/Service/ShareService.php5
-rw-r--r--lib/Service/SubscriptionService.php134
4 files changed, 258 insertions, 25 deletions
diff --git a/lib/Controller/SubscriptionApiController.php b/lib/Controller/SubscriptionApiController.php
new file mode 100644
index 00000000..b89f009c
--- /dev/null
+++ b/lib/Controller/SubscriptionApiController.php
@@ -0,0 +1,116 @@
+<?php
+/**
+ * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com>
+ *
+ * @author René Gieling <github@dartcafe.de>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCA\Polls\Controller;
+
+use Exception;
+use OCP\AppFramework\Db\DoesNotExistException;
+
+use OCP\IRequest;
+use OCP\ILogger;
+
+use OCP\AppFramework\ApiController;
+use OCP\AppFramework\Http;
+use OCP\AppFramework\Http\DataResponse;
+
+use OCA\Polls\Service\SubscriptionService;
+
+class SubscriptionApiController extends ApiController {
+
+ private $userId;
+ private $subscriptionService;
+ private $logger;
+
+ /**
+ * SubscriptionController constructor.
+ * @param string $appName
+ * @param $UserId
+ * @param SubscriptionService $subscriptionService
+ * @param IRequest $request
+ * @param ILogger $logger
+ */
+
+ public function __construct(
+ string $appName,
+ $userId,
+ SubscriptionService $subscriptionService,
+ IRequest $request,
+ ILogger $logger
+
+ ) {
+ parent::__construct($appName,
+ $request,
+ 'PUT, GET, DELETE',
+ 'Authorization, Content-Type, Accept',
+ 1728000);
+ $this->userId = $userId;
+ $this->subscriptionService = $subscriptionService;
+ $this->logger = $logger;
+ }
+
+ /**
+ * @NoAdminRequired
+ * CORS
+ * @NoCSRFRequired
+ * @param integer $pollId
+ * @return DataResponse
+ */
+ public function get($pollId) {
+ try {
+ return new DataResponse($this->subscriptionService->get($pollId), Http::STATUS_OK);
+ } catch (NotAuthorizedException $e) {
+ return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN);
+ } catch (DoesNotExistException $e) {
+ return new DataResponse('Not subscribed', Http::STATUS_NOT_FOUND);
+ }
+ }
+
+ /**
+ * @NoAdminRequired
+ * @CORS
+ * @NoCSRFRequired
+ * @param integer $pollId
+ */
+ public function subscribe($pollId) {
+ try {
+ return $this->subscriptionService->set($pollId, true);
+ return new DataResponse('Subscribed', Http::STATUS_OK);
+ } catch (NotAuthorizedException $e) {
+ return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN);
+ }
+ }
+ /**
+ * @NoAdminRequired
+ * @CORS
+ * @NoCSRFRequired
+ * @param integer $pollId
+ */
+ public function unsubscribe($pollId) {
+ try {
+ $this->subscriptionService->set($pollId, false);
+ return new DataResponse('Unsubscribed', Http::STATUS_OK);
+ } catch (NotAuthorizedException $e) {
+ return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN);
+ }
+ }
+}
diff --git a/lib/Controller/SubscriptionController.php b/lib/Controller/SubscriptionController.php
index 8a5bf445..d201fa6e 100644
--- a/lib/Controller/SubscriptionController.php
+++ b/lib/Controller/SubscriptionController.php
@@ -25,7 +25,6 @@ namespace OCA\Polls\Controller;
use Exception;
use OCP\AppFramework\Db\DoesNotExistException;
-use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\IRequest;
use OCP\ILogger;
@@ -73,19 +72,13 @@ class SubscriptionController extends Controller {
* @return DataResponse
*/
public function get($pollId) {
-
- if (!\OC::$server->getUserSession()->isLoggedIn()) {
- return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
- }
-
try {
- $this->mapper->findByUserAndPoll($pollId, $this->userId);
- } catch (MultipleObjectsReturnedException $e) {
- // should not happen, but who knows
+ return new DataResponse($this->subscriptionService->get($pollId), Http::STATUS_OK);
+ } catch (NotAuthorizedException $e) {
+ return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN);
} catch (DoesNotExistException $e) {
- return new DataResponse(null, Http::STATUS_NOT_FOUND);
+ return new DataResponse('Not subscribed', Http::STATUS_NOT_FOUND);
}
- return new DataResponse(null, Http::STATUS_OK);
}
/**
@@ -94,15 +87,10 @@ class SubscriptionController extends Controller {
* @param integer $pollId
*/
public function set($pollId, $subscribed) {
- if ($subscribed) {
- $subscription = new Subscription();
- $subscription->setPollId($pollId);
- $subscription->setUserId($this->userId);
- $this->mapper->insert($subscription);
- return true;
- } else {
- $this->mapper->unsubscribe($pollId, $this->userId);
- return false;
+ try {
+ return new DataResponse($this->subscriptionService->set($pollId, $subscribed), Http::STATUS_OK);
+ } catch (NotAuthorizedException $e) {
+ return new DataResponse('Unauthorized', Http::STATUS_FORBIDDEN);
}
}
}
diff --git a/lib/Service/ShareService.php b/lib/Service/ShareService.php
index c362e97a..0df25218 100644
--- a/lib/Service/ShareService.php
+++ b/lib/Service/ShareService.php
@@ -31,8 +31,6 @@ use OCP\Security\ISecureRandom;
use OCA\Polls\Exceptions\NotAuthorizedException;
use OCA\Polls\Exceptions\InvalidUsername;
-use OCA\Polls\Db\Poll;
-use OCA\Polls\Db\PollMapper;
use OCA\Polls\Db\Share;
use OCA\Polls\Db\ShareMapper;
use OCA\Polls\Service\MailService;
@@ -60,7 +58,6 @@ class ShareService {
* @param ILogger $logger
* @param ShareMapper $shareMapper
* @param Share $share
- * @param PollMapper $pollMapper
* @param SystemController $systemController
* @param MailService $mailService
* @param Acl $acl
@@ -71,7 +68,6 @@ class ShareService {
ILogger $logger,
ShareMapper $shareMapper,
Share $share,
- PollMapper $pollMapper,
SystemController $systemController,
MailService $mailService,
Acl $acl
@@ -80,7 +76,6 @@ class ShareService {
$this->userId = $userId;
$this->shareMapper = $shareMapper;
$this->share = $share;
- $this->pollMapper = $pollMapper;
$this->systemController = $systemController;
$this->mailService = $mailService;
$this->acl = $acl;
diff --git a/lib/Service/SubscriptionService.php b/lib/Service/SubscriptionService.php
new file mode 100644
index 00000000..a8801520
--- /dev/null
+++ b/lib/Service/SubscriptionService.php
@@ -0,0 +1,134 @@
+<?php
+/**
+ * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com>
+ *
+ * @author René Gieling <github@dartcafe.de>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCA\Polls\Service;
+
+use Exception;
+use OCA\Polls\Exceptions\NotAuthorizedException;
+use OCP\AppFramework\Db\MultipleObjectsReturnedException;
+use OCP\AppFramework\Db\DoesNotExistException;
+use OCP\ILogger;
+
+use OCA\Polls\Db\Subscription;
+use OCA\Polls\Db\SubscriptionMapper;
+use OCA\Polls\Model\Acl;
+
+class SubscriptionService {
+
+ private $userId;
+ private $acl;
+ private $subscriptionMapper;
+ private $logger;
+
+ /**
+ * SubscriptionController constructor.
+ * @param string $appName
+ * @param $UserId
+ * @param SubscriptionMapper $subscriptionMapper
+ * @param IRequest $request
+ * @param ILogger $logger
+ * @param Acl $acl
+ */
+
+ public function __construct(
+ string $appName,
+ $userId,
+ SubscriptionMapper $subscriptionMapper,
+ ILogger $logger,
+ Acl $acl
+ ) {
+ $this->userId = $userId;
+ $this->subscriptionMapper = $subscriptionMapper;
+ $this->acl = $acl;
+ $this->logger = $logger;
+ }
+
+ /**
+ * @NoAdminRequired
+ * @param integer $pollId
+ * @return DataResponse
+ */
+ public function get($pollId) {
+ if (!$this->acl->setPollId($pollId)->getAllowView()) {
+ throw new NotAuthorizedException;
+ }
+ try {
+ return $this->subscriptionMapper->findByUserAndPoll($pollId, $this->acl->getUserId());
+ } catch (MultipleObjectsReturnedException $e) {
+ // subscription should be unique. If duplicates are found resubscribe
+ // duplicates are removed in $this->set()
+ return $this->set($pollId, true);
+ }
+
+ }
+
+ /**
+ * @NoAdminRequired
+ * @param integer $pollId
+ */
+ public function set($pollId, $subscribed) {
+ if (!$this->acl->setPollId($pollId)->getAllowView()) {
+ throw new NotAuthorizedException;
+ }
+ try {
+ $subscription = $this->subscriptionMapper->findByUserAndPoll($pollId, $this->acl->getUserId());
+ if (!$subscribed) {
+ $this->subscriptionMapper->delete($subscription);
+ return 'Unsubscribed';
+ } else {
+ // subscription already exists, just return the existing subscription
+ return $subscription;
+ }
+ } catch (DoesNotExistException $e){
+ if ($subscribed) {
+ $subscription = new Subscription();
+ $subscription->setPollId($pollId);
+ $subscription->setUserId($this->acl->getUserId());
+
+ $this->subscriptionMapper->insert($subscription);
+ return $subscription;
+ } else {
+ // subscription is not found, just approve the unsubscription
+ return 'Unsubscribed';
+ }
+ } catch (MultipleObjectsReturnedException $e) {
+ // Duplicates should not exist but if found, fix it
+ // unsubscribe from all and resubscribe, if requested
+ $this->logger->debug('Multiple subscription (dulpicates) found');
+ $this->subscriptionMapper->unsubscribe($pollId, $this->acl->getUserId());
+ $this->logger->debug('Unsubscribed all for user ' . $this->acl->getUserId() . 'in poll' . $pollId);
+ if ($subscribed) {
+ $subscription = new Subscription();
+ $subscription->setPollId($pollId);
+ $subscription->setUserId($this->acl->getUserId());
+ $this->subscriptionMapper->insert($subscription);
+ $this->logger->debug('Added new subscription');
+ return $subscription;
+ } else {
+ return 'Unsubscribed';
+ }
+
+ }
+
+ }
+}