Skip to content

Commit

Permalink
Search provider (#386)
Browse files Browse the repository at this point in the history
* Search provider

* Search provider

* Remove unused classes

* Fix meta search for old HumHub version
  • Loading branch information
yurabakhtin authored Apr 22, 2024
1 parent cf55fd6 commit 1bcc0c4
Show file tree
Hide file tree
Showing 10 changed files with 255 additions and 34 deletions.
29 changes: 21 additions & 8 deletions Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@

namespace humhub\modules\mail;

use humhub\modules\mail\models\Message;
use humhub\modules\mail\models\UserMessageTag;
use humhub\modules\mail\permissions\StartConversation;
use humhub\modules\user\models\User;
use Yii;
use humhub\modules\mail\helpers\Url;
use humhub\modules\mail\models\Config;
use humhub\modules\mail\models\MessageEntry;
use humhub\modules\mail\models\Message;
use humhub\modules\mail\models\UserMessageTag;
use humhub\modules\mail\models\UserMessage;
use humhub\modules\mail\permissions\SendMail;
use humhub\modules\mail\permissions\StartConversation;
use humhub\modules\mail\search\SearchProvider;
use humhub\modules\mail\widgets\NewMessageButton;
use humhub\modules\mail\widgets\NotificationInbox;
use humhub\modules\mail\permissions\SendMail;
use humhub\modules\mail\models\Config;
use humhub\widgets\MetaSearchWidget;
use humhub\modules\user\models\User;
use Yii;

/**
* Description of Events
Expand All @@ -28,7 +30,6 @@
*/
class Events
{

/**
* @param $event
*/
Expand Down Expand Up @@ -218,4 +219,16 @@ public static function onRestApiAddRules()
], 'mail');
}

public static function onMetaSearchWidgetInit($event)
{
if (Yii::$app->user->isGuest) {
return;
}

/* @var MetaSearchWidget $widget */
$widget = $event->sender;

$widget->addProvider(SearchProvider::class);
}

}
3 changes: 2 additions & 1 deletion config.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
['class' => ProfileHeaderControls::class, 'event' => ProfileHeaderControls::EVENT_INIT, 'callback' => ['humhub\modules\mail\Events', 'onProfileHeaderControlsInit']],
['class' => IntegrityController::class, 'event' => IntegrityController::EVENT_ON_RUN, 'callback' => ['humhub\modules\mail\Events', 'onIntegrityCheck']],
['class' => 'humhub\modules\rest\Module', 'event' => 'restApiAddRules', 'callback' => ['humhub\modules\mail\Events', 'onRestApiAddRules']],
['class' => 'humhub\widgets\MetaSearchWidget', 'event' => 'init', 'callback' => ['humhub\modules\mail\Events', 'onMetaSearchWidgetInit']],
],
];
];
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changelog
=========

3.2.2 (Unreleased)
----------------------
- Enh #382: Implement provider for meta searching

3.2.1 (April 15, 2024)
----------------------
- Enh #370: Remove message entries on disable module
Expand Down
22 changes: 15 additions & 7 deletions models/forms/InboxFilterForm.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
<?php


namespace humhub\modules\mail\models\forms;


use humhub\modules\mail\models\Message;
use humhub\modules\mail\models\MessageEntry;
use humhub\modules\mail\models\UserMessage;
use humhub\modules\mail\models\MessageTag;
use humhub\modules\mail\models\UserMessageTag;
use humhub\modules\mail\Module;
use humhub\modules\ui\filter\models\QueryFilter;
Expand All @@ -16,11 +13,9 @@
use yii\db\conditions\ExistsCondition;
use yii\db\conditions\LikeCondition;
use yii\db\conditions\OrCondition;
use yii\db\Expression;

class InboxFilterForm extends QueryFilter
{

/**
* @var string
*/
Expand Down Expand Up @@ -81,6 +76,14 @@ public function rules()
public function init()
{
parent::init();

if ($this->autoLoad === self::AUTO_LOAD_ALL || $this->autoLoad === self::AUTO_LOAD_GET) {
$keyword = Yii::$app->request->get('keyword');
if ($keyword !== null) {
$this->term = $keyword;
}
}

$this->query = UserMessage::findByUser();
}

Expand All @@ -95,7 +98,7 @@ public function apply()

$this->query->andWhere(new OrCondition([
new ExistsCondition('EXISTS', $messageEntryContentSubQuery),
$this->createTermLikeCondition('message.title')
$this->createTermLikeCondition('message.title'),
]));
}

Expand Down Expand Up @@ -163,4 +166,9 @@ public function formName()
{
return '';
}
}

