Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.19% covered (warning)
85.19%
23 / 27
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
NotificationPreferenceResolver
85.19% covered (warning)
85.19%
23 / 27
60.00% covered (warning)
60.00%
3 / 5
14.64
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isEmailNotificationDisabled
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isInAppNotificationDisabled
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isNotificationDisabled
93.75% covered (success)
93.75%
15 / 16
0.00% covered (danger)
0.00%
0 / 1
7.01
 isActivitySettingEnabled
62.50% covered (warning)
62.50%
5 / 8
0.00% covered (danger)
0.00%
0 / 1
4.84
1<?php
2
3declare(strict_types=1);
4/**
5 * SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
6 * SPDX-License-Identifier: AGPL-3.0-or-later
7 */
8
9namespace OCA\Libresign\Service;
10
11use OCP\Config\IUserConfig;
12use OCP\IAppConfig;
13
14class NotificationPreferenceResolver {
15    public function __construct(
16        private IAppConfig $appConfig,
17        private IUserConfig $userConfig,
18        private ActivitySettingsStore $activitySettingsStore,
19    ) {
20    }
21
22    public function isEmailNotificationDisabled(
23        string $userId,
24        string $type,
25        bool $requireKnownActivitySetting = true,
26    ): bool {
27        return $this->isNotificationDisabled($userId, 'email', $type, $requireKnownActivitySetting);
28    }
29
30    public function isInAppNotificationDisabled(
31        string $userId,
32        string $type,
33        bool $requireKnownActivitySetting = true,
34    ): bool {
35        return $this->isNotificationDisabled($userId, 'notification', $type, $requireKnownActivitySetting);
36    }
37
38    private function isNotificationDisabled(
39        string $userId,
40        string $channel,
41        string $type,
42        bool $requireKnownActivitySetting,
43    ): bool {
44        if ($this->activitySettingsStore->isAvailable()) {
45            if ($requireKnownActivitySetting && !$this->activitySettingsStore->hasSetting($type)) {
46                return false;
47            }
48
49            $adminSetting = $this->activitySettingsStore->getAdminSetting($channel, $type);
50            if (!$this->isActivitySettingEnabled($adminSetting)) {
51                return true;
52            }
53
54            $userSetting = $this->activitySettingsStore->getUserSetting($userId, $channel, $type);
55            if (!$this->isActivitySettingEnabled($userSetting)) {
56                return true;
57            }
58
59            return false;
60        }
61
62        $configKey = sprintf('notify_%s_%s', $channel, $type);
63        if (!$this->isActivitySettingEnabled($this->appConfig->getValueString('activity', $configKey, '1'))) {
64            return true;
65        }
66
67        return !$this->isActivitySettingEnabled(
68            $this->userConfig->getValueString($userId, 'activity', $configKey, '1')
69        );
70    }
71
72    private function isActivitySettingEnabled(mixed $setting): bool {
73        if (is_bool($setting)) {
74            return $setting;
75        }
76        if (is_int($setting)) {
77            return $setting === 1;
78        }
79        if ($setting === null) {
80            return true;
81        }
82
83        $normalized = strtolower(trim((string)$setting));
84        return !in_array($normalized, ['', '0', 'false', 'off', 'no'], true);
85    }
86}