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: Disabled Notification E-Mails for installation sample contents
- Fix: No e-mail summary immediately after installation
- Enh: Added queuing for search updates of commments
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\live\NewComment as NewCommentLive;
use humhub\modules\comment\notifications\NewComment as NewCommentNotification;
use humhub\modules\content\components\ContentActiveRecord;
use humhub\modules\content\components\ContentAddonActiveRecord;
use humhub\modules\content\interfaces\ContentOwner;
use humhub\modules\content\widgets\richtext\RichText;
use humhub\modules\post\models\Post;
use humhub\modules\search\interfaces\Searchable;
use humhub\modules\search\libs\SearchHelper;
use humhub\modules\space\models\Space;
use humhub\modules\user\models\User;
use Yii;
@ -168,8 +170,10 @@ class Comment extends ContentAddonActiveRecord implements ContentOwner
*/
protected function updateContentSearch()
{
if ($this->getCommentedRecord() instanceof Searchable) {
Yii::$app->search->update($this->getCommentedRecord());
/** @var ContentActiveRecord $content */
$contentRecord = $this->getCommentedRecord();
if ($contentRecord !== null) {
SearchHelper::queueUpdate($contentRecord);
}
}

View File

@ -8,11 +8,11 @@
namespace humhub\modules\content;
use humhub\modules\content\components\ContentActiveRecord;
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\libs\SearchHelper;
use humhub\modules\user\events\UserEvent;
use Yii;
use yii\base\BaseObject;
@ -98,7 +98,7 @@ class Events extends BaseObject
'object' => $event->sender->object,
'seperator' => ' · ',
'template' => '<div class="wall-entry-controls">{content}</div>',
], ['sortOrder' => 10]
], ['sortOrder' => 10]
);
}
@ -124,12 +124,9 @@ class Events extends BaseObject
*/
public static function onContentActiveRecordSave($event)
{
if ($event->sender instanceof Searchable) {
Yii::$app->queue->push(new UpdateDocument([
'activeRecordClass' => get_class($event->sender),
'primaryKey' => $event->sender->id
]));
}
/** @var ContentActiveRecord $record */
$record = $event->sender;
SearchHelper::queueUpdate($record);
}
/**
@ -139,12 +136,9 @@ class Events extends BaseObject
*/
public static function onContentActiveRecordDelete($event)
{
if ($event->sender instanceof Searchable) {
Yii::$app->queue->push(new DeleteDocument([
'activeRecordClass' => get_class($event->sender),
'primaryKey' => $event->sender->id
]));
}
/** @var ContentActiveRecord $record */
$record = $event->sender;
SearchHelper::queueDelete($record);
}
}

View File

@ -10,10 +10,11 @@ namespace humhub\modules\search;
use Yii;
use yii\base\BaseObject;
use yii\console\Controller;
use yii\helpers\Console;
/**
* Description of SearchModuleEvents
* Search module event callbacks
*
* @author luke
*/
@ -25,18 +26,11 @@ class Events extends BaseObject
$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)
{
/** @var Controller $controller */
$controller = $event->sender;
$controller->stdout('Optimizing search index...');
Yii::$app->search->optimize();
$controller->stdout('done.' . PHP_EOL, Console::FG_GREEN);

View File

@ -8,7 +8,12 @@
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\db\ActiveRecord;
/**
* SearchHelper
@ -21,9 +26,10 @@ class SearchHelper extends BaseObject
/**
* Checks if given text matches a search query.
*
*
* @param string $query
* @param string $text
* @return boolean
*/
public static function matchQuery($query, $text)
{
@ -36,4 +42,47 @@ class SearchHelper extends BaseObject
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;
}
}