Fix files sharing legacy as well

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2017-02-08 11:55:50 +01:00
parent fb3821406a
commit 1badbe922e
No known key found for this signature in database
GPG Key ID: E166FD8976B3BAC8
7 changed files with 296 additions and 751 deletions

View File

@ -251,7 +251,7 @@ class Provider implements IProvider {
}
/**
* @param $parameter
* @param array|string $parameter
* @param IEvent|null $event
* @return array
* @throws \InvalidArgumentException
@ -276,6 +276,10 @@ class Provider implements IProvider {
];
}
/**
* @param string $uid
* @return array
*/
protected function getUser($uid) {
if (!isset($this->displayNames[$uid])) {
$this->displayNames[$uid] = $this->getDisplayName($uid);

View File

@ -0,0 +1,184 @@
<?php
/**
* @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
*
* @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\Activity\Providers;
use OCP\Activity\IEvent;
use OCP\Activity\IManager;
use OCP\Activity\IProvider;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\L10N\IFactory;
abstract class Base implements IProvider {
/** @var IFactory */
protected $languageFactory;
/** @var IL10N */
protected $l;
/** @var IURLGenerator */
protected $url;
/** @var IManager */
protected $activityManager;
/** @var IUserManager */
protected $userManager;
/** @var array */
protected $displayNames = [];
/**
* @param IFactory $languageFactory
* @param IURLGenerator $url
* @param IManager $activityManager
* @param IUserManager $userManager
*/
public function __construct(IFactory $languageFactory, IURLGenerator $url, IManager $activityManager, IUserManager $userManager) {
$this->languageFactory = $languageFactory;
$this->url = $url;
$this->activityManager = $activityManager;
$this->userManager = $userManager;
}
/**
* @param string $language
* @param IEvent $event
* @param IEvent|null $previousEvent
* @return IEvent
* @throws \InvalidArgumentException
* @since 11.0.0
*/
public function parse($language, IEvent $event, IEvent $previousEvent = null) {
if ($event->getApp() !== 'files_sharing') {
throw new \InvalidArgumentException();
}
$this->l = $this->languageFactory->get('files_sharing', $language);
if ($this->activityManager->isFormattingFilteredObject()) {
try {
return $this->parseShortVersion($event);
} catch (\InvalidArgumentException $e) {
// Ignore and simply use the long version...
}
}
return $this->parseLongVersion($event);
}
/**
* @param IEvent $event
* @return IEvent
* @throws \InvalidArgumentException
* @since 11.0.0
*/
abstract protected function parseShortVersion(IEvent $event);
/**
* @param IEvent $event
* @return IEvent
* @throws \InvalidArgumentException
* @since 11.0.0
*/
abstract protected function parseLongVersion(IEvent $event);
/**
* @param IEvent $event
* @param string $subject
* @param array $parameters
* @throws \InvalidArgumentException
*/
protected function setSubjects(IEvent $event, $subject, array $parameters) {
$placeholders = $replacements = [];
foreach ($parameters as $placeholder => $parameter) {
$placeholders[] = '{' . $placeholder . '}';
if ($parameter['type'] === 'file') {
$replacements[] = $parameter['path'];
} else {
$replacements[] = $parameter['name'];
}
}
$event->setParsedSubject(str_replace($placeholders, $replacements, $subject))
->setRichSubject($subject, $parameters);
}
/**
* @param array|string $parameter
* @param IEvent|null $event
* @return array
* @throws \InvalidArgumentException
*/
protected function getFile($parameter, IEvent $event = null) {
if (is_array($parameter)) {
$path = reset($parameter);
$id = (string) key($parameter);
} else if ($event !== null) {
// Legacy from before ownCloud 8.2
$path = $parameter;
$id = $event->getObjectId();
} else {
throw new \InvalidArgumentException('Could not generate file parameter');
}
return [
'type' => 'file',
'id' => $id,
'name' => basename($path),
'path' => trim($path, '/'),
'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]),
];
}
/**
* @param string $uid
* @return array
*/
protected function getUser($uid) {
if (!isset($this->displayNames[$uid])) {
$this->displayNames[$uid] = $this->getDisplayName($uid);
}
return [
'type' => 'user',
'id' => $uid,
'name' => $this->displayNames[$uid],
];
}
/**
* @param string $uid
* @return string
*/
protected function getDisplayName($uid) {
$user = $this->userManager->get($uid);
if ($user instanceof IUser) {
return $user->getDisplayName();
} else {
return $uid;
}
}
}

View File

@ -22,25 +22,9 @@
namespace OCA\Files_Sharing\Activity\Providers;
use OCP\Activity\IEvent;
use OCP\Activity\IManager;
use OCP\Activity\IProvider;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\L10N\IFactory;
class Downloads implements IProvider {
class Downloads extends Base {
/** @var IFactory */
protected $languageFactory;
/** @var IL10N */
protected $l;
/** @var IURLGenerator */
protected $url;
/** @var IManager */
protected $activityManager;
const SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED = 'public_shared_file_downloaded';
const SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED = 'public_shared_folder_downloaded';
@ -48,43 +32,6 @@ class Downloads implements IProvider {
const SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED = 'file_shared_with_email_downloaded';
const SUBJECT_SHARED_FOLDER_BY_EMAIL_DOWNLOADED = 'folder_shared_with_email_downloaded';
/**
* @param IFactory $languageFactory
* @param IURLGenerator $url
* @param IManager $activityManager
*/
public function __construct(IFactory $languageFactory, IURLGenerator $url, IManager $activityManager) {
$this->languageFactory = $languageFactory;
$this->url = $url;
$this->activityManager = $activityManager;
}
/**
* @param string $language
* @param IEvent $event
* @param IEvent|null $previousEvent
* @return IEvent
* @throws \InvalidArgumentException
* @since 11.0.0
*/
public function parse($language, IEvent $event, IEvent $previousEvent = null) {
if ($event->getApp() !== 'files_sharing') {
throw new \InvalidArgumentException();
}
$this->l = $this->languageFactory->get('files_sharing', $language);
if ($this->activityManager->isFormattingFilteredObject()) {
try {
return $this->parseShortVersion($event);
} catch (\InvalidArgumentException $e) {
// Ignore and simply use the long version...
}
}
return $this->parseLongVersion($event);
}
/**
* @param IEvent $event
* @return IEvent
@ -96,19 +43,17 @@ class Downloads implements IProvider {
if ($event->getSubject() === self::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED ||
$event->getSubject() === self::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED) {
$event->setParsedSubject($this->l->t('Downloaded via public link'))
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/download.svg')));
$subject = $this->l->t('Downloaded via public link');
} else if ($event->getSubject() === self::SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED ||
$event->getSubject() === self::SUBJECT_SHARED_FOLDER_BY_EMAIL_DOWNLOADED) {
$event->setParsedSubject($this->l->t('Downloaded by %1$s', $parsedParameters['email']['name']))
->setRichSubject($this->l->t('Downloaded by {email}'), [
'email' => $parsedParameters['email'],
])
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/download.svg')));
$subject = $this->l->t('Downloaded by {email}');
} else {
throw new \InvalidArgumentException();
}
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/download.svg')));
$this->setSubjects($event, $subject, $parsedParameters);
return $event;
}
@ -123,34 +68,24 @@ class Downloads implements IProvider {
if ($event->getSubject() === self::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED ||
$event->getSubject() === self::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED) {
$event->setParsedSubject($this->l->t('%1$s downloaded via public link', [
$parsedParameters['file']['path'],
]))
->setRichSubject($this->l->t('{file} downloaded via public link'), [
'file' => $parsedParameters['file'],
])
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/download.svg')));
$subject = $this->l->t('{file} downloaded via public link');
} else if ($event->getSubject() === self::SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED ||
$event->getSubject() === self::SUBJECT_SHARED_FOLDER_BY_EMAIL_DOWNLOADED) {
$event->setParsedSubject($this->l->t('%1$s downloaded %2$s', [
$parsedParameters['email']['name'],
$parsedParameters['file']['path'],
]))
->setRichSubject($this->l->t('{email} downloaded {file}'), [
'email' => $parsedParameters['email'],
'file' => $parsedParameters['file'],
])
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/download.svg')));
$subject = $this->l->t('{email} downloaded {file}');
} else {
throw new \InvalidArgumentException();
}
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/download.svg')));
$this->setSubjects($event, $subject, $parsedParameters);
return $event;
}
/**
* @param IEvent $event
* @return array
* @throws \InvalidArgumentException
*/
protected function getParsedParameters(IEvent $event) {
$subject = $event->getSubject();
@ -160,12 +95,12 @@ class Downloads implements IProvider {
case self::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED:
case self::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED:
return [
'file' => $this->generateFileParameter($event->getObjectId(), $parameters[0]),
'file' => $this->getFile($parameters[0], $event),
];
case self::SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED:
case self::SUBJECT_SHARED_FOLDER_BY_EMAIL_DOWNLOADED:
return [
'file' => $this->generateFileParameter($event->getObjectId(), $parameters[0]),
'file' => $this->getFile($parameters[0], $event),
'email' => [
'type' => 'email',
'id' => $parameters[1],
@ -176,19 +111,4 @@ class Downloads implements IProvider {
throw new \InvalidArgumentException();
}
/**
* @param int $id
* @param string $path
* @return array
*/
protected function generateFileParameter($id, $path) {
return [
'type' => 'file',
'id' => $id,
'name' => basename($path),
'path' => $path,
'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]),
];
}
}

View File

@ -22,79 +22,14 @@
namespace OCA\Files_Sharing\Activity\Providers;
use OCP\Activity\IEvent;
use OCP\Activity\IManager;
use OCP\Activity\IProvider;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\L10N\IFactory;
class Groups implements IProvider {
/** @var IFactory */
protected $languageFactory;
/** @var IL10N */
protected $l;
/** @var IURLGenerator */
protected $url;
/** @var IManager */
protected $activityManager;
/** @var IUserManager */
protected $userManager;
/** @var array */
protected $displayNames = [];
class Groups extends Base {
const SUBJECT_SHARED_GROUP_SELF = 'shared_group_self';
const SUBJECT_RESHARED_GROUP_BY = 'reshared_group_by';
const SUBJECT_UNSHARED_GROUP_SELF = 'unshared_group_self';
const SUBJECT_UNSHARED_GROUP_BY = 'unshared_group_by';
/**
* @param IFactory $languageFactory
* @param IURLGenerator $url
* @param IManager $activityManager
* @param IUserManager $userManager
*/
public function __construct(IFactory $languageFactory, IURLGenerator $url, IManager $activityManager, IUserManager $userManager) {
$this->languageFactory = $languageFactory;
$this->url = $url;
$this->activityManager = $activityManager;
$this->userManager = $userManager;
}
/**
* @param string $language
* @param IEvent $event
* @param IEvent|null $previousEvent
* @return IEvent
* @throws \InvalidArgumentException
* @since 11.0.0
*/
public function parse($language, IEvent $event, IEvent $previousEvent = null) {
if ($event->getApp() !== 'files_sharing') {
throw new \InvalidArgumentException();
}
$this->l = $this->languageFactory->get('files_sharing', $language);
if ($this->activityManager->isFormattingFilteredObject()) {
try {
return $this->parseShortVersion($event);
} catch (\InvalidArgumentException $e) {
// Ignore and simply use the long version...
}
}
return $this->parseLongVersion($event);
}
/**
* @param IEvent $event
* @return IEvent
@ -105,42 +40,20 @@ class Groups implements IProvider {
$parsedParameters = $this->getParsedParameters($event);
if ($event->getSubject() === self::SUBJECT_SHARED_GROUP_SELF) {
$event->setParsedSubject($this->l->t('Shared with group %1$s', [$parsedParameters['group']['name']]))
->setRichSubject($this->l->t('Shared with group {group}'), [
'group' => $parsedParameters['group'],
])
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$subject = $this->l->t('Shared with group {group}');
} else if ($event->getSubject() === self::SUBJECT_UNSHARED_GROUP_SELF) {
$event->setParsedSubject($this->l->t('Removed share for group %1$s', [$parsedParameters['group']['name']]))
->setRichSubject($this->l->t('Removed share for group {group}'), [
'group' => $parsedParameters['group'],
])
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$subject = $this->l->t('Removed share for group {group}');
} else if ($event->getSubject() === self::SUBJECT_RESHARED_GROUP_BY) {
$event->setParsedSubject($this->l->t('%2$s shared with group %1$s', [
$parsedParameters['group']['name'],
$parsedParameters['actor']['name'],
]))
->setRichSubject($this->l->t('{actor} shared with group {group}'), [
'group' => $parsedParameters['group'],
'actor' => $parsedParameters['actor'],
])
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$subject = $this->l->t('{actor} shared with group {group}');
} else if ($event->getSubject() === self::SUBJECT_UNSHARED_GROUP_BY) {
$event->setParsedSubject($this->l->t('%2$s removed share for group %1$s', [
$parsedParameters['group']['name'],
$parsedParameters['actor']['name'],
]))
->setRichSubject($this->l->t('{actor} removed share for group {group}'), [
'group' => $parsedParameters['group'],
'actor' => $parsedParameters['actor'],
])
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$subject = $this->l->t('{actor} removed share for group {group}');
} else {
throw new \InvalidArgumentException();
}
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$this->setSubjects($event, $subject, $parsedParameters);
return $event;
}
@ -154,39 +67,20 @@ class Groups implements IProvider {
$parsedParameters = $this->getParsedParameters($event);
if ($event->getSubject() === self::SUBJECT_SHARED_GROUP_SELF) {
$event->setParsedSubject($this->l->t('You shared %1$s with group %2$s', [
$parsedParameters['file']['path'],
$parsedParameters['group']['name'],
]))
->setRichSubject($this->l->t('You shared {file} with group {group}'), $parsedParameters)
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$subject = $this->l->t('You shared {file} with group {group}');
} else if ($event->getSubject() === self::SUBJECT_UNSHARED_GROUP_SELF) {
$event->setParsedSubject($this->l->t('You removed group %2$s from %1$s', [
$parsedParameters['file']['path'],
$parsedParameters['group']['name'],
]))
->setRichSubject($this->l->t('You removed group {group} from {file}'), $parsedParameters)
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$subject = $this->l->t('You removed group {group} from {file}');
} else if ($event->getSubject() === self::SUBJECT_RESHARED_GROUP_BY) {
$event->setParsedSubject($this->l->t('%3$s shared %1$s with group %2$s', [
$parsedParameters['file']['path'],
$parsedParameters['group']['name'],
$parsedParameters['actor']['name'],
]))
->setRichSubject($this->l->t('{actor} shared {file} with group {group}'), $parsedParameters)
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$subject = $this->l->t('{actor} shared {file} with group {group}');
} else if ($event->getSubject() === self::SUBJECT_UNSHARED_GROUP_BY) {
$event->setParsedSubject($this->l->t('%3$s removed group %2$s from %1$s', [
$parsedParameters['file']['path'],
$parsedParameters['group']['name'],
$parsedParameters['actor']['name'],
]))
->setRichSubject($this->l->t('{actor} removed group {group} from {file}'), $parsedParameters)
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$subject = $this->l->t('{actor} removed group {group} from {file}');
} else {
throw new \InvalidArgumentException();
}
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$this->setSubjects($event, $subject, $parsedParameters);
return $event;
}
@ -197,21 +91,19 @@ class Groups implements IProvider {
switch ($subject) {
case self::SUBJECT_RESHARED_GROUP_BY:
case self::SUBJECT_UNSHARED_GROUP_BY:
$id = key($parameters[0]);
return [
'file' => $this->generateFileParameter($id, $parameters[0][$id]),
'file' => $this->getFile($parameters[0], $event),
'group' => [
'type' => 'group',
'id' => $parameters[2],
'name' => $parameters[2],
],
'actor' => $this->generateUserParameter($parameters[1]),
'actor' => $this->getUser($parameters[1]),
];
case self::SUBJECT_SHARED_GROUP_SELF:
case self::SUBJECT_UNSHARED_GROUP_SELF:
$id = key($parameters[0]);
return [
'file' => $this->generateFileParameter($id, $parameters[0][$id]),
'file' => $this->getFile($parameters[0], $event),
'group' => [
'type' => 'group',
'id' => $parameters[1],
@ -221,48 +113,4 @@ class Groups implements IProvider {
}
return [];
}
/**
* @param int $id
* @param string $path
* @return array
*/
protected function generateFileParameter($id, $path) {
return [
'type' => 'file',
'id' => $id,
'name' => basename($path),
'path' => $path,
'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]),
];
}
/**
* @param string $uid
* @return array
*/
protected function generateUserParameter($uid) {
if (!isset($this->displayNames[$uid])) {
$this->displayNames[$uid] = $this->getDisplayName($uid);
}
return [
'type' => 'user',
'id' => $uid,
'name' => $this->displayNames[$uid],
];
}
/**
* @param string $uid
* @return string
*/
protected function getDisplayName($uid) {
$user = $this->userManager->get($uid);
if ($user instanceof IUser) {
return $user->getDisplayName();
} else {
return $uid;
}
}
}

View File

@ -22,33 +22,8 @@
namespace OCA\Files_Sharing\Activity\Providers;
use OCP\Activity\IEvent;
use OCP\Activity\IManager;
use OCP\Activity\IProvider;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\L10N\IFactory;
class PublicLinks implements IProvider {
/** @var IFactory */
protected $languageFactory;
/** @var IL10N */
protected $l;
/** @var IURLGenerator */
protected $url;
/** @var IManager */
protected $activityManager;
/** @var IUserManager */
protected $userManager;
/** @var array */
protected $displayNames = [];
class PublicLinks extends Base {
const SUBJECT_SHARED_LINK_SELF = 'shared_link_self';
const SUBJECT_RESHARED_LINK_BY = 'reshared_link_by';
@ -57,46 +32,6 @@ class PublicLinks implements IProvider {
const SUBJECT_LINK_EXPIRED = 'link_expired';
const SUBJECT_LINK_BY_EXPIRED = 'link_by_expired';
/**
* @param IFactory $languageFactory
* @param IURLGenerator $url
* @param IManager $activityManager
* @param IUserManager $userManager
*/
public function __construct(IFactory $languageFactory, IURLGenerator $url, IManager $activityManager, IUserManager $userManager) {
$this->languageFactory = $languageFactory;
$this->url = $url;
$this->activityManager = $activityManager;
$this->userManager = $userManager;
}
/**
* @param string $language
* @param IEvent $event
* @param IEvent|null $previousEvent
* @return IEvent
* @throws \InvalidArgumentException
* @since 11.0.0
*/
public function parse($language, IEvent $event, IEvent $previousEvent = null) {
if ($event->getApp() !== 'files_sharing') {
throw new \InvalidArgumentException();
}
$this->l = $this->languageFactory->get('files_sharing', $language);
if ($this->activityManager->isFormattingFilteredObject()) {
try {
return $this->parseShortVersion($event);
} catch (\InvalidArgumentException $e) {
// Ignore and simply use the long version...
}
}
return $this->parseLongVersion($event);
}
/**
* @param IEvent $event
* @return IEvent
@ -107,46 +42,25 @@ class PublicLinks implements IProvider {
$parsedParameters = $this->getParsedParameters($event);
if ($event->getSubject() === self::SUBJECT_SHARED_LINK_SELF) {
$event->setParsedSubject($this->l->t('Shared as public link'))
->setRichSubject($this->l->t('Shared as public link'))
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$subject = $this->l->t('Shared as public link');
} else if ($event->getSubject() === self::SUBJECT_UNSHARED_LINK_SELF) {
$event->setParsedSubject($this->l->t('Removed public link'))
->setRichSubject($this->l->t('Removed public link'))
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
// } else if ($event->getSubject() === self::SUBJECT_LINK_EXPIRED) {
// $event->setParsedSubject($this->l->t('Public link expired'))
// ->setRichSubject($this->l->t('Public link expired'))
// ->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$subject = $this->l->t('Removed public link');
} else if ($event->getSubject() === self::SUBJECT_LINK_EXPIRED) {
$subject = $this->l->t('Public link expired');
} else if ($event->getSubject() === self::SUBJECT_RESHARED_LINK_BY) {
$event->setParsedSubject($this->l->t('%1$s shared as public link', [
$parsedParameters['actor']['name'],
]))
->setRichSubject($this->l->t('{actor} shared as public link'), [
'actor' => $parsedParameters['actor'],
])
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$subject = $this->l->t('{actor} shared as public link');
} else if ($event->getSubject() === self::SUBJECT_UNSHARED_LINK_BY) {
$event->setParsedSubject($this->l->t('%1$s removed public link', [
$parsedParameters['actor']['name'],
]))
->setRichSubject($this->l->t('{actor} removed public link'), [
'actor' => $parsedParameters['actor'],
])
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
// } else if ($event->getSubject() === self::SUBJECT_LINK_BY_EXPIRED) {
// $event->setParsedSubject($this->l->t('Public link of %1$s expired', [
// $parsedParameters['actor']['name'],
// ]))
// ->setRichSubject($this->l->t('Public link of {actor} expired',[
// 'actor' => $parsedParameters['actor'],
// ]))
// ->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$subject = $this->l->t('{actor} removed public link');
} else if ($event->getSubject() === self::SUBJECT_LINK_BY_EXPIRED) {
$subject = $this->l->t('Public link of {actor} expired');
} else {
throw new \InvalidArgumentException();
}
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$this->setSubjects($event, $subject, $parsedParameters);
return $event;
}
@ -160,48 +74,25 @@ class PublicLinks implements IProvider {
$parsedParameters = $this->getParsedParameters($event);
if ($event->getSubject() === self::SUBJECT_SHARED_LINK_SELF) {
$event->setParsedSubject($this->l->t('You shared %1$s as public link', [
$parsedParameters['file']['path'],
]))
->setRichSubject($this->l->t('You shared {file} as public link'), $parsedParameters)
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$subject = $this->l->t('You shared {file} as public link');
} else if ($event->getSubject() === self::SUBJECT_UNSHARED_LINK_SELF) {
$event->setParsedSubject($this->l->t('You removed public link for %1$s', [
$parsedParameters['file']['path'],
]))
->setRichSubject($this->l->t('You removed public link for {file}'), $parsedParameters)
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
// } else if ($event->getSubject() === self::SUBJECT_LINK_EXPIRED) {
// $event->setParsedSubject($this->l->t('Public link expired'))
// ->setRichSubject($this->l->t('Public link expired'))
// ->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$subject = $this->l->t('You removed public link for {file}');
} else if ($event->getSubject() === self::SUBJECT_LINK_EXPIRED) {
$subject = $this->l->t('Public link expired for {file}');
} else if ($event->getSubject() === self::SUBJECT_RESHARED_LINK_BY) {
$event->setParsedSubject($this->l->t('%2$s shared %1$s as public link', [
$parsedParameters['file']['path'],
$parsedParameters['actor']['name'],
]))
->setRichSubject($this->l->t('{actor} shared {file} as public link'), $parsedParameters)
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$subject = $this->l->t('{actor} shared {file} as public link');
} else if ($event->getSubject() === self::SUBJECT_UNSHARED_LINK_BY) {
$event->setParsedSubject($this->l->t('%2$s removed public link for %1$s', [
$parsedParameters['file']['path'],
$parsedParameters['actor']['name'],
]))
->setRichSubject($this->l->t('{actor} removed public link for {file}'), $parsedParameters)
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
// } else if ($event->getSubject() === self::SUBJECT_LINK_BY_EXPIRED) {
// $event->setParsedSubject($this->l->t('Public link of %1$s expired', [
// $parsedParameters['actor']['name'],
// ]))
// ->setRichSubject($this->l->t('Public link of {actor} expired',[
// 'actor' => $parsedParameters['actor'],
// ]))
// ->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$subject = $this->l->t('{actor} removed public link for {file}');
} else if ($event->getSubject() === self::SUBJECT_LINK_BY_EXPIRED) {
$subject = $this->l->t('Public link of {actor} for {file} expired');
} else {
throw new \InvalidArgumentException();
}
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$this->setSubjects($event, $subject, $parsedParameters);
return $event;
}
@ -213,63 +104,17 @@ class PublicLinks implements IProvider {
case self::SUBJECT_SHARED_LINK_SELF:
case self::SUBJECT_UNSHARED_LINK_SELF:
case self::SUBJECT_LINK_EXPIRED:
$id = key($parameters[0]);
return [
'file' => $this->generateFileParameter($id, $parameters[0][$id]),
'file' => $this->getFile($parameters[0], $event),
];
case self::SUBJECT_RESHARED_LINK_BY:
case self::SUBJECT_UNSHARED_LINK_BY:
case self::SUBJECT_LINK_BY_EXPIRED:
$id = key($parameters[0]);
return [
'file' => $this->generateFileParameter($id, $parameters[0][$id]),
'actor' => $this->generateUserParameter($parameters[1]),
'file' => $this->getFile($parameters[0], $event),
'actor' => $this->getUser($parameters[1]),
];
}
return [];
}
/**
* @param int $id
* @param string $path
* @return array
*/
protected function generateFileParameter($id, $path) {
return [
'type' => 'file',
'id' => $id,
'name' => basename($path),
'path' => $path,
'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]),
];
}
/**
* @param string $uid
* @return array
*/
protected function generateUserParameter($uid) {
if (!isset($this->displayNames[$uid])) {
$this->displayNames[$uid] = $this->getDisplayName($uid);
}
return [
'type' => 'user',
'id' => $uid,
'name' => $this->displayNames[$uid],
];
}
/**
* @param string $uid
* @return string
*/
protected function getDisplayName($uid) {
$user = $this->userManager->get($uid);
if ($user instanceof IUser) {
return $user->getDisplayName();
} else {
return $uid;
}
}
}

View File

@ -28,62 +28,13 @@ use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\L10N\IFactory;
class RemoteShares implements IProvider {
/** @var IFactory */
protected $languageFactory;
/** @var IL10N */
protected $l;
/** @var IURLGenerator */
protected $url;
/** @var IManager */
protected $activityManager;
class RemoteShares extends Base {
const SUBJECT_REMOTE_SHARE_ACCEPTED = 'remote_share_accepted';
const SUBJECT_REMOTE_SHARE_DECLINED = 'remote_share_declined';
const SUBJECT_REMOTE_SHARE_RECEIVED = 'remote_share_received';
const SUBJECT_REMOTE_SHARE_UNSHARED = 'remote_share_unshared';
/**
* @param IFactory $languageFactory
* @param IURLGenerator $url
* @param IManager $activityManager
*/
public function __construct(IFactory $languageFactory, IURLGenerator $url, IManager $activityManager) {
$this->languageFactory = $languageFactory;
$this->url = $url;
$this->activityManager = $activityManager;
}
/**
* @param string $language
* @param IEvent $event
* @param IEvent|null $previousEvent
* @return IEvent
* @throws \InvalidArgumentException
* @since 11.0.0
*/
public function parse($language, IEvent $event, IEvent $previousEvent = null) {
if ($event->getApp() !== 'files_sharing') {
throw new \InvalidArgumentException();
}
$this->l = $this->languageFactory->get('files_sharing', $language);
if ($this->activityManager->isFormattingFilteredObject()) {
try {
return $this->parseShortVersion($event);
} catch (\InvalidArgumentException $e) {
// Ignore and simply use the long version...
}
}
return $this->parseLongVersion($event);
}
/**
* @param IEvent $event
* @return IEvent
@ -94,25 +45,16 @@ class RemoteShares implements IProvider {
$parsedParameters = $this->getParsedParameters($event);
if ($event->getSubject() === self::SUBJECT_REMOTE_SHARE_ACCEPTED) {
$event->setParsedSubject($this->l->t('%1$s accepted the remote share', [
$parsedParameters['user']['name'],
]))
->setRichSubject($this->l->t('{user} accepted the remote share'), [
'user' => $parsedParameters['user']
])
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$subject = $this->l->t('{user} accepted the remote share');
} else if ($event->getSubject() === self::SUBJECT_REMOTE_SHARE_DECLINED) {
$event->setParsedSubject($this->l->t('%1$s declined the remote share', [
$parsedParameters['user']['name'],
]))
->setRichSubject($this->l->t('{user} declined the remote share'), [
'user' => $parsedParameters['user']
])
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$subject = $this->l->t('{user} declined the remote share');
} else {
throw new \InvalidArgumentException();
}
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$this->setSubjects($event, $subject, $parsedParameters);
return $event;
}
@ -126,37 +68,20 @@ class RemoteShares implements IProvider {
$parsedParameters = $this->getParsedParameters($event);
if ($event->getSubject() === self::SUBJECT_REMOTE_SHARE_RECEIVED) {
$event->setParsedSubject($this->l->t('You received a new remote share %1$s from %2$s', [
$parsedParameters['file']['name'],
$parsedParameters['user']['name'],
]))
->setRichSubject($this->l->t('You received a new remote share {file} from {user}'), $parsedParameters)
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$subject = $this->l->t('You received a new remote share {file} from {user}');
} else if ($event->getSubject() === self::SUBJECT_REMOTE_SHARE_ACCEPTED) {
$event->setParsedSubject($this->l->t('%2$s accepted the remote share of %1$s', [
$parsedParameters['file']['name'],
$parsedParameters['user']['name'],
]))
->setRichSubject($this->l->t('{user} accepted the remote share of {file}'), $parsedParameters)
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$subject = $this->l->t('{user} accepted the remote share of {file}');
} else if ($event->getSubject() === self::SUBJECT_REMOTE_SHARE_DECLINED) {
$event->setParsedSubject($this->l->t('%2$s declined the remote share of %1$s', [
$parsedParameters['file']['name'],
$parsedParameters['user']['name'],
]))
->setRichSubject($this->l->t('{user} declined the remote share of {file}'), $parsedParameters)
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$subject = $this->l->t('{user} declined the remote share of {file}');
} else if ($event->getSubject() === self::SUBJECT_REMOTE_SHARE_UNSHARED) {
$event->setParsedSubject($this->l->t('%2$s unshared %1$s from you', [
$parsedParameters['file']['name'],
$parsedParameters['user']['name'],
]))
->setRichSubject($this->l->t('{user} unshared {file} from you'), $parsedParameters)
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$subject = $this->l->t('{user} unshared {file} from you');
} else {
throw new \InvalidArgumentException();
}
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$this->setSubjects($event, $subject, $parsedParameters);
return $event;
}
@ -167,48 +92,35 @@ class RemoteShares implements IProvider {
switch ($subject) {
case self::SUBJECT_REMOTE_SHARE_RECEIVED:
case self::SUBJECT_REMOTE_SHARE_UNSHARED:
$remoteUser = explode('@', $parameters[0], 2);
return [
'file' => [
'type' => 'pending-federated-share',
'id' => $parameters[1],
'name' => $parameters[1],
],
'user' => [
'type' => 'user',
'id' => $remoteUser[0],
'name' => $parameters[0],// Todo display name from contacts
'server' => $remoteUser[1],
],
'user' => $this->getFederatedUser($parameters[0]),
];
case self::SUBJECT_REMOTE_SHARE_ACCEPTED:
case self::SUBJECT_REMOTE_SHARE_DECLINED:
$remoteUser = explode('@', $parameters[0], 2);
return [
'file' => $this->generateFileParameter($event->getObjectId(), $event->getObjectName()),
'user' => [
'type' => 'user',
'id' => $remoteUser[0],
'name' => $parameters[0],// Todo display name from contacts
'server' => $remoteUser[1],
],
'file' => $this->getFile([$event->getObjectId() => $event->getObjectName()]),
'user' => $this->getFederatedUser($parameters[0]),
];
}
throw new \InvalidArgumentException();
}
/**
* @param int $id
* @param string $path
* @param string $cloudId
* @return array
*/
protected function generateFileParameter($id, $path) {
protected function getFederatedUser($cloudId) {
$remoteUser = explode('@', $cloudId, 2);
return [
'type' => 'file',
'id' => $id,
'name' => basename($path),
'path' => $path,
'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]),
'type' => 'user',
'id' => $remoteUser[0],
'name' => $cloudId,// Todo display name from contacts
'server' => $remoteUser[1],
];
}
}

View File

@ -22,33 +22,9 @@
namespace OCA\Files_Sharing\Activity\Providers;
use OCP\Activity\IEvent;
use OCP\Activity\IManager;
use OCP\Activity\IProvider;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\L10N\IFactory;
class Users implements IProvider {
class Users extends Base {
/** @var IFactory */
protected $languageFactory;
/** @var IL10N */
protected $l;
/** @var IURLGenerator */
protected $url;
/** @var IManager */
protected $activityManager;
/** @var IUserManager */
protected $userManager;
/** @var array */
protected $displayNames = [];
const SUBJECT_SHARED_USER_SELF = 'shared_user_self';
const SUBJECT_RESHARED_USER_BY = 'reshared_user_by';
@ -58,45 +34,6 @@ class Users implements IProvider {
const SUBJECT_SHARED_WITH_BY = 'shared_with_by';
const SUBJECT_UNSHARED_BY = 'unshared_by';
/**
* @param IFactory $languageFactory
* @param IURLGenerator $url
* @param IManager $activityManager
* @param IUserManager $userManager
*/
public function __construct(IFactory $languageFactory, IURLGenerator $url, IManager $activityManager, IUserManager $userManager) {
$this->languageFactory = $languageFactory;
$this->url = $url;
$this->activityManager = $activityManager;
$this->userManager = $userManager;
}
/**
* @param string $language
* @param IEvent $event
* @param IEvent|null $previousEvent
* @return IEvent
* @throws \InvalidArgumentException
* @since 11.0.0
*/
public function parse($language, IEvent $event, IEvent $previousEvent = null) {
if ($event->getApp() !== 'files_sharing') {
throw new \InvalidArgumentException();
}
$this->l = $this->languageFactory->get('files_sharing', $language);
if ($this->activityManager->isFormattingFilteredObject()) {
try {
return $this->parseShortVersion($event);
} catch (\InvalidArgumentException $e) {
// Ignore and simply use the long version...
}
}
return $this->parseLongVersion($event);
}
/**
* @param IEvent $event
* @return IEvent
@ -107,54 +44,25 @@ class Users implements IProvider {
$parsedParameters = $this->getParsedParameters($event);
if ($event->getSubject() === self::SUBJECT_SHARED_USER_SELF) {
$event->setParsedSubject($this->l->t('Shared with %1$s', [$parsedParameters['user']['name']]))
->setRichSubject($this->l->t('Shared with {user}'), [
'user' => $parsedParameters['user'],
])
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$subject = $this->l->t('Shared with {user}');
} else if ($event->getSubject() === self::SUBJECT_UNSHARED_USER_SELF) {
$event->setParsedSubject($this->l->t('Removed share for %1$s', [$parsedParameters['user']['name']]))
->setRichSubject($this->l->t('Removed share for {user}'), [
'user' => $parsedParameters['user'],
])
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$subject = $this->l->t('Removed share for {user}');
} else if ($event->getSubject() === self::SUBJECT_RESHARED_USER_BY) {
$event->setParsedSubject($this->l->t('%2$s shared with %1$s', [
$parsedParameters['user']['name'],
$parsedParameters['actor']['name'],
]))
->setRichSubject($this->l->t('{actor} shared with {user}'), [
'user' => $parsedParameters['user'],
'actor' => $parsedParameters['actor'],
])
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$subject = $this->l->t('{actor} shared with {user}');
} else if ($event->getSubject() === self::SUBJECT_UNSHARED_USER_BY) {
$event->setParsedSubject($this->l->t('%2$s removed share for %1$s', [
$parsedParameters['user']['name'],
$parsedParameters['actor']['name'],
]))
->setRichSubject($this->l->t('{actor} removed share for {user}'), [
'user' => $parsedParameters['user'],
'actor' => $parsedParameters['actor'],
])
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$subject = $this->l->t('{actor} removed share for {user}');
} else if ($event->getSubject() === self::SUBJECT_SHARED_WITH_BY) {
$event->setParsedSubject($this->l->t('Shared by %1$s', [$parsedParameters['actor']['name']]))
->setRichSubject($this->l->t('Shared by {actor}'), [
'actor' => $parsedParameters['actor'],
])
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$subject = $this->l->t('Shared by {actor}');
} else if ($event->getSubject() === self::SUBJECT_UNSHARED_BY) {
$event->setParsedSubject($this->l->t('%1$s removed share', [$parsedParameters['actor']['name']]))
->setRichSubject($this->l->t('{actor} removed share'), [
'actor' => $parsedParameters['actor'],
])
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$subject = $this->l->t('{actor} removed share');
} else {
throw new \InvalidArgumentException();
}
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$this->setSubjects($event, $subject, $parsedParameters);
return $event;
}
@ -168,54 +76,25 @@ class Users implements IProvider {
$parsedParameters = $this->getParsedParameters($event);
if ($event->getSubject() === self::SUBJECT_SHARED_USER_SELF) {
$event->setParsedSubject($this->l->t('You shared %1$s with %2$s', [
$parsedParameters['file']['path'],
$parsedParameters['user']['name'],
]))
->setRichSubject($this->l->t('You shared {file} with {user}'), $parsedParameters)
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$subject = $this->l->t('You shared {file} with {user}');
} else if ($event->getSubject() === self::SUBJECT_UNSHARED_USER_SELF) {
$event->setParsedSubject($this->l->t('You removed %2$s from %1$s', [
$parsedParameters['file']['path'],
$parsedParameters['user']['name'],
]))
->setRichSubject($this->l->t('You removed {user} from {file}'), $parsedParameters)
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$subject = $this->l->t('You removed {user} from {file}');
} else if ($event->getSubject() === self::SUBJECT_RESHARED_USER_BY) {
$event->setParsedSubject($this->l->t('%3$s shared %1$s with %2$s', [
$parsedParameters['file']['path'],
$parsedParameters['user']['name'],
$parsedParameters['actor']['name'],
]))
->setRichSubject($this->l->t('{actor} removed {user} from {file}'), $parsedParameters)
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$subject = $this->l->t('{actor} removed {user} from {file}');
} else if ($event->getSubject() === self::SUBJECT_UNSHARED_USER_BY) {
$event->setParsedSubject($this->l->t('%3$s removed %2$s from %1$s', [
$parsedParameters['file']['path'],
$parsedParameters['user']['name'],
$parsedParameters['actor']['name'],
]))
->setRichSubject($this->l->t('{actor} removed {user} from {file}'), $parsedParameters)
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$subject = $this->l->t('{actor} removed {user} from {file}');
} else if ($event->getSubject() === self::SUBJECT_SHARED_WITH_BY) {
$event->setParsedSubject($this->l->t('%2$s shared %1$s with you', [
$parsedParameters['file']['path'],
$parsedParameters['actor']['name'],
]))
->setRichSubject($this->l->t('{actor} shared {file} with you'), $parsedParameters)
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$subject = $this->l->t('{actor} shared {file} with you');
} else if ($event->getSubject() === self::SUBJECT_UNSHARED_BY) {
$event->setParsedSubject($this->l->t('%2$s removed you from %1$s', [
$parsedParameters['file']['path'],
$parsedParameters['actor']['name'],
]))
->setRichSubject($this->l->t('{actor} removed you from {file}'), $parsedParameters)
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$subject = $this->l->t('{actor} removed you from {file}');
} else {
throw new \InvalidArgumentException();
}
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
$this->setSubjects($event, $subject, $parsedParameters);
return $event;
}
@ -226,71 +105,24 @@ class Users implements IProvider {
switch ($subject) {
case self::SUBJECT_SHARED_USER_SELF:
case self::SUBJECT_UNSHARED_USER_SELF:
$id = key($parameters[0]);
return [
'file' => $this->generateFileParameter($id, $parameters[0][$id]),
'user' => $this->generateUserParameter($parameters[1]),
'file' => $this->getFile($parameters[0], $event),
'user' => $this->getUser($parameters[1]),
];
case self::SUBJECT_SHARED_WITH_BY:
case self::SUBJECT_UNSHARED_BY:
$id = key($parameters[0]);
return [
'file' => $this->generateFileParameter($id, $parameters[0][$id]),
'actor' => $this->generateUserParameter($parameters[1]),
'file' => $this->getFile($parameters[0], $event),
'actor' => $this->getUser($parameters[1]),
];
case self::SUBJECT_RESHARED_USER_BY:
case self::SUBJECT_UNSHARED_USER_BY:
$id = key($parameters[0]);
return [
'file' => $this->generateFileParameter($id, $parameters[0][$id]),
'user' => $this->generateUserParameter($parameters[2]),
'actor' => $this->generateUserParameter($parameters[1]),
'file' => $this->getFile($parameters[0], $event),
'user' => $this->getUser($parameters[2]),
'actor' => $this->getUser($parameters[1]),
];
}
return [];
}
/**
* @param int $id
* @param string $path
* @return array
*/
protected function generateFileParameter($id, $path) {
return [
'type' => 'file',
'id' => $id,
'name' => basename($path),
'path' => $path,
'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]),
];
}
/**
* @param string $uid
* @return array
*/
protected function generateUserParameter($uid) {
if (!isset($this->displayNames[$uid])) {
$this->displayNames[$uid] = $this->getDisplayName($uid);
}
return [
'type' => 'user',
'id' => $uid,
'name' => $this->displayNames[$uid],
];
}
/**
* @param string $uid
* @return string
*/
protected function getDisplayName($uid) {
$user = $this->userManager->get($uid);
if ($user instanceof IUser) {
return $user->getDisplayName();
} else {
return $uid;
}
}
}