88
99namespace OCA \Notifications \Model ;
1010
11+ use OCA \Notifications \AppInfo \Application ;
1112use OCP \AppFramework \Db \DoesNotExistException ;
1213use OCP \AppFramework \Db \MultipleObjectsReturnedException ;
1314use OCP \AppFramework \Db \QBMapper ;
1415use OCP \DB \Exception as DBException ;
1516use OCP \DB \QueryBuilder \IQueryBuilder ;
17+ use OCP \IAppConfig ;
1618use OCP \IDBConnection ;
1719
1820/**
2325 * @method list<Settings> findEntities(IQueryBuilder $query)
2426 */
2527class 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,34 @@ 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 )));
48+
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+ $ settings = new Settings ();
62+ $ settings ->setUserId ($ userId );
63+ /** @var Settings $settings */
64+ $ settings = $ this ->insert ($ settings );
4365
44- return $ this ->findEntity ($ query );
66+ return $ this ->setBatchSettingForUser ($ settings , $ defaultBatchtime );
67+ }
4568 }
4669
4770 /**
@@ -57,16 +80,7 @@ public function deleteSettingsByUser(string $userId): void {
5780 $ query ->executeStatement ();
5881 }
5982
60- public function setBatchSettingForUser (string $ userId , int $ batchSetting ): void {
61- try {
62- $ settings = $ this ->getSettingsByUser ($ userId );
63- } catch (DoesNotExistException ) {
64- $ settings = new Settings ();
65- $ settings ->setUserId ($ userId );
66- /** @var Settings $settings */
67- $ settings = $ this ->insert ($ settings );
68- }
69-
83+ public function setBatchSettingForUser (Settings $ settings , int $ batchSetting ): Settings {
7084 if ($ batchSetting === Settings::EMAIL_SEND_WEEKLY ) {
7185 $ batchTime = 3600 * 24 * 7 ;
7286 } elseif ($ batchSetting === Settings::EMAIL_SEND_DAILY ) {
@@ -91,6 +105,7 @@ public function setBatchSettingForUser(string $userId, int $batchSetting): void
91105 $ settings ->setNextSendTime (1 );
92106 }
93107 $ this ->update ($ settings );
108+ return $ settings ;
94109 }
95110
96111 /**
0 commit comments