Merge branch 'master' into develop

This commit is contained in:
Lucas Bartholemy 2023-04-27 23:06:17 +02:00
commit c8f1995e28
5 changed files with 51 additions and 26 deletions

View File

@ -5,6 +5,7 @@ HumHub Changelog
------------------- -------------------
- Fix #6251: Emulate execution on `readable()` content - Fix #6251: Emulate execution on `readable()` content
- Enh #6252: Implement new method to handle changing of content active record state - Enh #6252: Implement new method to handle changing of content active record state
- Enh #6255: Updated activities to same state as parent record
1.14.0 (April 20, 2023) 1.14.0 (April 20, 2023)
----------------------- -----------------------

View File

@ -5,33 +5,44 @@ namespace humhub\modules\activity\helpers;
use humhub\components\ActiveRecord; use humhub\components\ActiveRecord;
use humhub\modules\activity\models\Activity; use humhub\modules\activity\models\Activity;
use Yii; use Yii;
use yii\db\ActiveQuery;
class ActivityHelper class ActivityHelper
{ {
public static function deleteActivitiesForRecord(?ActiveRecord $record) public static function getActivitiesQuery(?ActiveRecord $record): ?ActiveQuery
{ {
if ($record === null) { if ($record === null) {
return; return null;
} }
$pk = $record->getPrimaryKey(); $pk = $record->getPrimaryKey();
// Check if primary key exists and is not array (multiple pk) // Check if primary key exists and is not array (multiple pk)
if ($pk !== null && !is_array($pk)) { if ($pk === null || is_array($pk)) {
return null;
}
$modelsActivity = Activity::find()->where([ return Activity::find()->where([
'object_id' => $pk, 'object_id' => $pk,
'object_model' => get_class($record) 'object_model' => get_class($record)
])->each(); ]);
}
foreach ($modelsActivity as $activity) { public static function deleteActivitiesForRecord(?ActiveRecord $record): void
{
$activitiesQuery = self::getActivitiesQuery($record);
if ($activitiesQuery === null) {
return;
}
foreach ($activitiesQuery->each() as $activity) {
/* @var Activity $activity */ /* @var Activity $activity */
$activity->hardDelete(); $activity->hardDelete();
} }
Yii::debug('Deleted activities for ' . get_class($record) . " with PK " . $pk, 'activity'); Yii::debug('Deleted activities for ' . get_class($record) . ' with PK ' . $record->getPrimaryKey(), 'activity');
}
} }
} }

View File

@ -14,8 +14,11 @@ class DeleteActivityTest extends HumHubDbTestCase
$post = Post::findOne(1); $post = Post::findOne(1);
$activity = TestActivity::instance()->about($post)->create(); $activity = TestActivity::instance()->about($post)->create();
$record = $activity->record; $record = $activity->record;
$this->assertNotNull(Activity::findOne(['id' => $record->id])); $this->assertNull(Activity::find()->where(['id' => $record->id])->readable()->one());
$post->delete(); $post->delete();
$this->assertNotNull(Activity::findOne(['id' => $record->id]));
$this->assertNull(Activity::find()->where(['id' => $record->id])->readable()->one());
$post->hardDelete();
$this->assertNull(Activity::findOne(['id' => $record->id])); $this->assertNull(Activity::findOne(['id' => $record->id]));
} }

View File

@ -8,25 +8,28 @@
namespace humhub\modules\content\components; namespace humhub\modules\content\components;
use humhub\components\ActiveRecord;
use humhub\libs\BasePermission;
use humhub\modules\activity\helpers\ActivityHelper;
use humhub\modules\activity\models\Activity;
use humhub\modules\content\interfaces\ContentOwner;
use humhub\modules\content\interfaces\SoftDeletable; use humhub\modules\content\interfaces\SoftDeletable;
use humhub\modules\content\models\Content;
use humhub\modules\content\models\Movable; use humhub\modules\content\models\Movable;
use humhub\modules\content\permissions\ManageContent;
use humhub\modules\content\widgets\stream\StreamEntryWidget; use humhub\modules\content\widgets\stream\StreamEntryWidget;
use humhub\modules\content\widgets\stream\WallStreamEntryWidget; use humhub\modules\content\widgets\stream\WallStreamEntryWidget;
use humhub\modules\topic\models\Topic; use humhub\modules\topic\models\Topic;
use humhub\modules\topic\widgets\TopicLabel; use humhub\modules\topic\widgets\TopicLabel;
use humhub\modules\user\behaviors\Followable; use humhub\modules\user\behaviors\Followable;
use humhub\modules\user\models\User; use humhub\modules\user\models\User;
use Yii;
use yii\base\Exception;
use humhub\modules\content\widgets\WallEntry; use humhub\modules\content\widgets\WallEntry;
use humhub\widgets\Label; use humhub\widgets\Label;
use humhub\libs\BasePermission; use Yii;
use humhub\modules\content\permissions\ManageContent; use yii\base\Exception;
use humhub\components\ActiveRecord;
use humhub\modules\content\models\Content;
use humhub\modules\content\interfaces\ContentOwner;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\base\ModelEvent; use yii\base\ModelEvent;
use yii\db\ActiveQuery;
/** /**
* ContentActiveRecord is the base ActiveRecord [[\yii\db\ActiveRecord]] for Content. * ContentActiveRecord is the base ActiveRecord [[\yii\db\ActiveRecord]] for Content.
@ -462,6 +465,15 @@ class ContentActiveRecord extends ActiveRecord implements ContentOwner, Movable,
*/ */
public function afterStateChange(?int $newState, ?int $previousState): void public function afterStateChange(?int $newState, ?int $previousState): void
{ {
// Activities should be updated to same state as parent Record
$activitiesQuery = ActivityHelper::getActivitiesQuery($this);
if ($activitiesQuery instanceof ActiveQuery) {
foreach ($activitiesQuery->each() as $activity) {
/* @var Activity $activity */
$activity->content->setState($newState);
$activity->content->save();
}
}
} }
/** /**

View File

@ -389,8 +389,6 @@ class Content extends ActiveRecord implements Movable, ContentOwner, SoftDeletab
return false; return false;
} }
ActivityHelper::deleteActivitiesForRecord($this->getModel());
Notification::deleteAll([ Notification::deleteAll([
'source_class' => get_class($this), 'source_class' => get_class($this),
'source_pk' => $this->getPrimaryKey(), 'source_pk' => $this->getPrimaryKey(),