From aeeb9f73190d5d63d84b7406cffa7fd63305ff73 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 5 Dec 2023 16:24:11 +0100 Subject: [PATCH] fix(preferences): Remove unchangable activity preferences Signed-off-by: Joas Schilling --- appinfo/info.xml | 3 +- .../RemoveFormerActivitySettings.php | 72 +++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 lib/BackgroundJob/RemoveFormerActivitySettings.php diff --git a/appinfo/info.xml b/appinfo/info.xml index fa92974df..509bd746a 100755 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -11,7 +11,7 @@ More information is available in the Activity documentation. - 2.21.0 + 2.21.1 agpl Frank Karlitschek Joas Schilling @@ -41,6 +41,7 @@ OCA\Activity\BackgroundJob\EmailNotification OCA\Activity\BackgroundJob\ExpireActivities OCA\Activity\BackgroundJob\DigestMail + OCA\Activity\BackgroundJob\RemoveFormerActivitySettings diff --git a/lib/BackgroundJob/RemoveFormerActivitySettings.php b/lib/BackgroundJob/RemoveFormerActivitySettings.php new file mode 100644 index 000000000..096f8b7f2 --- /dev/null +++ b/lib/BackgroundJob/RemoveFormerActivitySettings.php @@ -0,0 +1,72 @@ + + * + * @author Joas Schilling + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Activity\BackgroundJob; + +use OCP\Activity\IExtension; +use OCP\Activity\IManager; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\BackgroundJob\TimedJob; +use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\IDBConnection; + +class RemoveFormerActivitySettings extends TimedJob { + public function __construct( + ITimeFactory $time, + protected IManager $manager, + protected IDBConnection $db, + ) { + parent::__construct($time); + $this->setInterval(24 * 60 * 60); + } + + protected function run($argument): void { + $preferencesToKeep = [ + 'notify_setting_activity_digest', + 'notify_setting_batchtime', + 'notify_setting_self', + 'notify_setting_selfemail', + ]; + foreach ($this->manager->getSettings() as $setting) { + if ($setting->canChangeMail()) { + $preferencesToKeep[] = 'notify_' . IExtension::METHOD_MAIL . '_' . $setting->getIdentifier(); + } + if ($setting->canChangeNotification()) { + $preferencesToKeep[] = 'notify_' . IExtension::METHOD_NOTIFICATION . '_' . $setting->getIdentifier(); + } + } + + $this->removeFormerPreference($preferencesToKeep); + } + + protected function removeFormerPreference(array $preferencesToKeep): void { + $query = $this->db->getQueryBuilder(); + $query->delete('preferences') + ->where($query->expr()->eq('appid', $query->createNamedParameter('activity'))) + ->andWhere($query->expr()->like('configkey', $query->createNamedParameter($this->db->escapeLikeParameter('notify_') . '%'))) + ->andWhere($query->expr()->notIn('configkey', $query->createNamedParameter($preferencesToKeep, IQueryBuilder::PARAM_STR_ARRAY))); + $query->executeStatement(); + } +}