Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 73 additions & 1 deletion lib/Chat/AutoComplete/SearchPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@
use OCP\Collaboration\Collaborators\ISearchPlugin;
use OCP\Collaboration\Collaborators\ISearchResult;
use OCP\Collaboration\Collaborators\SearchResultType;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\IUserManager;

class SearchPlugin implements ISearchPlugin {
protected IUserManager $userManager;
protected IGroupManager $groupManager;
protected GuestManager $guestManager;
protected TalkSession $talkSession;
protected ParticipantService $participantService;
Expand All @@ -47,13 +50,15 @@ class SearchPlugin implements ISearchPlugin {
protected ?Room $room = null;

public function __construct(IUserManager $userManager,
IGroupManager $groupManager,
GuestManager $guestManager,
TalkSession $talkSession,
ParticipantService $participantService,
Util $util,
?string $userId,
IL10N $l) {
$this->userManager = $userManager;
$this->groupManager = $groupManager;
$this->guestManager = $guestManager;
$this->talkSession = $talkSession;
$this->participantService = $participantService;
Expand Down Expand Up @@ -82,7 +87,7 @@ public function search($search, $limit, $offset, ISearchResult $searchResult) {
}
}

$userIds = $guestAttendees = [];
$userIds = $groupIds = $guestAttendees = [];
if ($this->room->getType() === Room::TYPE_ONE_TO_ONE) {
// Add potential leavers of one-to-one rooms again.
$participants = json_decode($this->room->getName(), true);
Expand All @@ -97,11 +102,14 @@ public function search($search, $limit, $offset, ISearchResult $searchResult) {
$guestAttendees[] = $attendee;
} elseif ($attendee->getActorType() === Attendee::ACTOR_USERS) {
$userIds[] = $attendee->getActorId();
} elseif ($attendee->getActorType() === Attendee::ACTOR_GROUPS) {
$groupIds[] = $attendee->getActorId();
}
}
}

$this->searchUsers($search, $userIds, $searchResult);
$this->searchGroups($search, $groupIds, $searchResult);
$this->searchGuests($search, $guestAttendees, $searchResult);

return false;
Expand Down Expand Up @@ -157,6 +165,51 @@ protected function searchUsers(string $search, array $userIds, ISearchResult $se
$searchResult->addResultSet($type, $matches, $exactMatches);
}

protected function searchGroups(string $search, array $groupIds, ISearchResult $searchResult): void {
$search = strtolower($search);

$type = new SearchResultType('groups');

$matches = $exactMatches = [];
foreach ($groupIds as $groupId) {
if ($searchResult->hasResult($type, $groupId)) {
continue;
}

if ($search === '') {
$matches[] = $this->createGroupResult($groupId);
continue;
}

if (strtolower($groupId) === $search) {
$exactMatches[] = $this->createGroupResult($groupId);
continue;
}

if (stripos($groupId, $search) !== false) {
$matches[] = $this->createGroupResult($groupId);
continue;
}

$group = $this->groupManager->get($groupId);
if (!$group instanceof IGroup) {
continue;
}

if (strtolower($group->getDisplayName()) === $search) {
$exactMatches[] = $this->createGroupResult($group->getGID(), $group->getDisplayName());
continue;
}

if (stripos($group->getDisplayName(), $search) !== false) {
$matches[] = $this->createGroupResult($group->getGID(), $group->getDisplayName());
continue;
}
}

$searchResult->addResultSet($type, $matches, $exactMatches);
}

/**
* @param string $search
* @param Attendee[] $attendees
Expand Down Expand Up @@ -218,6 +271,25 @@ protected function createResult(string $type, string $uid, string $name): array
];
}

protected function createGroupResult(string $groupId, string $name = ''): array {
if ($name === '') {
$group = $this->groupManager->get($groupId);
if ($group instanceof IGroup) {
$name = $group->getDisplayName();
} else {
$name = $groupId;
}
}

return [
'label' => $name,
'value' => [
'shareType' => 'group',
'shareWith' => 'group/' . $groupId,
],
];
}

protected function createGuestResult(string $actorId, string $name): array {
return [
'label' => $name,
Expand Down
Loading