Added FollowEvent on create/remove following

This commit is contained in:
Lucas Bartholemy 2017-01-20 12:43:43 +01:00
parent 99ea7e3775
commit 8694225c73
4 changed files with 94 additions and 26 deletions

View File

@ -14,7 +14,6 @@ use humhub\modules\user\models\User;
use humhub\modules\user\widgets\UserListBox;
use humhub\modules\stream\actions\ContentContainerStream;
/**
* SpaceController is the main controller for spaces.
*
@ -61,7 +60,7 @@ class SpaceController extends \humhub\modules\content\components\ContentContaine
{
$space = $this->getSpace();
if(Yii::$app->request->get('tour')) {
if (Yii::$app->request->get('tour')) {
return $this->actionHome();
}
@ -105,14 +104,17 @@ class SpaceController extends \humhub\modules\content\components\ContentContaine
{
$this->forcePostRequest();
$space = $this->getSpace();
$success = false;
if (!$space->isMember()) {
// follow without notifications by default
$space->follow(null, false);
$success = $space->follow(null, false);
}
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = 'json';
return ['success' => true];
return ['success' => $success];
}
return $this->redirect($space->getUrl());
@ -125,11 +127,12 @@ class SpaceController extends \humhub\modules\content\components\ContentContaine
{
$this->forcePostRequest();
$space = $this->getSpace();
$space->unfollow();
$success = $space->unfollow();
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = 'json';
return ['success' => true];
return ['success' => $success];
}
return $this->redirect($space->getUrl());

View File

@ -0,0 +1,32 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2017 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\modules\user\events;
use yii\base\Event;
/**
* FollowEvent
*
* @since 1.2
* @author Luke
*/
class FollowEvent extends Event
{
/**
* @var \humhub\modules\user\models\User
*/
public $user;
/**
* @var \humhub\components\ActiveRecord the followed item
*/
public $target;
}

View File

@ -0,0 +1,27 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2017 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\modules\user\events;
use yii\base\Event;
/**
* UserEvent
*
* @since 1.2
* @author Luke
*/
class UserEvent extends Event
{
/**
* @var \humhub\modules\user\models\User the user
*/
public $user;
}

View File

@ -2,12 +2,13 @@
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2015 HumHub GmbH & Co. KG
* @copyright Copyright (c) 2017 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\modules\user\models;
use humhub\modules\user\events\FollowEvent;
use humhub\modules\activity\models\Activity;
use humhub\modules\space\models\Space;
use humhub\modules\user\models\User;
@ -24,6 +25,16 @@ use humhub\modules\user\models\User;
class Follow extends \yii\db\ActiveRecord
{
/**
* @event \humhub\modules\user\events\FollowEvent
*/
const EVENT_FOLLOWING_CREATED = 'followCreated';
/**
* @event \humhub\modules\user\events\FollowEvent
*/
const EVENT_FOLLOWING_REMOVED = 'followRemoved';
/**
* @inheritdoc
*/
@ -65,7 +76,8 @@ class Follow extends \yii\db\ActiveRecord
{
return [
[['object_model', 'object_id', 'user_id'], 'required'],
[['object_id', 'user_id', 'send_notifications'], 'integer'],
[['object_id', 'user_id'], 'integer'],
[['send_notifications'], 'boolean'],
[['object_model'], 'string', 'max' => 100]
];
}
@ -73,17 +85,6 @@ class Follow extends \yii\db\ActiveRecord
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'object_model' => 'Object Model',
'object_id' => 'Object ID',
'user_id' => 'User ID',
'send_notifications' => 'Send Notifications',
];
}
public function afterSave($insert, $changedAttributes)
{
if ($insert && $this->object_model == User::className()) {
@ -99,11 +100,17 @@ class Follow extends \yii\db\ActiveRecord
->save();
}
return parent::afterSave($insert, $changedAttributes);
$this->trigger(Follow::EVENT_FOLLOWING_CREATED, new FollowEvent(['user' => $this->user, 'target' => $this->getTarget()]));
parent::afterSave($insert, $changedAttributes);
}
/**
* @inheritdoc
*/
public function beforeDelete()
{
$this->trigger(Follow::EVENT_FOLLOWING_REMOVED, new FollowEvent(['user' => $this->user, 'target' => $this->getTarget()]));
// ToDo: Handle this via event of User Module
if ($this->object_model == User::className()) {
@ -137,12 +144,12 @@ class Follow extends \yii\db\ActiveRecord
* Returns all followed spaces of the given user as ActiveQuery.
*
* @param \humhub\modules\user\models\User $user
* @param boolean|null $withNotifications by notification setting (default is null without notification handling)
* @return \yii\db\ActiveQuery Space query of all followed spaces
*/
public static function getFollowedSpacesQuery(User $user, $withNotifications = null)
{
$query = Space::find()->leftJoin('user_follow', 'user_follow.user_id=:userId AND user_follow.object_model=:spaceModel',
[':userId' => $user->id, ':spaceModel' => Space::class]);
$query = Space::find()->leftJoin('user_follow', 'user_follow.user_id=:userId AND user_follow.object_model=:spaceModel', [':userId' => $user->id, ':spaceModel' => Space::class]);
if ($withNotifications === true) {
$query->where(['user_follow.send_notifications' => 1]);
@ -155,8 +162,7 @@ class Follow extends \yii\db\ActiveRecord
public static function getFollowersQuery(\yii\db\ActiveRecord $target, $withNotifications = null)
{
$query = User::find()->leftJoin('user_follow', 'user.id=user_follow.user_id AND user_follow.object_model=:spaceModel AND user_follow.object_id=:spaceId',
[':spaceModel' => $target->className(), ':spaceId' => $target->getPrimaryKey()]);
$query = User::find()->leftJoin('user_follow', 'user.id=user_follow.user_id AND user_follow.object_model=:spaceModel AND user_follow.object_id=:spaceId', [':spaceModel' => $target->className(), ':spaceId' => $target->getPrimaryKey()]);
if ($withNotifications === true) {
$query->where(['user_follow.send_notifications' => 1]);