Skip to content

Commit aa4948a

Browse files
committed
perf: Only apply default settings when user is created or settings are requested
Signed-off-by: provokateurin <kate@provokateurin.de>
1 parent b06229a commit aa4948a

File tree

6 files changed

+40
-97
lines changed

6 files changed

+40
-97
lines changed

lib/AppInfo/Application.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use OCA\Notifications\Capabilities;
1414
use OCA\Notifications\Listener\AddMissingIndicesListener;
1515
use OCA\Notifications\Listener\BeforeTemplateRenderedListener;
16-
use OCA\Notifications\Listener\PostLoginListener;
1716
use OCA\Notifications\Listener\UserCreatedListener;
1817
use OCA\Notifications\Listener\UserDeletedListener;
1918
use OCA\Notifications\Notifier\AdminNotifications;
@@ -24,7 +23,6 @@
2423
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
2524
use OCP\DB\Events\AddMissingIndicesEvent;
2625
use OCP\Notification\IManager;
27-
use OCP\User\Events\PostLoginEvent;
2826
use OCP\User\Events\UserCreatedEvent;
2927
use OCP\User\Events\UserDeletedEvent;
3028

@@ -46,7 +44,6 @@ public function register(IRegistrationContext $context): void {
4644
$context->registerEventListener(UserDeletedEvent::class, UserDeletedListener::class);
4745
$context->registerEventListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedListener::class);
4846
$context->registerEventListener(UserCreatedEvent::class, UserCreatedListener::class);
49-
$context->registerEventListener(PostLoginEvent::class, PostLoginListener::class);
5047
}
5148

5249
public function boot(IBootContext $context): void {

lib/BackgroundJob/GenerateUserSettings.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
use OCA\Notifications\Model\Settings;
1212
use OCA\Notifications\Model\SettingsMapper;
13-
use OCP\AppFramework\Db\DoesNotExistException;
1413
use OCP\AppFramework\Utility\ITimeFactory;
1514
use OCP\BackgroundJob\TimedJob;
1615
use OCP\IDBConnection;
@@ -46,15 +45,11 @@ protected function run($argument): void {
4645
return;
4746
}
4847

49-
try {
50-
$this->settingsMapper->getSettingsByUser($user->getUID());
51-
} catch (DoesNotExistException) {
52-
$settings = new Settings();
53-
$settings->setUserId($user->getUID());
54-
$settings->setNextSendTime(1);
55-
$settings->setBatchTime(Settings::EMAIL_SEND_3HOURLY);
48+
// Initializes the default settings
49+
$settings = $this->settingsMapper->getSettingsByUser($user->getUID());
50+
if ($settings->getLastSendId() === 0) {
5651
$settings->setLastSendId($maxId);
57-
$this->settingsMapper->insert($settings);
52+
$this->settingsMapper->update($settings);
5853
}
5954
});
6055
}

lib/Listener/PostLoginListener.php

Lines changed: 0 additions & 59 deletions
This file was deleted.

lib/Listener/UserCreatedListener.php

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
namespace OCA\Notifications\Listener;
1010

11-
use OCA\Notifications\AppInfo\Application;
1211
use OCA\Notifications\Model\Settings;
1312
use OCA\Notifications\Model\SettingsMapper;
1413
use OCP\EventDispatcher\Event;
@@ -34,20 +33,7 @@ public function handle(Event $event): void {
3433

3534
$userId = $event->getUser()->getUID();
3635

37-
$defaultSoundNotification = $this->config->getAppValue(Application::APP_ID, 'sound_notification') === 'yes' ? 'yes' : 'no';
38-
$defaultSoundTalk = $this->config->getAppValue(Application::APP_ID, 'sound_talk') === 'yes' ? 'yes' : 'no';
39-
$defaultBatchtime = (int)$this->config->getAppValue(Application::APP_ID, 'setting_batchtime');
40-
41-
if ($defaultBatchtime !== Settings::EMAIL_SEND_WEEKLY
42-
&& $defaultBatchtime !== Settings::EMAIL_SEND_DAILY
43-
&& $defaultBatchtime !== Settings::EMAIL_SEND_3HOURLY
44-
&& $defaultBatchtime !== Settings::EMAIL_SEND_HOURLY
45-
&& $defaultBatchtime !== Settings::EMAIL_SEND_OFF) {
46-
$defaultBatchtime = Settings::EMAIL_SEND_3HOURLY;
47-
}
48-
49-
$this->config->setUserValue($userId, Application::APP_ID, 'sound_notification', $defaultSoundNotification);
50-
$this->config->setUserValue($userId, Application::APP_ID, 'sound_talk', $defaultSoundTalk);
51-
$this->settingsMapper->setBatchSettingForUser($userId, $defaultBatchtime);
36+
// Initializes the default settings
37+
$this->settingsMapper->getSettingsByUser($userId);
5238
}
5339
}

lib/Model/SettingsMapper.php

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88

99
namespace OCA\Notifications\Model;
1010

11+
use OCA\Notifications\AppInfo\Application;
1112
use OCP\AppFramework\Db\DoesNotExistException;
1213
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
1314
use OCP\AppFramework\Db\QBMapper;
1415
use OCP\DB\Exception as DBException;
1516
use OCP\DB\QueryBuilder\IQueryBuilder;
17+
use OCP\IAppConfig;
1618
use OCP\IDBConnection;
1719

