Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
260 changes: 90 additions & 170 deletions apps/sharebymail/lib/Activity.php
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,11 +20,8 @@
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';
Expand All @@ -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,
Expand All @@ -47,14 +40,10 @@ public function __construct(
}

/**
* @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();
}
Expand All @@ -73,170 +62,113 @@ public function parse($language, IEvent $event, ?IEvent $previousEvent = null) {
}

/**
* @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();

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();
$objectId = $event->getObjectId();

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,
Expand All @@ -246,11 +178,7 @@ protected function generateFileParameter($id, $path): array {
];
}

/**
* @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);
}
Expand All @@ -262,23 +190,15 @@ protected function generateEmailParameter($email) {
];
}

/**
* @param string $uid
* @return array
*/
protected function generateUserParameter($uid) {
protected function generateUserParameter(string $uid): array {
return [
'type' => 'user',
'id' => $uid,
'name' => $this->userManager->getDisplayName($uid) ?? $uid,
];
}

/**
* @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,
Expand Down
Loading