|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @copyright Copyright (c) 2019 Julius Härtl <jus@bitgrid.net> |
| 4 | + * |
| 5 | + * @author Julius Härtl <jus@bitgrid.net> |
| 6 | + * |
| 7 | + * @license GNU AGPL version 3 or any later version |
| 8 | + * |
| 9 | + * This program is free software: you can redistribute it and/or modify |
| 10 | + * it under the terms of the GNU Affero General Public License as |
| 11 | + * published by the Free Software Foundation, either version 3 of the |
| 12 | + * License, or (at your option) any later version. |
| 13 | + * |
| 14 | + * This program is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU Affero General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU Affero General Public License |
| 20 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | + * |
| 22 | + */ |
| 23 | + |
| 24 | +namespace OCA\FirstRunWizard\Notification; |
| 25 | + |
| 26 | +use OCP\App\IAppManager; |
| 27 | +use OCP\App\ManagerEvent; |
| 28 | +use OCP\EventDispatcher\IEventDispatcher; |
| 29 | +use OCP\IConfig; |
| 30 | +use OCP\IGroupManager; |
| 31 | +use OCP\Notification\IManager as INotificationManager; |
| 32 | + |
| 33 | +class AppHint { |
| 34 | + |
| 35 | + /** @var INotificationManager */ |
| 36 | + protected $notificationManager; |
| 37 | + |
| 38 | + /** @var IGroupManager */ |
| 39 | + protected $groupManager; |
| 40 | + |
| 41 | + /** @var IAppManager */ |
| 42 | + protected $appManager; |
| 43 | + |
| 44 | + /** @var IConfig */ |
| 45 | + protected $config; |
| 46 | + |
| 47 | + /** @var IEventDispatcher */ |
| 48 | + private $dispatcher; |
| 49 | + |
| 50 | + /** @var string */ |
| 51 | + private $userId; |
| 52 | + |
| 53 | + public function __construct(INotificationManager $notificationManager, IGroupManager $groupManager, IAppManager $appManager, IConfig $config, IEventDispatcher $eventDispatcher, $userId) { |
| 54 | + $this->notificationManager = $notificationManager; |
| 55 | + $this->groupManager = $groupManager; |
| 56 | + $this->appManager =$appManager; |
| 57 | + $this->config = $config; |
| 58 | + $this->dispatcher = $eventDispatcher; |
| 59 | + $this->userId = $userId; |
| 60 | + } |
| 61 | + |
| 62 | + public function sendAppHintNotifications(): void { |
| 63 | + if ($this->userId !== null && $this->groupManager->isAdmin($this->userId) && $this->config->getUserValue($this->userId, 'firstrunwizard', 'apphint') !== 'yes') { |
| 64 | + $this->sendNotification('calendar', $this->userId); |
| 65 | + $this->sendNotification('contacts', $this->userId); |
| 66 | + $this->sendNotification('mail', $this->userId); |
| 67 | + $this->sendNotification('spreed', $this->userId); |
| 68 | + $this->config->setUserValue($this->userId, 'firstrunwizard', 'apphint', 'yes'); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + public function registerAppListener(): void { |
| 73 | + $this->dispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE, function(ManagerEvent $event) { |
| 74 | + $this->dismissNotification($event->getAppID()); |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + protected function sendNotification(string $app, string $user): void { |
| 79 | + $notification = $this->generateNotification($app, $user); |
| 80 | + if ( |
| 81 | + $this->config->getUserValue($this->userId, 'firstrunwizard', 'apphint') !== 'yes' |
| 82 | + && $this->notificationManager->getCount($notification) === 0 |
| 83 | + && !$this->appManager->isEnabledForUser($app) |
| 84 | + ) { |
| 85 | + $notification->setDateTime(new \DateTime()); |
| 86 | + $this->notificationManager->notify($notification); |
| 87 | + } |
| 88 | + if ($this->notificationManager->getCount($notification) === 0) { |
| 89 | + return; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + protected function dismissNotification(string $app) { |
| 94 | + $notification = $this->notificationManager->createNotification(); |
| 95 | + $notification->setApp('firstrunwizard') |
| 96 | + ->setSubject('apphint-'. $app) |
| 97 | + ->setObject('app', $app); |
| 98 | + $this->notificationManager->markProcessed($notification); |
| 99 | + } |
| 100 | + |
| 101 | + protected function generateNotification(string $app, string $user) { |
| 102 | + $notification = $this->notificationManager->createNotification(); |
| 103 | + $notification->setApp('firstrunwizard') |
| 104 | + ->setSubject('apphint-'. $app) |
| 105 | + ->setObject('app', $app) |
| 106 | + ->setUser($user); |
| 107 | + return $notification; |
| 108 | + } |
| 109 | +} |
0 commit comments