mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-11-10 00:34:50 +00:00
Add SearchController
This commit is contained in:
parent
3dafc278b5
commit
1252f76a2f
1 changed files with 42 additions and 0 deletions
42
app/Http/Controllers/SearchController.php
Normal file
42
app/Http/Controllers/SearchController.php
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\{Hashtag, Profile};
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
|
||||||
|
class SearchController extends Controller
|
||||||
|
{
|
||||||
|
public function searchAPI(Request $request, $tag)
|
||||||
|
{
|
||||||
|
$res = Cache::remember('api:search:tag:' . $tag, 1440, function() use($tag) {
|
||||||
|
$res = Hashtag::where('slug', 'like', '%'.$tag.'%')->get();
|
||||||
|
$tags = $res->map(function($item, $key) {
|
||||||
|
return [
|
||||||
|
'count' => $item->posts()->count(),
|
||||||
|
'url' => $item->url(),
|
||||||
|
'type' => 'hashtag',
|
||||||
|
'value' => $item->name,
|
||||||
|
'tokens' => explode('-', $item->name),
|
||||||
|
'name' => null
|
||||||
|
];
|
||||||
|
});
|
||||||
|
$res = Profile::where('username', 'like', '%'.$tag.'%')->get();
|
||||||
|
$profiles = $res->map(function($item, $key) {
|
||||||
|
return [
|
||||||
|
'count' => 0,
|
||||||
|
'url' => $item->url(),
|
||||||
|
'type' => 'profile',
|
||||||
|
'value' => $item->username,
|
||||||
|
'tokens' => [$item->username],
|
||||||
|
'name' => $item->name
|
||||||
|
];
|
||||||
|
});
|
||||||
|
$tags = $tags->push($profiles[0]);
|
||||||
|
return $tags;
|
||||||
|
});
|
||||||
|
|
||||||
|
return response()->json($res);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue