Skip to content

Commit

Permalink
Merge pull request #2573 from LibreSign/feature/store-notification-hi…
Browse files Browse the repository at this point in the history
…story

Store notification history at sign_request table
  • Loading branch information
vitormattos authored Mar 22, 2024
2 parents 9dcae58 + a48e4f9 commit bb434e7
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 116 deletions.
7 changes: 4 additions & 3 deletions lib/Activity/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\Db\File as FileEntity;
use OCA\Libresign\Db\SignRequest;
use OCA\Libresign\Db\SignRequestMapper;
use OCA\Libresign\Events\SendSignNotificationEvent;
use OCA\Libresign\Service\AccountService;
use OCA\Libresign\Service\IdentifyMethod\IIdentifyMethod;
Expand All @@ -50,6 +51,7 @@ public function __construct(
protected ITimeFactory $timeFactory,
protected AccountService $accountService,
protected IURLGenerator $url,
private SignRequestMapper $signRequestMapper,
) {
}

Expand All @@ -60,7 +62,6 @@ public function handle(Event $event): void {
$event->getSignRequest(),
$event->getLibreSignFile(),
$event->getIdentifyMethod(),
$event->isNew()
),
};
}
Expand All @@ -75,7 +76,6 @@ protected function generateNewSignNotificationActivity(
SignRequest $signRequest,
FileEntity $libreSignFile,
IIdentifyMethod $identifyMethod,
bool $isNew
): void {
$actor = $this->userSession->getUser();
if (!$actor instanceof IUser) {
Expand All @@ -96,7 +96,8 @@ protected function generateNewSignNotificationActivity(
// At notification app we can define the view and dismiss action
// Activity dont have this feature
->setGenerateNotification(false);
if ($isNew) {
$isFirstNotification = $this->signRequestMapper->incrementNotificationCounter($signRequest, 'activity');
if ($isFirstNotification) {
$subject = 'new_sign_request';
} else {
$subject = 'update_sign_request';
Expand Down
28 changes: 28 additions & 0 deletions lib/Db/SignRequestMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class SignRequestMapper extends QBMapper {
* @var SignRequest[]
*/
private $signers = [];
private bool $firstNotification = false;

public function __construct(
IDBConnection $db,
Expand All @@ -60,6 +61,33 @@ public function __construct(
parent::__construct($db, 'libresign_sign_request');
}

/**
* @return boolean true when is the first notification
*/
public function incrementNotificationCounter(SignRequest $signRequest, string $method): bool {
$this->db->beginTransaction();
try {
$fromDatabase = $this->getById($signRequest->getId());
$metadata = $fromDatabase->getMetadata();
if (!empty($metadata)) {
$metadata = json_decode($metadata, true);
}
if (!isset($metadata['notify'])) {
$this->firstNotification = true;
}
$metadata['notify'][] = [
'method' => $method,
'date' => time(),
];
$fromDatabase->setMetadata($metadata);
$this->update($fromDatabase);
$this->db->commit();
} catch (\Throwable) {
$this->db->rollBack();
}
return $this->firstNotification;
}

/**
* @inheritDoc
*/
Expand Down
5 changes: 0 additions & 5 deletions lib/Events/SendSignNotificationEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public function __construct(
private SignRequest $signRequest,
private FileEntity $libreSignFile,
private IIdentifyMethod $identifyMethod,
private bool $isNew
) {
}

Expand All @@ -46,10 +45,6 @@ public function getSignRequest(): SignRequest {
return $this->signRequest;
}

public function isNew(): bool {
return $this->isNew;
}

public function getIdentifyMethod(): IIdentifyMethod {
return $this->identifyMethod;
}
Expand Down
7 changes: 4 additions & 3 deletions lib/Listener/MailNotifyListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

use OCA\Libresign\Db\File as FileEntity;
use OCA\Libresign\Db\SignRequest;
use OCA\Libresign\Db\SignRequestMapper;
use OCA\Libresign\Events\SendSignNotificationEvent;
use OCA\Libresign\Service\IdentifyMethod\IdentifyMethodService;
use OCA\Libresign\Service\IdentifyMethod\IIdentifyMethod;
Expand All @@ -43,6 +44,7 @@ public function __construct(
protected IUserManager $userManager,
protected IdentifyMethodService $identifyMethodService,
protected MailService $mail,
private SignRequestMapper $signRequestMapper,
private LoggerInterface $logger,
) {
}
Expand All @@ -54,7 +56,6 @@ public function handle(Event $event): void {
$event->getSignRequest(),
$event->getLibreSignFile(),
$event->getIdentifyMethod(),
$event->isNew()
),
};
}
Expand All @@ -63,7 +64,6 @@ protected function sendMailNotification(
SignRequest $signRequest,
FileEntity $libreSignFile,
IIdentifyMethod $identifyMethod,
bool $isNew,
): void {
$actor = $this->userSession->getUser();
if (!$actor instanceof IUser) {
Expand All @@ -81,7 +81,8 @@ protected function sendMailNotification(
if (empty($email)) {
return;
}
if ($isNew) {
$isFirstNotification = $this->signRequestMapper->incrementNotificationCounter($signRequest, 'mail');
if ($isFirstNotification) {
$this->mail->notifyUnsignedUser($signRequest, $email);
return;
}
Expand Down
11 changes: 6 additions & 5 deletions lib/Listener/NotificationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use OCA\Libresign\AppInfo\Application as AppInfoApplication;
use OCA\Libresign\Db\File as FileEntity;
use OCA\Libresign\Db\SignRequest;
use OCA\Libresign\Db\SignRequestMapper;
use OCA\Libresign\Events\SendSignNotificationEvent;
use OCA\Libresign\Service\IdentifyMethod\IIdentifyMethod;
use OCP\AppFramework\Utility\ITimeFactory;
Expand All @@ -46,25 +47,24 @@ public function __construct(
protected IUserSession $userSession,
private ITimeFactory $timeFactory,
protected IURLGenerator $url,
private SignRequestMapper $signRequestMapper,
) {
}

public function handle(Event $event): void {
if ($event instanceof SendSignNotificationEvent) {
$this->sendNewSignNotification(
$this->sendSignNotification(
$event->getSignRequest(),
$event->getLibreSignFile(),
$event->getIdentifyMethod(),
$event->isNew()
);
}
}

private function sendNewSignNotification(
private function sendSignNotification(
SignRequest $signRequest,
FileEntity $libreSignFile,
IIdentifyMethod $identifyMethod,
bool $isNew
): void {
$actor = $this->userSession->getUser();
if (!$actor instanceof IUser) {
Expand All @@ -79,7 +79,8 @@ private function sendNewSignNotification(
->setObject('signRequest', (string) $signRequest->getId())
->setDateTime((new \DateTime())->setTimestamp($this->timeFactory->now()->getTimestamp()))
->setUser($identifyMethod->getEntity()->getIdentifierValue());
if ($isNew) {
$isFirstNotification = $this->signRequestMapper->incrementNotificationCounter($signRequest, 'notify');
if ($isFirstNotification) {
$subject = 'new_sign_request';
} else {
$subject = 'update_sign_request';
Expand Down
13 changes: 6 additions & 7 deletions lib/Service/IdentifyMethod/AbstractIdentifyMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function getSettings(): array {
return $this->settings;
}

public function notify(bool $isNew): bool {
public function notify(): bool {
if (!$this->willNotify) {
return false;
}
Expand All @@ -112,8 +112,7 @@ public function notify(bool $isNew): bool {
$this->identifyMethodService->getEventDispatcher()->dispatchTyped(new SendSignNotificationEvent(
$signRequest,
$libresignFile,
$this,
$isNew
$this
));
return true;
}
Expand Down Expand Up @@ -189,8 +188,8 @@ protected function updateIdentifiedAt(): void {
}
$this->getEntity()->setIdentifiedAtDate($this->identifyMethodService->getTimeFactory()->getDateTime());
$this->willNotify = false;
$isNew = $this->identifyMethodService->save($this->getEntity());
$this->notify($isNew);
$this->identifyMethodService->save($this->getEntity());
$this->notify();
}

protected function throwIfRenewalIntervalExpired(): void {
Expand Down Expand Up @@ -320,8 +319,8 @@ private function applyDefault(array $customConfig, array $default): array {
}

public function save(): void {
$isNew = $this->identifyMethodService->save($this->getEntity());
$this->notify($isNew);
$this->identifyMethodService->save($this->getEntity());
$this->notify();
}

public function delete(): void {
Expand Down
2 changes: 1 addition & 1 deletion lib/Service/IdentifyMethod/IIdentifyMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function getSignatureMethods(): array;
public function signatureMethodsToArray(): array;
public function getSettings(): array;
public function willNotifyUser(bool $willNotify): void;
public function notify(bool $isNew): bool;
public function notify(): bool;
public function validateToRequest(): void;
public function validateToCreateAccount(string $value): void;
public function validateToIdentify(): void;
Expand Down
9 changes: 3 additions & 6 deletions lib/Service/IdentifyMethod/IdentifyMethodService.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,14 @@ public function __construct(
) {
}

/**
* @return boolean is new instance
*/
public function save(IdentifyMethod $identifyMethod): bool {
public function save(IdentifyMethod $identifyMethod): void {
$this->refreshIdFromDatabaseIfNecessary($identifyMethod);
if ($identifyMethod->getId()) {
$this->identifyMethodMapper->update($identifyMethod);
return false;
return;
}
$this->identifyMethodMapper->insertOrUpdate($identifyMethod);
return true;
return;
}

public function delete(IdentifyMethod $identifyMethod): void {
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"rpkamp/mailhog-behat-extension": "^1.0"
},
"require-dev": {
"libresign/nextcloud-behat": "^0.12.0"
"libresign/nextcloud-behat": "^0.14.1"
},
"config": {
"allow-plugins": {
Expand Down
26 changes: 13 additions & 13 deletions tests/integration/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit bb434e7

Please sign in to comment.