Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement filter at Activity #2509

Merged
merged 11 commits into from
Mar 12, 2024
5 changes: 4 additions & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@
</settings>
<activity>
<settings>
<setting>OCA\Libresign\Activity\Setting</setting>
<setting>OCA\Libresign\Activity\FileToSign</setting>
</settings>
<filters>
<filter>OCA\Libresign\Activity\Filter</filter>
</filters>
<providers>
<provider>OCA\Libresign\Activity\Provider\SignRequest</provider>
</providers>
Expand Down
1 change: 1 addition & 0 deletions appinfo/routes/routesNotifyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
'ocs' => [
['name' => 'notify#signer', 'url' => '/api/{apiVersion}/notify/signer', 'verb' => 'POST', 'requirements' => $requirements],
['name' => 'notify#signers', 'url' => '/api/{apiVersion}/notify/signers', 'verb' => 'POST', 'requirements' => $requirements],
['name' => 'notify#notificationDismiss', 'url' => '/api/{apiVersion}/notif/notification', 'verb' => 'DELETE', 'requirements' => $requirements],
],
];
25 changes: 20 additions & 5 deletions lib/Activity/Setting.php → lib/Activity/FileToSign.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,21 @@

namespace OCA\Libresign\Activity;

use OCA\Libresign\AppInfo\Application;
use OCP\Activity\ActivitySettings;
use OCP\IL10N;

class Setting extends ActivitySettings {
class FileToSign extends ActivitySettings {
public function __construct(
protected IL10N $l,
) {
}

/**
* @return string Lowercase a-z and underscore only identifier. The type of table activity
* @since 20.0.0
*/
public function getIdentifier(): string {
return Application::APP_ID;
return 'file_to_sign';
}

/**
Expand Down Expand Up @@ -70,12 +73,24 @@ public function getPriority(): int {
* {@inheritdoc}
*/
public function canChangeNotification(): bool {
return false;
return true;
}
/**
* {@inheritdoc}
*/
public function canChangeMail() {
return true;
}
/**
* {@inheritdoc}
*/
public function isDefaultEnabledMail() {
return true;
}
/**
* {@inheritdoc}
*/
public function isDefaultEnabledNotification(): bool {
return false;
return true;
}
}
67 changes: 67 additions & 0 deletions lib/Activity/Filter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2024 Vitor Mattos <vitor@php.rio>
*
* @author Vitor Mattos <vitor@php.rio>
*
* @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\Libresign\Activity;

use OCA\Libresign\AppInfo\Application;
use OCP\Activity\IFilter;
use OCP\IL10N;
use OCP\IURLGenerator;

class Filter implements IFilter {
public function __construct(
protected IL10N $l,
protected IURLGenerator $url,
) {
$this->l = $l;
$this->url = $url;
}

public function getIdentifier() {
return Application::APP_ID;
}

public function getName() {
return 'LibreSign';
}

public function getPriority() {
return 31;
}

public function getIcon() {
return $this->url->getAbsoluteURL($this->url->imagePath('libresign', 'app-dark.svg'));
}

public function filterTypes(array $types) {
return ['file_to_sign'];
}

public function allowedApps() {
return [
'file_to_sign',
Application::APP_ID,
];
}
}
67 changes: 54 additions & 13 deletions lib/Activity/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@
namespace OCA\Libresign\Activity;

use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\Db\File as FileEntity;
use OCA\Libresign\Db\SignRequest;
use OCA\Libresign\Events\SendSignNotificationEvent;
use OCA\Libresign\Service\AccountService;
use OCA\Libresign\Service\IdentifyMethod\IIdentifyMethod;
use OCP\Activity\IManager;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserSession;
use Psr\Log\LoggerInterface;
Expand All @@ -45,13 +48,17 @@ public function __construct(
protected IUserSession $userSession,
protected LoggerInterface $logger,
protected ITimeFactory $timeFactory,
protected AccountService $accountService,
protected IURLGenerator $url,
) {
}

public function handle(Event $event): void {
/** @var SendSignNotificationEvent $event */
match (get_class($event)) {
SendSignNotificationEvent::class => $this->generateNewSignNotificationActivity(
$event->getSignRequest(),
$event->getLibreSignFile(),
$event->getIdentifyMethod(),
$event->isNew()
),
Expand All @@ -66,6 +73,7 @@ public function handle(Event $event): void {
*/
protected function generateNewSignNotificationActivity(
SignRequest $signRequest,
FileEntity $libreSignFile,
IIdentifyMethod $identifyMethod,
bool $isNew
): void {
Expand All @@ -79,28 +87,61 @@ protected function generateNewSignNotificationActivity(
try {
$event
->setApp(Application::APP_ID)
->setType(Application::APP_ID)
->setType('file_to_sign')
->setAuthor($actorId)
->setObject('sign', $signRequest->getId(), 'signRequest')
->setObject('signRequest', $signRequest->getId())
->setTimestamp($this->timeFactory->getTime())
->setAffectedUser($identifyMethod->getEntity()->getIdentifierValue());
->setAffectedUser($identifyMethod->getEntity()->getIdentifierValue())
// Activity notification was replaced by Notification app
// At notification app we can define the view and dismiss action
// Activity dont have this feature
->setGenerateNotification(false);
if ($isNew) {
$event->setSubject('new_sign_request', [
'from' => $actor->getUID(),
'signer' => $identifyMethod->getEntity()->getIdentifierValue(),
'signRequest' => $signRequest->getId(),
]);
$subject = 'new_sign_request';
} else {
$event->setSubject('update_sign_request', [
'from' => $actor->getUID(),
'signer' => $identifyMethod->getEntity()->getIdentifierValue(),
'signRequest' => $signRequest->getId(),
]);
$subject = 'update_sign_request';
}
$event->setSubject($subject, [
'from' => $this->getUserParameter(
$actor->getUID(),
$actor->getDisplayName(),
),
'file' => $this->getFileParameter($signRequest, $libreSignFile),
'signer' => $this->getUserParameter(
$identifyMethod->getEntity()->getIdentifierValue(),
$signRequest->getDisplayName(),
),
'signRequest' => [
'type' => 'sign-request',
'id' => $signRequest->getId(),
'name' => $actor->getDisplayName(),
],
]);
$this->activityManager->publish($event);
} catch (\InvalidArgumentException $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
return;
}
}

protected function getFileParameter(SignRequest $signRequest, FileEntity $libreSignFile) {
return [
'type' => 'file',
'id' => $libreSignFile->getNodeId(),
'name' => $libreSignFile->getName(),
'path' => $libreSignFile->getName(),
'link' => $this->url->linkToRouteAbsolute('libresign.page.sign', ['uuid' => $signRequest->getUuid()]),
];
}

protected function getUserParameter(
string $userId,
$displayName,
): array {
return [
'type' => 'user',
'id' => $userId,
'name' => $displayName,
];
}
}
47 changes: 36 additions & 11 deletions lib/Activity/Provider/SignRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\L10N\IFactory;
use OCP\RichObjectStrings\Definitions;

class SignRequest implements IProvider {
public function __construct(
protected IFactory $languageFactory,
protected IURLGenerator $url,
protected Definitions $definitions,
protected IManager $activityManager,
protected IUserManager $userManager,
) {
Expand All @@ -46,33 +48,56 @@ public function parse($language, IEvent $event, ?IEvent $previousEvent = null):
throw new \InvalidArgumentException('Wrong app');
}

$this->definitions->definitions['sign-request'] = [
'author' => 'LibreSign',
'since' => '28.0.0',
'parameters' => [
'id' => [
'since' => '28.0.0',
'required' => true,
'description' => 'The id of SignRequest object',
'example' => '12345',
],
'name' => [
'since' => '28.0.0',
'required' => true,
'description' => 'The display name of signer',
'example' => 'John Doe',
],
],
];

if ($this->activityManager->getRequirePNG()) {
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath(Application::APP_ID, 'app-dark.png')));
} else {
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath(Application::APP_ID, 'app-dark.svg')));
}

if ($event->getSubject() === 'new_sign_request') {
if (in_array($event->getSubject(), ['new_sign_request', 'update_sign_request'])) {
$l = $this->languageFactory->get(Application::APP_ID, $language);
$parameters = $event->getSubjectParameters();

$subject = $l->t('{from} invited you to sign a file');
$subject = $this->getParsedSubject($l, $event->getSubject());
$event->setParsedSubject(
str_replace(
'{from}',
$this->userManager->getDisplayName($parameters['from']) ?? $parameters['from'],
['{from}', '{file}'],
[
$parameters['from']['name'],
$parameters['file']['name'],
],
$subject
));
))
->setRichSubject($subject, $parameters);
}

