/** * Callback to validate module database records. * * @param Event $event */ public static function onIntegrityCheck($event) { $integrityController = $event->sender; $integrityController->showTestHeadline("Activity Module (" . Activity::find()->count() . " entries)"); // Loop over all comments foreach (Activity::find()->all() as $a) { // Check for object_model / object_id if ($a->object_model != "" && $a->object_id != "" && $a->getSource() === null) { if ($integrityController->showFix("Deleting activity id " . $a->id . " without existing target! (" . $a->object_model . ")")) { $a->delete(); } } // Check for moduleId is set if ($a->module == "") { if ($integrityController->showFix("Deleting activity id " . $a->id . " without module_id!")) { $a->delete(); } } // Check Activity class exists if (!class_exists($a->class)) { if ($integrityController->showFix("Deleting activity id " . $a->id . " class not exists! (" . $a->class . ")")) { $a->delete(); } } } }
public function up() { $this->renameClass('DropboxPost', DropboxPost::className()); $this->update('dropbox_file', ['object_model' => DropboxPost::className()]); foreach (\humhub\modules\activity\models\Activity::findAll(['module' => 'dropbox']) as $activity) { $activity->delete(); } }
public function up() { $this->renameClass('Note', Note::className()); $this->delete('notification', ['class' => 'NoteCreatedNotification']); $this->delete('notification', ['class' => 'NoteUpdatedNotification']); foreach (\humhub\modules\activity\models\Activity::findAll(['module' => 'notes']) as $activity) { $activity->delete(); } }
public function up() { $activities = (new \yii\db\Query())->select("activity.*, content.user_id, user_follow.id as follow_id")->from('activity')->leftJoin('content', 'content.object_model=:activityModel AND content.object_id=activity.id', [':activityModel' => Activity::className()])->leftJoin('user_follow', 'activity.object_model=user_follow.object_model AND activity.object_id=user_follow.object_id AND user_follow.user_id=content.user_id')->where(['class' => 'humhub\\modules\\user\\activities\\UserFollow', 'activity.object_model' => 'humhub\\modules\\user\\models\\User']); foreach ($activities->each() as $activity) { if ($activity['follow_id'] != "") { $this->updateSilent('activity', ['object_model' => humhub\modules\user\models\Follow::className(), 'object_id' => $activity['follow_id']], ['id' => $activity['id']]); } } }
/** * On delete of some active record, check if there are related activities and delete them. */ public static function onActiveRecordDelete($event) { $model = $event->sender->className(); $pk = $event->sender->getPrimaryKey(); // Check if primary key exists and is not array (multiple pk) if ($pk !== null && !is_array($pk)) { foreach (models\Activity::find()->where(['object_id' => $pk, 'object_model' => $model])->all() as $activity) { $activity->delete(); } } }
public function beforeDelete() { // ToDo: Handle this via event of User Module if ($this->object_model == User::className()) { $notification = new \humhub\modules\user\notifications\Followed(); $notification->originator = $this->user; $notification->delete($this->getTarget()); foreach (Activity::findAll(['object_model' => $this->className(), 'object_id' => $this->id]) as $activity) { $activity->delete(); } } return parent::beforeDelete(); }
public function testModeActivity() { Yii::$app->user->switchIdentity(User::findOne(['id' => 2])); // Post of User 2 - should not be included in activities $post = new Post(); $post->message = "P5"; $post->content->setContainer(Yii::$app->user->getIdentity()); $post->save(); $streamAction = new Stream('stream', Yii::$app->controller); $streamAction->mode = Stream::MODE_ACTIVITY; $streamAction->init(); $wallEntries = $streamAction->getWallEntries(); assert(count($wallEntries) == 4); foreach ($wallEntries as $entry) { assert($entry->content->object_model == Activity::className() && $entry->content->created_by != 2); } }
/** * Returns a JSON Response with links of an Activity * * @return string json */ public function actionInfo() { Yii::$app->response->format = 'json'; $json = []; $json['success'] = 'false'; $activityId = Yii::$app->request->get('id'); $activity = Activity::findOne(['id' => $activityId]); if ($activity !== null && $activity->content->canRead()) { $json['success'] = 'true'; $json['wallEntryId'] = ''; $source = $activity->getSource(); if ($source instanceof ContentActiveRecord || $source instanceof ContentAddonActiveRecord) { $json['wallEntryId'] = $source->content->getFirstWallEntryId(); } $json['permaLink'] = $activity->content->getUrl(); } return $json; }
public function up() { $this->renameClass('Activity', Activity::className()); // Space Created Activity - object_model/object_id (source fix) $activities = (new \yii\db\Query())->select("activity.*, content.space_id")->from('activity')->leftJoin('content', 'content.object_model=:activityModel AND content.object_id=activity.id', [':activityModel' => Activity::className()])->where(['class' => 'humhub\\modules\\space\\activities\\Created', 'activity.object_model' => ''])->all(); foreach ($activities as $activity) { $this->updateSilent('activity', ['object_model' => Space::className(), 'object_id' => $activity['space_id']], ['id' => $activity['id']]); } // Space Member added Activity - object_model/object_id (source fix) $activities = (new \yii\db\Query())->select("activity.*, content.space_id")->from('activity')->leftJoin('content', 'content.object_model=:activityModel AND content.object_id=activity.id', [':activityModel' => Activity::className()])->where(['class' => 'humhub\\modules\\space\\activities\\MemberAdded', 'activity.object_model' => ''])->all(); foreach ($activities as $activity) { $this->updateSilent('activity', ['object_model' => Space::className(), 'object_id' => $activity['space_id']], ['id' => $activity['id']]); } // Space Member removed Activity - object_model/object_id (source fix) $activities = (new \yii\db\Query())->select("activity.*, content.space_id")->from('activity')->leftJoin('content', 'content.object_model=:activityModel AND content.object_id=activity.id', [':activityModel' => Activity::className()])->where(['class' => 'humhub\\modules\\space\\activities\\MemberRemoved', 'activity.object_model' => ''])->all(); foreach ($activities as $activity) { $this->updateSilent('activity', ['object_model' => Space::className(), 'object_id' => $activity['space_id']], ['id' => $activity['id']]); } }
/** * Creates an activity * * @throws \yii\base\Exception */ public function create() { if ($this->moduleId == "") { throw new \yii\base\InvalidConfigException("No moduleId given!"); } if (!$this->source instanceof \yii\db\ActiveRecord) { throw new \yii\base\InvalidConfigException("Invalid source object given!"); } $model = new Activity(); $model->class = $this->className(); $model->module = $this->moduleId; $model->content->visibility = $this->visibility; $model->object_model = $this->source->className(); $model->object_id = $this->source->getPrimaryKey(); // Automatically determine content container if ($this->container == null) { if ($this->source instanceof ContentActiveRecord || $this->source instanceof ContentAddonActiveRecord) { $this->container = $this->source->content->container; // Take visibility from Content/Addon $model->content->visibility = $this->source->content->visibility; } elseif ($this->source instanceof ContentContainerActiveRecord) { $this->container = $this->source; } else { throw new \yii\base\InvalidConfigException("Could not determine content container for activity!"); } } $model->content->container = $this->container; // Automatically determine originator - if not set if ($this->originator !== null) { $model->content->user_id = $this->originator->id; } elseif ($this->source instanceof ContentActiveRecord) { $model->content->user_id = $this->source->content->user_id; } elseif ($this->source instanceof ContentAddonActiveRecord) { $model->content->user_id = $this->source->created_by; } else { throw new \yii\base\InvalidConfigException("Could not determine originator for activity!"); } if (!$model->validate() || !$model->save()) { throw new \yii\base\Exception("Could not save activity!" . $model->getErrors()); } }
public function validateVisibility() { if ($this->object_model == Activity::className() || $this->getPolymorphicRelation()->className() == Activity::className()) { return; } if ($this->container->className() == Space::className()) { if (!$this->container->canShare() && $this->visibility) { $this->addError('visibility', Yii::t('base', 'You cannot create public visible content!')); } } }
public function setupCriteria() { $this->activeQuery->joinWith('content'); $this->activeQuery->joinWith('content.createdBy'); $this->activeQuery->joinWith('content.contentContainer'); $this->activeQuery->limit($this->limit); $this->activeQuery->andWhere(['user.status' => User::STATUS_ENABLED]); /** * Handle Stream Mode (Normal Stream or Activity Stream) */ if ($this->mode == self::MODE_ACTIVITY) { $this->activeQuery->andWhere(['content.object_model' => \humhub\modules\activity\models\Activity::className()]); // Dont show own activities if ($this->user !== null) { $this->activeQuery->leftJoin('activity', 'content.object_id=activity.id AND content.object_model=:activityModel', ['activityModel' => \humhub\modules\activity\models\Activity::className()]); $this->activeQuery->andWhere('content.created_by != :userId', array(':userId' => $this->user->id)); } } else { $this->activeQuery->andWhere(['!=', 'content.object_model', \humhub\modules\activity\models\Activity::className()]); } /** * Setup Sorting */ if ($this->sort == self::SORT_UPDATED_AT) { $this->activeQuery->orderBy('wall_entry.updated_at DESC'); if ($this->from != "") { $this->activeQuery->andWhere("wall_entry.updated_at < (SELECT updated_at FROM wall_entry wd WHERE wd.id=" . $this->from . ")"); } } else { $this->activeQuery->orderBy('wall_entry.id DESC'); if ($this->from != "") { $this->activeQuery->andWhere("wall_entry.id < " . $this->from); } } }
/** * Counts all new Items for this membership */ public function countNewItems($since = "") { $query = WallEntry::find()->joinWith('content'); $query->where(['!=', 'content.object_model', Activity::className()]); $query->andWhere(['wall_entry.wall_id' => $this->space->wall_id]); $query->andWhere(['>', 'wall_entry.created_at', $this->last_visit]); $count = $query->count(); $count += Comment::find()->where(['space_id' => $this->space_id])->andWhere(['>', 'created_at', $this->last_visit])->count(); return $count; }
/** * @inheritdoc */ public function afterSave($insert, $changedAttributes) { // Auto follow this content if ($this->className() != \humhub\modules\activity\models\Activity::className()) { $this->follow($this->content->user_id); } // Set polymorphic relation if ($insert) { $this->content->object_model = $this->className(); $this->content->object_id = $this->getPrimaryKey(); } // Always save content $this->content->save(); parent::afterSave($insert, $changedAttributes); if ($insert && $this->autoAddToWall && $this->wallEntryClass != "") { $this->content->addToWall(); } // When Space Content, update also last visit if ($this->content->space_id) { $membership = $this->content->space->getMembership(); if ($membership) { $membership->updateLastVisit(); } } }
private function saveModelInstance() { $model = new Activity(); $model->class = $this->className(); $model->module = $this->moduleId; $model->object_model = $this->source->className(); $model->object_id = $this->source->getPrimaryKey(); $model->content->container = $this->container; $model->content->visibility = $this->getContentVisibility(); $model->content->created_by = $this->getOriginatorId(); if ($model->content->created_by == null) { throw new \yii\base\InvalidConfigException("Could not determine originator for activity!"); } if (!$model->validate() || !$model->save()) { throw new \yii\base\Exception("Could not save activity!" . $model->getErrors()); } }
/** * @inheritdoc */ public function afterSave($insert, $changedAttributes) { // Auto follow this content if ($this->className() != \humhub\modules\activity\models\Activity::className()) { $this->follow($this->content->created_by); } // Set polymorphic relation if ($insert) { $this->content->object_model = $this->className(); $this->content->object_id = $this->getPrimaryKey(); } // Always save content $this->content->save(); parent::afterSave($insert, $changedAttributes); if ($insert && $this->autoAddToWall && $this->wallEntryClass != "") { $this->content->addToWall(); } }
/** * Creates an activity * * @throws \yii\base\Exception */ public function create() { $model = new Activity(); $model->class = $this->className(); if ($this->moduleId == "") { throw new \yii\base\InvalidConfigException("No moduleId given!"); } $model->module = $this->moduleId; // Set content container and visibility if ($this->source instanceof ContentActiveRecord || $this->source instanceof ContentAddonActiveRecord) { $model->content->container = $this->source->content->container; $model->content->visibility = $this->source->content->visibility; } elseif ($this->source instanceof ContentContainerActiveRecord) { $model->content->visibility = $this->visibility; $model->content->container = $this->source; } else { throw new \yii\base\InvalidConfigException("Invalid source object type!"); } $model->object_model = $this->source->className(); $model->object_id = $this->source->getPrimaryKey(); // Set user if ($this->originator !== null) { $model->content->user_id = $this->originator->id; } elseif ($this->source instanceof ContentActiveRecord) { $model->content->user_id = $this->source->content->user_id; } elseif ($this->source instanceof ContentAddonActiveRecord) { $model->content->user_id = $this->source->created_by; } if (!$model->validate() || !$model->save()) { throw new \yii\base\Exception("Could not save activity!" . $model->getErrors()); } }