Skip to content

Commit 9a9ca2b

Browse files
authored
Merge pull request #6216 from LibreSign/feat/custom-message-for-signers
feat: custom message for signers
2 parents f68ceae + 587d80d commit 9a9ca2b

19 files changed

Lines changed: 367 additions & 21 deletions

lib/Controller/IdentifyAccountController.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
2020
use OCP\AppFramework\Http\DataResponse;
2121
use OCP\Collaboration\Collaborators\ISearch;
22+
use OCP\IConfig;
2223
use OCP\IRequest;
2324
use OCP\IURLGenerator;
25+
use OCP\IUserManager;
2426
use OCP\IUserSession;
2527
use OCP\Share\IShare;
2628

@@ -36,6 +38,8 @@ public function __construct(
3638
private IURLGenerator $urlGenerator,
3739
private Email $identifyEmailMethod,
3840
private Account $identifyAccountMethod,
41+
private IUserManager $userManager,
42+
private IConfig $config,
3943
) {
4044
parent::__construct(Application::APP_ID, $request);
4145
}
@@ -76,6 +80,7 @@ public function search(string $search = '', string $method = '', int $page = 1,
7680
$return = $this->addHerselfAccount($return, $search);
7781
$return = $this->addHerselfEmail($return, $search);
7882
$return = $this->replaceShareTypeByMethod($return);
83+
$return = $this->addEmailNotificationPreference($return);
7984
$return = $this->excludeNotAllowed($return);
8085

8186
return new DataResponse($return);
@@ -239,4 +244,62 @@ private function replaceShareTypeByMethod(array $list): array {
239244
}
240245
return $list;
241246
}
247+
248+
private function addEmailNotificationPreference(array $list): array {
249+
foreach ($list as $key => $item) {
250+
if ($item['method'] !== 'account') {
251+
continue;
252+
}
253+
254+
$user = $this->userManager->get($item['id']);
255+
if ($user === null) {
256+
$list[$key]['acceptsEmailNotifications'] = false;
257+
continue;
258+
}
259+
260+
$email = $user->getEMailAddress();
261+
if (empty($email)) {
262+
$list[$key]['acceptsEmailNotifications'] = false;
263+
continue;
264+
}
265+
266+
if ($this->isNotificationDisabledAtActivity($user->getUID(), 'libresign_file_to_sign')) {
267+
$list[$key]['acceptsEmailNotifications'] = false;
268+
continue;
269+
}
270+
271+
$list[$key]['acceptsEmailNotifications'] = true;
272+
}
273+
return $list;
274+
}
275+
276+
private function isNotificationDisabledAtActivity(string $userId, string $type): bool {
277+
if (!class_exists(\OCA\Activity\UserSettings::class)) {
278+
return false;
279+
}
280+
$activityUserSettings = \OCP\Server::get(\OCA\Activity\UserSettings::class);
281+
if ($activityUserSettings) {
282+
$manager = \OCP\Server::get(\OCP\Activity\IManager::class);
283+
try {
284+
$manager->getSettingById($type);
285+
} catch (\Exception $e) {
286+
return false;
287+
}
288+
289+
$adminSetting = $activityUserSettings->getAdminSetting('email', $type);
290+
if (!$adminSetting) {
291+
return true;
292+
}
293+
294+
$notificationSetting = $activityUserSettings->getUserSetting(
295+
$userId,
296+
'email',
297+
$type
298+
);
299+
if (!$notificationSetting) {
300+
return true;
301+
}
302+
}
303+
return false;
304+
}
242305
}

