2016-11-04 13:34:42 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-11-16 14:19:42 +03:00
|
|
|
* @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
|
2016-11-04 13:34:42 +03:00
|
|
|
*
|
2016-11-16 14:19:42 +03:00
|
|
|
* @license GNU AGPL version 3 or any later version
|
2016-11-04 13:34:42 +03:00
|
|
|
*
|
2016-11-16 14:19:42 +03:00
|
|
|
* 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.
|
2016-11-04 13:34:42 +03:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2016-11-16 14:19:42 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2016-11-04 13:34:42 +03:00
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
2016-11-16 14:19:42 +03:00
|
|
|
* 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/>.
|
2016-11-04 13:34:42 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Files\Activity;
|
|
|
|
|
|
|
|
use OCP\Activity\IEvent;
|
2016-11-25 17:21:16 +03:00
|
|
|
use OCP\Activity\IEventMerger;
|
2016-11-10 18:02:19 +03:00
|
|
|
use OCP\Activity\IManager;
|
2016-11-04 13:34:42 +03:00
|
|
|
use OCP\Activity\IProvider;
|
|
|
|
use OCP\IL10N;
|
|
|
|
use OCP\IURLGenerator;
|
2016-11-22 18:39:29 +03:00
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\IUserManager;
|
2016-12-01 13:35:23 +03:00
|
|
|
use OCP\L10N\IFactory;
|
2016-11-04 13:34:42 +03:00
|
|
|
|
|
|
|
class Provider implements IProvider {
|
|
|
|
|
2016-12-01 13:35:23 +03:00
|
|
|
/** @var IFactory */
|
|
|
|
protected $languageFactory;
|
|
|
|
|
2016-11-04 13:34:42 +03:00
|
|
|
/** @var IL10N */
|
|
|
|
protected $l;
|
|
|
|
|
|
|
|
/** @var IURLGenerator */
|
|
|
|
protected $url;
|
|
|
|
|
2016-11-10 18:02:19 +03:00
|
|
|
/** @var IManager */
|
|
|
|
protected $activityManager;
|
|
|
|
|
2016-11-22 18:39:29 +03:00
|
|
|
/** @var IUserManager */
|
|
|
|
protected $userManager;
|
|
|
|
|
2016-11-25 17:21:16 +03:00
|
|
|
/** @var IEventMerger */
|
|
|
|
protected $eventMerger;
|
|
|
|
|
2016-11-22 18:55:02 +03:00
|
|
|
/** @var string[] cached displayNames - key is the UID and value the displayname */
|
|
|
|
protected $displayNames = [];
|
|
|
|
|
2016-11-04 13:34:42 +03:00
|
|
|
/**
|
2016-12-01 13:35:23 +03:00
|
|
|
* @param IFactory $languageFactory
|
2016-11-04 13:34:42 +03:00
|
|
|
* @param IURLGenerator $url
|
2016-11-10 18:02:19 +03:00
|
|
|
* @param IManager $activityManager
|
2016-11-22 18:39:29 +03:00
|
|
|
* @param IUserManager $userManager
|
2016-11-25 17:21:16 +03:00
|
|
|
* @param IEventMerger $eventMerger
|
2016-11-04 13:34:42 +03:00
|
|
|
*/
|
2016-12-01 13:35:23 +03:00
|
|
|
public function __construct(IFactory $languageFactory, IURLGenerator $url, IManager $activityManager, IUserManager $userManager, IEventMerger $eventMerger) {
|
|
|
|
$this->languageFactory = $languageFactory;
|
2016-11-04 13:34:42 +03:00
|
|
|
$this->url = $url;
|
2016-11-10 18:02:19 +03:00
|
|
|
$this->activityManager = $activityManager;
|
2016-11-22 18:39:29 +03:00
|
|
|
$this->userManager = $userManager;
|
2016-11-25 17:21:16 +03:00
|
|
|
$this->eventMerger = $eventMerger;
|
2016-11-04 13:34:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-01 13:35:23 +03:00
|
|
|
* @param string $language
|
2016-11-04 13:34:42 +03:00
|
|
|
* @param IEvent $event
|
|
|
|
* @param IEvent|null $previousEvent
|
|
|
|
* @return IEvent
|
|
|
|
* @throws \InvalidArgumentException
|
2016-11-16 11:29:27 +03:00
|
|
|
* @since 11.0.0
|
2016-11-04 13:34:42 +03:00
|
|
|
*/
|
2016-12-01 13:35:23 +03:00
|
|
|
public function parse($language, IEvent $event, IEvent $previousEvent = null) {
|
2016-11-04 13:34:42 +03:00
|
|
|
if ($event->getApp() !== 'files') {
|
|
|
|
throw new \InvalidArgumentException();
|
|
|
|
}
|
|
|
|
|
2016-12-01 13:35:23 +03:00
|
|
|
$this->l = $this->languageFactory->get('files', $language);
|
|
|
|
|
2016-11-10 18:02:19 +03:00
|
|
|
if ($this->activityManager->isFormattingFilteredObject()) {
|
|
|
|
try {
|
2016-11-16 22:43:28 +03:00
|
|
|
return $this->parseShortVersion($event, $previousEvent);
|
2016-11-10 18:02:19 +03:00
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
// Ignore and simply use the long version...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-16 22:43:28 +03:00
|
|
|
return $this->parseLongVersion($event, $previousEvent);
|
2016-11-10 18:02:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IEvent $event
|
2016-11-16 22:43:28 +03:00
|
|
|
* @param IEvent|null $previousEvent
|
2016-11-10 18:02:19 +03:00
|
|
|
* @return IEvent
|
|
|
|
* @throws \InvalidArgumentException
|
2016-11-16 11:29:27 +03:00
|
|
|
* @since 11.0.0
|
2016-11-10 18:02:19 +03:00
|
|
|
*/
|
2016-11-16 22:43:28 +03:00
|
|
|
public function parseShortVersion(IEvent $event, IEvent $previousEvent = null) {
|
2017-02-07 13:30:23 +03:00
|
|
|
$parsedParameters = $this->getParameters($event);
|
2016-11-10 18:02:19 +03:00
|
|
|
|
|
|
|
if ($event->getSubject() === 'created_by') {
|
2016-11-16 22:43:28 +03:00
|
|
|
$subject = $this->l->t('Created by {user}');
|
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'add-color.svg')));
|
2016-11-10 18:02:19 +03:00
|
|
|
} else if ($event->getSubject() === 'changed_by') {
|
2016-11-16 22:43:28 +03:00
|
|
|
$subject = $this->l->t('Changed by {user}');
|
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'change.svg')));
|
2016-11-10 18:02:19 +03:00
|
|
|
} else if ($event->getSubject() === 'deleted_by') {
|
2016-11-16 22:43:28 +03:00
|
|
|
$subject = $this->l->t('Deleted by {user}');
|
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'delete-color.svg')));
|
2016-11-10 18:02:19 +03:00
|
|
|
} else if ($event->getSubject() === 'restored_by') {
|
2016-11-16 22:43:28 +03:00
|
|
|
$subject = $this->l->t('Restored by {user}');
|
2016-11-10 18:02:19 +03:00
|
|
|
} else if ($event->getSubject() === 'renamed_by') {
|
2016-11-16 22:43:28 +03:00
|
|
|
$subject = $this->l->t('Renamed by {user}');
|
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'change.svg')));
|
2016-11-10 18:02:19 +03:00
|
|
|
} else if ($event->getSubject() === 'moved_by') {
|
2016-11-16 22:43:28 +03:00
|
|
|
$subject = $this->l->t('Moved by {user}');
|
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'change.svg')));
|
2016-11-10 18:02:19 +03:00
|
|
|
} else {
|
|
|
|
throw new \InvalidArgumentException();
|
|
|
|
}
|
|
|
|
|
2016-11-16 22:43:28 +03:00
|
|
|
$this->setSubjects($event, $subject, $parsedParameters);
|
|
|
|
|
2017-02-07 13:30:23 +03:00
|
|
|
return $this->eventMerger->mergeEvents('user', $event, $previousEvent);
|
2016-11-10 18:02:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IEvent $event
|
2016-11-16 22:43:28 +03:00
|
|
|
* @param IEvent|null $previousEvent
|
2016-11-10 18:02:19 +03:00
|
|
|
* @return IEvent
|
|
|
|
* @throws \InvalidArgumentException
|
2016-11-16 11:29:27 +03:00
|
|
|
* @since 11.0.0
|
2016-11-10 18:02:19 +03:00
|
|
|
*/
|
2016-11-16 22:43:28 +03:00
|
|
|
public function parseLongVersion(IEvent $event, IEvent $previousEvent = null) {
|
2017-02-07 13:30:23 +03:00
|
|
|
$parsedParameters = $this->getParameters($event);
|
2016-11-10 18:02:19 +03:00
|
|
|
|
2016-11-04 13:34:42 +03:00
|
|
|
if ($event->getSubject() === 'created_self') {
|
2016-11-16 22:43:28 +03:00
|
|
|
$subject = $this->l->t('You created {file}');
|
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'add-color.svg')));
|
2016-11-04 13:34:42 +03:00
|
|
|
} else if ($event->getSubject() === 'created_by') {
|
2016-11-16 22:43:28 +03:00
|
|
|
$subject = $this->l->t('{user} created {file}');
|
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'add-color.svg')));
|
2016-11-04 13:34:42 +03:00
|
|
|
} else if ($event->getSubject() === 'created_public') {
|
2016-11-16 22:43:28 +03:00
|
|
|
$subject = $this->l->t('{file} was created in a public folder');
|
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'add-color.svg')));
|
2016-11-04 13:34:42 +03:00
|
|
|
} else if ($event->getSubject() === 'changed_self') {
|
2016-11-16 22:43:28 +03:00
|
|
|
$subject = $this->l->t('You changed {file}');
|
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'change.svg')));
|
2016-11-04 13:34:42 +03:00
|
|
|
} else if ($event->getSubject() === 'changed_by') {
|
2016-11-16 22:43:28 +03:00
|
|
|
$subject = $this->l->t('{user} changed {file}');
|
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'change.svg')));
|
2016-11-04 13:34:42 +03:00
|
|
|
} else if ($event->getSubject() === 'deleted_self') {
|
2016-11-16 22:43:28 +03:00
|
|
|
$subject = $this->l->t('You deleted {file}');
|
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'delete-color.svg')));
|
2016-11-04 13:34:42 +03:00
|
|
|
} else if ($event->getSubject() === 'deleted_by') {
|
2016-11-16 22:43:28 +03:00
|
|
|
$subject = $this->l->t('{user} deleted {file}');
|
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'delete-color.svg')));
|
2016-11-04 13:34:42 +03:00
|
|
|
} else if ($event->getSubject() === 'restored_self') {
|
2016-11-16 22:43:28 +03:00
|
|
|
$subject = $this->l->t('You restored {file}');
|
2016-11-04 13:34:42 +03:00
|
|
|
} else if ($event->getSubject() === 'restored_by') {
|
2016-11-16 22:43:28 +03:00
|
|
|
$subject = $this->l->t('{user} restored {file}');
|
2016-11-04 13:34:42 +03:00
|
|
|
} else if ($event->getSubject() === 'renamed_self') {
|
2016-11-16 22:43:28 +03:00
|
|
|
$subject = $this->l->t('You renamed {oldfile} to {newfile}');
|
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'change.svg')));
|
2016-11-04 13:34:42 +03:00
|
|
|
} else if ($event->getSubject() === 'renamed_by') {
|
2016-11-16 22:43:28 +03:00
|
|
|
$subject = $this->l->t('{user} renamed {oldfile} to {newfile}');
|
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'change.svg')));
|
2016-11-04 13:34:42 +03:00
|
|
|
} else if ($event->getSubject() === 'moved_self') {
|
2016-11-16 22:43:28 +03:00
|
|
|
$subject = $this->l->t('You moved {oldfile} to {newfile}');
|
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'change.svg')));
|
2016-11-04 13:34:42 +03:00
|
|
|
} else if ($event->getSubject() === 'moved_by') {
|
2016-11-16 22:43:28 +03:00
|
|
|
$subject = $this->l->t('{user} moved {oldfile} to {newfile}');
|
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'change.svg')));
|
2016-11-04 13:34:42 +03:00
|
|
|
} else {
|
|
|
|
throw new \InvalidArgumentException();
|
|
|
|
}
|
|
|
|
|
2016-11-16 22:43:28 +03:00
|
|
|
$this->setSubjects($event, $subject, $parsedParameters);
|
|
|
|
|
2016-11-25 17:21:16 +03:00
|
|
|
$event = $this->eventMerger->mergeEvents('file', $event, $previousEvent);
|
|
|
|
|
|
|
|
if ($event->getChildEvent() === null) {
|
|
|
|
// Couldn't group by file, maybe we can group by user
|
|
|
|
$event = $this->eventMerger->mergeEvents('user', $event, $previousEvent);
|
|
|
|
}
|
|
|
|
|
2016-11-04 13:34:42 +03:00
|
|
|
return $event;
|
|
|
|
}
|
|
|
|
|
2016-11-16 22:43:28 +03:00
|
|
|
protected function setSubjects(IEvent $event, $subject, array $parameters) {
|
|
|
|
$placeholders = $replacements = [];
|
|
|
|
foreach ($parameters as $placeholder => $parameter) {
|
|
|
|
$placeholders[] = '{' . $placeholder . '}';
|
|
|
|
if ($parameter['type'] === 'file') {
|
2016-12-15 17:53:48 +03:00
|
|
|
$replacements[] = $parameter['path'];
|
2016-11-16 22:43:28 +03:00
|
|
|
} else {
|
|
|
|
$replacements[] = $parameter['name'];
|
|
|
|
}
|
2016-11-04 13:34:42 +03:00
|
|
|
}
|
2016-11-16 22:43:28 +03:00
|
|
|
|
|
|
|
$event->setParsedSubject(str_replace($placeholders, $replacements, $subject))
|
|
|
|
->setRichSubject($subject, $parameters);
|
2016-11-04 13:34:42 +03:00
|
|
|
}
|
|
|
|
|
2017-02-07 13:30:23 +03:00
|
|
|
/**
|
|
|
|
* @param IEvent $event
|
|
|
|
* @return array
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
protected function getParameters(IEvent $event) {
|
|
|
|
$parameters = $event->getSubjectParameters();
|
|
|
|
switch ($event->getSubject()) {
|
2016-11-04 13:34:42 +03:00
|
|
|
case 'created_self':
|
|
|
|
case 'created_public':
|
|
|
|
case 'changed_self':
|
|
|
|
case 'deleted_self':
|
|
|
|
case 'restored_self':
|
|
|
|
return [
|
2017-02-07 13:30:23 +03:00
|
|
|
'file' => $this->getRichFileParameter($parameters[0], $event),
|
2016-11-04 13:34:42 +03:00
|
|
|
];
|
|
|
|
case 'created_by':
|
|
|
|
case 'changed_by':
|
|
|
|
case 'deleted_by':
|
|
|
|
case 'restored_by':
|
|
|
|
return [
|
2017-02-07 13:30:23 +03:00
|
|
|
'file' => $this->getRichFileParameter($parameters[0], $event),
|
2016-11-16 22:43:28 +03:00
|
|
|
'user' => $this->getRichUserParameter($parameters[1]),
|
2016-11-04 13:34:42 +03:00
|
|
|
];
|
|
|
|
case 'renamed_self':
|
|
|
|
case 'moved_self':
|
|
|
|
return [
|
2016-11-16 22:43:28 +03:00
|
|
|
'newfile' => $this->getRichFileParameter($parameters[0]),
|
|
|
|
'oldfile' => $this->getRichFileParameter($parameters[1]),
|
2016-11-04 13:34:42 +03:00
|
|
|
];
|
|
|
|
case 'renamed_by':
|
|
|
|
case 'moved_by':
|
|
|
|
return [
|
2016-11-16 22:43:28 +03:00
|
|
|
'newfile' => $this->getRichFileParameter($parameters[0]),
|
|
|
|
'user' => $this->getRichUserParameter($parameters[1]),
|
|
|
|
'oldfile' => $this->getRichFileParameter($parameters[2]),
|
2016-11-04 13:34:42 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2017-02-07 13:30:23 +03:00
|
|
|
/**
|
|
|
|
* @param $parameter
|
|
|
|
* @param IEvent|null $event
|
|
|
|
* @return array
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
protected function getRichFileParameter($parameter, IEvent $event = null) {
|
|
|
|
if (is_array($parameter)) {
|
|
|
|
$path = reset($parameter);
|
|
|
|
$id = 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');
|
|
|
|
}
|
2016-11-04 13:34:42 +03:00
|
|
|
return [
|
|
|
|
'type' => 'file',
|
|
|
|
'id' => $id,
|
|
|
|
'name' => basename($path),
|
2016-12-15 17:53:48 +03:00
|
|
|
'path' => trim($path, '/'),
|
|
|
|
'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]),
|
2016-11-04 13:34:42 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-11-22 18:39:29 +03:00
|
|
|
protected function getRichUserParameter($uid) {
|
|
|
|
if (!isset($this->displayNames[$uid])) {
|
|
|
|
$this->displayNames[$uid] = $this->getDisplayName($uid);
|
|
|
|
}
|
|
|
|
|
2016-11-04 13:34:42 +03:00
|
|
|
return [
|
|
|
|
'type' => 'user',
|
2016-11-22 18:39:29 +03:00
|
|
|
'id' => $uid,
|
|
|
|
'name' => $this->displayNames[$uid],
|
2016-11-04 13:34:42 +03:00
|
|
|
];
|
|
|
|
}
|
2016-11-22 18:39:29 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $uid
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getDisplayName($uid) {
|
|
|
|
$user = $this->userManager->get($uid);
|
|
|
|
if ($user instanceof IUser) {
|
|
|
|
return $user->getDisplayName();
|
|
|
|
} else {
|
|
|
|
return $uid;
|
|
|
|
}
|
|
|
|
}
|
2016-11-04 13:34:42 +03:00
|
|
|
}
|