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

Spotlight #152

Merged
merged 16 commits into from
Apr 13, 2022
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,5 @@ LANG_CONTRIBUTION_URL=null
POSTMARK_TOKEN=null

IPGEOLOCATION_KEY=null

WIKI_BASE_URL="https://wiki.pokemon3d.net"
42 changes: 42 additions & 0 deletions app/Helpers/WikiHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\Helpers;

use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Http;

class WikiHelper
{
public static function sendRequest($endpoint)
{
if (config('wiki.api_url') === null) {
return 'Wiki API URL is not set';
}
$url = config('wiki.api_url') . '?' . $endpoint;
$response = Http::withHeaders([
'Accept' => 'application/json',
])
->get($url)
->body();
return json_decode($response);
}

public static function getSearchResults(string $query)
{
if (empty($query)) {
return collect([]);
}
$endpoint =
'action=query&format=json&list=search&indexpageids=1&iwurl=1&srsearch=' .
$query .
'&srnamespace=0&srprop=size%7Cwordcount%7Ctimestamp%7Csnippet&srsort=relevance';
return self::sendRequest($endpoint);
}

public static function getAllPages(): Collection
{
$endpoint =
'action=query&format=json&list=allpages&indexpageids=1&iwurl=1&apnamespace=0&apfilterredir=nonredirects&aplimit=500&apdir=ascending';
return collect(self::sendRequest($endpoint)->query->allpages);
}
}
4 changes: 2 additions & 2 deletions app/Http/Controllers/Skin/SkinController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ class SkinController extends Controller
*
* @return \Illuminate\Http\Response
*/
public function show($uuid)
public function show(Skin $skin)
{
$skin = Skin::where('uuid', $uuid)
$skin = Skin::where('uuid', $skin->uuid)
->isPublic()
->first();
abort_unless($skin, 404);
Expand Down
47 changes: 47 additions & 0 deletions app/Spotlight/Logout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace App\Spotlight;

use Illuminate\Contracts\Auth\StatefulGuard;
use LivewireUI\Spotlight\Spotlight;
use LivewireUI\Spotlight\SpotlightCommand;

class Logout extends SpotlightCommand
{
/**
* This is the name of the command that will be shown in the Spotlight component.
*/
protected string $name = 'Logout';

/**
* This is the description of your command which will be shown besides the command name.
*/
protected string $description = 'Logout out of your account';

/**
* You can define any number of additional search terms (also known as synonyms)
* to be used when searching for this command.
*/
protected array $synonyms = [];

/**
* When all dependencies have been resolved the execute method is called.
* You can type-hint all resolved dependency you defined earlier.
*/
public function execute(Spotlight $spotlight, StatefulGuard $guard): void
{
$guard->logout();
$spotlight->redirect('/');
}

/**
* You can provide any custom logic you want to determine whether the
* command will be shown in the Spotlight component. If you don't have any
* logic you can remove this method. You can type-hint any dependencies you
* need and they will be resolved from the container.
*/
public function shouldBeShown(): bool
{
return true;
}
}
81 changes: 81 additions & 0 deletions app/Spotlight/ResourceSearch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace App\Spotlight;

use App\Models\Resource;
use LivewireUI\Spotlight\Spotlight;
use LivewireUI\Spotlight\SpotlightCommand;
use LivewireUI\Spotlight\SpotlightCommandDependencies;
use LivewireUI\Spotlight\SpotlightCommandDependency;
use LivewireUI\Spotlight\SpotlightSearchResult;

class ResourceSearch extends SpotlightCommand
{
/**
* This is the name of the command that will be shown in the Spotlight component.
*/
protected string $name = 'Search Resources';

/**
* This is the description of your command which will be shown besides the command name.
*/
protected string $description = 'Search for a resource by name.';

/**
* You can define any number of additional search terms (also known as synonyms)
* to be used when searching for this command.
*/
protected array $synonyms = ['resource', 'resources', 'resource search'];

/**
* Defining dependencies is optional. If you don't have any dependencies you can remove this method.
* Dependencies are asked from your user in the order you add the dependencies.
*/
public function dependencies(): ?SpotlightCommandDependencies
{
return SpotlightCommandDependencies::collection()->add(
// In this example we will register a 'team' dependency
SpotlightCommandDependency::make('resource')
// The default Spotlight placeholder will be changed to your dependency place holder
->setPlaceholder('For which resource do you want to search?')
);
}

/**
* Spotlight will resolve dependencies by calling the search method followed by your dependency name.
* The method will receive the search query as the parameter.
*/
public function searchResource($query)
{
return Resource::where('name', 'like', "%$query%")
->get()
->map(function (Resource $resource) {
// You must map your search result into SpotlightSearchResult objects
return new SpotlightSearchResult(
$resource->uuid,
$resource->name,
sprintf('Show details for %s', $resource->name)
);
});
}

/**
* When all dependencies have been resolved the execute method is called.
* You can type-hint all resolved dependency you defined earlier.
*/
public function execute(Spotlight $spotlight, Resource $resource)
{
$spotlight->redirectRoute('resource.uuid', $resource->uuid);
}

/**
* You can provide any custom logic you want to determine whether the
* command will be shown in the Spotlight component. If you don't have any
* logic you can remove this method. You can type-hint any dependencies you
* need and they will be resolved from the container.
*/
public function shouldBeShown(): bool
{
return true;
}
}
74 changes: 74 additions & 0 deletions app/Spotlight/SkinSearch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace App\Spotlight;

use App\Models\Skin;
use App\Models\User;
use LivewireUI\Spotlight\Spotlight;
use LivewireUI\Spotlight\SpotlightCommand;
use LivewireUI\Spotlight\SpotlightCommandDependencies;
use LivewireUI\Spotlight\SpotlightCommandDependency;
use LivewireUI\Spotlight\SpotlightSearchResult;

class SkinSearch extends SpotlightCommand
{
/**
* This is the name of the command that will be shown in the Spotlight component.
*/
protected string $name = 'Search Skins';

/**
* This is the description of your command which will be shown besides the command name.
*/
protected string $description = 'Search for a skin by name.';

/**
* You can define any number of additional search terms (also known as synonyms)
* to be used when searching for this command.
*/
protected array $synonyms = ['skin', 'skins', 'skin search'];

public function dependencies(): ?SpotlightCommandDependencies
{
return SpotlightCommandDependencies::collection()->add(
// In this example we will register a 'team' dependency
SpotlightCommandDependency::make('skin')
// The default Spotlight placeholder will be changed to your dependency place holder
->setPlaceholder('For which skin do you want to search?')
);
}

/**
* Spotlight will resolve dependencies by calling the search method followed by your dependency name.
* The method will receive the search query as the parameter.
*/
public function searchSkin($query)
{
return Skin::where('name', 'like', "%$query%")
->get()
->map(function (Skin $skin) {
// You must map your search result into SpotlightSearchResult objects
return new SpotlightSearchResult($skin->uuid, $skin->name, sprintf('Show details for %s', $skin->name));
});
}

/**
* When all dependencies have been resolved the execute method is called.
* You can type-hint all resolved dependency you defined earlier.
*/
public function execute(Spotlight $spotlight, Skin $skin)
{
$spotlight->redirectRoute('skin-show', $skin);
}

/**
* You can provide any custom logic you want to determine whether the
* command will be shown in the Spotlight component. If you don't have any
* logic you can remove this method. You can type-hint any dependencies you
* need and they will be resolved from the container.
*/
public function shouldBeShown(): bool
{
return true;
}
}
81 changes: 81 additions & 0 deletions app/Spotlight/UserSearch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace App\Spotlight;

use App\Models\User;
use LivewireUI\Spotlight\Spotlight;
use LivewireUI\Spotlight\SpotlightCommand;
use LivewireUI\Spotlight\SpotlightCommandDependencies;
use LivewireUI\Spotlight\SpotlightCommandDependency;
use LivewireUI\Spotlight\SpotlightSearchResult;

class UserSearch extends SpotlightCommand
{
/**
* This is the name of the command that will be shown in the Spotlight component.
*/
protected string $name = 'Search Users';

/**
* This is the description of your command which will be shown besides the command name.
*/
protected string $description = 'Search for a user by username.';

/**
* You can define any number of additional search terms (also known as synonyms)
* to be used when searching for this command.
*/
protected array $synonyms = ['user', 'users', 'person', 'people', 'member', 'members'];

/**
* Defining dependencies is optional. If you don't have any dependencies you can remove this method.
* Dependencies are asked from your user in the order you add the dependencies.
*/
public function dependencies(): ?SpotlightCommandDependencies
{
return SpotlightCommandDependencies::collection()->add(
// In this example we will register a 'team' dependency
SpotlightCommandDependency::make('user')
// The default Spotlight placeholder will be changed to your dependency place holder
->setPlaceholder('For which user do you want to search?')
);
}

/**
* Spotlight will resolve dependencies by calling the search method followed by your dependency name.
* The method will receive the search query as the parameter.
*/
public function searchUser($query)
{
return User::where('username', 'like', "%$query%")
->get()
->map(function (User $user) {
// You must map your search result into SpotlightSearchResult objects
return new SpotlightSearchResult(
$user->id,
$user->username,
sprintf('Show details for %s', $user->username)
);
});
}

/**
* When all dependencies have been resolved the execute method is called.
* You can type-hint all resolved dependency you defined earlier.
*/
public function execute(Spotlight $spotlight, User $user)
{
$spotlight->redirectRoute('member.show', $user);
}

/**
* You can provide any custom logic you want to determine whether the
* command will be shown in the Spotlight component. If you don't have any
* logic you can remove this method. You can type-hint any dependencies you
* need and they will be resolved from the container.
*/
public function shouldBeShown(): bool
{
return true;
}
}
Loading