|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @author Sujith Haridasan <[email protected]> |
| 4 | + * |
| 5 | + * @copyright Copyright (c) 2018, ownCloud GmbH |
| 6 | + * @license AGPL-3.0 |
| 7 | + * |
| 8 | + * This code is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU Affero General Public License, version 3, |
| 10 | + * as published by the Free Software Foundation. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU Affero General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Affero General Public License, version 3, |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/> |
| 19 | + * |
| 20 | + */ |
| 21 | + |
| 22 | +namespace OCA\Notifications\BackgroundJob; |
| 23 | + |
| 24 | +use OC\BackgroundJob\Job; |
| 25 | +use OC\User\Manager; |
| 26 | +use OCA\Notifications\Configuration\OptionsStorage; |
| 27 | +use OCA\Notifications\Mailer\NotificationMailer; |
| 28 | +use OCA\Notifications\Mailer\NotificationMailerAdapter; |
| 29 | +use OCP\IConfig; |
| 30 | +use OCP\IGroupManager; |
| 31 | +use OCP\ILogger; |
| 32 | +use OCP\IRequest; |
| 33 | +use OCP\IURLGenerator; |
| 34 | +use OCP\Mail\IMailer; |
| 35 | +use OCP\Share\Exceptions\ShareNotFound; |
| 36 | +use OCP\Share\IManager; |
| 37 | +use OCP\Notification\IManager as NotificationManager; |
| 38 | + |
| 39 | +/** |
| 40 | + * Class MailNotificationSender |
| 41 | + * |
| 42 | + * @package OCA\Notifications\BackgroundJob |
| 43 | + */ |
| 44 | +class MailNotificationSender extends Job { |
| 45 | + |
| 46 | + /** @var IManager */ |
| 47 | + private $shareManager; |
| 48 | + |
| 49 | + /** @var \OC\Group\Manager|IGroupManager */ |
| 50 | + private $groupManager; |
| 51 | + |
| 52 | + /** @var Manager */ |
| 53 | + private $userManager; |
| 54 | + |
| 55 | + /** @var NotificationManager */ |
| 56 | + private $notificationManager; |
| 57 | + |
| 58 | + /** @var IConfig */ |
| 59 | + private $config; |
| 60 | + |
| 61 | + /** @var IMailer */ |
| 62 | + private $mailer; |
| 63 | + |
| 64 | + /** @var NotificationMailerAdapter */ |
| 65 | + private $mailerAdapter; |
| 66 | + |
| 67 | + /** @var IURLGenerator */ |
| 68 | + private $urlGenerator; |
| 69 | + |
| 70 | + /** @var IRequest */ |
| 71 | + private $request; |
| 72 | + |
| 73 | + /** @var ILogger */ |
| 74 | + private $logger; |
| 75 | + |
| 76 | + public function __construct(IManager $shareManager = null, |
| 77 | + IGroupManager $groupManager = null, |
| 78 | + Manager $userManager = null, |
| 79 | + NotificationManager $notificationManager = null, |
| 80 | + NotificationMailerAdapter $mailerAdapter = null, |
| 81 | + IMailer $mailer = null, |
| 82 | + IConfig $config = null, |
| 83 | + IURLGenerator $urlGenerator = null, |
| 84 | + IRequest $request = null, |
| 85 | + ILogger $logger = null) { |
| 86 | + $this->shareManager = $shareManager ? $shareManager : \OC::$server->getShareManager(); |
| 87 | + $this->groupManager = $groupManager ? $groupManager : \OC::$server->getGroupManager(); |
| 88 | + $this->userManager = $userManager ? $userManager : \OC::$server->getUserManager(); |
| 89 | + $this->notificationManager = $notificationManager ? $notificationManager : \OC::$server->getNotificationManager(); |
| 90 | + $this->urlGenerator = $urlGenerator ? $urlGenerator : \OC::$server->getURLGenerator(); |
| 91 | + $this->request = $request ? $request : \OC::$server->getRequest(); |
| 92 | + $this->logger = $logger ? $logger : \OC::$server->getLogger(); |
| 93 | + $this->mailer = $mailer ? $mailer : \OC::$server->getMailer(); |
| 94 | + $this->config = $config ? $config : \OC::$server->getConfig(); |
| 95 | + |
| 96 | + $optionsStorage = new OptionsStorage($this->config); |
| 97 | + $notificationMailer = new NotificationMailer($this->notificationManager, $this->mailer, $optionsStorage); |
| 98 | + |
| 99 | + $this->mailerAdapter = $mailerAdapter ? $mailerAdapter : |
| 100 | + new NotificationMailerAdapter($notificationMailer, $this->userManager, $this->logger, $this->urlGenerator); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @param $notificationURL |
| 105 | + * @return string |
| 106 | + */ |
| 107 | + public function fixURL($notificationURL) { |
| 108 | + $url = $this->request->getServerProtocol() . '://' . $this->request->getServerHost(); |
| 109 | + $partURL = \explode($url, $notificationURL)[1]; |
| 110 | + $webRoot = $this->getArgument()['webroot']; |
| 111 | + if (\strpos($partURL, $webRoot) === false) { |
| 112 | + $notificationURL = $url . $this->getArgument()['webroot'] . $partURL; |
| 113 | + } |
| 114 | + return $notificationURL; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @param $shareFullId |
| 119 | + */ |
| 120 | + public function sendNotify($shareFullId) { |
| 121 | + $notificationList = []; |
| 122 | + $maxCountSendNotification = 100; |
| 123 | + |
| 124 | + //Check if its an internal share or not |
| 125 | + try { |
| 126 | + $share = $this->shareManager->getShareById($shareFullId); |
| 127 | + } catch (ShareNotFound $e) { |
| 128 | + return null; |
| 129 | + } |
| 130 | + |
| 131 | + $users = []; |
| 132 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
| 133 | + //Notify all the group members |
| 134 | + $group = $this->groupManager->get($share->getSharedWith()); |
| 135 | + $users = $group->getUsers(); |
| 136 | + } elseif ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
| 137 | + $users[] = $this->userManager->get($share->getSharedWith()); |
| 138 | + } |
| 139 | + |
| 140 | + foreach ($users as $user) { |
| 141 | + if ($user->getEMailAddress() === null) { |
| 142 | + \OC::$server->getLogger()->warning("Mail notification can not be sent to " . $user->getUID() . " for share " . $share->getName() . " as email for user is not set"); |
| 143 | + continue; |
| 144 | + } |
| 145 | + $notification = $this->notificationManager->createNotification(); |
| 146 | + $notification->setApp('files_sharing') |
| 147 | + ->setUser($user->getUID()) |
| 148 | + ->setDateTime(new \DateTime()) |
| 149 | + ->setObject('local_share', $shareFullId); |
| 150 | + |
| 151 | + $fileLink = $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileId' => $share->getNode()->getId()]); |
| 152 | + |
| 153 | + $fileLink = $this->fixURL($fileLink); |
| 154 | + $notification->setLink($fileLink); |
| 155 | + |
| 156 | + $notification->setSubject('local_share', [$share->getShareOwner(), $share->getSharedBy(), $share->getNode()->getName()]); |
| 157 | + $notification->setMessage('local_share', [$share->getShareOwner(), $share->getSharedBy(), $share->getNode()->getName()]); |
| 158 | + |
| 159 | + //Adding action because sendMail requires an action for the notificaiton |
| 160 | + $acceptAction = $notification->createAction(); |
| 161 | + $acceptAction->setLabel('email'); |
| 162 | + $acceptAction->setLink($fileLink, 'POST'); |
| 163 | + $notification->addAction($acceptAction); |
| 164 | + |
| 165 | + if (\count($notificationList) < $maxCountSendNotification) { |
| 166 | + $notificationList[] = $notification; |
| 167 | + } else { |
| 168 | + foreach ($notificationList as $notificationDispatch) { |
| 169 | + $this->mailerAdapter->sendMail($notificationDispatch); |
| 170 | + } |
| 171 | + |
| 172 | + //Once users in notificationList recieve notification reset the list |
| 173 | + $notificationList = [$notification]; |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + if (\count($notificationList) < $maxCountSendNotification) { |
| 178 | + foreach ($notificationList as $notificationDispatch) { |
| 179 | + $this->mailerAdapter->sendMail($notificationDispatch); |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + public function execute($jobList, ILogger $logger = null) { |
| 185 | + $this->sendNotify($this->getArgument()['shareFullId']); |
| 186 | + $jobList->remove($this); |
| 187 | + } |
| 188 | + |
| 189 | + public function run($argument) { |
| 190 | + $this->sendNotify($this->getArgument()['shareFullId']); |
| 191 | + //Remove the joblist once the notification is completed. |
| 192 | + \OC::$server->getJobList()->removeById($this->getId()); |
| 193 | + } |
| 194 | +} |
0 commit comments