Merge pull request #3033 from acs-ferreira/patch-17

Clean Comment Model
This commit is contained in:
Lucas Bartholemy 2018-02-25 20:42:06 +01:00 committed by GitHub
commit 2478709d7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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,20 +267,22 @@ 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)
if ($this->created_by == $userId) {
return true;
}
if (Yii::$app->user->isAdmin()) {
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;
}