public function isFiltered(): bool
{
return !empty($this->term) || !empty($this->participants) || !empty($this->tags);
}
}
4 changes: 2 additions & 2 deletions module.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
"messenger",
"communication"
],
"version": "3.2.1",
"version": "3.2.2",
"humhub": {
"minVersion": "1.15"
"minVersion": "1.16"
},
"homepage": "https://github.com/humhub/mail",
"authors": [
Expand Down
106 changes: 106 additions & 0 deletions search/SearchProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/

namespace humhub\modules\mail\search;

use humhub\interfaces\MetaSearchProviderInterface;
use humhub\modules\mail\models\forms\InboxFilterForm;
use humhub\services\MetaSearchService;
use Yii;

/**
* Mail Meta Search Provider
*/
class SearchProvider implements MetaSearchProviderInterface
{
private ?MetaSearchService $service = null;
public ?string $keyword = null;
public string|array|null $route = '/mail/mail/index';

/**
* @inheritdoc
*/
public function getName(): string
{
return Yii::t('MailModule.base', 'Messages');
}

/**
* @inheritdoc
*/
public function getSortOrder(): int
{
return 400;
}

/**
* @inheritdoc
*/
public function getRoute(): string|array
{
return $this->route;
}

/**
* @inheritdoc
*/
public function getAllResultsText(): string
{
return $this->getService()->hasResults()
? Yii::t('base', 'Show all results')
: Yii::t('MailModule.base', 'Advanced Messages Search');
}

/**
* @inheritdoc
*/
public function getIsHiddenWhenEmpty(): bool
{
return true;
}

/**
* @inheritdoc
*/
public function getResults(int $maxResults): array
{
$filter = new InboxFilterForm(['term' => $this->getKeyword()]);
$filter->apply();
$totalCount = $filter->query->count();
$resultsQuery = $filter->query->limit($maxResults);

$results = [];
foreach ($resultsQuery->all() as $userMessage) {
$results[] = new SearchRecord($userMessage);
}

return [
'totalCount' => $totalCount,
'results' => $results,
];
}

/**
* @inheritdoc
*/
public function getService(): MetaSearchService
{
if ($this->service === null) {
$this->service = new MetaSearchService($this);
}

return $this->service;
}

/**
* @inheritdoc
*/
public function getKeyword(): ?string
{
return $this->keyword;
}
}
72 changes: 72 additions & 0 deletions search/SearchRecord.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/

namespace humhub\modules\mail\search;

use humhub\interfaces\MetaSearchResultInterface;
use humhub\modules\mail\helpers\Url;
use humhub\modules\mail\models\UserMessage;
use humhub\modules\mail\widgets\InboxMessagePreview;
use humhub\modules\ui\icon\widgets\Icon;
use humhub\modules\user\models\User;
use humhub\modules\user\widgets\Image;

/**
* Search Record for Message Entry
*/
class SearchRecord implements MetaSearchResultInterface
{
public ?InboxMessagePreview $preview = null;

public function __construct(UserMessage $userMessage)
{
$this->preview = new InboxMessagePreview(['userMessage' => $userMessage]);
}

/**
* @inheritdoc
*/
public function getImage(): string
{
$lastParticipant = $this->preview->lastParticipant();

if ($lastParticipant instanceof User) {
return Image::widget([
'user' => $lastParticipant,
'width' => 36,
'link' => false,
'hideOnlineStatus' => true,
]);
}

return Icon::get('envelope');
}

/**
* @inheritdoc
*/
public function getTitle(): string
{
return $this->preview->getMessage()->title;
}

/**
* @inheritdoc
*/
public function getDescription(): string
{
return $this->preview->getMessagePreview();
}

/**
* @inheritdoc
*/
public function getUrl(): string
{
return Url::toMessenger($this->preview->getMessage());
}
}
16 changes: 7 additions & 9 deletions widgets/InboxMessagePreview.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php


namespace humhub\modules\mail\widgets;


use DateTime;
use DateTimeZone;
use humhub\components\Widget;
Expand Down Expand Up @@ -34,7 +32,7 @@ public function run()
'messageText' => $this->getMessagePreview(),
'messageTime' => $this->getMessageTime(),
'lastParticipant' => $this->lastParticipant(),
'options' => $this->getOptions()
'options' => $this->getOptions(),
]);
}

Expand All @@ -48,11 +46,11 @@ private function getOptions(): array
'message-id' => $message->id,
'action-click' => 'mail.notification.loadMessage',
'action-url' => Url::toMessenger($message),
]
],
];
}

private function getMessage(): Message
public function getMessage(): Message
{
if ($this->_message === null) {
$this->_message = $this->userMessage->message;
Expand All @@ -61,7 +59,7 @@ private function getMessage(): Message
return $this->_message;
}

private function lastParticipant(): ?User
public function lastParticipant(): ?User
{
return $this->isGroupChat()
? $this->getLastEntry()->user
Expand All @@ -88,7 +86,7 @@ private function getMessageTitle(): string
{
if ($this->isGroupChat()) {
$suffix = ', ' . Yii::t('MailModule.base', '{n,plural,=1{# other} other{# others}}', [
'n' => $this->getMessage()->getUsersCount() - 2
'n' => $this->getMessage()->getUsersCount() - 2,
]);
} else {
$suffix = '';
Expand All @@ -97,7 +95,7 @@ private function getMessageTitle(): string
return $this->getUsername() . $suffix;
}

private function getMessagePreview(): string
public function getMessagePreview(): string
{
switch ($this->getLastEntry()->type) {
case AbstractMessageEntry::TYPE_USER_JOINED:
Expand Down Expand Up @@ -131,7 +129,7 @@ private function getMessageTime(): string
if ($datetime->format('Y-m-d') === date('Y-m-d')) {
// Show time for today
return Yii::$app->formatter->asTime($datetime, 'short');
} else if (time() - $datetime->getTimestamp() < 3600 * 24 * 7) {
} elseif (time() - $datetime->getTimestamp() < 3600 * 24 * 7) {
// Show week day for week ago messages
switch ($datetime->format('w')) {
case 0: return Yii::t('MailModule.base', 'Sunday');
Expand Down
Loading

0 comments on commit 1bcc0c4

Please sign in to comment.