Skip to content
Merged
Show file tree
Hide file tree
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
45 changes: 13 additions & 32 deletions lib/Listener/MailNotifyListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use OCA\Libresign\Service\IdentifyMethod\IdentifyService;
use OCA\Libresign\Service\IdentifyMethod\IIdentifyMethod;
use OCA\Libresign\Service\MailService;
use OCA\Libresign\Service\NotificationPreferenceResolver;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\IUser;
Expand All @@ -33,6 +34,7 @@ public function __construct(
protected MailService $mail,
private SignRequestMapper $signRequestMapper,
private LoggerInterface $logger,
private NotificationPreferenceResolver $notificationPreferenceResolver,
) {
}

Expand Down Expand Up @@ -82,7 +84,11 @@ protected function sendSignMailNotification(
$users = $this->userManager->getByEmail($email);
if (count($users) === 1) {
$userId = $users[0]->getUID();
if ($this->isNotificationDisabledAtActivity($userId, SendSignNotificationEvent::FILE_TO_SIGN)) {
if ($this->notificationPreferenceResolver->isEmailNotificationDisabled(
$userId,
SendSignNotificationEvent::FILE_TO_SIGN,
true,
)) {
return;
}
}
Expand Down Expand Up @@ -112,7 +118,11 @@ protected function sendSignedMailNotification(
if ($identifyMethod->getEntity()->isDeletedAccount()) {
return;
}
if ($this->isNotificationDisabledAtActivity($libreSignFile->getUserId(), SignedEvent::FILE_SIGNED)) {
if ($this->notificationPreferenceResolver->isEmailNotificationDisabled(
$libreSignFile->getUserId(),
SignedEvent::FILE_SIGNED,
true,
)) {
return;
}

Expand Down Expand Up @@ -158,7 +168,7 @@ protected function sendCanceledMailNotification(
$users = $this->userManager->getByEmail($email);
if (count($users) === 1) {
$userId = $users[0]->getUID();
if ($this->isNotificationDisabledAtActivity($userId, SignRequestCanceledEvent::SIGN_REQUEST_CANCELED)) {
if ($this->notificationPreferenceResolver->isEmailNotificationDisabled($userId, SignRequestCanceledEvent::SIGN_REQUEST_CANCELED, true)) {
return;
}
}
Expand All @@ -170,33 +180,4 @@ protected function sendCanceledMailNotification(
return;
}
}

private function isNotificationDisabledAtActivity(string $userId, string $type): bool {
if (!class_exists(\OCA\Activity\UserSettings::class)) {
return false;
}
$activityUserSettings = \OCP\Server::get(\OCA\Activity\UserSettings::class);
if ($activityUserSettings) {
$manager = \OCP\Server::get(\OCP\Activity\IManager::class);
try {
$manager->getSettingById($type);
} catch (\Exception) {
return false;
}

$adminSetting = $activityUserSettings->getAdminSetting('email', $type);
if (!$adminSetting) {
return true;
}
$notificationSetting = $activityUserSettings->getUserSetting(
$userId,
'email',
$type
);
if (!$notificationSetting) {
return true;
}
}
return false;
}
}
41 changes: 8 additions & 33 deletions lib/Listener/NotificationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use OCA\Libresign\Events\SignedEvent;
use OCA\Libresign\Events\SignRequestCanceledEvent;
use OCA\Libresign\Service\IdentifyMethod\IIdentifyMethod;
use OCA\Libresign\Service\NotificationPreferenceResolver;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
Expand All @@ -34,6 +35,7 @@ public function __construct(
private ITimeFactory $timeFactory,
protected IURLGenerator $url,
private SignRequestMapper $signRequestMapper,
private NotificationPreferenceResolver $notificationPreferenceResolver,
) {
}

Expand Down Expand Up @@ -72,9 +74,10 @@ private function sendSignNotification(
if ($identifyMethod->getEntity()->isDeletedAccount()) {
return;
}
$notificationDisabled = $this->isNotificationDisabledAtActivity(
$notificationDisabled = $this->notificationPreferenceResolver->isInAppNotificationDisabled(
$identifyMethod->getEntity()->getIdentifierValue(),
SendSignNotificationEvent::FILE_TO_SIGN,
true,
);
if ($notificationDisabled) {
return;
Expand Down Expand Up @@ -117,9 +120,10 @@ private function sendSignedNotification(
if ($identifyMethod->getEntity()->isDeletedAccount()) {
return;
}
$notificationDisabled = $this->isNotificationDisabledAtActivity(
$notificationDisabled = $this->notificationPreferenceResolver->isInAppNotificationDisabled(
$libreSignFile->getUserId(),
SignedEvent::FILE_SIGNED,
true,
);
if ($notificationDisabled) {
return;
Expand Down Expand Up @@ -169,9 +173,10 @@ private function sendCanceledNotification(
if ($identifyMethod->getEntity()->isDeletedAccount()) {
return;
}
$notificationDisabled = $this->isNotificationDisabledAtActivity(
$notificationDisabled = $this->notificationPreferenceResolver->isInAppNotificationDisabled(
$identifyMethod->getEntity()->getIdentifierValue(),
SignRequestCanceledEvent::SIGN_REQUEST_CANCELED,
true,
);
if ($notificationDisabled) {
return;
Expand Down Expand Up @@ -204,36 +209,6 @@ private function sendCanceledNotification(
$this->notificationManager->notify($notification);
}

private function isNotificationDisabledAtActivity(string $userId, string $type): bool {
if (!class_exists(\OCA\Activity\UserSettings::class)) {
return false;
}
$activityUserSettings = \OCP\Server::get(\OCA\Activity\UserSettings::class);
if ($activityUserSettings) {
$manager = \OCP\Server::get(\OCP\Activity\IManager::class);
try {
$manager->getSettingById($type);
} catch (\Exception) {
return false;
}

$adminSetting = $activityUserSettings->getAdminSetting('notification', $type);
if (!$adminSetting) {
return true;
}

$notificationSetting = $activityUserSettings->getUserSetting(
$userId,
'notification',
$type
);
if ($notificationSetting === false) {
return true;
}
}
return false;
}

/**
* @psalm-return array{type: 'file', id: string, name: string, path: string, link: string}
*/
Expand Down
64 changes: 64 additions & 0 deletions lib/Service/ActivitySettingsStore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Libresign\Service;

class ActivitySettingsStore {
private const USER_SETTINGS_CLASS = 'OCA\\Activity\\UserSettings';

private bool $resolved = false;
private ?object $activityUserSettings = null;

public function isAvailable(): bool {
return $this->getActivityUserSettings() !== null;
}

public function hasSetting(string $type): bool {
if (!$this->isAvailable()) {
return false;
}

try {
$manager = \OCP\Server::get(\OCP\Activity\IManager::class);
$manager->getSettingById($type);
return true;
} catch (\Throwable) {
return false;
}
}

public function getAdminSetting(string $channel, string $type): mixed {
return $this->getActivityUserSettings()?->getAdminSetting($channel, $type);
}

public function getUserSetting(string $userId, string $channel, string $type): mixed {
return $this->getActivityUserSettings()?->getUserSetting($userId, $channel, $type);
}

private function getActivityUserSettings(): ?object {
if ($this->resolved) {
return $this->activityUserSettings;
}

$this->resolved = true;
if (!class_exists(self::USER_SETTINGS_CLASS)) {
return null;
}

try {
$activityUserSettings = \OCP\Server::get(self::USER_SETTINGS_CLASS);
if (is_object($activityUserSettings)) {
$this->activityUserSettings = $activityUserSettings;
}
} catch (\Throwable) {
$this->activityUserSettings = null;
}

return $this->activityUserSettings;
}
}
27 changes: 7 additions & 20 deletions lib/Service/Identify/ResultEnricher.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use OCA\Libresign\Service\IdentifyMethod\Account;
use OCA\Libresign\Service\IdentifyMethod\Email;
use OCA\Libresign\Service\NotificationPreferenceResolver;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
Expand All @@ -20,6 +21,7 @@ public function __construct(
private IUserManager $userManager,
private Email $identifyEmailMethod,
private Account $identifyAccountMethod,
private NotificationPreferenceResolver $notificationPreferenceResolver,
) {
}

Expand Down Expand Up @@ -111,7 +113,11 @@ public function addEmailNotificationPreference(array $list): array {
continue;
}

$acceptsNotifications = !$this->isNotificationDisabledAtActivity($user->getUID(), 'libresign_file_to_sign');
$acceptsNotifications = !$this->notificationPreferenceResolver->isEmailNotificationDisabled(
$user->getUID(),
'libresign_file_to_sign',
false,
);

if ($acceptsNotifications) {
$list[$key]['emailAddress'] = $email;
Expand All @@ -126,23 +132,4 @@ private function userMatchesSearch(IUser $user, string $searchLower): bool {
|| str_contains(strtolower($user->getDisplayName()), $searchLower)
|| ($user->getEMailAddress() !== null && str_contains($user->getEMailAddress(), $searchLower));
}

private function isNotificationDisabledAtActivity(string $userId, string $type): bool {
if (!class_exists(\OCA\Activity\UserSettings::class)) {
return false;
}
$activityUserSettings = \OCP\Server::get(\OCA\Activity\UserSettings::class);

$adminSetting = $activityUserSettings->getAdminSetting('email', $type);
if (!$adminSetting) {
return true;
}

$userSetting = $activityUserSettings->getUserSetting($userId, 'email', $type);
if (!$userSetting) {
return true;
}

return false;
}
}
86 changes: 86 additions & 0 deletions lib/Service/NotificationPreferenceResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Libresign\Service;

use OCP\Config\IUserConfig;
use OCP\IAppConfig;

class NotificationPreferenceResolver {
public function __construct(
private IAppConfig $appConfig,
private IUserConfig $userConfig,
private ActivitySettingsStore $activitySettingsStore,
) {
}

public function isEmailNotificationDisabled(
string $userId,
string $type,
bool $requireKnownActivitySetting = true,
): bool {
return $this->isNotificationDisabled($userId, 'email', $type, $requireKnownActivitySetting);
}

public function isInAppNotificationDisabled(
string $userId,
string $type,
bool $requireKnownActivitySetting = true,
): bool {
return $this->isNotificationDisabled($userId, 'notification', $type, $requireKnownActivitySetting);
}

private function isNotificationDisabled(
string $userId,
string $channel,
string $type,
bool $requireKnownActivitySetting,
): bool {
if ($this->activitySettingsStore->isAvailable()) {
if ($requireKnownActivitySetting && !$this->activitySettingsStore->hasSetting($type)) {
return false;
}

$adminSetting = $this->activitySettingsStore->getAdminSetting($channel, $type);
if (!$this->isActivitySettingEnabled($adminSetting)) {
return true;
}

$userSetting = $this->activitySettingsStore->getUserSetting($userId, $channel, $type);
if (!$this->isActivitySettingEnabled($userSetting)) {
return true;
}

return false;
}

$configKey = sprintf('notify_%s_%s', $channel, $type);
if (!$this->isActivitySettingEnabled($this->appConfig->getValueString('activity', $configKey, '1'))) {
return true;
}

return !$this->isActivitySettingEnabled(
$this->userConfig->getValueString($userId, 'activity', $configKey, '1')
);
}

private function isActivitySettingEnabled(mixed $setting): bool {
if (is_bool($setting)) {
return $setting;
}
if (is_int($setting)) {
return $setting === 1;
}
if ($setting === null) {
return true;
}

$normalized = strtolower(trim((string)$setting));
return !in_array($normalized, ['', '0', 'false', 'off', 'no'], true);
}
}
Loading
Loading