lib/Db/IdentifyMethodMapper.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ public function searchByIdentifierValue(string $search, string $userId, string $
125125
->join('im', 'libresign_sign_request', 'sr',
126126
$qb->expr()->eq('sr.id', 'im.sign_request_id'),
127127
)
128+
->where($qb->expr()->neq('im.identifier_value', $qb->createNamedParameter('deleted_users')))
128129
->setMaxResults($limit)
129130
->setFirstResult($offset);
130131

lib/Db/SignRequestMapper.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,17 @@ public function incrementNotificationCounter(SignRequest $signRequest, string $m
6060
if (!isset($metadata['notify'])) {
6161
$this->firstNotification = true;
6262
}
63-
$metadata['notify'][] = [
63+
64+
$notificationEntry = [
6465
'method' => $method,
6566
'date' => time(),
6667
];
68+
69+
if (!empty($fromDatabase->getDescription())) {
70+
$notificationEntry['description'] = $fromDatabase->getDescription();
71+
}
72+
73+
$metadata['notify'][] = $notificationEntry;
6774
$fromDatabase->setMetadata($metadata);
6875
$this->update($fromDatabase);
6976
$this->db->commit();

lib/Listener/MailNotifyListener.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ protected function sendSignMailNotification(
8989

9090
$isFirstNotification = $this->signRequestMapper->incrementNotificationCounter($signRequest, 'mail');
9191
if ($isFirstNotification) {
92-
$this->mail->notifyUnsignedUser($signRequest, $email);
92+
$this->mail->notifyUnsignedUser($signRequest, $email, $signRequest->getDescription());
9393
return;
9494
}
95-
$this->mail->notifySignDataUpdated($signRequest, $email);
95+
$this->mail->notifySignDataUpdated($signRequest, $email, $signRequest->getDescription());
9696
} catch (\InvalidArgumentException $e) {
9797
$this->logger->error($e->getMessage(), ['exception' => $e]);
9898
return;
@@ -174,6 +174,17 @@ private function isNotificationDisabledAtActivity(string $userId, string $type):
174174
}
175175
$activityUserSettings = \OCP\Server::get(\OCA\Activity\UserSettings::class);
176176
if ($activityUserSettings) {
177+
$manager = \OCP\Server::get(\OCP\Activity\IManager::class);
178+
try {
179+
$manager->getSettingById($type);
180+
} catch (\Exception $e) {
181+
return false;
182+
}
183+
184+
$adminSetting = $activityUserSettings->getAdminSetting('email', $type);
185+
if (!$adminSetting) {
186+
return true;
187+
}
177188
$notificationSetting = $activityUserSettings->getUserSetting(
178189
$userId,
179190
'email',

lib/Listener/NotificationListener.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,23 @@ private function isNotificationDisabledAtActivity(string $userId, string $type):
210210
}
211211
$activityUserSettings = \OCP\Server::get(\OCA\Activity\UserSettings::class);
212212
if ($activityUserSettings) {
213+
$manager = \OCP\Server::get(\OCP\Activity\IManager::class);
214+
try {
215+
$manager->getSettingById($type);
216+
} catch (\Exception $e) {
217+
return false;
218+
}
219+
220+
$adminSetting = $activityUserSettings->getAdminSetting('notification', $type);
221+
if (!$adminSetting) {
222+
return true;
223+
}
224+
213225
$notificationSetting = $activityUserSettings->getUserSetting(
214226
$userId,
215227
'notification',
216228
$type
217229
);
218-
// If setting is explicitly false, notifications are disabled
219-
// If setting is null/not configured, notifications are enabled by default
220230
if ($notificationSetting === false) {
221231
return true;
222232
}

lib/Listener/TwofactorGatewayListener.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,16 @@ protected function sendSignNotification(
8181
}
8282

8383
$isFirstNotification = $this->signRequestMapper->incrementNotificationCounter($signRequest, $entity->getIdentifierKey());
84+
85+
$message = '';
86+
if (!empty($signRequest->getDescription())) {
87+
$message = $signRequest->getDescription() . "\n\n";
88+
}
89+
8490
if ($isFirstNotification) {
85-
$message = $this->l10n->t('There is a document for you to sign. Access the link below:');
91+
$message .= $this->l10n->t('There is a document for you to sign. Access the link below:');
8692
} else {
87-
$message = $this->l10n->t('Changes have been made in a file that you have to sign. Access the link below:');
93+
$message .= $this->l10n->t('Changes have been made in a file that you have to sign. Access the link below:');
8894
}
8995
$message .= "\n";
9096
$link = $this->urlGenerator->linkToRouteAbsolute('libresign.page.sign', ['uuid' => $signRequest->getUuid()]);

lib/ResponseDefinitions.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
* email?: string,
2929
* account?: string,
3030
* },
31+
* displayName?: string,
32+
* description?: string,
33+
* notify?: non-negative-int,
3134
* signingOrder?: non-negative-int,
3235
* }
3336
* @psalm-type LibresignNewFile = array{
@@ -55,6 +58,7 @@
5558
* subname: string,
5659
* shareType: 0|4,
5760
* icon?: 'icon-mail'|'icon-user',
61+
* acceptsEmailNotifications?: boolean,
5862
* }
5963
* @psalm-type LibresignPagination = array{
6064
* total: non-negative-int,

lib/Service/MailService.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,18 @@ private function getFileById(int $fileId): File {
4545
/**
4646
* @psalm-suppress MixedMethodCall
4747
*/
48-
public function notifySignDataUpdated(SignRequest $data, string $email): void {
48+
public function notifySignDataUpdated(SignRequest $data, string $email, ?string $description = null): void {
4949
$emailTemplate = $this->mailer->createEMailTemplate('settings.TestEmail');
5050
// TRANSLATORS The subject of the email that is sent after changes are made to the signature request that may affect something for the signer who will sign the document. Some possible reasons: URL for signature changed (when the URL expires), the person who requested the signature sent a notification
5151
$emailTemplate->setSubject($this->l10n->t('LibreSign: Changes into a file for you to sign'));
5252
$emailTemplate->addHeader();
5353
$emailTemplate->addHeading($this->l10n->t('File to sign'), false);
54+
55+
if (!empty($description)) {
56+
$emailTemplate->addBodyText($description);
57+
$emailTemplate->addBodyText('');
58+
}
59+
5460
$emailTemplate->addBodyText($this->l10n->t('Changes have been made in a file that you have to sign. Access the link below:'));
5561
$link = $this->urlGenerator->linkToRouteAbsolute('libresign.page.sign', ['uuid' => $data->getUuid()]);
5662
$file = $this->getFileById($data->getFileId());
@@ -76,11 +82,17 @@ public function notifySignDataUpdated(SignRequest $data, string $email): void {
7682
/**
7783
* @psalm-suppress MixedMethodCall
7884
*/
79-
public function notifyUnsignedUser(SignRequest $data, string $email): void {
85+
public function notifyUnsignedUser(SignRequest $data, string $email, ?string $description = null): void {
8086
$emailTemplate = $this->mailer->createEMailTemplate('settings.TestEmail');
8187
$emailTemplate->setSubject($this->l10n->t('LibreSign: There is a file for you to sign'));
8288
$emailTemplate->addHeader();
8389
$emailTemplate->addHeading($this->l10n->t('File to sign'), false);
90+
91+
if (!empty($description)) {
92+
$emailTemplate->addBodyText($description);
93+
$emailTemplate->addBodyText('');
94+
}
95+
8496
$emailTemplate->addBodyText($this->l10n->t('There is a document for you to sign. Access the link below:'));
8597
$link = $this->urlGenerator->linkToRouteAbsolute('libresign.page.sign', ['uuid' => $data->getUuid()]);
8698
$file = $this->getFileById($data->getFileId());

openapi-full.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,9 @@
453453
"icon-mail",
454454
"icon-user"
455455
]
456+
},
457+
"acceptsEmailNotifications": {
458+
"type": "boolean"
456459
}
457460
}
458461
},
@@ -514,6 +517,17 @@
514517
}
515518
}
516519
},
520+
"displayName": {
521+
"type": "string"
522+
},
523+
"description": {
524+
"type": "string"
525+
},
526+
"notify": {
527+
"type": "integer",
528+
"format": "int64",
529+
"minimum": 0
530+
},
517531
"signingOrder": {
518532
"type": "integer",
519533
"format": "int64",

openapi.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,9 @@
383383
"icon-mail",
384384
"icon-user"
385385
]
386+
},
387+
"acceptsEmailNotifications": {
388+
"type": "boolean"
386389
}
387390
}
388391
},
@@ -444,6 +447,17 @@
444447
}
445448
}
446449
},
450+
"displayName": {
451+
"type": "string"
452+
},
453+
"description": {
454+
"type": "string"
455+
},
456+
"notify": {
457+
"type": "integer",
458+
"format": "int64",
459+
"minimum": 0
460+
},
447461
"signingOrder": {
448462
"type": "integer",
449463
"format": "int64",

0 commit comments

Comments
 (0)