Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command to repair notifications and properly handle mail sending … #333

Merged
merged 7 commits into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

<commands>
<command>OCA\Notifications\Command\Generate</command>
<command>OCA\Notifications\Command\RepairNotifications</command>
</commands>
<settings>
<personal>OCA\Notifications\Panels\Personal\NotificationsPanel</personal>
Expand Down
115 changes: 115 additions & 0 deletions lib/Command/RepairNotifications.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php
/**
* @author Jannik Stehle <[email protected]>
* @author Jan Ackermann <[email protected]>
*
* @copyright Copyright (c) 2021, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OCA\Notifications\Command;

use OCP\IDBConnection;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class RepairNotifications extends Command {

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

public static $availableSubjects = [
'relativeLinks'
];

/**
* @param IDBConnection $connection
*/
public function __construct(IDBConnection $connection) {
parent::__construct();
$this->connection = $connection;
}

protected function configure() {
$this
->setName('notifications:repairNotifications')
->setDescription('Repair existing notifications')
->addArgument('subject', InputArgument::REQUIRED, 'Subject to repair')
;
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @return false|int
*/
protected function execute(InputInterface $input, OutputInterface $output) {
$subject = $input->getArgument('subject');

if (!\in_array($subject, self::$availableSubjects)) {
throw new \LogicException('Invalid subject');
JammingBen marked this conversation as resolved.
Show resolved Hide resolved
}

$sql = $this->connection->getQueryBuilder();
$sql->select(['notification_id', 'link', 'actions'])
->from('notifications')
->where($sql->expr()->like('link', $sql->createPositionalParameter('http%')))
->orWhere($sql->expr()->like('actions', $sql->createPositionalParameter('%"link":"http%')));

$result = $sql->execute()->fetchAll();
JammingBen marked this conversation as resolved.
Show resolved Hide resolved

if (!$result) {
$output->writeln('No notifications found to repair.');
return 0;
}

$output->writeln(\sprintf('%s notification(s) found to repair', \count($result)));

foreach ($result as $row) {
$output->writeln(\sprintf('Repairing notification with ID %s...', $row['notification_id']));
JammingBen marked this conversation as resolved.
Show resolved Hide resolved

$sql = $this->connection->getQueryBuilder();
$sql->update('notifications')
->where($sql->expr()->eq('notification_id', $sql->createNamedParameter($row['notification_id'])));

$linkUrlComponents = \parse_url($row['link']);
if (\array_key_exists('scheme', $linkUrlComponents)) {
JammingBen marked this conversation as resolved.
Show resolved Hide resolved
$newLink = \parse_url($row['link'], PHP_URL_PATH);
$sql->set('link', $sql->createNamedParameter($newLink));
}

if (\strpos($row['actions'], 'http') !== false) {
$actions = \json_decode($row['actions'], true);

foreach ($actions as &$action) {
JammingBen marked this conversation as resolved.
Show resolved Hide resolved
$actionUrlComponents = \parse_url($action['link']);
if (\array_key_exists('scheme', $actionUrlComponents)) {
$action['link'] = \parse_url($action['link'], PHP_URL_PATH);
}
}

$sql->set('actions', $sql->createNamedParameter(\json_encode($actions)));
}

$sql->execute();
}

$output->writeln('Done');
return 0;
}
}
12 changes: 11 additions & 1 deletion lib/Mailer/NotificationMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use OCP\Mail\IMailer;
use OCP\Template;
use OCA\Notifications\Configuration\OptionsStorage;
use OCP\IURLGenerator;

/**
* The class will focus on sending notifications via email. In addition, some email-related
Expand All @@ -41,10 +42,14 @@ class NotificationMailer {
/** @var OptionsStorage */
private $optionsStorage;

