Enh: Added global (default) notification space settings.

This commit is contained in:
buddh4 2017-03-16 14:27:14 +01:00
parent 4a60d75e98
commit e423d5a7d2
7 changed files with 213 additions and 17 deletions

View File

@ -4,6 +4,8 @@ HumHub Change Log
1.2.0-beta.3 under developement
--------------------------------
- Enh: #2359 Use Jplayer playlist feature for post mp3
- Enh: added js module 'ui.view' for view state and utils - Changed 'ui.state.getState' to 'ui.view.getState'
- Enh: added view helper as getHeight/Width and isSmall/Medium/Normal (width) to 'ui.view' js module
- Fix: removed popover image preview from mobile

View File

@ -1,4 +1,4 @@
<?php //[STAMP] e93907d5924b39a3cd4134cc6a6ea3a3
<?php //[STAMP] 0468b7e2480518455b63a22d3aa6f7c2
namespace content\_generated;
// This class was automatically generated by build task

View File

@ -151,5 +151,144 @@ class ContentCreatedTest extends HumHubDbTestCase
$this->assertMailSent(0, 'ContentCreated Notification Mail sent');
}
/**
* Admin and User2 are member of Space1 -> Space1 is set to default notification space.
* After User2 posts new content Admin user should automatically be notified.
*/
public function testDefaultSpaceFollowPrivatePostNotification()
{
$this->becomeUser('User2');
Yii::$app->notification->setSpaces(['5396d499-20d6-4233-800b-c6c86e5fa34a']);
$post = new \humhub\modules\post\models\Post(['message' => 'MyTestContent']);
$post->content->setContainer(Space::findOne(['id' => 1]));
$post->content->visibility = \humhub\modules\content\models\Content::VISIBILITY_PRIVATE;
$post->save();
// Note Admin is following Space2 so we expect one notification mail.
$this->assertMailSent(1, 'ContentCreated Notification Mail sent');
}
/**
* Admin and User2 are member of Space1 -> Space1 is set to default notification space.
* Admin explicitly removes the default notification space.
*/
public function testExplicitlyDisableDefaultNotificationSpace()
{
$this->becomeUser('Admin');
Yii::$app->notification->setSpaces(['5396d499-20d6-4233-800b-c6c86e5fa34a']);
// Setting empty spaceguids settings
$settings = new \humhub\modules\notification\models\forms\NotificationSettings(['user' => User::findOne(['id' => 1])]);
$settings->save();
$this->becomeUser('User2');
$post = new \humhub\modules\post\models\Post(['message' => 'MyTestContent']);
$post->content->setContainer(Space::findOne(['id' => 1]));
$post->content->visibility = \humhub\modules\content\models\Content::VISIBILITY_PRIVATE;
$post->save();
// Note Admin is following Space2 so we expect one notification mail.
$this->assertMailSent(0, 'ContentCreated Notification Mail sent');
}
/**
* After User2 posts new public post Admin all other users should be notified.
*/
public function testDefaultSpaceFollowPublicPostNotification()
{
$this->becomeUser('User2');
Yii::$app->notification->setSpaces(['5396d499-20d6-4233-800b-c6c86e5fa34a']);
$post = new \humhub\modules\post\models\Post(['message' => 'MyTestContent']);
$post->content->setContainer(Space::findOne(['id' => 1]));
$post->content->visibility = \humhub\modules\content\models\Content::VISIBILITY_PUBLIC;
$post->save();
// Note Admin is following Space2 so we expect one notification mail.
$this->assertMailSent(3, 'ContentCreated Notification Mail sent');
}
/**
* Disable space as member and post public content.
*/
public function testExplicitlyDisableDefaultNotificationPublicMember()
{
$this->becomeUser('Admin');
Yii::$app->notification->setSpaces(['5396d499-20d6-4233-800b-c6c86e5fa34a']);
// Setting empty spaceguids settings
$settings = new \humhub\modules\notification\models\forms\NotificationSettings(['user' => User::findOne(['id' => 1])]);
$settings->save();
$this->becomeUser('User2');
$post = new \humhub\modules\post\models\Post(['message' => 'MyTestContent']);
$post->content->setContainer(Space::findOne(['id' => 1]));
$post->content->visibility = \humhub\modules\content\models\Content::VISIBILITY_PUBLIC;
$post->save();
// Note Admin is following Space2 so we expect one notification mail.
$this->assertMailSent(2, 'ContentCreated Notification Mail sent');
}
/**
* Disable space as member and post public content.
*/
public function testExplicitlyDisableDefaultNotificationPublic()
{
$this->becomeUser('User1');
Yii::$app->notification->setSpaces(['5396d499-20d6-4233-800b-c6c86e5fa34a']);
// Setting empty spaceguids settings
$settings = new \humhub\modules\notification\models\forms\NotificationSettings(['user' => User::findOne(['id' => 2])]);
$settings->save();
$this->becomeUser('User2');
$post = new \humhub\modules\post\models\Post(['message' => 'MyTestContent']);
$post->content->setContainer(Space::findOne(['id' => 1]));
$post->content->visibility = \humhub\modules\content\models\Content::VISIBILITY_PUBLIC;
$post->save();
// Note Admin is following Space2 so we expect one notification mail.
$this->assertMailSent(2, 'ContentCreated Notification Mail sent');
}
/**
* Disable space as member and post public content.
*/
public function testExplicitlyDisableDefaultNotificationPublic2()
{
$this->becomeUser('User1');
Yii::$app->notification->setSpaces(['5396d499-20d6-4233-800b-c6c86e5fa34a']);
// Setting empty spaceguids settings
$settings = new \humhub\modules\notification\models\forms\NotificationSettings(['user' => User::findOne(['id' => 2])]);
$settings->save();
$this->becomeUser('Admin');
Yii::$app->notification->setSpaces(['5396d499-20d6-4233-800b-c6c86e5fa34a']);
// Setting empty spaceguids settings
$settings = new \humhub\modules\notification\models\forms\NotificationSettings(['user' => User::findOne(['id' => 1])]);
$settings->save();
$this->becomeUser('User2');
$post = new \humhub\modules\post\models\Post(['message' => 'MyTestContent']);
$post->content->setContainer(Space::findOne(['id' => 1]));
$post->content->visibility = \humhub\modules\content\models\Content::VISIBILITY_PUBLIC;
$post->save();
// Note Admin is following Space2 so we expect one notification mail.
$this->assertMailSent(1, 'ContentCreated Notification Mail sent');
}
}