return $event;
}

protected function getUser(string $uid): array {
return [
'type' => 'user',
'id' => $uid,
'name' => $this->userManager->getDisplayName($uid) ?? $uid,
];
private function getParsedSubject($l, $subject) {
if ($subject === 'new_sign_request') {
return $l->t('{from} requested your signature on {file}');
} elseif ($subject === 'update_sign_request') {
return $l->t('{from} made changes on {file}');
}
}
}
4 changes: 4 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use OCA\Libresign\Files\TemplateLoader as FilesTemplateLoader;
use OCA\Libresign\Listener\BeforeNodeDeletedListener;
use OCA\Libresign\Listener\LoadSidebarListener;
use OCA\Libresign\Listener\MailNotifyListener;
use OCA\Libresign\Listener\NotificationListener;
use OCA\Libresign\Listener\SignedListener;
use OCA\Libresign\Middleware\GlobalInjectionMiddleware;
Expand Down Expand Up @@ -77,5 +78,8 @@ public function register(IRegistrationContext $context): void {

// Notification listeners
$context->registerEventListener(SendSignNotificationEvent::class, NotificationListener::class);

// MailNotify listener
$context->registerEventListener(SendSignNotificationEvent::class, MailNotifyListener::class);
}
}
8 changes: 8 additions & 0 deletions lib/Command/Developer/Reset.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ private function resetActivity(string $user): void {
$delete->andWhere($delete->expr()->eq('amq_affecteduser', $delete->createNamedParameter($user)));
}
$delete->executeStatement();

$delete = $this->db->getQueryBuilder();
$delete->delete('activity')
->where($delete->expr()->eq('app', $delete->createNamedParameter(Application::APP_ID)));
if ($user) {
$delete->andWhere($delete->expr()->eq('user', $delete->createNamedParameter($user)));
}
$delete->executeStatement();
} catch (\Throwable $e) {
}
}
Expand Down
Loading
Loading