mirror of
https://github.com/nextcloud/server.git
synced 2026-05-28 04:32:30 -04:00
Merge pull request #57785 from nextcloud/reactorShareByMail
refactor(shareByMail): Strict types and code simplification
This commit is contained in:
commit
5bc3ae7a75
1 changed files with 89 additions and 169 deletions
|
|
@ -1,9 +1,12 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace OCA\ShareByMail;
|
||||
|
||||
use OCP\Activity\Exceptions\UnknownActivityException;
|
||||
|
|
@ -17,11 +20,8 @@ use OCP\IUserManager;
|
|||
use OCP\L10N\IFactory;
|
||||
|
||||
class Activity implements IProvider {
|
||||
/** @var IL10N */
|
||||
protected $l;
|
||||
|
||||
/** @var array */
|
||||
protected $contactNames = [];
|
||||
protected IL10N $l;
|
||||
protected array $contactNames = [];
|
||||
|
||||
public const SUBJECT_SHARED_EMAIL_SELF = 'shared_with_email_self';
|
||||
public const SUBJECT_SHARED_EMAIL_BY = 'shared_with_email_by';
|
||||
|
|
@ -30,13 +30,6 @@ class Activity implements IProvider {
|
|||
public const SUBJECT_UNSHARED_EMAIL_SELF = 'unshared_with_email_self';
|
||||
public const SUBJECT_UNSHARED_EMAIL_BY = 'unshared_with_email_by';
|
||||
|
||||
/**
|
||||
* @param IFactory $languageFactory
|
||||
* @param IURLGenerator $url
|
||||
* @param IManager $activityManager
|
||||
* @param IUserManager $userManager
|
||||
* @param IContactsManager $contactsManager
|
||||
*/
|
||||
public function __construct(
|
||||
protected IFactory $languageFactory,
|
||||
protected IURLGenerator $url,
|
||||
|
|
@ -47,14 +40,10 @@ class Activity implements IProvider {
|
|||
}
|
||||
|
||||
/**
|
||||
* @param string $language
|
||||
* @param IEvent $event
|
||||
* @param IEvent|null $previousEvent
|
||||
* @return IEvent
|
||||
* @throws UnknownActivityException
|
||||
* @since 11.0.0
|
||||
*/
|
||||
public function parse($language, IEvent $event, ?IEvent $previousEvent = null) {
|
||||
public function parse($language, IEvent $event, ?IEvent $previousEvent = null): IEvent {
|
||||
if ($event->getApp() !== 'sharebymail') {
|
||||
throw new UnknownActivityException();
|
||||
}
|
||||
|
|
@ -73,170 +62,113 @@ class Activity implements IProvider {
|
|||
}
|
||||
|
||||
/**
|
||||
* @param IEvent $event
|
||||
* @return IEvent
|
||||
* @throws \InvalidArgumentException
|
||||
* @since 11.0.0
|
||||
*/
|
||||
public function parseShortVersion(IEvent $event) {
|
||||
public function parseShortVersion(IEvent $event): IEvent {
|
||||
$parsedParameters = $this->getParsedParameters($event);
|
||||
|
||||
if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_SELF) {
|
||||
$event->setRichSubject($this->l->t('Shared with {email}'), [
|
||||
'email' => $parsedParameters['email'],
|
||||
]);
|
||||
if ($this->activityManager->getRequirePNG()) {
|
||||
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
|
||||
} else {
|
||||
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
|
||||
}
|
||||
} elseif ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_BY) {
|
||||
$event->setRichSubject($this->l->t('Shared with {email} by {actor}'), [
|
||||
'email' => $parsedParameters['email'],
|
||||
'actor' => $parsedParameters['actor'],
|
||||
]);
|
||||
if ($this->activityManager->getRequirePNG()) {
|
||||
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
|
||||
} else {
|
||||
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
|
||||
}
|
||||
} elseif ($event->getSubject() === self::SUBJECT_UNSHARED_EMAIL_SELF) {
|
||||
$event->setRichSubject($this->l->t('Unshared from {email}'), [
|
||||
'email' => $parsedParameters['email'],
|
||||
]);
|
||||
if ($this->activityManager->getRequirePNG()) {
|
||||
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
|
||||
} else {
|
||||
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
|
||||
}
|
||||
} elseif ($event->getSubject() === self::SUBJECT_UNSHARED_EMAIL_BY) {
|
||||
$event->setRichSubject($this->l->t('Unshared from {email} by {actor}'), [
|
||||
'email' => $parsedParameters['email'],
|
||||
'actor' => $parsedParameters['actor'],
|
||||
]);
|
||||
if ($this->activityManager->getRequirePNG()) {
|
||||
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
|
||||
} else {
|
||||
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
|
||||
}
|
||||
} elseif ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND) {
|
||||
$event->setRichSubject($this->l->t('Password for mail share sent to {email}'), [
|
||||
'email' => $parsedParameters['email']
|
||||
]);
|
||||
if ($this->activityManager->getRequirePNG()) {
|
||||
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
|
||||
} else {
|
||||
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
|
||||
}
|
||||
} elseif ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF) {
|
||||
$event->setRichSubject($this->l->t('Password for mail share sent to you'));
|
||||
if ($this->activityManager->getRequirePNG()) {
|
||||
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
|
||||
} else {
|
||||
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
|
||||
}
|
||||
} else {
|
||||
throw new \InvalidArgumentException();
|
||||
}
|
||||
$iconFile = $this->activityManager->getRequirePNG() ? 'actions/share.png' : 'actions/share.svg';
|
||||
$iconUrl = $this->url->getAbsoluteURL($this->url->imagePath('core', $iconFile));
|
||||
|
||||
[$subject, $params] = match ($event->getSubject()) {
|
||||
self::SUBJECT_SHARED_EMAIL_SELF => [
|
||||
$this->l->t('Shared with {email}'),
|
||||
['email' => $parsedParameters['email']]
|
||||
],
|
||||
self::SUBJECT_SHARED_EMAIL_BY => [
|
||||
$this->l->t('Shared with {email} by {actor}'),
|
||||
['email' => $parsedParameters['email'], 'actor' => $parsedParameters['actor']]
|
||||
],
|
||||
self::SUBJECT_UNSHARED_EMAIL_SELF => [
|
||||
$this->l->t('Unshared from {email}'),
|
||||
['email' => $parsedParameters['email']]
|
||||
],
|
||||
self::SUBJECT_UNSHARED_EMAIL_BY => [
|
||||
$this->l->t('Unshared from {email} by {actor}'),
|
||||
['email' => $parsedParameters['email'], 'actor' => $parsedParameters['actor']]
|
||||
],
|
||||
self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND => [
|
||||
$this->l->t('Password for mail share sent to {email}'),
|
||||
['email' => $parsedParameters['email']]
|
||||
],
|
||||
self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF => [
|
||||
$this->l->t('Password for mail share sent to you'),
|
||||
[]
|
||||
],
|
||||
default => throw new UnknownActivityException(),
|
||||
};
|
||||
|
||||
$event->setRichSubject($subject, $params);
|
||||
$event->setIcon($iconUrl);
|
||||
|
||||
return $event;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param IEvent $event
|
||||
* @return IEvent
|
||||
* @throws \InvalidArgumentException
|
||||
* @since 11.0.0
|
||||
*/
|
||||
public function parseLongVersion(IEvent $event) {
|
||||
public function parseLongVersion(IEvent $event): IEvent {
|
||||
$parsedParameters = $this->getParsedParameters($event);
|
||||
|
||||
if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_SELF) {
|
||||
$event->setRichSubject($this->l->t('You shared {file} with {email} by mail'), $parsedParameters);
|
||||
if ($this->activityManager->getRequirePNG()) {
|
||||
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
|
||||
} else {
|
||||
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
|
||||
}
|
||||
} elseif ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_BY) {
|
||||
$event->setRichSubject($this->l->t('{actor} shared {file} with {email} by mail'), $parsedParameters);
|
||||
if ($this->activityManager->getRequirePNG()) {
|
||||
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
|
||||
} else {
|
||||
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
|
||||
}
|
||||
} elseif ($event->getSubject() === self::SUBJECT_UNSHARED_EMAIL_SELF) {
|
||||
$event->setRichSubject($this->l->t('You unshared {file} from {email} by mail'), $parsedParameters);
|
||||
if ($this->activityManager->getRequirePNG()) {
|
||||
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
|
||||
} else {
|
||||
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
|
||||
}
|
||||
} elseif ($event->getSubject() === self::SUBJECT_UNSHARED_EMAIL_BY) {
|
||||
$event->setRichSubject($this->l->t('{actor} unshared {file} from {email} by mail'), $parsedParameters);
|
||||
if ($this->activityManager->getRequirePNG()) {
|
||||
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
|
||||
} else {
|
||||
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
|
||||
}
|
||||
} elseif ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND) {
|
||||
$event->setRichSubject($this->l->t('Password to access {file} was sent to {email}'), $parsedParameters);
|
||||
if ($this->activityManager->getRequirePNG()) {
|
||||
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
|
||||
} else {
|
||||
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
|
||||
}
|
||||
} elseif ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF) {
|
||||
$event->setRichSubject($this->l->t('Password to access {file} was sent to you'), $parsedParameters);
|
||||
if ($this->activityManager->getRequirePNG()) {
|
||||
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
|
||||
} else {
|
||||
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
|
||||
}
|
||||
} else {
|
||||
throw new \InvalidArgumentException();
|
||||
}
|
||||
$iconFile = $this->activityManager->getRequirePNG() ? 'actions/share.png' : 'actions/share.svg';
|
||||
$iconUrl = $this->url->getAbsoluteURL($this->url->imagePath('core', $iconFile));
|
||||
|
||||
$subject = match ($event->getSubject()) {
|
||||
self::SUBJECT_SHARED_EMAIL_SELF
|
||||
=> $this->l->t('You shared {file} with {email} by mail'),
|
||||
self::SUBJECT_SHARED_EMAIL_BY
|
||||
=> $this->l->t('{actor} shared {file} with {email} by mail'),
|
||||
self::SUBJECT_UNSHARED_EMAIL_SELF
|
||||
=> $this->l->t('You unshared {file} from {email} by mail'),
|
||||
self::SUBJECT_UNSHARED_EMAIL_BY
|
||||
=> $this->l->t('{actor} unshared {file} from {email} by mail'),
|
||||
self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND
|
||||
=> $this->l->t('Password to access {file} was sent to {email}'),
|
||||
self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF
|
||||
=> $this->l->t('Password to access {file} was sent to you'),
|
||||
default => throw new UnknownActivityException(),
|
||||
};
|
||||
|
||||
$event->setRichSubject($subject, $parsedParameters);
|
||||
$event->setIcon($iconUrl);
|
||||
|
||||
return $event;
|
||||
}
|
||||
|
||||
protected function getParsedParameters(IEvent $event) {
|
||||
protected function getParsedParameters(IEvent $event): array {
|
||||
$subject = $event->getSubject();
|
||||
$parameters = $event->getSubjectParameters();
|
||||
$objectId = $event->getObjectId();
|
||||
|
||||
switch ($subject) {
|
||||
case self::SUBJECT_SHARED_EMAIL_SELF:
|
||||
case self::SUBJECT_UNSHARED_EMAIL_SELF:
|
||||
return [
|
||||
'file' => $this->generateFileParameter($event->getObjectId(), $parameters[0]),
|
||||
'email' => $this->generateEmailParameter($parameters[1]),
|
||||
];
|
||||
case self::SUBJECT_SHARED_EMAIL_BY:
|
||||
case self::SUBJECT_UNSHARED_EMAIL_BY:
|
||||
return [
|
||||
'file' => $this->generateFileParameter($event->getObjectId(), $parameters[0]),
|
||||
'email' => $this->generateEmailParameter($parameters[1]),
|
||||
'actor' => $this->generateUserParameter($parameters[2]),
|
||||
];
|
||||
case self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND:
|
||||
return [
|
||||
'file' => $this->generateFileParameter($event->getObjectId(), $parameters[0]),
|
||||
'email' => $this->generateEmailParameter($parameters[1]),
|
||||
];
|
||||
case self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF:
|
||||
return [
|
||||
'file' => $this->generateFileParameter($event->getObjectId(), $parameters[0]),
|
||||
];
|
||||
}
|
||||
throw new \InvalidArgumentException();
|
||||
return match ($subject) {
|
||||
self::SUBJECT_SHARED_EMAIL_SELF,
|
||||
self::SUBJECT_UNSHARED_EMAIL_SELF,
|
||||
self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND => [
|
||||
'file' => $this->generateFileParameter($objectId, $parameters[0]),
|
||||
'email' => $this->generateEmailParameter($parameters[1]),
|
||||
],
|
||||
|
||||
self::SUBJECT_SHARED_EMAIL_BY,
|
||||
self::SUBJECT_UNSHARED_EMAIL_BY => [
|
||||
'file' => $this->generateFileParameter($objectId, $parameters[0]),
|
||||
'email' => $this->generateEmailParameter($parameters[1]),
|
||||
'actor' => $this->generateUserParameter($parameters[2]),
|
||||
],
|
||||
|
||||
self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF => [
|
||||
'file' => $this->generateFileParameter($objectId, $parameters[0]),
|
||||
],
|
||||
|
||||
default => throw new UnknownActivityException(),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @param string $path
|
||||
* @return array<string,string>
|
||||
*/
|
||||
protected function generateFileParameter($id, $path): array {
|
||||
protected function generateFileParameter(int $id, string $path): array {
|
||||
return [
|
||||
'type' => 'file',
|
||||
'id' => (string)$id,
|
||||
|
|
@ -246,11 +178,7 @@ class Activity implements IProvider {
|
|||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $email
|
||||
* @return array
|
||||
*/
|
||||
protected function generateEmailParameter($email) {
|
||||
protected function generateEmailParameter(string $email): array {
|
||||
if (!isset($this->contactNames[$email])) {
|
||||
$this->contactNames[$email] = $this->getContactName($email);
|
||||
}
|
||||
|
|
@ -262,11 +190,7 @@ class Activity implements IProvider {
|
|||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $uid
|
||||
* @return array
|
||||
*/
|
||||
protected function generateUserParameter($uid) {
|
||||
protected function generateUserParameter(string $uid): array {
|
||||
return [
|
||||
'type' => 'user',
|
||||
'id' => $uid,
|
||||
|
|
@ -274,11 +198,7 @@ class Activity implements IProvider {
|
|||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $email
|
||||
* @return string
|
||||
*/
|
||||
protected function getContactName($email) {
|
||||
protected function getContactName(string $email): string {
|
||||
$addressBookContacts = $this->contactsManager->search($email, ['EMAIL'], [
|
||||
'limit' => 1,
|
||||
'enumeration' => false,
|
||||
|
|
|
|||
Loading…
Reference in a new issue