Skip to content

Commit

Permalink
Merge pull request #1007 from nextcloud/seperate-sending-invitations
Browse files Browse the repository at this point in the history
Separate sending invitations
  • Loading branch information
dartcafe authored Jul 7, 2020
2 parents 3c965b1 + 89d609b commit 3f9ba10
Show file tree
Hide file tree
Showing 13 changed files with 256 additions and 97 deletions.
2 changes: 2 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
['name' => 'share#add', 'url' => '/share/add', 'verb' => 'POST'],
['name' => 'share#delete', 'url' => '/share/delete', 'verb' => 'POST'],
['name' => 'share#createPersonalShare', 'url' => '/share/create/s', 'verb' => 'POST'],
['name' => 'share#sendInvitation', 'url' => '/share/send/{token}', 'verb' => 'POST'],

// ['name' => 'share#getShares', 'url' => '/shares/get/{pollId}', 'verb' => 'GET'],
// ['name' => 'share#get', 'url' => '/share/get/{token}', 'verb' => 'GET'],
Expand Down Expand Up @@ -98,6 +99,7 @@
['name' => 'share_api#get', 'url' => '/api/v1.0/share/{token}', 'verb' => 'GET'],
['name' => 'share_api#add', 'url' => '/api/v1.0/share', 'verb' => 'POST'],
['name' => 'share_api#delete', 'url' => '/api/v1.0/share/{token}', 'verb' => 'DELETE'],
['name' => 'share_api#sendInvitation', 'url' => '/api/v1.0/share/send/{token}', 'verb' => 'POST'],

['name' => 'subscription_api#get', 'url' => '/api/v1.0/poll/{pollId}/subscription', 'verb' => 'GET'],
['name' => 'subscription_api#subscribe', 'url' => '/api/v1.0/poll/{pollId}/subscription', 'verb' => 'PUT'],
Expand Down
3 changes: 3 additions & 0 deletions img/mail.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions lib/Controller/ShareApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,25 @@
use OCP\AppFramework\Http\DataResponse;

use OCA\Polls\Service\ShareService;
use OCA\Polls\Service\MailService;

