diff --git a/controllers/rest/EntryController.php b/controllers/rest/EntryController.php index fac9a3a8..2c59440f 100644 --- a/controllers/rest/EntryController.php +++ b/controllers/rest/EntryController.php @@ -1,4 +1,5 @@ true, + MessageUserLeft::SETTING_ENABLE_MESSAGE_USER_LEFT => true, + ]; + public function init() { parent::init(); @@ -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); } /** @@ -57,7 +75,7 @@ public static function getModule() public function rules() { return [ - [['showInTopNav', 'newUserRestrictionEnabled'], 'boolean'], + [['showInTopNav', 'newUserRestrictionEnabled', 'enableMessageUserJoined', 'enableMessageUserLeft'], 'boolean'], [['newUserConversationRestriction', 'newUserMessageRestriction', 'userConversationRestriction', @@ -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'), ]; } @@ -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()) { diff --git a/models/UserMessage.php b/models/UserMessage.php index 002e80a9..533da247 100644 --- a/models/UserMessage.php +++ b/models/UserMessage.php @@ -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. @@ -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; /** @@ -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); } } @@ -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); + } } } diff --git a/models/states/AbstractMessageState.php b/models/states/AbstractMessageState.php index 2f493bfa..dd3bda6f 100644 --- a/models/states/AbstractMessageState.php +++ b/models/states/AbstractMessageState.php @@ -1,4 +1,5 @@ +?>
-
Messenger module configuration'); ?>
-
'configure-form']); ?> - field($model, 'showInTopNav')->checkbox(); ?> -
field($model, 'userConversationRestriction')->textInput(['type' => 'number']); ?> field($model, 'userMessageRestriction')->textInput(['type' => 'number']); ?> -
field($model, 'newUserRestrictionEnabled')->checkbox(['id' => 'newUserCheckbox']); ?>
@@ -27,16 +22,16 @@ field($model, 'newUserConversationRestriction')->textInput(['type' => 'number']); ?> field($model, 'newUserMessageRestriction')->textInput(['type' => 'number']); ?>
- +
+ field($model, 'enableMessageUserJoined')->checkbox(); ?> + field($model, 'enableMessageUserLeft')->checkbox(); ?>
- submit() ?>
- entry->type) { case AbstractMessageEntry::TYPE_USER_JOINED: - return $this->isOwn() - ? Yii::t('MailModule.base', 'You joined the conversation.') - : Yii::t('MailModule.base', '{username} joined the conversation.', ['username' => $this->username]); - + if ($config->enableMessageUserJoined) { + return $this->isOwn() + ? Yii::t('MailModule.base', 'You joined the conversation.') + : Yii::t('MailModule.base', '{username} joined the conversation.', ['username' => $this->username]); + } + break; case AbstractMessageEntry::TYPE_USER_LEFT: - return $this->isOwn() - ? Yii::t('MailModule.base', 'You left the conversation.') - : Yii::t('MailModule.base', '{username} left the conversation.', ['username' => $this->username]); + if ($config->enableMessageUserLeft) { + return $this->isOwn() + ? Yii::t('MailModule.base', 'You left the conversation.') + : Yii::t('MailModule.base', '{username} left the conversation.', ['username' => $this->username]); + } + break; } return null; diff --git a/widgets/ConversationView.php b/widgets/ConversationView.php index a291d45d..ce69bc61 100644 --- a/widgets/ConversationView.php +++ b/widgets/ConversationView.php @@ -1,4 +1,5 @@ getLastEntry()->type) { case AbstractMessageEntry::TYPE_USER_JOINED: - return $this->isOwnLastEntry() - ? Yii::t('MailModule.base', 'You joined the conversation.') - : Yii::t('MailModule.base', '{username} joined the conversation.', ['username' => $this->getUsername()]); + // Check if showing user joined message is enabled + if ($config->enableMessageUserJoined) { + return $this->isOwnLastEntry() + ? Yii::t('MailModule.base', 'You joined the conversation.') + : Yii::t('MailModule.base', '{username} joined the conversation.', ['username' => $this->getUsername()]); + } + break; case AbstractMessageEntry::TYPE_USER_LEFT: - return $this->isOwnLastEntry() - ? Yii::t('MailModule.base', 'You left the conversation.') - : Yii::t('MailModule.base', '{username} left the conversation.', ['username' => $this->getUsername()]); + // Check if showing user left message is enabled + if ($config->enableMessageUserLeft) { + return $this->isOwnLastEntry() + ? Yii::t('MailModule.base', 'You left the conversation.') + : Yii::t('MailModule.base', '{username} left the conversation.', ['username' => $this->getUsername()]); + } + break; } if ($this->isGroupChat()) { diff --git a/widgets/MailRichtextEditor.php b/widgets/MailRichtextEditor.php index d567a489..a8861819 100644 --- a/widgets/MailRichtextEditor.php +++ b/widgets/MailRichtextEditor.php @@ -1,4 +1,5 @@