Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Group Gambit Improvements #2192

Merged
merged 4 commits into from Jun 8, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 0 additions & 13 deletions src/Group/GroupRepository.php
Expand Up @@ -41,19 +41,6 @@ public function findOrFail($id, User $actor = null)
return $this->scopeVisibleTo($query, $actor)->firstOrFail();
}

/**
* Find a group by name.
*
* @param string $name
* @return User|null
*/
public function findByName($name, User $actor = null)
{
$query = Group::where('name_singular', $name)->orWhere('name_plural', $name);

return $this->scopeVisibleTo($query, $actor)->first();
}

/**
* Scope a query to only include records that are visible to a user.
*
Expand Down
24 changes: 15 additions & 9 deletions src/User/Search/Gambit/GroupGambit.php
Expand Up @@ -9,6 +9,7 @@

namespace Flarum\User\Search\Gambit;

use Flarum\Group\Group;
use Flarum\Group\GroupRepository;
use Flarum\Search\AbstractRegexGambit;
use Flarum\Search\AbstractSearch;
Expand Down Expand Up @@ -44,18 +45,23 @@ protected function conditions(AbstractSearch $search, array $matches, $negate)
throw new LogicException('This gambit can only be applied on a UserSearch');
}

$groupNames = $this->extractGroupNames($matches);
$groupIdentifiers = $this->extractGroupIdentifiers($matches);

// TODO: Use a JOIN instead (and don't forget to remove the findByName() method again)
$ids = [];
foreach ($groupNames as $name) {
$group = $this->groups->findByName($name);
if ($group && count($group->users)) {
$ids = array_merge($ids, $group->users->pluck('id')->all());
$groupQuery = Group::whereVisibleTo($search->getActor());

foreach ($groupIdentifiers as $identifier) {
if (is_numeric($identifier)) {
$groupQuery->orWhere('id', $identifier);
} else {
$groupQuery->orWhere('name_singular', $identifier)->orWhere('name_plural', $identifier);
}
}

$search->getQuery()->whereIn('id', $ids, 'and', $negate);
$userIds = $groupQuery->join('group_user', 'groups.id', 'group_user.group_id')
->pluck('group_user.user_id')
->all();

$search->getQuery()->whereIn('id', $userIds, 'and', $negate);
}

/**
Expand All @@ -64,7 +70,7 @@ protected function conditions(AbstractSearch $search, array $matches, $negate)
* @param array $matches
* @return array
*/
protected function extractGroupNames(array $matches)
protected function extractGroupIdentifiers(array $matches)
{
return explode(',', trim($matches[1], '"'));
}
Expand Down