diff --git a/js/notifications-admin-settings.mjs b/js/notifications-admin-settings.mjs index dcfe33e64..0354d4ba4 100644 --- a/js/notifications-admin-settings.mjs +++ b/js/notifications-admin-settings.mjs @@ -1,3 +1,3 @@ /*! third party licenses: js/vendor.LICENSE.txt */ -import{r,a as h,o as d,w as l,b as u,e as _,h as E,t as c,f as m,F as S,i as N,v as L,j as g,c as b}from"./style-CuI75DCK.chunk.mjs";import{_ as v,t as n,N as A,c as y,v as D,a as I,s as w,l as M}from"./_plugin-vue_export-helper-DKkIhHsV.chunk.mjs";import{N as k}from"./NcSettingsSection-CtSg7lH8-8ZOwf6a-.chunk.mjs";const s={EMAIL_SEND_OFF:0,EMAIL_SEND_HOURLY:1,EMAIL_SEND_3HOURLY:2,EMAIL_SEND_DAILY:3,EMAIL_SEND_WEEKLY:4},x={name:"AdminSettings",components:{NcCheckboxRadioSwitch:A,NcSettingsSection:k},data(){return{batchtime_options:[{text:n("notifications","Never"),value:s.EMAIL_SEND_OFF},{text:n("notifications","1 hour"),value:s.EMAIL_SEND_HOURLY},{text:n("notifications","3 hours"),value:s.EMAIL_SEND_3HOURLY},{text:n("notifications","1 day"),value:s.EMAIL_SEND_DAILY},{text:n("notifications","1 week"),value:s.EMAIL_SEND_WEEKLY}],config:M("notifications","config")}},methods:{t:n,async updateSettings(){try{const e=new FormData;e.append("batchSetting",this.config.setting_batchtime),e.append("soundNotification",this.config.sound_notification?"yes":"no"),e.append("soundTalk",this.config.sound_talk?"yes":"no"),await y.post(D("apps/notifications/api/v2/settings/admin"),e),I(n("notifications","Your settings have been updated."))}catch(e){w(n("notifications","An error occurred while updating your settings.")),console.error(e)}}}},U={for:"notify_setting_batchtime",class:"notification-frequency__label"},V=["value"];function Y(e,i,O,R,a,o){const f=r("NcCheckboxRadioSwitch"),p=r("NcSettingsSection");return d(),h(p,{name:o.t("notifications","Notifications defaults"),description:o.t("notifications","Configure the default notification settings for new users")},{default:l(()=>[u("p",null,[u("label",U,c(o.t("notifications","Send email reminders about unhandled notifications after:")),1),E(u("select",{id:"notify_setting_batchtime","onUpdate:modelValue":i[0]||(i[0]=t=>a.config.setting_batchtime=t),class:"notification-frequency__select",onChange:i[1]||(i[1]=t=>o.updateSettings())},[(d(!0),m(S,null,N(a.batchtime_options,t=>(d(),m("option",{key:t.value,value:t.value},c(t.text),9,V))),128))],544),[[L,a.config.setting_batchtime]])]),_(f,{modelValue:a.config.sound_notification,"onUpdate:modelValue":[i[2]||(i[2]=t=>a.config.sound_notification=t),o.updateSettings]},{default:l(()=>[g(c(o.t("notifications","Play sound when a new notification arrives")),1)]),_:1},8,["modelValue","onUpdate:modelValue"]),_(f,{modelValue:a.config.sound_talk,"onUpdate:modelValue":[i[3]||(i[3]=t=>a.config.sound_talk=t),o.updateSettings]},{default:l(()=>[g(c(o.t("notifications","Play sound when a call started (requires Nextcloud Talk)")),1)]),_:1},8,["modelValue","onUpdate:modelValue"])]),_:1},8,["name","description"])}const F=v(x,[["render",Y]]);b(F).mount("#notifications-admin-settings"); +import{r as _,a as h,o as d,w as l,b as u,e as r,h as E,t as c,f as m,F as S,i as N,v as L,j as g,c as b}from"./style-CuI75DCK.chunk.mjs";import{_ as v,t as n,N as A,c as y,v as D,a as I,s as w,l as M}from"./_plugin-vue_export-helper-DKkIhHsV.chunk.mjs";import{N as k}from"./NcSettingsSection-CtSg7lH8-8ZOwf6a-.chunk.mjs";const s={EMAIL_SEND_OFF:0,EMAIL_SEND_HOURLY:1,EMAIL_SEND_3HOURLY:2,EMAIL_SEND_DAILY:3,EMAIL_SEND_WEEKLY:4},x={name:"AdminSettings",components:{NcCheckboxRadioSwitch:A,NcSettingsSection:k},data(){return{batchtime_options:[{text:n("notifications","Never"),value:s.EMAIL_SEND_OFF},{text:n("notifications","1 hour"),value:s.EMAIL_SEND_HOURLY},{text:n("notifications","3 hours"),value:s.EMAIL_SEND_3HOURLY},{text:n("notifications","1 day"),value:s.EMAIL_SEND_DAILY},{text:n("notifications","1 week"),value:s.EMAIL_SEND_WEEKLY}],config:M("notifications","config")}},methods:{t:n,async updateSettings(){try{const e=new FormData;e.append("batchSetting",this.config.setting_batchtime),e.append("soundNotification",this.config.sound_notification?"yes":"no"),e.append("soundTalk",this.config.sound_talk?"yes":"no"),await y.post(D("apps/notifications/api/v2/settings/admin"),e),I(n("notifications","Your settings have been updated."))}catch(e){w(n("notifications","An error occurred while updating your settings.")),console.error(e)}}}},U={for:"notify_setting_batchtime",class:"notification-frequency__label"},V=["value"];function Y(e,i,O,R,a,o){const f=_("NcCheckboxRadioSwitch"),p=_("NcSettingsSection");return d(),h(p,{name:o.t("notifications","Notifications defaults"),description:o.t("notifications","Configure the default notification settings")},{default:l(()=>[u("p",null,[u("label",U,c(o.t("notifications","Send email reminders about unhandled notifications after:")),1),E(u("select",{id:"notify_setting_batchtime","onUpdate:modelValue":i[0]||(i[0]=t=>a.config.setting_batchtime=t),class:"notification-frequency__select",onChange:i[1]||(i[1]=t=>o.updateSettings())},[(d(!0),m(S,null,N(a.batchtime_options,t=>(d(),m("option",{key:t.value,value:t.value},c(t.text),9,V))),128))],544),[[L,a.config.setting_batchtime]])]),r(f,{modelValue:a.config.sound_notification,"onUpdate:modelValue":[i[2]||(i[2]=t=>a.config.sound_notification=t),o.updateSettings]},{default:l(()=>[g(c(o.t("notifications","Play sound when a new notification arrives")),1)]),_:1},8,["modelValue","onUpdate:modelValue"]),r(f,{modelValue:a.config.sound_talk,"onUpdate:modelValue":[i[3]||(i[3]=t=>a.config.sound_talk=t),o.updateSettings]},{default:l(()=>[g(c(o.t("notifications","Play sound when a call started (requires Nextcloud Talk)")),1)]),_:1},8,["modelValue","onUpdate:modelValue"])]),_:1},8,["name","description"])}const F=v(x,[["render",Y]]);b(F).mount("#notifications-admin-settings"); //# sourceMappingURL=notifications-admin-settings.mjs.map diff --git a/js/notifications-admin-settings.mjs.map b/js/notifications-admin-settings.mjs.map index fac50d040..3567c4e4f 100644 --- a/js/notifications-admin-settings.mjs.map +++ b/js/notifications-admin-settings.mjs.map @@ -1 +1 @@ -{"version":3,"file":"notifications-admin-settings.mjs","sources":["../src/views/AdminSettings.vue","../src/adminSettings.js"],"sourcesContent":["\n\n\n\n\n","/**\n * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors\n * SPDX-License-Identifier: AGPL-3.0-or-later\n */\n\nimport { createApp } from 'vue'\nimport AdminSettings from './views/AdminSettings.vue'\n\n// Styles\nimport '@nextcloud/dialogs/style.css'\n\nexport default createApp(AdminSettings).mount('#notifications-admin-settings')\n"],"names":["EmailFrequency","_sfc_main","NcCheckboxRadioSwitch","NcSettingsSection","t","loadState","form","axios","generateOcsUrl","showSuccess","error","showError","_createBlock","_component_NcSettingsSection","$options","_createElementVNode","_hoisted_1","_toDisplayString","_cache","$event","$data","_createElementBlock","_Fragment","_renderList","option","_hoisted_2","_vModelSelect","_createVNode","_component_NcCheckboxRadioSwitch","createApp","AdminSettings"],"mappings":";6TA8CA,MAAMA,EAAiB,CACtB,eAAgB,EAChB,kBAAmB,EACnB,mBAAoB,EACpB,iBAAkB,EAClB,kBAAmB,CACpB,EAEKC,EAAU,CACd,KAAM,gBACN,WAAY,CACX,sBAAAC,EACA,kBAAAC,CACA,EAED,MAAO,CACN,MAAO,CACN,kBAAmB,CAClB,CAAE,KAAMC,EAAE,gBAAiB,OAAO,EAAG,MAAOJ,EAAe,cAAgB,EAC3E,CAAE,KAAMI,EAAE,gBAAiB,QAAQ,EAAG,MAAOJ,EAAe,iBAAmB,EAC/E,CAAE,KAAMI,EAAE,gBAAiB,SAAS,EAAG,MAAOJ,EAAe,kBAAoB,EACjF,CAAE,KAAMI,EAAE,gBAAiB,OAAO,EAAG,MAAOJ,EAAe,gBAAkB,EAC7E,CAAE,KAAMI,EAAE,gBAAiB,QAAQ,EAAG,MAAOJ,EAAe,iBAAmB,CAC/E,EAED,OAAQK,EAAU,gBAAiB,QAAQ,CAC5C,CACA,EAED,QAAS,GACRD,EAEA,MAAM,gBAAiB,CACtB,GAAI,CACH,MAA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,OAAO,kCAAPF,EAAA,CAAA,IAAAA,EAAA,CAAA,EAAAC,GAAAC,EAAA,OAAO,WAAUD,GACLL,EAAc,4BACnC,IAAoF,KAAjFA,EAAC,EAAA,gBAAA,0DAAA,CAAA,EAAA,CAAA,8GCrBQe,EAAUC,CAAa,EAAE,MAAM,+BAA+B"} \ No newline at end of file +{"version":3,"file":"notifications-admin-settings.mjs","sources":["../src/views/AdminSettings.vue","../src/adminSettings.js"],"sourcesContent":["\n\n\n\n\n","/**\n * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors\n * SPDX-License-Identifier: AGPL-3.0-or-later\n */\n\nimport { createApp } from 'vue'\nimport AdminSettings from './views/AdminSettings.vue'\n\n// Styles\nimport '@nextcloud/dialogs/style.css'\n\nexport default createApp(AdminSettings).mount('#notifications-admin-settings')\n"],"names":["EmailFrequency","_sfc_main","NcCheckboxRadioSwitch","NcSettingsSection","t","loadState","form","axios","generateOcsUrl","showSuccess","error","showError","_createBlock","_component_NcSettingsSection","$options","_createElementVNode","_hoisted_1","_toDisplayString","_cache","$event","$data","_createElementBlock","_Fragment","_renderList","option","_hoisted_2","_vModelSelect","_createVNode","_component_NcCheckboxRadioSwitch","createApp","AdminSettings"],"mappings":";kUA8CA,MAAMA,EAAiB,CACtB,eAAgB,EAChB,kBAAmB,EACnB,mBAAoB,EACpB,iBAAkB,EAClB,kBAAmB,CACpB,EAEKC,EAAU,CACd,KAAM,gBACN,WAAY,CACX,sBAAAC,EACA,kBAAAC,CACA,EAED,MAAO,CACN,MAAO,CACN,kBAAmB,CAClB,CAAE,KAAMC,EAAE,gBAAiB,OAAO,EAAG,MAAOJ,EAAe,cAAgB,EAC3E,CAAE,KAAMI,EAAE,gBAAiB,QAAQ,EAAG,MAAOJ,EAAe,iBAAmB,EAC/E,CAAE,KAAMI,EAAE,gBAAiB,SAAS,EAAG,MAAOJ,EAAe,kBAAoB,EACjF,CAAE,KAAMI,EAAE,gBAAiB,OAAO,EAAG,MAA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OAAO,0CAAPF,EAAA,CAAA,IAAAA,EAAA,CAAA,EAAAC,GAAAC,EAAA,OAAO,mBAAkBD,GACbL,EAAc,4BACnC,IAAsE,KAAnEA,EAAC,EAAA,gBAAA,4CAAA,CAAA,EAAA,CAAA,iDAELa,EAIwBC,EAAA,CAHd,WAAAR,EAAA,OAAO,kCAAPF,EAAA,CAAA,IAAAA,EAAA,CAAA,EAAAC,GAAAC,EAAA,OAAO,WAAUD,GACLL,EAAc,4BACnC,IAAoF,KAAjFA,EAAC,EAAA,gBAAA,0DAAA,CAAA,EAAA,CAAA,8GCrBQe,EAAUC,CAAa,EAAE,MAAM,+BAA+B"} \ No newline at end of file diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 92a48c92b..7c5c313f9 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -13,7 +13,6 @@ use OCA\Notifications\Capabilities; use OCA\Notifications\Listener\AddMissingIndicesListener; use OCA\Notifications\Listener\BeforeTemplateRenderedListener; -use OCA\Notifications\Listener\PostLoginListener; use OCA\Notifications\Listener\UserCreatedListener; use OCA\Notifications\Listener\UserDeletedListener; use OCA\Notifications\Notifier\AdminNotifications; @@ -24,7 +23,6 @@ use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent; use OCP\DB\Events\AddMissingIndicesEvent; use OCP\Notification\IManager; -use OCP\User\Events\PostLoginEvent; use OCP\User\Events\UserCreatedEvent; use OCP\User\Events\UserDeletedEvent; @@ -47,7 +45,6 @@ public function register(IRegistrationContext $context): void { $context->registerEventListener(UserDeletedEvent::class, UserDeletedListener::class); $context->registerEventListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedListener::class); $context->registerEventListener(UserCreatedEvent::class, UserCreatedListener::class); - $context->registerEventListener(PostLoginEvent::class, PostLoginListener::class); } #[\Override] diff --git a/lib/BackgroundJob/GenerateUserSettings.php b/lib/BackgroundJob/GenerateUserSettings.php index 1c984260a..1d4ac3096 100644 --- a/lib/BackgroundJob/GenerateUserSettings.php +++ b/lib/BackgroundJob/GenerateUserSettings.php @@ -10,7 +10,6 @@ use OCA\Notifications\Model\Settings; use OCA\Notifications\Model\SettingsMapper; -use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\TimedJob; use OCP\IDBConnection; @@ -47,15 +46,11 @@ protected function run($argument): void { return; } - try { - $this->settingsMapper->getSettingsByUser($user->getUID()); - } catch (DoesNotExistException) { - $settings = new Settings(); - $settings->setUserId($user->getUID()); - $settings->setNextSendTime(1); - $settings->setBatchTime(Settings::EMAIL_SEND_3HOURLY); + // Initializes the default settings + $settings = $this->settingsMapper->getSettingsByUser($user->getUID()); + if ($settings->getLastSendId() === 0) { $settings->setLastSendId($maxId); - $this->settingsMapper->insert($settings); + $this->settingsMapper->update($settings); } }); } diff --git a/lib/Controller/SettingsController.php b/lib/Controller/SettingsController.php index de35ac3e6..fa99df6c2 100644 --- a/lib/Controller/SettingsController.php +++ b/lib/Controller/SettingsController.php @@ -45,7 +45,7 @@ public function __construct( #[OpenAPI] #[ApiRoute(verb: 'POST', url: '/api/{apiVersion}/settings', requirements: ['apiVersion' => '(v2)'])] public function personal(int $batchSetting, string $soundNotification, string $soundTalk): DataResponse { - $this->settingsMapper->setBatchSettingForUser($this->userId, $batchSetting); + $this->settingsMapper->setBatchSettingForUser($this->settingsMapper->getSettingsByUser($this->userId), $batchSetting); $this->config->setUserValue($this->userId, Application::APP_ID, 'sound_notification', $soundNotification !== 'no' ? 'yes' : 'no'); $this->config->setUserValue($this->userId, Application::APP_ID, 'sound_talk', $soundTalk !== 'no' ? 'yes' : 'no'); diff --git a/lib/Listener/BeforeTemplateRenderedListener.php b/lib/Listener/BeforeTemplateRenderedListener.php index a3b0c94ce..2eb9b27fe 100644 --- a/lib/Listener/BeforeTemplateRenderedListener.php +++ b/lib/Listener/BeforeTemplateRenderedListener.php @@ -12,6 +12,7 @@ use OCA\Notifications\AppInfo\Application; use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent; use OCP\AppFramework\Http\TemplateResponse; +use OCP\AppFramework\Services\IAppConfig; use OCP\AppFramework\Services\IInitialState; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; @@ -30,6 +31,7 @@ public function __construct( protected IUserSession $userSession, protected IInitialState $initialState, protected IManager $notificationManager, + protected IAppConfig $appConfig, ) { } @@ -49,25 +51,14 @@ public function handle(Event $event): void { return; } - $this->initialState->provideInitialState( - 'sound_notification', - $this->config->getUserValue( - $user->getUID(), - Application::APP_ID, - 'sound_notification', - 'yes' - ) === 'yes' - ); + $defaultSoundNotification = $this->appConfig->getAppValueString(Application::APP_ID, 'sound_notification') === 'yes' ? 'yes' : 'no'; + $userSoundNotification = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'sound_notification', $defaultSoundNotification) === 'yes'; + $defaultSoundTalk = $this->appConfig->getAppValueString(Application::APP_ID, 'sound_talk') === 'yes' ? 'yes' : 'no'; + $userSoundTalk = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'sound_talk', $defaultSoundTalk) === 'yes'; - $this->initialState->provideInitialState( - 'sound_talk', - $this->config->getUserValue( - $user->getUID(), - Application::APP_ID, - 'sound_talk', - 'yes' - ) === 'yes' - ); + $this->initialState->provideInitialState('sound_notification', $userSoundNotification); + + $this->initialState->provideInitialState('sound_talk', $userSoundTalk); /** * We want to keep offering our push notification service for free, but large diff --git a/lib/Listener/PostLoginListener.php b/lib/Listener/PostLoginListener.php deleted file mode 100644 index 18888b9c9..000000000 --- a/lib/Listener/PostLoginListener.php +++ /dev/null @@ -1,60 +0,0 @@ - - */ -class PostLoginListener implements IEventListener { - public function __construct( - private SettingsMapper $settingsMapper, - private IConfig $config, - ) { - } - - #[\Override] - public function handle(Event $event): void { - if (!($event instanceof PostLoginEvent)) { - // Unrelated - return; - } - - $userId = $event->getUser()->getUID(); - - try { - $this->settingsMapper->getSettingsByUser($userId); - } catch (DoesNotExistException) { - $defaultSoundNotification = $this->config->getAppValue(Application::APP_ID, 'sound_notification') === 'yes' ? 'yes' : 'no'; - $defaultSoundTalk = $this->config->getAppValue(Application::APP_ID, 'sound_talk') === 'yes' ? 'yes' : 'no'; - $defaultBatchtime = (int)$this->config->getAppValue(Application::APP_ID, 'setting_batchtime'); - - if ($defaultBatchtime !== Settings::EMAIL_SEND_WEEKLY - && $defaultBatchtime !== Settings::EMAIL_SEND_DAILY - && $defaultBatchtime !== Settings::EMAIL_SEND_3HOURLY - && $defaultBatchtime !== Settings::EMAIL_SEND_HOURLY - && $defaultBatchtime !== Settings::EMAIL_SEND_OFF) { - $defaultBatchtime = Settings::EMAIL_SEND_3HOURLY; - } - - $this->config->setUserValue($userId, Application::APP_ID, 'sound_notification', $defaultSoundNotification); - $this->config->setUserValue($userId, Application::APP_ID, 'sound_talk', $defaultSoundTalk); - $this->settingsMapper->setBatchSettingForUser($userId, $defaultBatchtime); - } - } -} diff --git a/lib/Listener/UserCreatedListener.php b/lib/Listener/UserCreatedListener.php index 386842d60..583a448e1 100644 --- a/lib/Listener/UserCreatedListener.php +++ b/lib/Listener/UserCreatedListener.php @@ -8,7 +8,6 @@ namespace OCA\Notifications\Listener; -use OCA\Notifications\AppInfo\Application; use OCA\Notifications\Model\Settings; use OCA\Notifications\Model\SettingsMapper; use OCP\EventDispatcher\Event; @@ -35,20 +34,7 @@ public function handle(Event $event): void { $userId = $event->getUser()->getUID(); - $defaultSoundNotification = $this->config->getAppValue(Application::APP_ID, 'sound_notification') === 'yes' ? 'yes' : 'no'; - $defaultSoundTalk = $this->config->getAppValue(Application::APP_ID, 'sound_talk') === 'yes' ? 'yes' : 'no'; - $defaultBatchtime = (int)$this->config->getAppValue(Application::APP_ID, 'setting_batchtime'); - - if ($defaultBatchtime !== Settings::EMAIL_SEND_WEEKLY - && $defaultBatchtime !== Settings::EMAIL_SEND_DAILY - && $defaultBatchtime !== Settings::EMAIL_SEND_3HOURLY - && $defaultBatchtime !== Settings::EMAIL_SEND_HOURLY - && $defaultBatchtime !== Settings::EMAIL_SEND_OFF) { - $defaultBatchtime = Settings::EMAIL_SEND_3HOURLY; - } - - $this->config->setUserValue($userId, Application::APP_ID, 'sound_notification', $defaultSoundNotification); - $this->config->setUserValue($userId, Application::APP_ID, 'sound_talk', $defaultSoundTalk); - $this->settingsMapper->setBatchSettingForUser($userId, $defaultBatchtime); + // Initializes the default settings + $this->settingsMapper->getSettingsByUser($userId); } } diff --git a/lib/MailNotifications.php b/lib/MailNotifications.php index 610c47c31..c14afd37b 100644 --- a/lib/MailNotifications.php +++ b/lib/MailNotifications.php @@ -11,6 +11,7 @@ use OCA\Notifications\Model\Settings; use OCA\Notifications\Model\SettingsMapper; +use OCP\AppFramework\Services\IAppConfig; use OCP\AppFramework\Utility\ITimeFactory; use OCP\Defaults; use OCP\IConfig; @@ -35,6 +36,7 @@ class MailNotifications { public function __construct( protected IConfig $config, + protected IAppConfig $appConfig, protected IManager $manager, protected Handler $handler, protected IUserManager $userManager, @@ -69,6 +71,7 @@ public function sendEmails(int $batchSize, int $sendTime): void { /** @psalm-var array $userTimezones */ $userTimezones = $this->config->getUserValueForUsers('core', 'timezone', $userIds); $userEnabled = $this->config->getUserValueForUsers('core', 'enabled', $userIds); + $defaultBatchTime = SettingsMapper::batchSettingToTime($this->appConfig->getAppValueInt('setting_batchtime')); $fallbackLang = $this->config->getSystemValue('force_language', null); if (is_string($fallbackLang)) { @@ -81,12 +84,17 @@ public function sendEmails(int $batchSize, int $sendTime): void { } foreach ($userSettings as $settings) { + $batchTime = $settings->getBatchTime(); + if ($batchTime === Settings::EMAIL_SEND_DEFAULT) { + $batchTime = $defaultBatchTime; + } + $userId = $settings->getUserId(); if (isset($userEnabled[$userId]) && $userEnabled[$userId] === 'false') { // User is disabled, skip sending the email for them if ($settings->getNextSendTime() <= $sendTime) { $settings->setNextSendTime( - $sendTime + $settings->getBatchTime() + $sendTime + $batchTime ); $this->settingsMapper->update($settings); } @@ -101,11 +109,11 @@ public function sendEmails(int $batchSize, int $sendTime): void { $notifications = $this->handler->getAfterId($settings->getLastSendId(), $userId); if (!empty($notifications)) { $oldestNotification = end($notifications); - $shouldSendAfter = $oldestNotification->getDateTime()->getTimestamp() + $settings->getBatchTime(); + $shouldSendAfter = $oldestNotification->getDateTime()->getTimestamp() + $batchTime; if ($shouldSendAfter <= $sendTime) { // User has notifications that should send - $this->sendEmailToUser($settings, $notifications, $languageCode, $timezone); + $this->sendEmailToUser($settings, $notifications, $languageCode, $timezone, $batchTime); } else { // User has notifications but we didn't reach the timeout yet, // So delay sending to the time of the notification + batch setting @@ -113,7 +121,7 @@ public function sendEmails(int $batchSize, int $sendTime): void { $this->settingsMapper->update($settings); } } else { - $settings->setNextSendTime($sendTime + $settings->getBatchTime()); + $settings->setNextSendTime($sendTime + $batchTime); $this->settingsMapper->update($settings); } } @@ -127,7 +135,7 @@ public function sendEmails(int $batchSize, int $sendTime): void { * @param string $language * @param string $timezone */ - protected function sendEmailToUser(Settings $settings, array $notifications, string $language, string $timezone): void { + protected function sendEmailToUser(Settings $settings, array $notifications, string $language, string $timezone, int $batchTime): void { $lastSendId = array_key_first($notifications); $lastSendTime = $this->timeFactory->getTime(); @@ -164,7 +172,7 @@ protected function sendEmailToUser(Settings $settings, array $notifications, str } $settings->setLastSendId($lastSendId); - $settings->setNextSendTime($lastSendTime + $settings->getBatchTime()); + $settings->setNextSendTime($lastSendTime + $batchTime); $this->settingsMapper->update($settings); } } diff --git a/lib/Model/Settings.php b/lib/Model/Settings.php index e747c4e67..8167a39ca 100644 --- a/lib/Model/Settings.php +++ b/lib/Model/Settings.php @@ -23,6 +23,7 @@ * @method int getNextSendTime() */ class Settings extends Entity { + public const EMAIL_SEND_DEFAULT = 5; public const EMAIL_SEND_WEEKLY = 4; public const EMAIL_SEND_DAILY = 3; public const EMAIL_SEND_3HOURLY = 2; diff --git a/lib/Model/SettingsMapper.php b/lib/Model/SettingsMapper.php index 335961dc7..359d23680 100644 --- a/lib/Model/SettingsMapper.php +++ b/lib/Model/SettingsMapper.php @@ -9,7 +9,6 @@ namespace OCA\Notifications\Model; use OCP\AppFramework\Db\DoesNotExistException; -use OCP\AppFramework\Db\MultipleObjectsReturnedException; use OCP\AppFramework\Db\QBMapper; use OCP\DB\Exception as DBException; use OCP\DB\QueryBuilder\IQueryBuilder; @@ -30,18 +29,25 @@ public function __construct(IDBConnection $db) { /** * @param string $userId * @return Settings - * @throws DBException - * @throws MultipleObjectsReturnedException - * @throws DoesNotExistException */ public function getSettingsByUser(string $userId): Settings { - $query = $this->db->getQueryBuilder(); + try { + $query = $this->db->getQueryBuilder(); - $query->select('*') - ->from($this->getTableName()) - ->where($query->expr()->eq('user_id', $query->createNamedParameter($userId))); + $query->select('*') + ->from($this->getTableName()) + ->where($query->expr()->eq('user_id', $query->createNamedParameter($userId))); + + return $this->findEntity($query); + } catch (DoesNotExistException) { + $settings = new Settings(); + $settings->setUserId($userId); + $settings->setBatchTime(Settings::EMAIL_SEND_DEFAULT); + /** @var Settings $settings */ + $settings = $this->insert($settings); - return $this->findEntity($query); + return $settings; + } } /** @@ -57,28 +63,8 @@ public function deleteSettingsByUser(string $userId): void { $query->executeStatement(); } - public function setBatchSettingForUser(string $userId, int $batchSetting): void { - try { - $settings = $this->getSettingsByUser($userId); - } catch (DoesNotExistException) { - $settings = new Settings(); - $settings->setUserId($userId); - /** @var Settings $settings */ - $settings = $this->insert($settings); - } - - if ($batchSetting === Settings::EMAIL_SEND_WEEKLY) { - $batchTime = 3600 * 24 * 7; - } elseif ($batchSetting === Settings::EMAIL_SEND_DAILY) { - $batchTime = 3600 * 24; - } elseif ($batchSetting === Settings::EMAIL_SEND_3HOURLY) { - $batchTime = 3600 * 3; - } elseif ($batchSetting === Settings::EMAIL_SEND_HOURLY) { - $batchTime = 3600; - } else { - $batchTime = 0; // Off - } - + public function setBatchSettingForUser(Settings $settings, int $batchSetting): Settings { + $batchTime = self::batchSettingToTime($batchSetting); $settings->setBatchTime($batchTime); if ($batchTime === 0) { // When mails are Off, we don't set a "next send time" so it can be @@ -91,6 +77,7 @@ public function setBatchSettingForUser(string $userId, int $batchSetting): void $settings->setNextSendTime(1); } $this->update($settings); + return $settings; } /** @@ -119,4 +106,26 @@ public function createSettingsFromRow(array $row): Settings { 'next_send_time' => (int)$row['next_send_time'], ]); } + + public static function batchSettingToTime(int $batchSetting): int { + return match ($batchSetting) { + Settings::EMAIL_SEND_WEEKLY => 3600 * 24 * 7, + Settings::EMAIL_SEND_DAILY => 3600 * 24, + Settings::EMAIL_SEND_3HOURLY => 3600 * 3, + Settings::EMAIL_SEND_HOURLY => 3600, + Settings::EMAIL_SEND_DEFAULT => Settings::EMAIL_SEND_DEFAULT, + default => 0, + }; + } + + public static function batchTimeToSetting(int $batchTime): int { + return match ($batchTime) { + 3600 * 24 * 7 => Settings::EMAIL_SEND_WEEKLY, + 3600 * 24 => Settings::EMAIL_SEND_DAILY, + 3600 * 3 => Settings::EMAIL_SEND_3HOURLY, + 3600 => Settings::EMAIL_SEND_HOURLY, + Settings::EMAIL_SEND_DEFAULT => Settings::EMAIL_SEND_DEFAULT, + default => Settings::EMAIL_SEND_OFF, + }; + } } diff --git a/lib/Settings/Personal.php b/lib/Settings/Personal.php index 1a9b6cd41..73561f6bb 100644 --- a/lib/Settings/Personal.php +++ b/lib/Settings/Personal.php @@ -14,6 +14,7 @@ use OCA\Notifications\Model\SettingsMapper; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Http\TemplateResponse; +use OCP\AppFramework\Services\IAppConfig; use OCP\AppFramework\Services\IInitialState; use OCP\IConfig; use OCP\IL10N; @@ -25,6 +26,7 @@ class Personal implements ISettings { public function __construct( protected IConfig $config, + protected IAppConfig $appConfig, protected IL10N $l10n, protected IUserSession $session, protected SettingsMapper $settingsMapper, @@ -41,34 +43,31 @@ public function getForm(): TemplateResponse { $user = $this->session->getUser(); try { $settings = $this->settingsMapper->getSettingsByUser($user->getUID()); - - if ($settings->getBatchTime() === 3600 * 24 * 7) { - $settingBatchTime = Settings::EMAIL_SEND_WEEKLY; - } elseif ($settings->getBatchTime() === 3600 * 24) { - $settingBatchTime = Settings::EMAIL_SEND_DAILY; - } elseif ($settings->getBatchTime() === 3600 * 3) { - $settingBatchTime = Settings::EMAIL_SEND_3HOURLY; - } elseif ($settings->getBatchTime() === 3600) { - $settingBatchTime = Settings::EMAIL_SEND_HOURLY; - } else { - $settingBatchTime = Settings::EMAIL_SEND_OFF; + $settingBatchTime = SettingsMapper::batchTimeToSetting($settings->getBatchTime()); + if ($settings->getBatchTime() === Settings::EMAIL_SEND_DEFAULT) { + $settingBatchTime = $this->appConfig->getAppValueInt('setting_batchtime'); } } catch (DoesNotExistException) { $settings = new Settings(); $settings->setUserId($user->getUID()); - $settings->setBatchTime(3600 * 3); + $settings->setBatchTime(Settings::EMAIL_SEND_DEFAULT); $settings->setNextSendTime(1); $this->settingsMapper->insert($settings); - $settingBatchTime = Settings::EMAIL_SEND_3HOURLY; + $settingBatchTime = $this->appConfig->getAppValueInt('setting_batchtime'); } + $defaultSoundNotification = $this->appConfig->getAppValueBool('sound_notification') ? 'yes' : 'no'; + $userSoundNotification = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'sound_notification', $defaultSoundNotification) === 'yes'; + $defaultSoundTalk = $this->appConfig->getAppValueBool('sound_talk') ? 'yes' : 'no'; + $userSoundTalk = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'sound_talk', $defaultSoundTalk) === 'yes'; + $this->initialState->provideInitialState('config', [ 'setting' => 'personal', 'is_email_set' => (bool)$user->getEMailAddress(), 'setting_batchtime' => $settingBatchTime, - 'sound_notification' => $this->config->getUserValue($user->getUID(), Application::APP_ID, 'sound_notification', 'yes') === 'yes', - 'sound_talk' => $this->config->getUserValue($user->getUID(), Application::APP_ID, 'sound_talk', 'yes') === 'yes', + 'sound_notification' => $userSoundNotification, + 'sound_talk' => $userSoundTalk, ]); return new TemplateResponse('notifications', 'settings/personal'); diff --git a/src/views/AdminSettings.vue b/src/views/AdminSettings.vue index 298e61c2a..c63d93f33 100644 --- a/src/views/AdminSettings.vue +++ b/src/views/AdminSettings.vue @@ -6,7 +6,7 @@