Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Configurable Join & Left Messages #427

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions controllers/rest/EntryController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2020 HumHub GmbH & Co. KG
Expand Down
1 change: 1 addition & 0 deletions controllers/rest/MessageController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2020 HumHub GmbH & Co. KG
Expand Down
1 change: 1 addition & 0 deletions controllers/rest/TagController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2020 HumHub GmbH & Co. KG
Expand Down
1 change: 1 addition & 0 deletions controllers/rest/UserController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2020 HumHub GmbH & Co. KG
Expand Down
1 change: 1 addition & 0 deletions helpers/RestDefinitions.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2020 HumHub GmbH & Co. KG
Expand Down
1 change: 1 addition & 0 deletions models/AbstractMessageEntry.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2023 HumHub GmbH & Co. KG
Expand Down
53 changes: 49 additions & 4 deletions models/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace humhub\modules\mail\models;

use DateInterval;
use DateTime;
use humhub\modules\user\models\User;
use Yii;
use DateTime;
use DateInterval;
use humhub\modules\mail\Module;
use humhub\modules\user\models\User;
use humhub\modules\mail\models\states\MessageUserLeft;
use humhub\modules\mail\models\states\MessageUserJoined;

/**
* ConfigureForm defines the configurable fields.
Expand All @@ -30,6 +32,20 @@ class Config extends \yii\base\Model

public $userMessageRestriction = null;

public $enableMessageUserJoined = true;

public $enableMessageUserLeft = true;

/**
* The settings array, containing the necessary settings for the MessageUserJoined and MessageUserLeft states.
*
* @var array
*/
private $settings = [
MessageUserJoined::SETTING_ENABLE_MESSAGE_USER_JOINED => true,
MessageUserLeft::SETTING_ENABLE_MESSAGE_USER_LEFT => true,
];

public function init()
{
parent::init();
Expand All @@ -41,6 +57,8 @@ public function init()
$this->newUserMessageRestriction = (int) $module->settings->get('newUserMessageRestriction', $this->newUserMessageRestriction);
$this->userConversationRestriction = (int) $module->settings->get('userConversationRestriction', $this->userConversationRestriction);
$this->userMessageRestriction = (int) $module->settings->get('userMessageRestriction', $this->userMessageRestriction);
$this->enableMessageUserJoined = (bool) $module->settings->get('enableMessageUserJoined', $this->enableMessageUserJoined);
$this->enableMessageUserLeft = (bool) $module->settings->get('enableMessageUserLeft', $this->enableMessageUserLeft);
}

/**
Expand All @@ -57,7 +75,7 @@ public static function getModule()
public function rules()
{
return [
[['showInTopNav', 'newUserRestrictionEnabled'], 'boolean'],
[['showInTopNav', 'newUserRestrictionEnabled', 'enableMessageUserJoined', 'enableMessageUserLeft'], 'boolean'],
[['newUserConversationRestriction',
'newUserMessageRestriction',
'userConversationRestriction',
Expand All @@ -81,6 +99,8 @@ public function attributeLabels()
'newUserMessageRestriction' => Yii::t('MailModule.base', 'Max number of messages allowed for a new user per day'),
'userConversationRestriction' => Yii::t('MailModule.base', 'Max number of new conversations allowed for a user per day'),
'userMessageRestriction' => Yii::t('MailModule.base', 'Max messages allowed per day'),
'enableMessageUserJoined' => Yii::t('MailModule.base', 'Enable "User Joined" message'),
'enableMessageUserLeft' => Yii::t('MailModule.base', 'Enable "User Left" message'),
];
}

Expand All @@ -98,9 +118,34 @@ public function save()
$module->settings->set('newUserMessageRestriction', $this->newUserMessageRestriction);
$module->settings->set('userConversationRestriction', $this->userConversationRestriction);
$module->settings->set('userMessageRestriction', $this->userMessageRestriction);
$module->settings->set('enableMessageUserJoined', $this->enableMessageUserJoined);
$module->settings->set('enableMessageUserLeft', $this->enableMessageUserLeft);
return true;
}

public function afterSave($insert, $changedAttributes)
{
parent::afterSave($insert, $changedAttributes);

// Update the settings for MessageUserJoined and MessageUserLeft states
$module = $this->getModule();
$module->settings->set(MessageUserJoined::SETTING_ENABLE_MESSAGE_USER_JOINED, $this->enableMessageUserJoined);
$module->settings->set(MessageUserLeft::SETTING_ENABLE_MESSAGE_USER_LEFT, $this->enableMessageUserLeft);
}

/**
* @inheritdoc
*/
public function afterDelete()
{
parent::afterDelete();

// Inform about user left if the corresponding setting is enabled
if ($this->settings[MessageUserLeft::SETTING_ENABLE_MESSAGE_USER_LEFT]) {
MessageUserLeft::inform($this->message, $this->user);
}
}

