aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files/lib/Activity/Provider.php
diff options
context:
space:
mode:
Diffstat (limited to 'apps/files/lib/Activity/Provider.php')
-rw-r--r--apps/files/lib/Activity/Provider.php197
1 files changed, 86 insertions, 111 deletions
diff --git a/apps/files/lib/Activity/Provider.php b/apps/files/lib/Activity/Provider.php
index 2f6e406978a..3ef79ac107f 100644
--- a/apps/files/lib/Activity/Provider.php
+++ b/apps/files/lib/Activity/Provider.php
@@ -1,30 +1,12 @@
<?php
+
/**
- * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
- *
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Joas Schilling <coding@schilljs.com>
- * @author Morris Jobke <hey@morrisjobke.de>
- *
- * @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/>.
- *
+ * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
-
namespace OCA\Files\Activity;
+use OCP\Activity\Exceptions\UnknownActivityException;
use OCP\Activity\IEvent;
use OCP\Activity\IEventMerger;
use OCP\Activity\IManager;
@@ -38,62 +20,28 @@ use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\IL10N;
use OCP\IURLGenerator;
-use OCP\IUser;
use OCP\IUserManager;
use OCP\L10N\IFactory;
class Provider implements IProvider {
-
- /** @var IFactory */
- protected $languageFactory;
-
/** @var IL10N */
protected $l;
- /** @var IL10N */
- protected $activityLang;
-
- /** @var IURLGenerator */
- protected $url;
-
- /** @var IManager */
- protected $activityManager;
-
- /** @var IUserManager */
- protected $userManager;
-
- /** @var IRootFolder */
- protected $rootFolder;
-
- /** @var IEventMerger */
- protected $eventMerger;
-
- /** @var ICloudIdManager */
- protected $cloudIdManager;
-
- /** @var IContactsManager */
- protected $contactsManager;
/** @var string[] cached displayNames - key is the cloud id and value the displayname */
protected $displayNames = [];
protected $fileIsEncrypted = false;
- public function __construct(IFactory $languageFactory,
- IURLGenerator $url,
- IManager $activityManager,
- IUserManager $userManager,
- IRootFolder $rootFolder,
- ICloudIdManager $cloudIdManager,
- IContactsManager $contactsManager,
- IEventMerger $eventMerger) {
- $this->languageFactory = $languageFactory;
- $this->url = $url;
- $this->activityManager = $activityManager;
- $this->userManager = $userManager;
- $this->rootFolder = $rootFolder;
- $this->cloudIdManager = $cloudIdManager;
- $this->contactsManager = $contactsManager;
- $this->eventMerger = $eventMerger;
+ public function __construct(
+ protected IFactory $languageFactory,
+ protected IURLGenerator $url,
+ protected IManager $activityManager,
+ protected IUserManager $userManager,
+ protected IRootFolder $rootFolder,
+ protected ICloudIdManager $cloudIdManager,
+ protected IContactsManager $contactsManager,
+ protected IEventMerger $eventMerger,
+ ) {
}
/**
@@ -101,21 +49,20 @@ class Provider implements IProvider {
* @param IEvent $event
* @param IEvent|null $previousEvent
* @return IEvent
- * @throws \InvalidArgumentException
+ * @throws UnknownActivityException
* @since 11.0.0
*/
- public function parse($language, IEvent $event, IEvent $previousEvent = null) {
+ public function parse($language, IEvent $event, ?IEvent $previousEvent = null) {
if ($event->getApp() !== 'files') {
- throw new \InvalidArgumentException();
+ throw new UnknownActivityException();
}
$this->l = $this->languageFactory->get('files', $language);
- $this->activityLang = $this->languageFactory->get('activity', $language);
if ($this->activityManager->isFormattingFilteredObject()) {
try {
return $this->parseShortVersion($event, $previousEvent);
- } catch (\InvalidArgumentException $e) {
+ } catch (UnknownActivityException) {
// Ignore and simply use the long version...
}
}
@@ -135,10 +82,10 @@ class Provider implements IProvider {
* @param IEvent $event
* @param IEvent|null $previousEvent
* @return IEvent
- * @throws \InvalidArgumentException
+ * @throws UnknownActivityException
* @since 11.0.0
*/
- public function parseShortVersion(IEvent $event, IEvent $previousEvent = null) {
+ public function parseShortVersion(IEvent $event, ?IEvent $previousEvent = null): IEvent {
$parsedParameters = $this->getParameters($event);
if ($event->getSubject() === 'created_by') {
@@ -160,12 +107,12 @@ class Provider implements IProvider {
$subject = $this->l->t('Moved by {user}');
$this->setIcon($event, 'change');
} else {
- throw new \InvalidArgumentException();
+ throw new UnknownActivityException();
}
if (!isset($parsedParameters['user'])) {
// External user via public link share
- $subject = str_replace('{user}', $this->activityLang->t('"remote user"'), $subject);
+ $subject = str_replace('{user}', $this->l->t('"remote account"'), $subject);
}
$this->setSubjects($event, $subject, $parsedParameters);
@@ -177,10 +124,10 @@ class Provider implements IProvider {
* @param IEvent $event
* @param IEvent|null $previousEvent
* @return IEvent
- * @throws \InvalidArgumentException
+ * @throws UnknownActivityException
* @since 11.0.0
*/
- public function parseLongVersion(IEvent $event, IEvent $previousEvent = null) {
+ public function parseLongVersion(IEvent $event, ?IEvent $previousEvent = null): IEvent {
$this->fileIsEncrypted = false;
$parsedParameters = $this->getParameters($event);
@@ -230,10 +177,42 @@ class Provider implements IProvider {
$subject = $this->l->t('{user} restored {file}');
$this->setIcon($event, 'actions/history', 'core');
} elseif ($event->getSubject() === 'renamed_self') {
- $subject = $this->l->t('You renamed {oldfile} to {newfile}');
+ $oldFileName = $parsedParameters['oldfile']['name'];
+ $newFileName = $parsedParameters['newfile']['name'];
+
+ if ($this->isHiddenFile($oldFileName)) {
+ if ($this->isHiddenFile($newFileName)) {
+ $subject = $this->l->t('You renamed {oldfile} (hidden) to {newfile} (hidden)');
+ } else {
+ $subject = $this->l->t('You renamed {oldfile} (hidden) to {newfile}');
+ }
+ } else {
+ if ($this->isHiddenFile($newFileName)) {
+ $subject = $this->l->t('You renamed {oldfile} to {newfile} (hidden)');
+ } else {
+ $subject = $this->l->t('You renamed {oldfile} to {newfile}');
+ }
+ }
+
$this->setIcon($event, 'change');
} elseif ($event->getSubject() === 'renamed_by') {
- $subject = $this->l->t('{user} renamed {oldfile} to {newfile}');
+ $oldFileName = $parsedParameters['oldfile']['name'];
+ $newFileName = $parsedParameters['newfile']['name'];
+
+ if ($this->isHiddenFile($oldFileName)) {
+ if ($this->isHiddenFile($newFileName)) {
+ $subject = $this->l->t('{user} renamed {oldfile} (hidden) to {newfile} (hidden)');
+ } else {
+ $subject = $this->l->t('{user} renamed {oldfile} (hidden) to {newfile}');
+ }
+ } else {
+ if ($this->isHiddenFile($newFileName)) {
+ $subject = $this->l->t('{user} renamed {oldfile} to {newfile} (hidden)');
+ } else {
+ $subject = $this->l->t('{user} renamed {oldfile} to {newfile}');
+ }
+ }
+
$this->setIcon($event, 'change');
} elseif ($event->getSubject() === 'moved_self') {
$subject = $this->l->t('You moved {oldfile} to {newfile}');
@@ -242,7 +221,7 @@ class Provider implements IProvider {
$subject = $this->l->t('{user} moved {oldfile} to {newfile}');
$this->setIcon($event, 'change');
} else {
- throw new \InvalidArgumentException();
+ throw new UnknownActivityException();
}
if ($this->fileIsEncrypted) {
@@ -251,7 +230,7 @@ class Provider implements IProvider {
if (!isset($parsedParameters['user'])) {
// External user via public link share
- $subject = str_replace('{user}', $this->activityLang->t('"remote user"'), $subject);
+ $subject = str_replace('{user}', $this->l->t('"remote account"'), $subject);
}
$this->setSubjects($event, $subject, $parsedParameters);
@@ -270,27 +249,20 @@ class Provider implements IProvider {
return $event;
}
- 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'];
- }
- }
+ private function isHiddenFile(string $filename): bool {
+ return strlen($filename) > 0 && $filename[0] === '.';
+ }
- $event->setParsedSubject(str_replace($placeholders, $replacements, $subject))
- ->setRichSubject($subject, $parameters);
+ protected function setSubjects(IEvent $event, string $subject, array $parameters): void {
+ $event->setRichSubject($subject, $parameters);
}
/**
* @param IEvent $event
* @return array
- * @throws \InvalidArgumentException
+ * @throws UnknownActivityException
*/
- protected function getParameters(IEvent $event) {
+ protected function getParameters(IEvent $event): array {
$parameters = $event->getSubjectParameters();
switch ($event->getSubject()) {
case 'created_self':
@@ -343,18 +315,18 @@ class Provider implements IProvider {
* @param array|string $parameter
* @param IEvent|null $event
* @return array
- * @throws \InvalidArgumentException
+ * @throws UnknownActivityException
*/
- protected function getFile($parameter, IEvent $event = null) {
+ protected function getFile($parameter, ?IEvent $event = null): array {
if (is_array($parameter)) {
$path = reset($parameter);
- $id = (string) key($parameter);
+ $id = (int)key($parameter);
} elseif ($event !== null) {
// Legacy from before ownCloud 8.2
$path = $parameter;
$id = $event->getObjectId();
} else {
- throw new \InvalidArgumentException('Could not generate file parameter');
+ throw new UnknownActivityException('Could not generate file parameter');
}
$encryptionContainer = $this->getEndToEndEncryptionContainer($id, $path);
@@ -363,14 +335,14 @@ class Provider implements IProvider {
try {
$fullPath = rtrim($encryptionContainer->getPath(), '/');
// Remove /user/files/...
- list(,,, $path) = explode('/', $fullPath, 4);
+ [,,, $path] = explode('/', $fullPath, 4);
if (!$path) {
throw new InvalidPathException('Path could not be split correctly');
}
return [
'type' => 'file',
- 'id' => $encryptionContainer->getId(),
+ 'id' => (string)$encryptionContainer->getId(),
'name' => $encryptionContainer->getName(),
'path' => $path,
'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $encryptionContainer->getId()]),
@@ -383,7 +355,7 @@ class Provider implements IProvider {
return [
'type' => 'file',
- 'id' => $id,
+ 'id' => (string)$id,
'name' => basename($path),
'path' => trim($path, '/'),
'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]),
@@ -410,8 +382,8 @@ class Provider implements IProvider {
}
$userFolder = $this->rootFolder->getUserFolder($this->activityManager->getCurrentUserId());
- $files = $userFolder->getById($fileId);
- if (empty($files)) {
+ $file = $userFolder->getFirstNodeById($fileId);
+ if (!$file) {
try {
// Deleted, try with parent
$file = $this->findExistingParent($userFolder, dirname($path));
@@ -427,8 +399,6 @@ class Provider implements IProvider {
return $file;
}
- $file = array_shift($files);
-
if ($file instanceof Folder && $file->isEncrypted()) {
// If the folder is encrypted, it is the Container,
// but can be the name is just fine.
@@ -491,12 +461,12 @@ class Provider implements IProvider {
*/
protected function getUser($uid) {
// First try local user
- $user = $this->userManager->get($uid);
- if ($user instanceof IUser) {
+ $displayName = $this->userManager->getDisplayName($uid);
+ if ($displayName !== null) {
return [
'type' => 'user',
- 'id' => $user->getUID(),
- 'name' => $user->getDisplayName(),
+ 'id' => $uid,
+ 'name' => $displayName,
];
}
@@ -524,7 +494,12 @@ class Provider implements IProvider {
return $this->displayNames[$search];
}
- $addressBookContacts = $this->contactsManager->search($search, ['CLOUD']);
+ $addressBookContacts = $this->contactsManager->search($search, ['CLOUD'], [
+ 'limit' => 1,
+ 'enumeration' => false,
+ 'fullmatch' => false,
+ 'strict_search' => true,
+ ]);
foreach ($addressBookContacts as $contact) {
if (isset($contact['isLocalSystemBook'])) {
continue;