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

Sending unseen notifications as email periodically. #1058

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,17 @@
<nextcloud min-version="23" max-version="23" />
</dependencies>

<background-jobs>
<job>OCA\Notifications\BackgroundJob\SendNotificationMails</job>
</background-jobs>

<commands>
<command>OCA\Notifications\Command\Generate</command>
<command>OCA\Notifications\Command\TestPush</command>
</commands>

<settings>
<personal>OCA\Notifications\Settings\Personal</personal>
<personal-section>OCA\Notifications\Settings\PersonalSection</personal-section>
</settings>
</info>
2 changes: 2 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@
['name' => 'Push#removeDevice', 'url' => '/api/{apiVersion}/push', 'verb' => 'DELETE', 'requirements' => ['apiVersion' => 'v2']],

['name' => 'API#generateNotification', 'url' => '/api/{apiVersion}/admin_notifications/{userId}', 'verb' => 'POST', 'requirements' => ['apiVersion' => 'v(1|2)']],

['name' => 'Settings#personal', 'url' => '/api/{apiVersion}/settings', 'verb' => 'POST', 'requirements' => ['apiVersion' => 'v2']],
],
];
3 changes: 3 additions & 0 deletions js/notifications-userSettings.js

Large diffs are not rendered by default.

54 changes: 54 additions & 0 deletions js/notifications-userSettings.js.LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*!
* The buffer module from node.js, for the browser.
*
* @author Feross Aboukhadijeh <[email protected]> <http://feross.org>
* @license MIT
*/

/*!
* Vue.js v2.6.14
* (c) 2014-2021 Evan You
* Released under the MIT License.
*/

/**
* @copyright Copyright (c) 2019 Greta Doci <[email protected]>
*
* @author Greta Doci <[email protected]>
*
* @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/>.
*
*/

/**
* @copyright Copyright (c) 2021 Julien Barnoin <[email protected]>
*
* @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/>.
*
*/
1 change: 1 addition & 0 deletions js/notifications-userSettings.js.map

Large diffs are not rendered by default.

50 changes: 50 additions & 0 deletions lib/BackgroundJob/SendNotificationMails.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2021 Julien Barnoin <[email protected]>
*
* @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\Notifications\BackgroundJob;

use OCP\BackgroundJob\TimedJob;
use OCA\Notifications\MailNotifications;
use OCP\AppFramework\Utility\ITimeFactory;

class SendNotificationMails extends TimedJob {

/** @var ITimeFactory */
protected $timeFactory;
/** @var MailNotifications */
protected $mailNotifications;

public function __construct(ITimeFactory $timeFactory, MailNotifications $mailNotifications) {
parent::__construct($timeFactory);

// run every 15 min
$this->setInterval(60 * 15);

$this->timeFactory = $timeFactory;
$this->mailNotifications = $mailNotifications;
}

protected function run($argument) {
$this->mailNotifications->sendEmails();
}
}
90 changes: 90 additions & 0 deletions lib/Controller/SettingsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2021 Julien Barnoin <[email protected]>
*
* @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\Notifications\Controller;

use OCP\AppFramework\OCSController;
use OCP\AppFramework\Http\DataResponse;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IUserSession;

class SettingsController extends OCSController {
/** @var \OCP\IConfig */
protected $config;

/** @var \OCP\IL10N */
protected $l10n;

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

public function __construct(string $appName,
IRequest $request,
IConfig $config,
IL10N $l10n,
IUserSession $userSession) {
parent::__construct($appName, $request);
$this->config = $config;
$this->l10n = $l10n;
$this->user = $userSession->getUser()->getUID();
}

/**
* @NoAdminRequired
*
* @param int $notify_setting_batchtime
* @param bool $notifications_email_enabled
* @return DataResponse
*/
public function personal(
int $notify_setting_batchtime = \OCA\Notifications\Settings\Personal::EMAIL_SEND_HOURLY,
bool $notifications_email_enabled = false
): DataResponse {
$email_batch_time = 3600;
if ($notify_setting_batchtime === \OCA\Notifications\Settings\Personal::EMAIL_SEND_DAILY) {
$email_batch_time = 3600 * 24;
} elseif ($notify_setting_batchtime === \OCA\Notifications\Settings\Personal::EMAIL_SEND_WEEKLY) {
$email_batch_time = 3600 * 24 * 7;
} elseif ($notify_setting_batchtime === \OCA\Notifications\Settings\Personal::EMAIL_SEND_ASAP) {
$email_batch_time = 0;
}

$this->config->setUserValue(
$this->user, 'notifications',
'notify_setting_batchtime',
(string) $email_batch_time
);
$this->config->setUserValue(
$this->user, 'notifications',
'notifications_email_enabled',
$notifications_email_enabled ? '1' : '0'
);

return new DataResponse([
'message' => $this->l10n->t('Your settings have been updated.'),
]);
}
}
32 changes: 32 additions & 0 deletions lib/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,38 @@ public function getById(int $id, string $user): INotification {
}
}

/**
* Get the notifications after (and excluding) the given id
*
* @param int $startAfterId
* @param string $user
* @param int $limit
* @return array [notification_id => INotification]
* @throws NotificationNotFoundException
*/
public function getAfterId(int $startAfterId, string $user, $limit = 25): array {
$sql = $this->connection->getQueryBuilder();
$sql->select('*')
->from('notifications')
->orderBy('notification_id', 'DESC')
->setMaxResults($limit)
->where($sql->expr()->gt('notification_id', $sql->createNamedParameter($startAfterId)))
->andWhere($sql->expr()->eq('user', $sql->createNamedParameter($user)));
$statement = $sql->executeQuery();

$notifications = [];
while ($row = $statement->fetch()) {
try {
$notifications[(int)$row['notification_id']] = $this->notificationFromRow($row);
} catch (\InvalidArgumentException $e) {
continue;
}
}
$statement->closeCursor();

return $notifications;
}

/**
* Return the notifications matching the given Notification
*
Expand Down
Loading