From e857f1450b8e916b675a907fe75f236ad5cd9c99 Mon Sep 17 00:00:00 2001 From: dartcafe Date: Sun, 26 Jul 2020 19:35:11 +0200 Subject: copy participants addresses to clipboard --- lib/Controller/PollApiController.php | 19 ++++++++++ lib/Controller/PollController.php | 17 +++++++++ lib/Db/ShareMapper.php | 22 +++++++++++ lib/Db/Vote.php | 2 +- lib/Service/MailService.php | 29 ++++++++++++++- lib/Service/PollService.php | 72 ++++++++++++++++++++++++++++-------- 6 files changed, 144 insertions(+), 17 deletions(-) (limited to 'lib') diff --git a/lib/Controller/PollApiController.php b/lib/Controller/PollApiController.php index 20eb2ea0..c2e4750f 100644 --- a/lib/Controller/PollApiController.php +++ b/lib/Controller/PollApiController.php @@ -202,6 +202,25 @@ } } + /** + * Collect email addresses from particitipants + * @NoAdminRequired + * @CORS + * @NoCSRFRequired + * @param Array $poll + * @return DataResponse + */ + + public function getParticipantsEmailAddresses($pollId) { + try { + return new DataResponse($this->pollService->getParticipantsEmailAddresses($pollId), Http::STATUS_OK); + } catch (DoesNotExistException $e) { + return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND); + } catch (NotAuthorizedException $e) { + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); + } + } + /** * Get valid values for configuration options * @NoAdminRequired diff --git a/lib/Controller/PollController.php b/lib/Controller/PollController.php index d1b4945b..04445482 100644 --- a/lib/Controller/PollController.php +++ b/lib/Controller/PollController.php @@ -267,4 +267,21 @@ class PollController extends Controller { } } + /** + * Collect email addresses from particitipants + * @NoAdminRequired + * @param Array $poll + * @return DataResponse + */ + + public function getParticipantsEmailAddresses($pollId) { + try { + return new DataResponse($this->pollService->getParticipantsEmailAddresses($pollId), Http::STATUS_OK); + } catch (DoesNotExistException $e) { + return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND); + } catch (NotAuthorizedException $e) { + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); + } + } + } diff --git a/lib/Db/ShareMapper.php b/lib/Db/ShareMapper.php index 3a4408f3..87df1051 100644 --- a/lib/Db/ShareMapper.php +++ b/lib/Db/ShareMapper.php @@ -70,6 +70,28 @@ class ShareMapper extends QBMapper { return $this->findEntities($qb); } + /** + * @param int $pollId + * @param string $userId + * @throws \OCP\AppFramework\Db\DoesNotExistException if not found + * @return array + */ + + public function findByPollAndUser($pollId, $userId) { + $qb = $this->db->getQueryBuilder(); + + $qb->select('*') + ->from($this->getTableName()) + ->where( + $qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT)) + ) + ->andWhere( + $qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)) + ); + + return $this->findEntity($qb); + } + /** * @param string $token * @throws \OCP\AppFramework\Db\DoesNotExistException if not found diff --git a/lib/Db/Vote.php b/lib/Db/Vote.php index 85b63c09..b304c149 100644 --- a/lib/Db/Vote.php +++ b/lib/Db/Vote.php @@ -71,7 +71,7 @@ class Vote extends Entity implements JsonSerializable { ]; } - private function getDisplayName() { + public function getDisplayName() { if (\OC::$server->getUserManager()->get($this->userId) instanceof IUser) { return \OC::$server->getUserManager()->get($this->userId)->getDisplayName(); } else { diff --git a/lib/Service/MailService.php b/lib/Service/MailService.php index 54fe8a34..2456fb84 100644 --- a/lib/Service/MailService.php +++ b/lib/Service/MailService.php @@ -156,6 +156,32 @@ class MailService { } + + /** + * @param integer $pollId + * @param string $userId + * @return string + */ + public function resolveEmailAddress($pollId, $userId) { + $contactsManager = \OC::$server->getContactsManager(); + + if ($this->userManager->get($userId) instanceof IUser) { + return \OC::$server->getConfig()->getUserValue($userId, 'settings', 'email'); + } + + // if $userId is no site user, eval via shares + try { + $share = $this->shareMapper->findByPollAndUser($pollId, $userId); + if ($share->getUserEmail()) { + return $share->getUserEmail(); + } + } catch (\Exception $e) { + // catch silently + } + return $userId; + } + + /** * @param Share $share * @param String $defaultLang @@ -219,7 +245,7 @@ class MailService { return; } - } elseif ($share->getType() === 'external' || $share->getType() === 'email') { + } elseif ($share->getType() === 'external') { $recipients[] = array( 'userId' => $share->getUserId(), 'eMailAddress' => $share->getUserEmail(), @@ -341,6 +367,7 @@ class MailService { if ($this->userManager->get($subscription->getUserId()) instanceof IUser) { $lang = $this->config->getUserValue($subscription->getUserId(), 'core', 'lang'); } else { + $lang = $this->config->getUserValue($poll->getOwner(), 'core', 'lang'); continue; } diff --git a/lib/Service/PollService.php b/lib/Service/PollService.php index e730b575..73b2044c 100644 --- a/lib/Service/PollService.php +++ b/lib/Service/PollService.php @@ -21,23 +21,26 @@ * */ - namespace OCA\Polls\Service; +namespace OCA\Polls\Service; - use Exception; - use OCP\AppFramework\Db\DoesNotExistException; - use OCA\Polls\Exceptions\EmptyTitleException; - use OCA\Polls\Exceptions\InvalidAccessException; - use OCA\Polls\Exceptions\InvalidShowResultsException; - use OCA\Polls\Exceptions\InvalidPollTypeException; - use OCA\Polls\Exceptions\NotAuthorizedException; +use Exception; +use OCP\AppFramework\Db\DoesNotExistException; +use OCA\Polls\Exceptions\EmptyTitleException; +use OCA\Polls\Exceptions\InvalidAccessException; +use OCA\Polls\Exceptions\InvalidShowResultsException; +use OCA\Polls\Exceptions\InvalidPollTypeException; +use OCA\Polls\Exceptions\NotAuthorizedException; - use OCA\Polls\Db\PollMapper; - use OCA\Polls\Db\Poll; - use OCA\Polls\Service\LogService; - use OCA\Polls\Model\Acl; +use OCA\Polls\Db\PollMapper; +use OCA\Polls\Db\Poll; +use OCA\Polls\Db\VoteMapper; +use OCA\Polls\Db\Vote; +use OCA\Polls\Service\LogService; +use OCA\Polls\Service\MailService; +use OCA\Polls\Model\Acl; - class PollService { +class PollService { /** @var PollMapper */ private $pollMapper; @@ -45,9 +48,18 @@ /** @var Poll */ private $poll; + /** @var VoteMapper */ + private $voteMapper; + + /** @var Vote */ + private $vote; + /** @var LogService */ private $logService; + /** @var MailService */ + private $mailService; + /** @var Acl */ private $acl; @@ -55,19 +67,28 @@ * PollController constructor. * @param PollMapper $pollMapper * @param Poll $poll + * @param VoteMapper $voteMapper + * @param Vote $vote * @param LogService $logService + * @param MailService $mailService * @param Acl $acl */ public function __construct( PollMapper $pollMapper, Poll $poll, - LogService $logService, + VoteMapper $voteMapper, + Vote $vote, + LogService $logService, + MailService $mailService, Acl $acl ) { $this->pollMapper = $pollMapper; $this->poll = $poll; - $this->logService = $logService; + $this->voteMapper = $voteMapper; + $this->vote = $vote; + $this->logService = $logService; + $this->mailService = $mailService; $this->acl = $acl; } @@ -304,6 +325,27 @@ return $this->pollMapper->insert($this->poll); } + /** + * Collect email addresses from particitipants + * @NoAdminRequired + * @param Array $poll + * @return DataResponse + */ + + public function getParticipantsEmailAddresses($pollId) { + $this->poll = $this->pollMapper->find($pollId); + if (!$this->acl->setPollId($pollId)->getAllowEdit()) { + return []; + } + + $votes = $this->voteMapper->findParticipantsByPoll($pollId); + foreach ($votes as $vote) { + $list[] = $vote->getDisplayName() . ' <' . $this->mailService->resolveEmailAddress($pollId, $vote->getUserId()) . '>'; + } + return array_unique($list); + } + + /** * Get valid values for configuration options * @NoAdminRequired -- cgit v1.2.3