Skip to content

Commit

Permalink
Enh: Added new conversation restrictions
Browse files Browse the repository at this point in the history
  • Loading branch information
buddh4 committed Mar 11, 2019
1 parent 8b01596 commit da725fa
Show file tree
Hide file tree
Showing 14 changed files with 3,295 additions and 130 deletions.
9 changes: 4 additions & 5 deletions Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
use humhub\modules\user\models\User;
use Yii;
use yii\helpers\Url;
use humhub\modules\mail\models\Message;
use humhub\modules\mail\models\MessageEntry;
use humhub\modules\mail\models\UserMessage;
use humhub\modules\mail\widgets\NewMessageButton;
use humhub\modules\mail\widgets\Notifications;
use humhub\modules\mail\permissions\SendMail;
use humhub\modules\mail\models\ConfigureForm;
use humhub\modules\mail\models\Config;

/**
* Description of Events
Expand Down Expand Up @@ -61,11 +60,11 @@ public static function onTopMenuInit($event)
$showInTopNav = false;

// Workaround for module update problem
if (method_exists(ConfigureForm::getModule(), 'showInTopNav')) {
$showInTopNav = ConfigureForm::getModule()->showInTopNav();
if (method_exists(Config::getModule(), 'showInTopNav')) {
$showInTopNav = Config::getModule()->showInTopNav();
}

if(!ConfigureForm::getModule()->showInTopNav()){
if(!Config::getModule()->showInTopNav()){
$event->sender->addItem([
'label' => Yii::t('MailModule.base', 'Messages'),
'url' => Url::to(['/mail/mail/index']),
Expand Down
4 changes: 2 additions & 2 deletions controllers/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace humhub\modules\mail\controllers;

use Yii;
use humhub\modules\mail\models\ConfigureForm;
use humhub\modules\mail\models\Config;
use humhub\models\Setting;

/**
Expand All @@ -26,7 +26,7 @@ class ConfigController extends \humhub\modules\admin\components\Controller
*/
public function actionIndex()
{
$form = new ConfigureForm();
$form = new Config();

if ($form->load(Yii::$app->request->post()) && $form->save()) {
$this->view->saved();
Expand Down
56 changes: 11 additions & 45 deletions controllers/MailController.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,16 @@ public function actionNotificationList()
$query = UserMessage::getByUser(null, 'message.updated_at DESC')->limit(5);
return $this->renderAjax('notificationList', ['userMessages' => $query->all()]);
}

/**
* Used by user picker, searches user which are allwed messaging permissions
* for the current user (v1.1).
*
*
* @param null $id
* @param $keyword
* @return string
* @throws HttpException
* @throws \Throwable
*/
public function actionSearchUser($id = null, $keyword)
{
Expand All @@ -202,8 +206,9 @@ public function actionSearchUser($id = null, $keyword)
}

$result = UserPicker::filter([
'query' => User::find(),
'keyword' => $keyword,
'permission' => new SendMail(),
'permission' => (!Yii::$app->user->isAdmin()) ? new SendMail() : null,
'fillUser' => true,
'disableFillUser' => true,
'disabledText' => Yii::t('MailModule.base','You are not allowed to start a conversation with this user.')
Expand Down Expand Up @@ -299,51 +304,12 @@ public function actionCreate()
$model->recipient = $user->guid;
}
}



if ($model->load(Yii::$app->request->post()) && $model->validate()) {
// Create new Message
$message = new Message();
$message->title = $model->title;
$message->save();

// Attach Message Entry
$messageEntry = new MessageEntry();
$messageEntry->message_id = $message->id;
$messageEntry->user_id = Yii::$app->user->id;
$messageEntry->content = $model->message;
$messageEntry->save();

// Attach also Recipients
foreach ($model->getRecipients() as $recipient) {
$userMessage = new UserMessage();
$userMessage->message_id = $message->id;
$userMessage->user_id = $recipient->id;
$userMessage->save();
}

// Inform recipients (We need to add all before)
foreach ($model->getRecipients() as $recipient) {
try {
$message->notify($recipient);
} catch(\Exception $e) {
Yii::error('Could not send notification e-mail to: '. $recipient->username.". Error:". $e->getMessage());
}
}

// Attach User Message
$userMessage = new UserMessage();
$userMessage->message_id = $message->id;
$userMessage->user_id = Yii::$app->user->id;
$userMessage->is_originator = 1;
$userMessage->last_viewed = new \yii\db\Expression('NOW()');
$userMessage->save();

return $this->htmlRedirect(['index', 'id' => $message->id]);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->htmlRedirect(['index', 'id' => $model->messageInstance->id]);
}

return $this->renderAjax('create', array('model' => $model));
return $this->renderAjax('create', ['model' => $model]);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Changelog
=========
1.0.7 (March 11, 2019)
-----------------------
- Enh: Added new conversation restrictions

1.0.6 (November 16, 2018)
-----------------------
- Fix #131 wrong norwegain translation
Expand Down
161 changes: 161 additions & 0 deletions models/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php

namespace humhub\modules\mail\models;

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

/**
* ConfigureForm defines the configurable fields.
*
* @package humhub\modules\mail\models
* @author Akopian Gaik
*/
class Config extends \yii\base\Model
{

public $showInTopNav;

public $newUserRestrictionEnabled = 0;

public $newUserSinceDays = 3;

public $newUserConversationRestriction = 2;

public $newUserMessageRestriction = 5;

public $userConversationRestriction = 15;

public $userMessageRestriction = null;

public function init()
{
parent::init();
$module = $this->getModule();
$this->showInTopNav = !$module->showInTopNav();
$this->newUserRestrictionEnabled = (int) $module->settings->get('newUserRestrictionEnabled', $this->newUserRestrictionEnabled);
$this->newUserSinceDays = (int) $module->settings->get('newUserSinceDays', $this->newUserSinceDays);
$this->newUserConversationRestriction = (int) $module->settings->get('newUserConversationRestriction', $this->newUserConversationRestriction);
$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);
}

/**
* @return Module
*/
public static function getModule()
{
return Yii::$app->getModule('mail');
}

/**
* Declares the validation rules.
*/
public function rules()
{
return [
[['showInTopNav', 'newUserRestrictionEnabled'], 'boolean'],
[['newUserConversationRestriction',
'newUserMessageRestriction',
'userConversationRestriction',
'userMessageRestriction'], 'integer', 'min' => 0],
['newUserSinceDays', 'integer', 'min' => 1]
];
}

/**
* Declares customized attribute labels.
* If not declared here, an attribute would have a label that is
* the same as its name with the first letter in upper case.
*/
public function attributeLabels()
{
return [
'showInTopNav' => Yii::t('MailModule.base', 'Show menu item in top Navigation'),
'newUserRestrictionEnabled' => Yii::t('MailModule.base', 'Seperate restrictions for new users'),
'newUserSinceDays' => Yii::t('MailModule.base', 'Until a user is member since (days)'),
'newUserConversationRestriction' => Yii::t('MailModule.base', 'Max number of new conversations allowed for a new user per day'),
'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')
];
}