View File

@ -9,6 +9,7 @@ use humhub\modules\user\models\Follow;
use humhub\modules\space\models\Space;
use humhub\modules\content\components\ContentContainerActiveRecord;
use humhub\modules\content\models\Content;
use humhub\modules\content\models\ContentContainerSetting;
/**
* The NotificationManager component is responsible for sending BaseNotifications to Users over different
@ -178,29 +179,76 @@ class NotificationManager
*/
public function getContainerFollowers(ContentContainerActiveRecord $container, $public = true)
{
$result = [];
if ($container instanceof Space) {
$isDefault = $this->isDefaultNotificationSpace($container);
$members = Membership::getSpaceMembersQuery($container, true, true)->all();
$followers = (!$public) ? [] : Follow::getFollowersQuery($container, true)->all();
return array_merge($members, $followers);
if ($public) {
// Add explicit follower and non explicit follower if $isDefault
$followers = $this->findFollowers($container, $isDefault)->all();
$result = array_merge($members, $followers);
} else if ($isDefault) {
// Add all members without explicit following and no notification settings.
$followers = Membership::getSpaceMembersQuery($container, true, false)
->andWhere(['not exists', $this->findNotExistingSettingSubQuery()])->all();
$result = array_merge($members, $followers);
} else {
$result = $members;
}
} else if ($container instanceof User) {
// Note the notification follow logic for users is currently not implemented.
// TODO: perhaps return only friends if public is false?
return (!$public) ? [] : Follow::getFollowersQuery($container, true)->all();
$result = (!$public) ? [] : Follow::getFollowersQuery($container, true)->all();
}
return $result;
}
private function isDefaultNotificationSpace($container)
{
$defaultSpaces = Yii::$app->getModule('notification')->settings->getSerialized('sendNotificationSpaces');
return (empty($defaultSpaces)) ? false : in_array($container->guid, $defaultSpaces);
}
private function findFollowers($container, $isDefault = false)
{
// Find all followers with send_notifications = 1
$query = Follow::getFollowersQuery($container, true);
if ($isDefault) {
// Add all user with no notification setting
$query->orWhere([
'and', 'user.status=1', ['not exists', $this->findNotExistingSettingSubQuery()]
]);
}
return $query;
}
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']);
}
/**
* Returns all spaces this user is following (including member spaces) with sent_notification setting.
*
* @param User $user
* @return type
* @return Space[]
*/
public function getSpaces(User $user)
{
$memberSpaces = Membership::getUserSpaceQuery($user, true, true)->all();
$followSpaces = Follow::getFollowedSpacesQuery($user, true)->all();
$isLoaded = (Yii::$app->getModule('notification')->settings->user($user)->get('notification.like_email') !== null);
if ($isLoaded) {
$memberSpaces = Membership::getUserSpaceQuery($user, true, true)->all();
$followSpaces = Follow::getFollowedSpacesQuery($user, true)->all();
return array_merge($memberSpaces, $followSpaces);
return array_merge($memberSpaces, $followSpaces);
} else {
return Space::findAll(['guid' => Yii::$app->getModule('notification')->settings->getSerialized('sendNotificationSpaces')]);
}
}
/**
@ -209,12 +257,18 @@ class NotificationManager
* @param User $user
* @return type
*/
public function getNonNotificationSpaces(User $user)
public function getNonNotificationSpaces(User $user = null, $limit = 25)
{
$memberSpaces = Membership::getUserSpaceQuery($user, true, false)->all();
$followSpaces = Follow::getFollowedSpacesQuery($user, false)->all();
if ($user) {
$memberSpaces = Membership::getUserSpaceQuery($user, true, false)->limit($limit)->all();
$limit -= count($memberSpaces);
$followSpaces = Follow::getFollowedSpacesQuery($user, false)->limit($limit)->all();
return array_merge($memberSpaces, $followSpaces);
return array_merge($memberSpaces, $followSpaces);
} else {
return Space::find()->where(['not in', 'guid', Yii::$app->getModule('notification')->settings->getSerialized('sendNotificationSpaces')])
->limit($limit)->all();
}
}
/**

View File

@ -52,6 +52,8 @@ class NotificationSettings extends \yii\base\Model
$this->spaceGuids = array_map(function ($space) {
return $space->guid;
}, $spaces);
} else {
$this->spaceGuids = Yii::$app->getModule('notification')->settings->getSerialized('sendNotificationSpaces');
}
$this->desktopNotifications = Yii::$app->notification->getDesktopNoficationSettings($this->user);
@ -231,7 +233,7 @@ class NotificationSettings extends \yii\base\Model
$settings->delete($target->getSettingKey($category));
}
}
Yii::$app->notification->setSpaces($this->user, []);
Yii::$app->notification->setSpaces([], $this->user);
return true;
}

View File

@ -14,7 +14,7 @@ use yii\widgets\ActiveForm;
<?= humhub\modules\notification\widgets\NotificationSettingsForm::widget([
'model' => $model,
'form' => $form,
'showSpaces' => false
'showSpaces' => true
]) ?>
<br />
<button type="submit" class="btn btn-primary" data-ui-loader><?= Yii::t('base', 'Save');?></button>

View File

@ -36,9 +36,8 @@ class NotificationSettingsForm extends \yii\base\Widget
*/
public function run()
{
if($this->model->user) {
$this->defaultSpaces = Yii::$app->notification->getNonNotificationSpaces($this->model->user);
}
$this->defaultSpaces = Yii::$app->notification->getNonNotificationSpaces($this->model->user);
return $this->render('notificationSettingsForm', [
'form' => $this->form,