From 84902a6f903c368743086e894548402d9c8810da Mon Sep 17 00:00:00 2001 From: Carlos Ferreira Date: Mon, 19 Feb 2018 15:19:57 +0100 Subject: [PATCH 1/2] Clean Comment Model --- .../humhub/modules/comment/models/Comment.php | 36 +++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/protected/humhub/modules/comment/models/Comment.php b/protected/humhub/modules/comment/models/Comment.php index d472e90835..51c3349129 100644 --- a/protected/humhub/modules/comment/models/Comment.php +++ b/protected/humhub/modules/comment/models/Comment.php @@ -2,18 +2,26 @@ /** * @link https://www.humhub.org/ - * @copyright Copyright (c) 2017 HumHub GmbH & Co. KG + * @copyright Copyright (c) 2018 HumHub GmbH & Co. KG * @license https://www.humhub.com/licences */ namespace humhub\modules\comment\models; use humhub\modules\user\models\User; -use Yii; use humhub\modules\post\models\Post; use humhub\modules\content\interfaces\ContentOwner; use humhub\modules\comment\activities\NewComment; +use humhub\modules\comment\notifications\NewComment as NewCommentNotification; +use humhub\modules\comment\live\NewComment as NewCommentLive; use humhub\modules\content\components\ContentAddonActiveRecord; +use humhub\components\behaviors\PolymorphicRelation; +use humhub\modules\user\models\Mentioning; +use humhub\modules\search\interfaces\Searchable; +use humhub\modules\space\models\Space; +use Yii; +use yii\db\ActiveRecord; +use yii\base\Exception; /** * This is the model class for table "comment". @@ -62,9 +70,9 @@ class Comment extends ContentAddonActiveRecord implements ContentOwner { return [ [ - 'class' => \humhub\components\behaviors\PolymorphicRelation::className(), + 'class' => PolymorphicRelation::className(), 'mustBeInstanceOf' => [ - \yii\db\ActiveRecord::className(), + ActiveRecord::className(), ] ] ]; @@ -87,7 +95,7 @@ class Comment extends ContentAddonActiveRecord implements ContentOwner { try { $this->updateContentSearch(); - } catch (\yii\base\Exception $ex) { + } catch (Exception $ex) { Yii::error($ex); } @@ -119,7 +127,7 @@ class Comment extends ContentAddonActiveRecord implements ContentOwner // Handle mentioned users // Execute before NewCommentNotification to avoid double notification when mentioned. - $mentionedUsers = \humhub\modules\user\models\Mentioning::parse($this, $this->message); + $mentionedUsers = Mentioning::parse($this, $this->message); if ($insert) { $followers = $this->getCommentedRecord()->getFollowers(null, true); @@ -128,13 +136,13 @@ class Comment extends ContentAddonActiveRecord implements ContentOwner // Update updated_at etc.. $this->refresh(); - \humhub\modules\comment\notifications\NewComment::instance() + NewCommentNotification::instance() ->from(Yii::$app->user->getIdentity()) ->about($this) ->sendBulk($followers); if ($this->content->container) { - Yii::$app->live->send(new \humhub\modules\comment\live\NewComment([ + Yii::$app->live->send(new NewCommentLive([ 'contentContainerId' => $this->content->container->id, 'visibility' => $this->content->visibility, 'contentId' => $this->content->id, @@ -156,13 +164,13 @@ class Comment extends ContentAddonActiveRecord implements ContentOwner */ private function filterMentionings(&$followers, $mentionedUsers) { - if(empty($mentionedUsers)) { + if (empty($mentionedUsers)) { return; } foreach($followers as $i => $follower) { foreach($mentionedUsers as $mentioned) { - if($follower->is($mentioned)) { + if ($follower->is($mentioned)) { unset($followers[$i]); continue 2; } @@ -176,7 +184,7 @@ class Comment extends ContentAddonActiveRecord implements ContentOwner */ protected function updateContentSearch() { - if ($this->getCommentedRecord() instanceof \humhub\modules\search\interfaces\Searchable) { + if ($this->getCommentedRecord() instanceof Searchable) { Yii::$app->search->update($this->getCommentedRecord()); } } @@ -259,10 +267,10 @@ class Comment extends ContentAddonActiveRecord implements ContentOwner return $this->message; } - public function canDelete($userId = "") + public function canDelete($userId = '') { - if ($userId == "") + if ($userId == '') $userId = Yii::$app->user->id; if ($this->created_by == $userId) @@ -272,7 +280,7 @@ class Comment extends ContentAddonActiveRecord implements ContentOwner return true; } - if ($this->content->container instanceof \humhub\modules\space\models\Space && $this->content->container->isAdmin($userId)) { + if ($this->content->container instanceof Space && $this->content->container->isAdmin($userId)) { return true; } From 37aa7ae42055c2c9024459b78d3c82f8d7261b05 Mon Sep 17 00:00:00 2001 From: Carlos Ferreira Date: Mon, 19 Feb 2018 17:54:11 +0100 Subject: [PATCH 2/2] Inline control structures are not allowed --- protected/humhub/modules/comment/models/Comment.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/protected/humhub/modules/comment/models/Comment.php b/protected/humhub/modules/comment/models/Comment.php index 51c3349129..457ce3d8d0 100644 --- a/protected/humhub/modules/comment/models/Comment.php +++ b/protected/humhub/modules/comment/models/Comment.php @@ -270,11 +270,13 @@ class Comment extends ContentAddonActiveRecord implements ContentOwner public function canDelete($userId = '') { - if ($userId == '') + if ($userId == '') { $userId = Yii::$app->user->id; + } - if ($this->created_by == $userId) + if ($this->created_by == $userId) { return true; + } if (Yii::$app->user->isAdmin()) { return true;