diff --git a/CHANGELOG.md b/CHANGELOG.md index b21d8dfc40..119046ea32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ HumHub Changelog ------------------- - Fix #6251: Emulate execution on `readable()` content - 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) ----------------------- diff --git a/protected/humhub/modules/activity/helpers/ActivityHelper.php b/protected/humhub/modules/activity/helpers/ActivityHelper.php index 42a8de06ff..b828d1ed9d 100644 --- a/protected/humhub/modules/activity/helpers/ActivityHelper.php +++ b/protected/humhub/modules/activity/helpers/ActivityHelper.php @@ -5,33 +5,44 @@ namespace humhub\modules\activity\helpers; use humhub\components\ActiveRecord; use humhub\modules\activity\models\Activity; use Yii; +use yii\db\ActiveQuery; class ActivityHelper { - public static function deleteActivitiesForRecord(?ActiveRecord $record) + public static function getActivitiesQuery(?ActiveRecord $record): ?ActiveQuery { if ($record === null) { - return; + return null; } $pk = $record->getPrimaryKey(); // Check if primary key exists and is not array (multiple pk) - if ($pk !== null && !is_array($pk)) { - - $modelsActivity = Activity::find()->where([ - 'object_id' => $pk, - 'object_model' => get_class($record) - ])->each(); - - foreach ($modelsActivity as $activity) { - /* @var Activity $activity */ - $activity->hardDelete(); - } - - Yii::debug('Deleted activities for ' . get_class($record) . " with PK " . $pk, 'activity'); + if ($pk === null || is_array($pk)) { + return null; } + + return Activity::find()->where([ + 'object_id' => $pk, + 'object_model' => get_class($record) + ]); + } + + public static function deleteActivitiesForRecord(?ActiveRecord $record): void + { + $activitiesQuery = self::getActivitiesQuery($record); + + if ($activitiesQuery === null) { + return; + } + + foreach ($activitiesQuery->each() as $activity) { + /* @var Activity $activity */ + $activity->hardDelete(); + } + + Yii::debug('Deleted activities for ' . get_class($record) . ' with PK ' . $record->getPrimaryKey(), 'activity'); } } diff --git a/protected/humhub/modules/activity/tests/codeception/unit/DeleteActivityTest.php b/protected/humhub/modules/activity/tests/codeception/unit/DeleteActivityTest.php index 06b80b79ad..bb96e1fe81 100644 --- a/protected/humhub/modules/activity/tests/codeception/unit/DeleteActivityTest.php +++ b/protected/humhub/modules/activity/tests/codeception/unit/DeleteActivityTest.php @@ -14,8 +14,11 @@ class DeleteActivityTest extends HumHubDbTestCase $post = Post::findOne(1); $activity = TestActivity::instance()->about($post)->create(); $record = $activity->record; - $this->assertNotNull(Activity::findOne(['id' => $record->id])); + $this->assertNull(Activity::find()->where(['id' => $record->id])->readable()->one()); $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])); } @@ -28,4 +31,4 @@ class DeleteActivityTest extends HumHubDbTestCase $post->createdBy->delete(); $this->assertNull(Activity::findOne(['id' => $record->id])); } -} \ No newline at end of file +} diff --git a/protected/humhub/modules/content/components/ContentActiveRecord.php b/protected/humhub/modules/content/components/ContentActiveRecord.php index da1c942700..11d95acde7 100644 --- a/protected/humhub/modules/content/components/ContentActiveRecord.php +++ b/protected/humhub/modules/content/components/ContentActiveRecord.php @@ -8,25 +8,28 @@ 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\models\Content; 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\WallStreamEntryWidget; use humhub\modules\topic\models\Topic; use humhub\modules\topic\widgets\TopicLabel; use humhub\modules\user\behaviors\Followable; use humhub\modules\user\models\User; -use Yii; -use yii\base\Exception; use humhub\modules\content\widgets\WallEntry; use humhub\widgets\Label; -use humhub\libs\BasePermission; -use humhub\modules\content\permissions\ManageContent; -use humhub\components\ActiveRecord; -use humhub\modules\content\models\Content; -use humhub\modules\content\interfaces\ContentOwner; +use Yii; +use yii\base\Exception; use yii\base\InvalidConfigException; use yii\base\ModelEvent; +use yii\db\ActiveQuery; /** * 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 { + // 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(); + } + } } /** diff --git a/protected/humhub/modules/content/models/Content.php b/protected/humhub/modules/content/models/Content.php index 5096843c6a..84d00e5abb 100644 --- a/protected/humhub/modules/content/models/Content.php +++ b/protected/humhub/modules/content/models/Content.php @@ -389,8 +389,6 @@ class Content extends ActiveRecord implements Movable, ContentOwner, SoftDeletab return false; } - ActivityHelper::deleteActivitiesForRecord($this->getModel()); - Notification::deleteAll([ 'source_class' => get_class($this), 'source_pk' => $this->getPrimaryKey(),