public function save()
{
if(!$this->validate()) {
return false;
}

$module = static::getModule();
$module->settings->set('showInTopNav', $this->showInTopNav);
$module->settings->set('newUserRestrictionEnabled', $this->newUserRestrictionEnabled);
$module->settings->set('newUserSinceDays', $this->newUserSinceDays);
$module->settings->set('newUserConversationRestriction', $this->newUserConversationRestriction);
$module->settings->set('newUserMessageRestriction', $this->newUserMessageRestriction);
$module->settings->set('userConversationRestriction', $this->userConversationRestriction);
$module->settings->set('userMessageRestriction', $this->userMessageRestriction);
return true;
}

public function canCreateConversation(User $originator)
{
if($originator->isSystemAdmin()) {
return true;
}

$maxConversations = $this->isNewUser($originator)
? $this->newUserConversationRestriction
: $this->userConversationRestriction;

return $maxConversations === null || $this->getConversationCount($originator) < $maxConversations;
}

public function isNewUser(User $originator)
{
if(!$this->newUserRestrictionEnabled) {
return false;
}

return (new DateTime($originator->created_at))->diff(new DateTime())->days < $this->newUserSinceDays;
}

public function getConversationCount($originator)
{
$module = static::getModule();
$lastTS = $module->settings->contentContainer($originator)->get('conversationCountTime');

if(!$lastTS) {
$module->settings->contentContainer($originator)->set('conversationCountTime', time());
$module->settings->contentContainer($originator)->set('conversationCount', 0);
return 0;
}

$lastDate = (new \DateTime())->setTimestamp($lastTS);
$today = (new \DateTime())->setTime(0,0,0);

if($today > $lastDate) {
$module->settings->contentContainer($originator)->set('conversationCountTime', time());
$module->settings->contentContainer($originator)->set('conversationCount', 0);
return 0;
}

return (integer) static::getModule()->settings->contentContainer($originator)->get('conversationCount', 0);
}

public function reset($originator)
{
$module = static::getModule();
$module->settings->contentContainer($originator)->set('conversationCountTime', null);
$module->settings->contentContainer($originator)->set('conversationCount', null);
}

public function incrementConversationCount($originator)
{
static::getModule()->settings->contentContainer($originator)->set('conversationCount', ($this->getConversationCount($originator) + 1));
}
}
66 changes: 0 additions & 66 deletions models/ConfigureForm.php

This file was deleted.

Loading

0 comments on commit da725fa

Please sign in to comment.