public function canCreateConversation(User $originator)
{
if ($originator->isSystemAdmin()) {
Expand Down
23 changes: 18 additions & 5 deletions models/UserMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace humhub\modules\mail\models;

use Yii;
use humhub\components\ActiveRecord;
use humhub\modules\mail\models\states\MessageUserJoined;
use humhub\modules\mail\models\states\MessageUserLeft;
use humhub\modules\user\models\User;
use Yii;
use humhub\modules\mail\models\states\MessageUserLeft;
use humhub\modules\mail\models\states\MessageUserJoined;

/**
* This class represents the relation between users and conversations.
Expand All @@ -32,6 +32,16 @@
*/
class UserMessage extends ActiveRecord
{
/**
* The settings array, containing the necessary settings for the MessageUserJoined and MessageUserLeft states.
*
* @var array
*/
private $settings = [
MessageUserJoined::SETTING_ENABLE_MESSAGE_USER_JOINED => true,
MessageUserLeft::SETTING_ENABLE_MESSAGE_USER_LEFT => true,
];

public bool $informAfterAdd = true;

/**
Expand Down Expand Up @@ -129,7 +139,7 @@ public function afterSave($insert, $changedAttributes)
{
parent::afterSave($insert, $changedAttributes);

if ($insert && $this->informAfterAdd) {
if ($insert && $this->informAfterAdd && MessageUserJoined::isEnabled($this->settings)) {
MessageUserJoined::inform($this->message, $this->user);
}
}
Expand All @@ -140,6 +150,9 @@ public function afterSave($insert, $changedAttributes)
public function afterDelete()
{
parent::afterDelete();
MessageUserLeft::inform($this->message, $this->user);

if (MessageUserLeft::isEnabled($this->settings)) {
MessageUserLeft::inform($this->message, $this->user);
}
}
}
1 change: 1 addition & 0 deletions models/states/AbstractMessageState.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2023 HumHub GmbH & Co. KG
Expand Down
33 changes: 33 additions & 0 deletions models/states/MessageUserJoined.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2023 HumHub GmbH & Co. KG
Expand All @@ -15,11 +16,43 @@
*/
class MessageUserJoined extends AbstractMessageState
{
/**
* The default value for the 'enableMessageUserJoined' setting.
*/
public const DEFAULT_ENABLE_MESSAGE_USER_JOINED = true;

/**
* The key for the 'enableMessageUserJoined' setting.
*/
public const SETTING_ENABLE_MESSAGE_USER_JOINED = 'enableMessageUserJoined';

/**
* @inheritdoc
*/
public static function type(): int
{
return self::TYPE_USER_JOINED;
}

/**
* Checks if the MessageUserJoined state is enabled.
*
* @param array $settings The settings array, where the 'enableMessageUserJoined' setting is stored.
* @return bool
*/
public static function isEnabled(array $settings): bool
{
return $settings[self::SETTING_ENABLE_MESSAGE_USER_JOINED] ?? self::DEFAULT_ENABLE_MESSAGE_USER_JOINED;
}

/**
* Checks if the MessageUserJoined state is disabled.
*
* @param array $settings The settings array, where the 'enableMessageUserJoined' setting is stored.
* @return bool
*/
public static function isDisabled(array $settings): bool
{
return !self::isEnabled($settings);
}
}
33 changes: 33 additions & 0 deletions models/states/MessageUserLeft.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2023 HumHub GmbH & Co. KG
Expand All @@ -15,11 +16,43 @@
*/
class MessageUserLeft extends AbstractMessageState
{
/**
* The default value for the 'enableMessageUserLeft' setting.
*/
public const DEFAULT_ENABLE_MESSAGE_USER_LEFT = true;

/**
* The key for the 'enableMessageUserLeft' setting.
*/
public const SETTING_ENABLE_MESSAGE_USER_LEFT = 'enableMessageUserLeft';

/**
* @inheritdoc
*/
public static function type(): int
{
return self::TYPE_USER_LEFT;
}

/**
* Checks if the MessageUserLeft state is enabled.
*
* @param array $settings The settings array, where the 'enableMessageUserLeft' setting is stored.
* @return bool
*/
public static function isEnabled(array $settings): bool
{
return $settings[self::SETTING_ENABLE_MESSAGE_USER_LEFT] ?? self::DEFAULT_ENABLE_MESSAGE_USER_LEFT;
}

/**
* Checks if the MessageUserLeft state is disabled.
*
* @param array $settings The settings array, where the 'enableMessageUserLeft' setting is stored.
* @return bool
*/
public static function isDisabled(array $settings): bool
{
return !self::isEnabled($settings);
}
}
1 change: 1 addition & 0 deletions notifications/ConversationNotification.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2017 HumHub GmbH & Co. KG
Expand Down
1 change: 1 addition & 0 deletions notifications/ConversationNotificationCategory.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2017 HumHub GmbH & Co. KG
Expand Down
1 change: 1 addition & 0 deletions notifications/MailNotification.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2017 HumHub GmbH & Co. KG
Expand Down
1 change: 1 addition & 0 deletions search/SearchProvider.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @link https://www.humhub.org/
* @copyright Copyright (c) HumHub GmbH & Co. KG
Expand Down
1 change: 1 addition & 0 deletions search/SearchRecord.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @link https://www.humhub.org/
* @copyright Copyright (c) HumHub GmbH & Co. KG
Expand Down
1 change: 1 addition & 0 deletions tests/codeception/_bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2015 HumHub GmbH & Co. KG
Expand Down
1 change: 1 addition & 0 deletions tests/codeception/acceptance/_bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Initialize the HumHub Application for functional testing. The default application configuration for this suite can be overwritten
* in @tests/config/functional.php
Expand Down
1 change: 1 addition & 0 deletions tests/codeception/api/_bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Initialize the HumHub Application for functional testing. The default application configuration for this suite can be overwritten
* in @tests/config/functional.php
Expand Down
1 change: 1 addition & 0 deletions tests/codeception/functional/_bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Initialize the HumHub Application for functional testing. The default application configuration for this suite can be overwritten
* in @tests/config/functional.php
Expand Down
1 change: 1 addition & 0 deletions tests/codeception/unit/_bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* The bootstrapping of unit tests is done by yii\codeception\DbTestCase or tests\codeception\_support\HumHubDbTestCase
* These classes will automatically load the config from @tests/codeception/config/unit.php
Expand Down
1 change: 1 addition & 0 deletions tests/config/api.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Here you can overwrite your functional humhub config. The default config resiedes in @humhubTests/codeception/config/config.php
*/
Expand Down
1 change: 1 addition & 0 deletions tests/config/common.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* This config is shared by all suites (unit/functional/acceptance) and can be overwritten by a suite config (e.g. functional.php)
*/
Expand Down
1 change: 1 addition & 0 deletions tests/config/functional.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Here you can overwrite the default config for the functional suite. The default config resides in @humhubTests/codeception/config/config.php
*/
Expand Down
1 change: 1 addition & 0 deletions tests/config/unit.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Here you can overwrite your functional humhub config. The default config resiedes in @humhubTests/codeception/config/config.php
*/
Expand Down
Loading