2016-11-07 14:08:56 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-11-24 14:46:38 +00:00
|
|
|
* @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
|
2016-11-07 14:08:56 +00: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\ShareByMail;
|
|
|
|
|
2016-11-24 14:46:38 +00:00
|
|
|
use OCP\Activity\IEvent;
|
2016-11-07 14:08:56 +00:00
|
|
|
use OCP\Activity\IManager;
|
2016-11-24 14:46:38 +00:00
|
|
|
use OCP\Activity\IProvider;
|
2016-11-25 10:17:06 +00:00
|
|
|
use OCP\Contacts\IManager as IContactsManager;
|
2016-11-07 14:08:56 +00:00
|
|
|
use OCP\IL10N;
|
2016-11-24 14:46:38 +00:00
|
|
|
use OCP\IURLGenerator;
|
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\IUserManager;
|
2016-12-01 10:35:23 +00:00
|
|
|
use OCP\L10N\IFactory;
|
2016-11-07 14:08:56 +00:00
|
|
|
|
2016-11-24 14:46:38 +00:00
|
|
|
class Activity implements IProvider {
|
2016-11-07 14:08:56 +00:00
|
|
|
|
2016-12-01 10:35:23 +00:00
|
|
|
/** @var IFactory */
|
|
|
|
protected $languageFactory;
|
|
|
|
|
2016-11-24 14:46:38 +00:00
|
|
|
/** @var IL10N */
|
|
|
|
protected $l;
|
2016-11-07 14:08:56 +00:00
|
|
|
|
2016-11-24 14:46:38 +00:00
|
|
|
/** @var IURLGenerator */
|
|
|
|
protected $url;
|
2016-11-07 14:08:56 +00:00
|
|
|
|
|
|
|
/** @var IManager */
|
2016-11-24 14:46:38 +00:00
|
|
|
protected $activityManager;
|
2016-11-07 14:08:56 +00:00
|
|
|
|
2016-11-24 14:46:38 +00:00
|
|
|
/** @var IUserManager */
|
|
|
|
protected $userManager;
|
2016-11-25 10:17:06 +00:00
|
|
|
/** @var IContactsManager */
|
|
|
|
protected $contactsManager;
|
2016-11-07 14:08:56 +00:00
|
|
|
|
2016-11-24 14:46:38 +00:00
|
|
|
/** @var array */
|
|
|
|
protected $displayNames = [];
|
2016-11-07 14:08:56 +00:00
|
|
|
|
2016-11-25 10:17:06 +00:00
|
|
|
/** @var array */
|
|
|
|
protected $contactNames = [];
|
|
|
|
|
2016-11-24 14:46:38 +00:00
|
|
|
const SUBJECT_SHARED_EMAIL_SELF = 'shared_with_email_self';
|
|
|
|
const SUBJECT_SHARED_EMAIL_BY = 'shared_with_email_by';
|
2016-11-07 14:08:56 +00:00
|
|
|
|
|
|
|
/**
|
2016-12-01 10:35:23 +00:00
|
|
|
* @param IFactory $languageFactory
|
2016-11-24 14:46:38 +00:00
|
|
|
* @param IURLGenerator $url
|
|
|
|
* @param IManager $activityManager
|
|
|
|
* @param IUserManager $userManager
|
2016-11-25 10:17:06 +00:00
|
|
|
* @param IContactsManager $contactsManager
|
2016-11-07 14:08:56 +00:00
|
|
|
*/
|
2016-12-01 10:35:23 +00:00
|
|
|
public function __construct(IFactory $languageFactory, IURLGenerator $url, IManager $activityManager, IUserManager $userManager, IContactsManager $contactsManager) {
|
|
|
|
$this->languageFactory = $languageFactory;
|
2016-11-24 14:46:38 +00:00
|
|
|
$this->url = $url;
|
|
|
|
$this->activityManager = $activityManager;
|
|
|
|
$this->userManager = $userManager;
|
2016-11-25 10:17:06 +00:00
|
|
|
$this->contactsManager = $contactsManager;
|
2016-11-07 14:08:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-01 10:35:23 +00:00
|
|
|
* @param string $language
|
2016-11-24 14:46:38 +00:00
|
|
|
* @param IEvent $event
|
|
|
|
* @param IEvent|null $previousEvent
|
|
|
|
* @return IEvent
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
* @since 11.0.0
|
2016-11-07 14:08:56 +00:00
|
|
|
*/
|
2016-12-01 10:35:23 +00:00
|
|
|
public function parse($language, IEvent $event, IEvent $previousEvent = null) {
|
2016-11-24 14:46:38 +00:00
|
|
|
if ($event->getApp() !== 'sharebymail') {
|
|
|
|
throw new \InvalidArgumentException();
|
2016-11-07 14:08:56 +00:00
|
|
|
}
|
|
|
|
|
2016-12-01 10:35:23 +00:00
|
|
|
$this->l = $this->languageFactory->get('sharebymail', $language);
|
|
|
|
|
2016-11-07 14:08:56 +00:00
|
|
|
if ($this->activityManager->isFormattingFilteredObject()) {
|
2016-11-24 14:46:38 +00:00
|
|
|
try {
|
|
|
|
return $this->parseShortVersion($event);
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
// Ignore and simply use the long version...
|
2016-11-07 14:08:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-24 14:46:38 +00:00
|
|
|
return $this->parseLongVersion($event);
|
2016-11-07 14:08:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-24 14:46:38 +00:00
|
|
|
* @param IEvent $event
|
|
|
|
* @return IEvent
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
* @since 11.0.0
|
2016-11-07 14:08:56 +00:00
|
|
|
*/
|
2016-11-24 14:46:38 +00:00
|
|
|
public function parseShortVersion(IEvent $event) {
|
|
|
|
$parsedParameters = $this->getParsedParameters($event);
|
|
|
|
|
|
|
|
if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_SELF) {
|
|
|
|
$event->setParsedSubject($this->l->t('Shared with %1$s', [
|
|
|
|
$parsedParameters['email']['name'],
|
|
|
|
]))
|
|
|
|
->setRichSubject($this->l->t('Shared with {email}'), [
|
|
|
|
'email' => $parsedParameters['email'],
|
|
|
|
])
|
|
|
|
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
|
|
|
|
} else if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_BY) {
|
|
|
|
$event->setParsedSubject($this->l->t('Shared with %1$s by %2$s', [
|
|
|
|
$parsedParameters['email']['name'],
|
|
|
|
$parsedParameters['actor']['name'],
|
|
|
|
]))
|
|
|
|
->setRichSubject($this->l->t('Shared with {email} by {actor}'), [
|
|
|
|
'email' => $parsedParameters['email'],
|
|
|
|
'actor' => $parsedParameters['actor'],
|
|
|
|
])
|
|
|
|
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
throw new \InvalidArgumentException();
|
2016-11-07 14:08:56 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 14:46:38 +00:00
|
|
|
return $event;
|
2016-11-07 14:08:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-24 14:46:38 +00:00
|
|
|
* @param IEvent $event
|
|
|
|
* @return IEvent
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
* @since 11.0.0
|
2016-11-07 14:08:56 +00:00
|
|
|
*/
|
2016-11-24 14:46:38 +00:00
|
|
|
public function parseLongVersion(IEvent $event) {
|
|
|
|
$parsedParameters = $this->getParsedParameters($event);
|
|
|
|
|
|
|
|
if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_SELF) {
|
|
|
|
$event->setParsedSubject($this->l->t('You shared %1$s with %2$s by mail', [
|
|
|
|
$parsedParameters['file']['path'],
|
|
|
|
$parsedParameters['email']['name'],
|
|
|
|
]))
|
|
|
|
->setRichSubject($this->l->t('You shared {file} with {email} by mail'), $parsedParameters)
|
|
|
|
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
|
|
|
|
} else if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_BY) {
|
|
|
|
$event->setParsedSubject($this->l->t('%3$s shared %1$s with %2$s by mail', [
|
|
|
|
$parsedParameters['file']['path'],
|
|
|
|
$parsedParameters['email']['name'],
|
|
|
|
$parsedParameters['actor']['name'],
|
|
|
|
]))
|
|
|
|
->setRichSubject($this->l->t('{actor} shared {file} with {email} by mail'), $parsedParameters)
|
|
|
|
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
|
|
|
|
} else {
|
|
|
|
throw new \InvalidArgumentException();
|
2016-11-07 14:08:56 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 14:46:38 +00:00
|
|
|
return $event;
|
2016-11-07 14:08:56 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 14:46:38 +00:00
|
|
|
protected function getParsedParameters(IEvent $event) {
|
|
|
|
$subject = $event->getSubject();
|
|
|
|
$parameters = $event->getSubjectParameters();
|
2016-11-07 14:08:56 +00:00
|
|
|
|
2016-11-24 14:46:38 +00:00
|
|
|
switch ($subject) {
|
2016-11-25 10:45:40 +00:00
|
|
|
case self::SUBJECT_SHARED_EMAIL_SELF:
|
2016-11-24 14:46:38 +00:00
|
|
|
return [
|
|
|
|
'file' => $this->generateFileParameter((int) $event->getObjectId(), $parameters[0]),
|
|
|
|
'email' => $this->generateEmailParameter($parameters[1]),
|
|
|
|
];
|
2016-11-25 10:45:40 +00:00
|
|
|
case self::SUBJECT_SHARED_EMAIL_BY:
|
2016-11-24 14:46:38 +00:00
|
|
|
return [
|
|
|
|
'file' => $this->generateFileParameter((int) $event->getObjectId(), $parameters[0]),
|
|
|
|
'email' => $this->generateEmailParameter($parameters[1]),
|
|
|
|
'actor' => $this->generateUserParameter($parameters[2]),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
throw new \InvalidArgumentException();
|
2016-11-07 14:08:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-24 14:46:38 +00:00
|
|
|
* @param int $id
|
|
|
|
* @param string $path
|
|
|
|
* @return array
|
2016-11-07 14:08:56 +00:00
|
|
|
*/
|
2016-11-24 14:46:38 +00:00
|
|
|
protected function generateFileParameter($id, $path) {
|
|
|
|
return [
|
|
|
|
'type' => 'file',
|
|
|
|
'id' => $id,
|
|
|
|
'name' => basename($path),
|
2016-12-15 14:53:48 +00:00
|
|
|
'path' => trim($path, '/'),
|
2016-11-24 14:46:38 +00:00
|
|
|
'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]),
|
|
|
|
];
|
2016-11-07 14:08:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-24 14:46:38 +00:00
|
|
|
* @param string $email
|
|
|
|
* @return array
|
2016-11-07 14:08:56 +00:00
|
|
|
*/
|
2016-11-24 14:46:38 +00:00
|
|
|
protected function generateEmailParameter($email) {
|
2016-11-25 10:17:06 +00:00
|
|
|
if (!isset($this->contactNames[$email])) {
|
|
|
|
$this->contactNames[$email] = $this->getContactName($email);
|
|
|
|
}
|
|
|
|
|
2016-11-24 14:46:38 +00:00
|
|
|
return [
|
|
|
|
'type' => 'email',
|
|
|
|
'id' => $email,
|
2016-11-25 10:17:06 +00:00
|
|
|
'name' => $this->contactNames[$email],
|
2016-11-24 14:46:38 +00:00
|
|
|
];
|
2016-11-07 14:08:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-24 14:46:38 +00:00
|
|
|
* @param string $uid
|
|
|
|
* @return array
|
2016-11-07 14:08:56 +00:00
|
|
|
*/
|
2016-11-24 14:46:38 +00:00
|
|
|
protected function generateUserParameter($uid) {
|
|
|
|
if (!isset($this->displayNames[$uid])) {
|
|
|
|
$this->displayNames[$uid] = $this->getDisplayName($uid);
|
2016-11-07 14:08:56 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 14:46:38 +00:00
|
|
|
return [
|
|
|
|
'type' => 'user',
|
|
|
|
'id' => $uid,
|
|
|
|
'name' => $this->displayNames[$uid],
|
|
|
|
];
|
2016-11-07 14:08:56 +00:00
|
|
|
}
|
|
|
|
|
2016-11-25 10:17:06 +00:00
|
|
|
/**
|
|
|
|
* @param string $email
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getContactName($email) {
|
|
|
|
$addressBookContacts = $this->contactsManager->search($email, ['EMAIL']);
|
|
|
|
|
|
|
|
foreach ($addressBookContacts as $contact) {
|
|
|
|
if (isset($contact['isLocalSystemBook'])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_array($email, $contact['EMAIL'])) {
|
|
|
|
return $contact['FN'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $email;
|
|
|
|
}
|
|
|
|
|
2016-11-07 14:08:56 +00:00
|
|
|
/**
|
2016-11-24 14:46:38 +00:00
|
|
|
* @param string $uid
|
|
|
|
* @return string
|
2016-11-07 14:08:56 +00:00
|
|
|
*/
|
2016-11-24 14:46:38 +00:00
|
|
|
protected function getDisplayName($uid) {
|
|
|
|
$user = $this->userManager->get($uid);
|
|
|
|
if ($user instanceof IUser) {
|
|
|
|
return $user->getDisplayName();
|
|
|
|
} else {
|
|
|
|
return $uid;
|
2016-11-07 14:08:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|