Skip to content

Commit

Permalink
Enh #6950: Ability to reset some notification settings to all users w…
Browse files Browse the repository at this point in the history
…ithout resetting "Like" notifications by email
  • Loading branch information
marc-farre committed Apr 22, 2024
1 parent 72b0bb8 commit a09cc58
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 17 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG-DEV.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ HumHub Changelog
- Fix #6909: Fix the marketplace searching when a module config file has missed fields
- Enh #6920: Enhancing of meta search for advanced search providers
- Enh #6952: Improve container title encoding in header
- Fix #6954: Search out of viewport on mobile

- Fix #6954: Search out of viewport on mobile
- Enh #6950: Ability to reset some notification settings to all users without resetting "Like" notifications by email

1.16.0-beta.2 (April 9, 2024)
-----------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ class NotificationManager
*/
public const EVENT_SEARCH_MODULE_NOTIFICATIONS = 'searchModuleNotifications';

/**
* User setting name to know if the user has modified default notification settings
*/
public const IS_TOUCHED_SETTINGS = 'is_touched_settings';

/**
*
* @var array Target configuration.
Expand Down Expand Up @@ -266,7 +271,7 @@ private function findNotExistingSettingSubQuery()
return ContentContainerSetting::find()
->where('contentcontainer_setting.contentcontainer_id=user.contentcontainer_id')
->andWhere(['contentcontainer_setting.module_id' => 'notification'])
->andWhere(['contentcontainer_setting.name' => 'notification.like_email']);
->andWhere(['contentcontainer_setting.name' => self::IS_TOUCHED_SETTINGS]);
}

/**
Expand All @@ -282,7 +287,7 @@ public function getSpaces(User $user)

$result = array_merge($memberSpaces, $followSpaces);

if ($this->isUntouchedSettings($user)) {
if (!static::isTouchedSettings($user)) {
$result = array_merge($result, Space::find()
->where(['guid' => Yii::$app->getModule('notification')->settings->getSerialized('sendNotificationSpaces')])
->visible($user)
Expand All @@ -293,9 +298,14 @@ public function getSpaces(User $user)
return $result;
}

private function isUntouchedSettings(User $user)
/**
* @throws \Throwable
*/
public static function isTouchedSettings(User $user): bool
{
return Yii::$app->getModule('notification')->settings->user($user)->get('notification.like_email') === null;
/** @var Module $module */
$module = Yii::$app->getModule('notification');
return (bool)$module->settings->user($user)?->get(self::IS_TOUCHED_SETTINGS);
}

/**
Expand Down Expand Up @@ -379,6 +389,7 @@ public function resetSpaces()
*/
public function setDesktopNoficationSettings($value = 0, User $user = null)
{
/** @var Module $module */
$module = Yii::$app->getModule('notification');
$settingManager = ($user) ? $module->settings->user($user) : $module->settings;
$settingManager->set('enable_html5_desktop_notifications', $value);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

use humhub\modules\notification\components\NotificationManager;
use humhub\modules\notification\Module;
use humhub\modules\user\models\User;
use yii\db\Migration;

/**
* Class m240422_162959_new_is_untouched_settings
*/
class m240422_162959_new_is_untouched_settings extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
foreach (User::find()->each() as $user) {
/** @var Module $module */
$module = Yii::$app->getModule('notification');
try {
$settingsManager = $module->settings->user($user);
if ($settingsManager && $settingsManager->get('notification.like_email') !== null) {
$settingsManager->set(NotificationManager::IS_TOUCHED_SETTINGS, true);
}
} catch (\Throwable $e) {
}
}
}

/**
* {@inheritdoc}
*/
public function safeDown()
{
echo "m240422_162959_new_is_untouched_settings cannot be reverted.\n";

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@

namespace humhub\modules\notification\models\forms;

use humhub\components\Module;
use humhub\modules\admin\permissions\ManageSettings;
use humhub\modules\admin\permissions\ManageUsers;
use humhub\modules\content\models\ContentContainerSetting;
use humhub\modules\notification\components\NotificationCategory;
use humhub\modules\notification\components\NotificationManager;
use humhub\modules\notification\targets\BaseTarget;
use humhub\modules\user\models\User;
use Yii;
use yii\base\Model;
use yii\web\HttpException;
use humhub\modules\notification\targets\BaseTarget;
use humhub\modules\admin\permissions\ManageSettings;

/**
* Description of NotificationSettings
Expand Down Expand Up @@ -104,13 +106,12 @@ public function attributeLabels()

/**
* Checks if this form has already been saved before.
* @return bool
* @throws \Throwable
*/
public function isUserSettingLoaded()
public function isTouchedSettings(): bool
{
if ($this->user) {
return $this->getSettings()->get('enable_html5_desktop_notifications') !== null ||
$this->getSettings()->get('notification.like_email') !== null;
return NotificationManager::isTouchedSettings($this->user);
}

return false;
Expand Down Expand Up @@ -170,6 +171,10 @@ public function save()

$settings = $this->getSettings();

if ($this->user) {
$settings->set(NotificationManager::IS_TOUCHED_SETTINGS, true);
}

// Save all active settings
foreach ($this->settings as $settingKey => $value) {
$settings->set($settingKey, $value);
Expand Down Expand Up @@ -221,6 +226,7 @@ private function saveSpaceSettings()

public function getSettings()
{
/** @var Module $module */
$module = Yii::$app->getModule('notification');

return ($this->user) ? $module->settings->user($this->user) : $module->settings;
Expand All @@ -244,7 +250,8 @@ public function resetUserSettings()
}

$settings = $this->getSettings();
$settings->delete('enable_html5_desktop_notifications');
$settings?->delete(NotificationManager::IS_TOUCHED_SETTINGS);
$settings?->delete('enable_html5_desktop_notifications');
foreach ($this->targets() as $target) {
foreach ($this->categories() as $category) {
$settings->delete($target->getSettingKey($category));
Expand All @@ -265,10 +272,11 @@ public function canResetAllUsers()

/**
* Resets all settings stored for all current user
* @throws \Throwable
*/
public function resetAllUserSettings()
{
$notificationSettings = ['enable_html5_desktop_notifications'];
$notificationSettings = [NotificationManager::IS_TOUCHED_SETTINGS, 'enable_html5_desktop_notifications'];
foreach ($this->targets() as $target) {
foreach ($this->categories() as $category) {
$notificationSettings[] = $target->getSettingKey($category);
Expand All @@ -282,7 +290,9 @@ public function resetAllUserSettings()

Yii::$app->notification->resetSpaces();

$settingsManager = Yii::$app->getModule('notification')->settings->user();
$settingsManager->reload();
/** @var Module $module */
$module = Yii::$app->getModule('notification');
$settingsManager = $module->settings->user();
$settingsManager?->reload();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

<br/>
<button type="submit" class="btn btn-primary" data-ui-loader><?= Yii::t('base', 'Save'); ?></button>
<?php if ($model->isUserSettingLoaded()): ?>
<?php if ($model->isTouchedSettings()): ?>
<a href="#" class="btn btn-default pull-right" data-action-click="post"
data-action-url="<?= Url::to(['reset']) ?>"
data-ui-loader><?= Yii::t('ActivityModule.base', 'Reset to defaults') ?></a>
Expand Down

0 comments on commit a09cc58

Please sign in to comment.