1820
/**
@@ -23,7 +25,10 @@
2325
* @method list<Settings> findEntities(IQueryBuilder $query)
2426
*/
2527
class SettingsMapper extends QBMapper {
26-
public function __construct(IDBConnection $db) {
28+
public function __construct(
29+
IDBConnection $db,
30+
private IAppConfig $appConfig,
31+
) {
2732
parent::__construct($db, 'notifications_settings', Settings::class);
2833
}
2934

@@ -32,16 +37,29 @@ public function __construct(IDBConnection $db) {
3237
* @return Settings
3338
* @throws DBException
3439
* @throws MultipleObjectsReturnedException
35-
* @throws DoesNotExistException
3640
*/
3741
public function getSettingsByUser(string $userId): Settings {
38-
$query = $this->db->getQueryBuilder();
42+
try {
43+
$query = $this->db->getQueryBuilder();
3944

40-
$query->select('*')
41-
->from($this->getTableName())
42-
->where($query->expr()->eq('user_id', $query->createNamedParameter($userId)));
45+
$query->select('*')
46+
->from($this->getTableName())
47+
->where($query->expr()->eq('user_id', $query->createNamedParameter($userId)));
4348

44-
return $this->findEntity($query);
49+
return $this->findEntity($query);
50+
} catch (DoesNotExistException) {
51+
$defaultBatchtime = (int)$this->appConfig->getValueString(Application::APP_ID, 'setting_batchtime');
52+
53+
if ($defaultBatchtime !== Settings::EMAIL_SEND_WEEKLY
54+
&& $defaultBatchtime !== Settings::EMAIL_SEND_DAILY
55+
&& $defaultBatchtime !== Settings::EMAIL_SEND_3HOURLY
56+
&& $defaultBatchtime !== Settings::EMAIL_SEND_HOURLY
57+
&& $defaultBatchtime !== Settings::EMAIL_SEND_OFF) {
58+
$defaultBatchtime = Settings::EMAIL_SEND_3HOURLY;
59+
}
60+
61+
return $this->setBatchSettingForUser($userId, $defaultBatchtime);
62+
}
4563
}
4664

4765
/**
@@ -57,7 +75,7 @@ public function deleteSettingsByUser(string $userId): void {
5775
$query->executeStatement();
5876
}
5977

60-
public function setBatchSettingForUser(string $userId, int $batchSetting): void {
78+
public function setBatchSettingForUser(string $userId, int $batchSetting): Settings {
6179
try {
6280
$settings = $this->getSettingsByUser($userId);
6381
} catch (DoesNotExistException) {
@@ -91,6 +109,7 @@ public function setBatchSettingForUser(string $userId, int $batchSetting): void
91109
$settings->setNextSendTime(1);
92110
}
93111
$this->update($settings);
112+
return $settings;
94113
}
95114

96115
/**

lib/Settings/Personal.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use OCA\Notifications\Model\SettingsMapper;
1515
use OCP\AppFramework\Db\DoesNotExistException;
1616
use OCP\AppFramework\Http\TemplateResponse;
17+
use OCP\AppFramework\Services\IAppConfig;
1718
use OCP\AppFramework\Services\IInitialState;
1819
use OCP\IConfig;
1920
use OCP\IL10N;
@@ -25,6 +26,7 @@
2526
class Personal implements ISettings {
2627
public function __construct(
2728
protected IConfig $config,
29+
protected IAppConfig $appConfig,
2830
protected IL10N $l10n,
2931
protected IUserSession $session,
3032
protected SettingsMapper $settingsMapper,
@@ -65,12 +67,15 @@ public function getForm(): TemplateResponse {
6567
$settingBatchTime = Settings::EMAIL_SEND_3HOURLY;
6668
}
6769

70+
$defaultSoundNotification = $this->appConfig->getAppValueString(Application::APP_ID, 'sound_notification') === 'yes' ? 'yes' : 'no';
71+
$defaultSoundTalk = $this->appConfig->getAppValueString(Application::APP_ID, 'sound_talk') === 'yes' ? 'yes' : 'no';
72+
6873
$this->initialState->provideInitialState('config', [
6974
'setting' => 'personal',
7075
'is_email_set' => (bool)$user->getEMailAddress(),
7176
'setting_batchtime' => $settingBatchTime,
72-
'sound_notification' => $this->config->getUserValue($user->getUID(), Application::APP_ID, 'sound_notification', 'yes') === 'yes',
73-
'sound_talk' => $this->config->getUserValue($user->getUID(), Application::APP_ID, 'sound_talk', 'yes') === 'yes',
77+
'sound_notification' => $this->config->getUserValue($user->getUID(), Application::APP_ID, 'sound_notification', $defaultSoundNotification) === 'yes',
78+
'sound_talk' => $this->config->getUserValue($user->getUID(), Application::APP_ID, 'sound_talk', $defaultSoundTalk) === 'yes',
7479
]);
7580

7681
return new TemplateResponse('notifications', 'settings/personal');

0 commit comments

Comments
 (0)