class ShareApiController extends ApiController {

private $shareService;
private $mailService;

/**
* ShareController constructor.
* @param string $appName
* @param string $userId
* @param IRequest $request
* @param MailService $mailService
* @param ShareService $shareService
*/
public function __construct(
string $appName,
IRequest $request,
MailService $mailService,
ShareService $shareService
) {
parent::__construct($appName,
Expand All @@ -58,6 +62,7 @@ public function __construct(
'Authorization, Content-Type, Accept',
1728000);
$this->shareService = $shareService;
$this->mailService = $mailService;
}

/**
Expand Down Expand Up @@ -120,6 +125,23 @@ public function add($pollId, $type, $userId = '', $userEmail = '') {

}

/**
* SendInvitation
* Sent invitation mails for a share
* @NoAdminRequired
* @CORS
* @NoCSRFRequired
* @param string $token
* @return DataResponse
*/
public function sendInvitation($token) {
try {
return new DataResponse($this->mailService->sendInvitationMail($token), Http::STATUS_OK);
} catch (Exception $e) {
return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
}
}

/**
* delete share
* @NoAdminRequired
Expand Down
37 changes: 27 additions & 10 deletions lib/Controller/ShareController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@

use OCA\Polls\Model\Acl;
use OCA\Polls\Service\ShareService;
use OCA\Polls\Service\MailService;

class ShareController extends Controller {

private $logger;
private $shareService;
private $mailService;
private $userId;

/**
Expand All @@ -52,19 +54,22 @@ class ShareController extends Controller {
* @param string $userId
* @param IRequest $request
* @param ILogger $logger
* @param MailService $mailService
* @param ShareService $shareService
*/
public function __construct(
string $appName,
$userId,
IRequest $request,
ILogger $logger,
MailService $mailService,
ShareService $shareService
) {
parent::__construct($appName, $request);
$this->logger = $logger;
$this->userId = $userId;
$this->shareService = $shareService;
$this->mailService = $mailService;
}

/**
Expand All @@ -75,21 +80,14 @@ public function __construct(
* @param Array $share
* @return DataResponse
*/
public function add($pollId, $share) {
try {
$return = $this->shareService->write(
$pollId,
$share['type'],
$share['userId'],
isset($share['userEmail']) ? $share['userEmail'] : ''
);
return new DataResponse($return, Http::STATUS_CREATED);
public function add($pollId, $type, $userId = '', $userEmail = '') {
try {
return new DataResponse(['share' => $this->shareService->add($pollId, $type, $userId, $userEmail)], Http::STATUS_CREATED);
} catch (NotAuthorizedException $e) {
return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
} catch (\Exception $e) {
return new DataResponse($e, Http::STATUS_CONFLICT);
}

}

/**
Expand All @@ -116,6 +114,25 @@ public function createPersonalShare($token, $userName) {
}
}

/**
* SendInvitation
* Sent invitation mails for a share
* @NoAdminRequired
* @PublicPage
* @NoCSRFRequired
* @param string $token
* @return DataResponse
*/
public function sendInvitation($token) {
try {
$sentResult = $this->mailService->sendInvitationMail($token);
$share = $this->shareService->get($token);
return new DataResponse(['share' => $share, 'sentResult' => $sentResult], Http::STATUS_OK);
} catch (Exception $e) {
return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
}
}

/**
* remove
* remove share
Expand Down
8 changes: 7 additions & 1 deletion lib/Db/Share.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
* @method void setUserId(string $value)
* @method string getUserEmail()
* @method void setUserEmail(string $value)
* @method integer getInvitationSent()
* @method void setInvitationSent(integer $value)
*/
class Share extends Entity implements JsonSerializable {

Expand All @@ -59,6 +61,9 @@ class Share extends Entity implements JsonSerializable {
/** @var string $userEmail */
protected $userEmail;

/** @var string $invitationSent */
protected $invitationSent;

public function jsonSerialize() {

return [
Expand All @@ -68,7 +73,8 @@ public function jsonSerialize() {
'pollId' => intval($this->pollId),
'userId' => $this->userId,
'userEmail' => $this->userEmail,
'displayName' => $this->getDisplayName()
'displayName' => $this->getDisplayName(),
'invitationSent' => intval($this->invitationSent)
];
}

Expand Down
90 changes: 90 additions & 0 deletions lib/Migration/Version0105Date20200704084037.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* @copyright Copyright (c) 2017 René Gieling <github@dartcafe.de>
*
* @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\Migration;

use Doctrine\DBAL\Types\Type;
use OCP\DB\ISchemaWrapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Migration\SimpleMigrationStep;
use OCP\Migration\IOutput;

/**
* Installation class for the polls app.
* Initial db creation
*/
class Version0105Date20200704084037 extends SimpleMigrationStep {

/** @var IDBConnection */
protected $connection;

/** @var IConfig */
protected $config;

/**
* @param IDBConnection $connection
* @param IConfig $config
*/
public function __construct(IDBConnection $connection, IConfig $config) {
$this->connection = $connection;
$this->config = $config;
}

/**
* @param IOutput $output
* @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
* @return null|ISchemaWrapper
* @since 13.0.0
*/
public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
if ($schema->hasTable('polls_share')) {
$table = $schema->getTable('polls_share');
if (!$table->hasColumn('invitation_sent')) {
$table->addColumn('invitation_sent', Type::INTEGER, [
'length' => 11,
'notnull' => true,
'default' => 0
]);
}
}

return $schema;
}
/**
* @param IOutput $output
* @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
* @since 13.0.0
*/
public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) {
$query = $this->connection->getQueryBuilder();
$query->update('polls_share')
->set('invitation_sent', 'id');
$query->execute();
}
}
12 changes: 7 additions & 5 deletions lib/Service/MailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ private function getRecipientsByShare($share, $defaultLang = 'en', $skipUser = n

$recipients[] = array(
'userId' => $share->getUserId(),
'eMailAddress' => null,
'displayName' => null,
'eMailAddress' => \OC::$server->getConfig()->getUserValue($share->getUserId(), 'settings', 'email'),
'displayName' => $this->userManager->get($share->getUserId())->getDisplayName(),
'language' => $this->config->getUserValue(
$share->getUserId(),
'core', 'lang'
Expand Down Expand Up @@ -229,9 +229,9 @@ private function getRecipientsByShare($share, $defaultLang = 'en', $skipUser = n

$recipients[] = array(
'userId' => $member,
'eMailAddress' => null,
'displayName' => null,
'language' => $this->config->getUserValue($share->getUserId(), 'core', 'lang'),
'eMailAddress' => \OC::$server->getConfig()->getUserValue($member, 'settings', 'email'),
'displayName' => $this->userManager->get($member)->getDisplayName(),
'language' => $this->config->getUserValue($member, 'core', 'lang'),
'link' => $this->urlGenerator->getAbsoluteURL(
$this->urlGenerator->linkToRoute(
'polls.page.indexvote', ['id' => $share->getPollId()]
Expand Down Expand Up @@ -300,6 +300,8 @@ public function sendInvitationMail($token) {
$recipient['eMailAddress'],
$recipient['displayName']
);
$share->setInvitationSent(time());
$this->shareMapper->update($share);
$sentMails[] = $recipient;
} catch (Exception $e) {
$abortedMails[] = $recipient;
Expand Down
Loading

0 comments on commit 3f9ba10

Please sign in to comment.