Enh: Added queuing for search updates of commments

This commit is contained in:
Lucas Bartholemy 2018-08-10 08:15:31 +02:00
parent 1073f6b1dd
commit 04e6e3a9c4
5 changed files with 72 additions and 29 deletions

View File

@ -7,6 +7,8 @@ HumHub Change Log
- Fix #3241: Profile header space count invalid - Fix #3241: Profile header space count invalid
- Fix: Disabled Notification E-Mails for installation sample contents - Fix: Disabled Notification E-Mails for installation sample contents
- Fix: No e-mail summary immediately after installation - Fix: No e-mail summary immediately after installation
- Enh: Added queuing for search updates of commments
1.3.1 (August 7, 2018) 1.3.1 (August 7, 2018)
----------------------- -----------------------

View File

@ -12,11 +12,13 @@ use humhub\components\behaviors\PolymorphicRelation;
use humhub\modules\comment\activities\NewComment; use humhub\modules\comment\activities\NewComment;
use humhub\modules\comment\live\NewComment as NewCommentLive; use humhub\modules\comment\live\NewComment as NewCommentLive;
use humhub\modules\comment\notifications\NewComment as NewCommentNotification; use humhub\modules\comment\notifications\NewComment as NewCommentNotification;
use humhub\modules\content\components\ContentActiveRecord;
use humhub\modules\content\components\ContentAddonActiveRecord; use humhub\modules\content\components\ContentAddonActiveRecord;
use humhub\modules\content\interfaces\ContentOwner; use humhub\modules\content\interfaces\ContentOwner;
use humhub\modules\content\widgets\richtext\RichText; use humhub\modules\content\widgets\richtext\RichText;
use humhub\modules\post\models\Post; use humhub\modules\post\models\Post;
use humhub\modules\search\interfaces\Searchable; use humhub\modules\search\interfaces\Searchable;
use humhub\modules\search\libs\SearchHelper;
use humhub\modules\space\models\Space; use humhub\modules\space\models\Space;
use humhub\modules\user\models\User; use humhub\modules\user\models\User;
use Yii; use Yii;
@ -168,8 +170,10 @@ class Comment extends ContentAddonActiveRecord implements ContentOwner
*/ */
protected function updateContentSearch() protected function updateContentSearch()
{ {
if ($this->getCommentedRecord() instanceof Searchable) { /** @var ContentActiveRecord $content */
Yii::$app->search->update($this->getCommentedRecord()); $contentRecord = $this->getCommentedRecord();
if ($contentRecord !== null) {
SearchHelper::queueUpdate($contentRecord);
} }
} }

View File

@ -8,11 +8,11 @@
namespace humhub\modules\content; namespace humhub\modules\content;
use humhub\modules\content\components\ContentActiveRecord;
use humhub\modules\content\models\Content; use humhub\modules\content\models\Content;
use humhub\modules\search\jobs\DeleteDocument;
use humhub\modules\search\jobs\UpdateDocument;
use humhub\modules\user\events\UserEvent;
use humhub\modules\search\interfaces\Searchable; use humhub\modules\search\interfaces\Searchable;
use humhub\modules\search\libs\SearchHelper;
use humhub\modules\user\events\UserEvent;
use Yii; use Yii;
use yii\base\BaseObject; use yii\base\BaseObject;
@ -124,12 +124,9 @@ class Events extends BaseObject
*/ */
public static function onContentActiveRecordSave($event) public static function onContentActiveRecordSave($event)
{ {
if ($event->sender instanceof Searchable) { /** @var ContentActiveRecord $record */
Yii::$app->queue->push(new UpdateDocument([ $record = $event->sender;
'activeRecordClass' => get_class($event->sender), SearchHelper::queueUpdate($record);
'primaryKey' => $event->sender->id
]));
}
} }
/** /**
@ -139,12 +136,9 @@ class Events extends BaseObject
*/ */
public static function onContentActiveRecordDelete($event) public static function onContentActiveRecordDelete($event)
{ {
if ($event->sender instanceof Searchable) { /** @var ContentActiveRecord $record */
Yii::$app->queue->push(new DeleteDocument([ $record = $event->sender;
'activeRecordClass' => get_class($event->sender), SearchHelper::queueDelete($record);
'primaryKey' => $event->sender->id
]));
}
} }
} }

View File

@ -10,10 +10,11 @@ namespace humhub\modules\search;
use Yii; use Yii;
use yii\base\BaseObject; use yii\base\BaseObject;
use yii\console\Controller;
use yii\helpers\Console; use yii\helpers\Console;
/** /**
* Description of SearchModuleEvents * Search module event callbacks
* *
* @author luke * @author luke
*/ */
@ -25,18 +26,11 @@ class Events extends BaseObject
$event->sender->addWidget(widgets\SearchMenu::class); $event->sender->addWidget(widgets\SearchMenu::class);
} }
public static function onAfterSaveComment($event)
{
$comment = $event->sender;
if ($comment->content->getPolymorphicRelation() instanceof ISearchable) {
Yii::app()->search->update($comment->content->getPolymorphicRelation());
}
}
public static function onHourlyCron($event) public static function onHourlyCron($event)
{ {
/** @var Controller $controller */
$controller = $event->sender; $controller = $event->sender;
$controller->stdout('Optimizing search index...'); $controller->stdout('Optimizing search index...');
Yii::$app->search->optimize(); Yii::$app->search->optimize();
$controller->stdout('done.' . PHP_EOL, Console::FG_GREEN); $controller->stdout('done.' . PHP_EOL, Console::FG_GREEN);

View File

@ -8,7 +8,12 @@
namespace humhub\modules\search\libs; namespace humhub\modules\search\libs;
use humhub\modules\search\interfaces\Searchable;
use humhub\modules\search\jobs\DeleteDocument;
use humhub\modules\search\jobs\UpdateDocument;
use Yii;
use yii\base\BaseObject; use yii\base\BaseObject;
use yii\db\ActiveRecord;
/** /**
* SearchHelper * SearchHelper
@ -24,6 +29,7 @@ class SearchHelper extends BaseObject
* *
* @param string $query * @param string $query
* @param string $text * @param string $text
* @return boolean
*/ */
public static function matchQuery($query, $text) public static function matchQuery($query, $text)
{ {
@ -36,4 +42,47 @@ class SearchHelper extends BaseObject
return false; return false;
} }
/**
* Queues search index update of an active record
*
* @param ActiveRecord $record
* @return bool
*/
public static function queueUpdate(ActiveRecord $record)
{
if ($record instanceof Searchable) {
$pk = $record->getPrimaryKey();
if (!empty($pk) && !is_array($pk)) {
Yii::$app->queue->push(new UpdateDocument([
'activeRecordClass' => get_class($record),
'primaryKey' => $pk
]));
return true;
}
}
return false;
}
/**
* Queues search index delete of an active record
*
* @param ActiveRecord $record
* @return bool
*/
public static function queueDelete(ActiveRecord $record)
{
if ($record instanceof Searchable) {
$pk = $record->getPrimaryKey();
if (!empty($pk) && !is_array($pk)) {
Yii::$app->queue->push(new DeleteDocument([
'activeRecordClass' => get_class($record),
'primaryKey' => $pk
]));
return true;
}
}
return false;
}
} }