Updated activities to same state as parent record (#6255)

* Updated activities to same state as parent record

* Update CHANGELOG.md

* Fix delete activity test
This commit is contained in:
Yuriy Bakhtin 2023-04-27 19:12:10 +04:00 committed by GitHub
parent 460a428b78
commit bc3013c95b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 51 additions and 26 deletions

View File

@ -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)
-----------------------

View File

@ -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');
}
}

View File

@ -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]));
}
}
}

View File

@ -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();
}
}
}
/**

View File

@ -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(),