Skip to content

Commit bb434e7

Browse files
authored
Merge pull request #2573 from LibreSign/feature/store-notification-history
Store notification history at sign_request table
2 parents 9dcae58 + a48e4f9 commit bb434e7

File tree

13 files changed

+92
-116
lines changed

13 files changed

+92
-116
lines changed

lib/Activity/Listener.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use OCA\Libresign\AppInfo\Application;
2828
use OCA\Libresign\Db\File as FileEntity;
2929
use OCA\Libresign\Db\SignRequest;
30+
use OCA\Libresign\Db\SignRequestMapper;
3031
use OCA\Libresign\Events\SendSignNotificationEvent;
3132
use OCA\Libresign\Service\AccountService;
3233
use OCA\Libresign\Service\IdentifyMethod\IIdentifyMethod;
@@ -50,6 +51,7 @@ public function __construct(
5051
protected ITimeFactory $timeFactory,
5152
protected AccountService $accountService,
5253
protected IURLGenerator $url,
54+
private SignRequestMapper $signRequestMapper,
5355
) {
5456
}
5557

@@ -60,7 +62,6 @@ public function handle(Event $event): void {
6062
$event->getSignRequest(),
6163
$event->getLibreSignFile(),
6264
$event->getIdentifyMethod(),
63-
$event->isNew()
6465
),
6566
};
6667
}
@@ -75,7 +76,6 @@ protected function generateNewSignNotificationActivity(
7576
SignRequest $signRequest,
7677
FileEntity $libreSignFile,
7778
IIdentifyMethod $identifyMethod,
78-
bool $isNew
7979
): void {
8080
$actor = $this->userSession->getUser();
8181
if (!$actor instanceof IUser) {
@@ -96,7 +96,8 @@ protected function generateNewSignNotificationActivity(
9696
// At notification app we can define the view and dismiss action
9797
// Activity dont have this feature
9898
->setGenerateNotification(false);
99-
if ($isNew) {
99+
$isFirstNotification = $this->signRequestMapper->incrementNotificationCounter($signRequest, 'activity');
100+
if ($isFirstNotification) {
100101
$subject = 'new_sign_request';
101102
} else {
102103
$subject = 'update_sign_request';

lib/Db/SignRequestMapper.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class SignRequestMapper extends QBMapper {
4848
* @var SignRequest[]
4949
*/
5050
private $signers = [];
51+
private bool $firstNotification = false;
5152

5253
public function __construct(
5354
IDBConnection $db,
@@ -60,6 +61,33 @@ public function __construct(
6061
parent::__construct($db, 'libresign_sign_request');
6162
}
6263

64+
/**
65+
* @return boolean true when is the first notification
66+
*/
67+
public function incrementNotificationCounter(SignRequest $signRequest, string $method): bool {
68+
$this->db->beginTransaction();
69+
try {
70+
$fromDatabase = $this->getById($signRequest->getId());
71+
$metadata = $fromDatabase->getMetadata();
72+
if (!empty($metadata)) {
73+
$metadata = json_decode($metadata, true);
74+
}
75+
if (!isset($metadata['notify'])) {
76+
$this->firstNotification = true;
77+
}
78+
$metadata['notify'][] = [
79+
'method' => $method,
80+
'date' => time(),
81+
];
82+
$fromDatabase->setMetadata($metadata);
83+
$this->update($fromDatabase);
84+
$this->db->commit();
85+
} catch (\Throwable) {
86+
$this->db->rollBack();
87+
}
88+
return $this->firstNotification;
89+
}
90+
6391
/**
6492
* @inheritDoc
6593
*/

lib/Events/SendSignNotificationEvent.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public function __construct(
3434
private SignRequest $signRequest,
3535
private FileEntity $libreSignFile,
3636
private IIdentifyMethod $identifyMethod,
37-
private bool $isNew
3837
) {
3938
}
4039

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

49-
public function isNew(): bool {
50-
return $this->isNew;
51-
}
52-
5348
public function getIdentifyMethod(): IIdentifyMethod {
5449
return $this->identifyMethod;
5550
}

lib/Listener/MailNotifyListener.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
use OCA\Libresign\Db\File as FileEntity;
2828
use OCA\Libresign\Db\SignRequest;
29+
use OCA\Libresign\Db\SignRequestMapper;
2930
use OCA\Libresign\Events\SendSignNotificationEvent;
3031
use OCA\Libresign\Service\IdentifyMethod\IdentifyMethodService;
3132
use OCA\Libresign\Service\IdentifyMethod\IIdentifyMethod;
@@ -43,6 +44,7 @@ public function __construct(
4344
protected IUserManager $userManager,
4445
protected IdentifyMethodService $identifyMethodService,
4546
protected MailService $mail,
47+
private SignRequestMapper $signRequestMapper,
4648
private LoggerInterface $logger,
4749
) {
4850
}
@@ -54,7 +56,6 @@ public function handle(Event $event): void {
5456
$event->getSignRequest(),
5557
$event->getLibreSignFile(),
5658
$event->getIdentifyMethod(),
57-
$event->isNew()
5859
),
5960
};
6061
}
@@ -63,7 +64,6 @@ protected function sendMailNotification(
6364
SignRequest $signRequest,
6465
FileEntity $libreSignFile,
6566
IIdentifyMethod $identifyMethod,
66-
bool $isNew,
6767
): void {
6868
$actor = $this->userSession->getUser();
6969
if (!$actor instanceof IUser) {
@@ -81,7 +81,8 @@ protected function sendMailNotification(
8181
if (empty($email)) {
8282
return;
8383
}
84-
if ($isNew) {
84+
$isFirstNotification = $this->signRequestMapper->incrementNotificationCounter($signRequest, 'mail');
85+
if ($isFirstNotification) {
8586
$this->mail->notifyUnsignedUser($signRequest, $email);
8687
return;
8788
}

lib/Listener/NotificationListener.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use OCA\Libresign\AppInfo\Application as AppInfoApplication;
2828
use OCA\Libresign\Db\File as FileEntity;
2929
use OCA\Libresign\Db\SignRequest;
30+
use OCA\Libresign\Db\SignRequestMapper;
3031
use OCA\Libresign\Events\SendSignNotificationEvent;
3132
use OCA\Libresign\Service\IdentifyMethod\IIdentifyMethod;
3233
use OCP\AppFramework\Utility\ITimeFactory;
@@ -46,25 +47,24 @@ public function __construct(
4647
protected IUserSession $userSession,
4748
private ITimeFactory $timeFactory,
4849
protected IURLGenerator $url,
50+
private SignRequestMapper $signRequestMapper,
4951
) {
5052
}
5153

5254
public function handle(Event $event): void {
5355
if ($event instanceof SendSignNotificationEvent) {
54-
$this->sendNewSignNotification(
56+
$this->sendSignNotification(
5557
$event->getSignRequest(),
5658
$event->getLibreSignFile(),
5759
$event->getIdentifyMethod(),
58-
$event->isNew()
5960
);
6061
}
6162
}
6263

63-
private function sendNewSignNotification(
64+
private function sendSignNotification(
6465
SignRequest $signRequest,
6566
FileEntity $libreSignFile,
6667
IIdentifyMethod $identifyMethod,
67-
bool $isNew
6868
): void {
6969
$actor = $this->userSession->getUser();
7070
if (!$actor instanceof IUser) {
@@ -79,7 +79,8 @@ private function sendNewSignNotification(
7979
->setObject('signRequest', (string) $signRequest->getId())
8080
->setDateTime((new \DateTime())->setTimestamp($this->timeFactory->now()->getTimestamp()))
8181
->setUser($identifyMethod->getEntity()->getIdentifierValue());
82-
if ($isNew) {
82+
$isFirstNotification = $this->signRequestMapper->incrementNotificationCounter($signRequest, 'notify');
83+
if ($isFirstNotification) {
8384
$subject = 'new_sign_request';
8485
} else {
8586
$subject = 'update_sign_request';

lib/Service/IdentifyMethod/AbstractIdentifyMethod.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function getSettings(): array {
103103
return $this->settings;
104104
}
105105

106-
public function notify(bool $isNew): bool {
106+
public function notify(): bool {
107107
if (!$this->willNotify) {
108108
return false;
109109
}
@@ -112,8 +112,7 @@ public function notify(bool $isNew): bool {
112112
$this->identifyMethodService->getEventDispatcher()->dispatchTyped(new SendSignNotificationEvent(
113113
$signRequest,
114114
$libresignFile,
115-
$this,
116-
$isNew
115+
$this
117116
));
118117
return true;
119118
}
@@ -189,8 +188,8 @@ protected function updateIdentifiedAt(): void {
189188
}
190189
$this->getEntity()->setIdentifiedAtDate($this->identifyMethodService->getTimeFactory()->getDateTime());
191190
$this->willNotify = false;
192-
$isNew = $this->identifyMethodService->save($this->getEntity());
193-
$this->notify($isNew);
191+
$this->identifyMethodService->save($this->getEntity());
192+
$this->notify();
194193
}
195194

196195
protected function throwIfRenewalIntervalExpired(): void {
@@ -320,8 +319,8 @@ private function applyDefault(array $customConfig, array $default): array {
320319
}
321320

322321
public function save(): void {
323-
$isNew = $this->identifyMethodService->save($this->getEntity());
324-
$this->notify($isNew);
322+
$this->identifyMethodService->save($this->getEntity());
323+
$this->notify();
325324
}
326325

327326
public function delete(): void {

lib/Service/IdentifyMethod/IIdentifyMethod.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function getSignatureMethods(): array;
4343
public function signatureMethodsToArray(): array;
4444
public function getSettings(): array;
4545
public function willNotifyUser(bool $willNotify): void;
46-
public function notify(bool $isNew): bool;
46+
public function notify(): bool;
4747
public function validateToRequest(): void;
4848
public function validateToCreateAccount(string $value): void;
4949
public function validateToIdentify(): void;

lib/Service/IdentifyMethod/IdentifyMethodService.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,14 @@ public function __construct(
6060
) {
6161
}
6262

63-
/**
64-
* @return boolean is new instance
65-
*/
66-
public function save(IdentifyMethod $identifyMethod): bool {
63+
public function save(IdentifyMethod $identifyMethod): void {
6764
$this->refreshIdFromDatabaseIfNecessary($identifyMethod);
6865
if ($identifyMethod->getId()) {
6966
$this->identifyMethodMapper->update($identifyMethod);
70-
return false;
67+
return;
7168
}
7269
$this->identifyMethodMapper->insertOrUpdate($identifyMethod);
73-
return true;
70+
return;
7471
}
7572

7673
public function delete(IdentifyMethod $identifyMethod): void {

tests/integration/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"rpkamp/mailhog-behat-extension": "^1.0"
1010
},
1111
"require-dev": {
12-
"libresign/nextcloud-behat": "^0.12.0"
12+
"libresign/nextcloud-behat": "^0.14.1"
1313
},
1414
"config": {
1515
"allow-plugins": {

tests/integration/composer.lock

Lines changed: 13 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)