public function __construct(IManager $manager, IMailer $mailer, OptionsStorage $optionsStorage) {
/** @var IURLGenerator */
private $urlGenerator;

public function __construct(IManager $manager, IMailer $mailer, OptionsStorage $optionsStorage, IURLGenerator $urlGenerator) {
$this->manager = $manager;
$this->mailer = $mailer;
$this->optionsStorage = $optionsStorage;
$this->urlGenerator = $urlGenerator;
}

/**
Expand All @@ -71,8 +76,13 @@ public function sendNotification(INotification $notification, $serverUrl, $email
$emailMessage->setTo([$emailAddress]);

$notificationLink = $notification->getLink();
$components = \parse_url($notificationLink);
$linkIsAbsolute = \array_key_exists('host', $components);

if ($notificationLink === '') {
$notificationLink = $serverUrl;
} elseif ($linkIsAbsolute !== true) {
$notificationLink = $this->urlGenerator->getAbsoluteURL($notificationLink);
}

$parsedSubject = $notification->getParsedSubject();
Expand Down
83 changes: 83 additions & 0 deletions tests/Unit/Command/RepairNotificationsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* @author Jannik Stehle <[email protected]>
* @author Jan Ackermann <[email protected]>
*
* @copyright Copyright (c) 2021, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OCA\Notifications\Tests\Unit\Command;

use Doctrine\DBAL\Driver\Statement;
use OCA\Notifications\Command\Generate;
use OCA\Notifications\Command\RepairNotifications;
use OCA\Notifications\Tests\Unit\TestCase;
use OCP\DB\QueryBuilder\IExpressionBuilder;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use Symfony\Component\Console\Tester\CommandTester;

class RepairNotificationsTest extends TestCase {

/** @var IDBConnection | \PHPUnit\Framework\MockObject\MockObject */
protected $connection;
/** @var Generate */
protected $command;
/** @var CommandTester */
protected $tester;

protected function setUp(): void {
parent::setUp();

$this->connection = $this->createMock(IDBConnection::class);
$this->command = new RepairNotifications($this->connection);
$this->tester = new CommandTester($this->command);
}

public function testInvalidSubject() {
$this->expectException(\LogicException::class);

$options = [];
$input = ['subject' => 'test'];
$this->tester->execute($input, $options);
}

public function testRepairLinks() {
$options = [];
$input = ['subject' => RepairNotifications::$availableSubjects[0]];

$dbResult = [
['notification_id' => 1, 'link' => 'http://owncloud.com/test', 'actions' => '[]']
];

$exprBuilder = $this->createMock(IExpressionBuilder::class);
$statementMock = $this->createMock(Statement::class);
$statementMock->method('fetchAll')->willReturn($dbResult);
$qbMock = $this->createMock(IQueryBuilder::class);
$qbMock->method('select')->willReturnSelf();
$qbMock->method('from')->willReturnSelf();
$qbMock->method('update')->willReturnSelf();
$qbMock->method('where')->willReturnSelf();
$qbMock->method('expr')->willReturn($exprBuilder);
$qbMock->method('execute')->willReturn($statementMock);

$this->connection->method('getQueryBuilder')->willReturn($qbMock);

$response = $this->tester->execute($input, $options);
$this->assertEquals(0, $response);
}
}
11 changes: 5 additions & 6 deletions tests/Unit/Mailer/NotificationMailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
namespace OCA\Notifications\Tests\Unit\Mailer;

use OC\Mail\Mailer;
use OCP\IURLGenerator;
use OCP\Notification\IManager;
use OCP\Notification\INotification;
use OCP\L10N\IFactory;
Expand All @@ -36,10 +37,10 @@ class NotificationMailerTest extends \Test\TestCase {
private $mailer;
/** @var OptionsStorage */
private $optionsStorage;
/** @var IFactory */
private $l10nFactory;
/** @var NotificationMailer*/
private $notificationMailer;
/** @var IURLGenerator*/
private $urlGenerator;

protected function setUp(): void {
parent::setUp();
Expand All @@ -57,11 +58,11 @@ protected function setUp(): void {
->disableOriginalConstructor()
->getMock();

$this->l10nFactory = $this->getMockBuilder(IFactory::class)
$this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)
->disableOriginalConstructor()
->getMock();

$this->notificationMailer = new NotificationMailer($this->manager, $this->mailer, $this->optionsStorage, $this->l10nFactory);
$this->notificationMailer = new NotificationMailer($this->manager, $this->mailer, $this->optionsStorage, $this->urlGenerator);
}

public function emailProvider() {
Expand Down Expand Up @@ -106,7 +107,6 @@ public function testSendNotification() {
return \vsprintf($text, $params);
}));

$this->l10nFactory->method('get')->willReturn($mockedL10N);
$this->mailer->expects($this->once())->method('send');

$this->optionsStorage->method('getOptions')
Expand Down Expand Up @@ -144,7 +144,6 @@ public function testSendNotificationFailedRecipients() {
return \vsprintf($text, $params);
}));

$this->l10nFactory->method('get')->willReturn($mockedL10N);
$this->mailer->expects($this->once())
->method('send')
->willReturn(['userTest1']);
Expand Down