2019-10-28 22:27:59 +03:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
|
2019-08-22 04:17:17 +03:00
|
|
|
* @copyright Copyright (c) 2019, Joas Schilling <coding@schilljs.com>
|
2019-10-28 22:27:59 +03:00
|
|
|
*
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2019-08-22 04:17:17 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2019-10-28 22:27:59 +03:00
|
|
|
*
|
|
|
|
* @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\Files_Sharing\Notification;
|
|
|
|
|
|
|
|
use OCP\Files\IRootFolder;
|
2019-08-22 04:17:17 +03:00
|
|
|
use OCP\IL10N;
|
2019-09-04 17:50:52 +03:00
|
|
|
use OCP\IGroupManager;
|
2019-08-22 04:17:17 +03:00
|
|
|
use OCP\IURLGenerator;
|
2019-09-04 17:50:52 +03:00
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\IUserManager;
|
2019-10-28 22:27:59 +03:00
|
|
|
use OCP\L10N\IFactory;
|
|
|
|
use OCP\Notification\AlreadyProcessedException;
|
|
|
|
use OCP\Notification\INotification;
|
|
|
|
use OCP\Notification\INotifier;
|
|
|
|
use OCP\Share\Exceptions\ShareNotFound;
|
|
|
|
use OCP\Share\IManager;
|
2019-08-22 04:17:17 +03:00
|
|
|
use OCP\Share\IShare;
|
2019-10-28 22:27:59 +03:00
|
|
|
|
|
|
|
class Notifier implements INotifier {
|
2019-09-09 15:31:14 +03:00
|
|
|
public const INCOMING_USER_SHARE = 'incoming_user_share';
|
|
|
|
public const INCOMING_GROUP_SHARE = 'incoming_group_share';
|
2019-10-28 22:27:59 +03:00
|
|
|
|
|
|
|
/** @var IFactory */
|
|
|
|
protected $l10nFactory;
|
|
|
|
/** @var IManager */
|
|
|
|
private $shareManager;
|
|
|
|
/** @var IRootFolder */
|
|
|
|
private $rootFolder;
|
2019-09-04 17:50:52 +03:00
|
|
|
/** @var IGroupManager */
|
|
|
|
protected $groupManager;
|
|
|
|
/** @var IUserManager */
|
|
|
|
protected $userManager;
|
2019-08-22 04:17:17 +03:00
|
|
|
/** @var IURLGenerator */
|
|
|
|
protected $url;
|
|
|
|
|
2019-10-28 22:27:59 +03:00
|
|
|
|
|
|
|
public function __construct(IFactory $l10nFactory,
|
|
|
|
IManager $shareManager,
|
2019-08-22 04:17:17 +03:00
|
|
|
IRootFolder $rootFolder,
|
2019-09-04 17:50:52 +03:00
|
|
|
IGroupManager $groupManager,
|
|
|
|
IUserManager $userManager,
|
2019-08-22 04:17:17 +03:00
|
|
|
IURLGenerator $url) {
|
2019-10-28 22:27:59 +03:00
|
|
|
$this->l10nFactory = $l10nFactory;
|
|
|
|
$this->shareManager = $shareManager;
|
|
|
|
$this->rootFolder = $rootFolder;
|
2019-09-04 17:50:52 +03:00
|
|
|
$this->groupManager = $groupManager;
|
|
|
|
$this->userManager = $userManager;
|
2019-08-22 04:17:17 +03:00
|
|
|
$this->url = $url;
|
2019-10-28 22:27:59 +03:00
|
|
|
}
|
|
|
|
|
2019-08-22 04:17:17 +03:00
|
|
|
/**
|
|
|
|
* Identifier of the notifier, only use [a-z0-9_]
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @since 17.0.0
|
|
|
|
*/
|
2019-10-28 22:27:59 +03:00
|
|
|
public function getID(): string {
|
|
|
|
return 'files_sharing';
|
|
|
|
}
|
|
|
|
|
2019-08-22 04:17:17 +03:00
|
|
|
/**
|
|
|
|
* Human readable name describing the notifier
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @since 17.0.0
|
|
|
|
*/
|
2019-10-28 22:27:59 +03:00
|
|
|
public function getName(): string {
|
2019-08-22 04:17:17 +03:00
|
|
|
return $this->l10nFactory->get('files_sharing')->t('File sharing');
|
2019-10-28 22:27:59 +03:00
|
|
|
}
|
|
|
|
|
2019-08-22 04:17:17 +03:00
|
|
|
/**
|
|
|
|
* @param INotification $notification
|
|
|
|
* @param string $languageCode The code of the language that should be used to prepare the notification
|
|
|
|
* @return INotification
|
|
|
|
* @throws \InvalidArgumentException When the notification was not prepared by a notifier
|
|
|
|
* @throws AlreadyProcessedException When the notification is not needed anymore and should be deleted
|
|
|
|
* @since 9.0.0
|
|
|
|
*/
|
2019-10-28 22:27:59 +03:00
|
|
|
public function prepare(INotification $notification, string $languageCode): INotification {
|
|
|
|
if ($notification->getApp() !== 'files_sharing' ||
|
2019-08-22 04:17:17 +03:00
|
|
|
($notification->getSubject() !== 'expiresTomorrow' &&
|
|
|
|
$notification->getObjectType() !== 'share')) {
|
2019-10-28 22:27:59 +03:00
|
|
|
throw new \InvalidArgumentException('Unhandled app or subject');
|
|
|
|
}
|
|
|
|
|
|
|
|
$l = $this->l10nFactory->get('files_sharing', $languageCode);
|
|
|
|
$attemptId = $notification->getObjectId();
|
|
|
|
|
|
|
|
try {
|
|
|
|
$share = $this->shareManager->getShareById($attemptId);
|
|
|
|
} catch (ShareNotFound $e) {
|
|
|
|
throw new AlreadyProcessedException();
|
|
|
|
}
|
|
|
|
|
2019-08-22 04:17:17 +03:00
|
|
|
if ($notification->getSubject() === 'expiresTomorrow') {
|
|
|
|
$notification = $this->parseShareExpiration($share, $notification, $l);
|
|
|
|
} else {
|
|
|
|
$notification = $this->parseShareInvitation($share, $notification, $l);
|
|
|
|
}
|
|
|
|
return $notification;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function parseShareExpiration(IShare $share, INotification $notification, IL10N $l): INotification {
|
2019-10-28 22:27:59 +03:00
|
|
|
$node = $share->getNode();
|
|
|
|
$userFolder = $this->rootFolder->getUserFolder($notification->getUser());
|
|
|
|
$path = $userFolder->getRelativePath($node->getPath());
|
|
|
|
|
|
|
|
$notification
|
|
|
|
->setParsedSubject($l->t('Share will expire tomorrow'))
|
|
|
|
->setParsedMessage($l->t('One or more of your shares will expire tomorrow'))
|
|
|
|
->setRichMessage(
|
|
|
|
$l->t('Your share of {node} will expire tomorrow'),
|
|
|
|
[
|
|
|
|
'node' => [
|
|
|
|
'type' => 'file',
|
|
|
|
'id' => $node->getId(),
|
|
|
|
'name' => $node->getName(),
|
|
|
|
'path' => $path,
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
return $notification;
|
|
|
|
}
|
2019-08-22 04:17:17 +03:00
|
|
|
|
|
|
|
protected function parseShareInvitation(IShare $share, INotification $notification, IL10N $l): INotification {
|
2019-09-04 17:50:52 +03:00
|
|
|
|
2019-08-22 04:17:17 +03:00
|
|
|
if ($share->getShareType() === IShare::TYPE_USER) {
|
2019-09-04 17:50:52 +03:00
|
|
|
if ($share->getStatus() !== IShare::STATUS_PENDING) {
|
2019-08-22 04:17:17 +03:00
|
|
|
throw new AlreadyProcessedException();
|
|
|
|
}
|
2019-09-04 17:50:52 +03:00
|
|
|
} else if ($share->getShareType() === IShare::TYPE_GROUP) {
|
2019-08-22 04:17:17 +03:00
|
|
|
if ($share->getStatus() !== IShare::STATUS_PENDING) {
|
|
|
|
throw new AlreadyProcessedException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($notification->getSubject()) {
|
2019-09-09 15:31:14 +03:00
|
|
|
case self::INCOMING_USER_SHARE:
|
2019-09-04 17:50:52 +03:00
|
|
|
if ($share->getSharedWith() !== $notification->getUser()) {
|
|
|
|
throw new AlreadyProcessedException();
|
|
|
|
}
|
|
|
|
|
2019-09-09 15:40:14 +03:00
|
|
|
$sharer = $this->userManager->get($share->getSharedBy());
|
|
|
|
if (!$sharer instanceof IUser) {
|
|
|
|
throw new \InvalidArgumentException('Temporary failure');
|
|
|
|
}
|
|
|
|
|
|
|
|
$subject = $l->t('You received {share} as a share by {user}');
|
2019-08-22 04:17:17 +03:00
|
|
|
$subjectParameters = [
|
|
|
|
'share' => [
|
|
|
|
'type' => 'highlight',
|
|
|
|
'id' => $notification->getObjectId(),
|
|
|
|
'name' => $share->getNode()->getName(),
|
|
|
|
],
|
|
|
|
'user' => [
|
|
|
|
'type' => 'user',
|
2019-09-09 15:40:14 +03:00
|
|
|
'id' => $sharer->getUID(),
|
|
|
|
'name' => $sharer->getDisplayName(),
|
2019-08-22 04:17:17 +03:00
|
|
|
],
|
|
|
|
];
|
2019-09-04 17:50:52 +03:00
|
|
|
break;
|
2019-08-22 04:17:17 +03:00
|
|
|
|
2019-09-09 15:31:14 +03:00
|
|
|
case self::INCOMING_GROUP_SHARE:
|
2019-09-04 17:50:52 +03:00
|
|
|
$user = $this->userManager->get($notification->getUser());
|
|
|
|
if (!$user instanceof IUser) {
|
|
|
|
throw new AlreadyProcessedException();
|
2019-08-22 04:17:17 +03:00
|
|
|
}
|
|
|
|
|
2019-09-04 17:50:52 +03:00
|
|
|
$group = $this->groupManager->get($share->getSharedWith());
|
|
|
|
if (!$group->inGroup($user)) {
|
|
|
|
throw new AlreadyProcessedException();
|
|
|
|
}
|
2019-08-22 04:17:17 +03:00
|
|
|
|
2019-09-04 17:50:52 +03:00
|
|
|
if ($share->getPermissions() === 0) {
|
|
|
|
// Already rejected
|
|
|
|
throw new AlreadyProcessedException();
|
|
|
|
}
|
2019-08-22 04:17:17 +03:00
|
|
|
|
2019-09-09 15:40:14 +03:00
|
|
|
$sharer = $this->userManager->get($share->getSharedBy());
|
|
|
|
if (!$sharer instanceof IUser) {
|
|
|
|
throw new \InvalidArgumentException('Temporary failure');
|
|
|
|
}
|
|
|
|
|
|
|
|
$subject = $l->t('You received {share} to group {group} as a share by {user}');
|
2019-09-04 17:50:52 +03:00
|
|
|
$subjectParameters = [
|
|
|
|
'share' => [
|
|
|
|
'type' => 'highlight',
|
|
|
|
'id' => $notification->getObjectId(),
|
|
|
|
'name' => $share->getNode()->getName(),
|
|
|
|
],
|
|
|
|
'group' => [
|
|
|
|
'type' => 'user-group',
|
|
|
|
'id' => $group->getGID(),
|
|
|
|
'name' => $group->getDisplayName(),
|
|
|
|
],
|
|
|
|
'user' => [
|
|
|
|
'type' => 'user',
|
2019-09-09 15:40:14 +03:00
|
|
|
'id' => $sharer->getUID(),
|
|
|
|
'name' => $sharer->getDisplayName(),
|
2019-09-04 17:50:52 +03:00
|
|
|
],
|
|
|
|
];
|
2019-08-22 04:17:17 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new \InvalidArgumentException('Invalid subject');
|
|
|
|
}
|
2019-09-04 17:50:52 +03:00
|
|
|
|
|
|
|
$placeholders = $replacements = [];
|
|
|
|
foreach ($subjectParameters as $placeholder => $parameter) {
|
|
|
|
$placeholders[] = '{' . $placeholder . '}';
|
|
|
|
$replacements[] = $parameter['name'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$notification->setParsedSubject(str_replace($placeholders, $replacements, $subject))
|
|
|
|
->setRichSubject($subject, $subjectParameters)
|
|
|
|
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
|
|
|
|
|
|
|
|
$acceptAction = $notification->createAction();
|
|
|
|
$acceptAction->setParsedLabel($l->t('Accept'))
|
|
|
|
->setLink($this->url->linkToOCSRouteAbsolute('files_sharing.ShareAPI.acceptShare', ['id' => $share->getId()]), 'POST')
|
|
|
|
->setPrimary(true);
|
|
|
|
$notification->addParsedAction($acceptAction);
|
|
|
|
|
|
|
|
$rejectAction = $notification->createAction();
|
|
|
|
$rejectAction->setParsedLabel($l->t('Reject'))
|
|
|
|
->setLink($this->url->linkToOCSRouteAbsolute('files_sharing.ShareAPI.deleteShare', ['id' => $share->getId()]), 'DELETE')
|
|
|
|
->setPrimary(false);
|
|
|
|
$notification->addParsedAction($rejectAction);
|
|
|
|
|
|
|
|
return $notification;
|
2019-08-22 04:17:17 +03:00
|
|
|
}
|
2019-10-28 22:27:59 +03:00
|
|
|
}
|