123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- <?php
- namespace App\Http\Controllers;
- use Auth;
- use App\Hashtag;
- use App\Profile;
- use App\Status;
- use Illuminate\Http\Request;
- use App\Util\ActivityPub\Helpers;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Str;
- use App\Transformer\Api\{
- AccountTransformer,
- HashtagTransformer,
- StatusTransformer,
- };
- class SearchController extends Controller
- {
- public function __construct()
- {
- $this->middleware('auth');
- }
- public function searchAPI(Request $request)
- {
- $this->validate($request, [
- 'q' => 'required|string|min:3|max:120',
- 'src' => 'required|string|in:metro',
- 'v' => 'required|integer|in:1'
- ]);
- $tag = $request->input('q');
- $tag = e(urldecode($tag));
- $hash = hash('sha256', $tag);
- $tokens = Cache::remember('api:search:tag:'.$hash, now()->addMinutes(5), function () use ($tag) {
- $tokens = [];
- if(Helpers::validateUrl($tag) != false && config('pixelfed.activitypub_enabled') == true && config('pixelfed.remote_follow_enabled') == true) {
- $remote = Helpers::fetchFromUrl($tag);
- if(isset($remote['type']) && in_array($remote['type'], ['Create', 'Person']) == true) {
- $type = $remote['type'];
- if($type == 'Person') {
- $item = Helpers::profileFirstOrNew($tag);
- $tokens['profiles'] = [[
- 'count' => 1,
- 'url' => $item->url(),
- 'type' => 'profile',
- 'value' => $item->username,
- 'tokens' => [$item->username],
- 'name' => $item->name,
- 'entity' => [
- 'id' => $item->id,
- 'following' => $item->followedBy(Auth::user()->profile),
- 'thumb' => $item->avatarUrl()
- ]
- ]];
- } else if ($type == 'Create') {
- $item = Helpers::statusFirstOrFetch($tag, false);
- $tokens['posts'] = [[
- 'count' => 0,
- 'url' => $item->url(),
- 'type' => 'status',
- 'value' => "by {$item->profile->username} <span class='float-right'>{$item->created_at->diffForHumans(null, true, true)}</span>",
- 'tokens' => [$item->caption],
- 'name' => $item->caption,
- 'thumb' => $item->thumb(),
- ]];
- }
- }
- }
- $htag = Str::startsWith($tag, '#') == true ? mb_substr($tag, 1) : $tag;
- $hashtags = Hashtag::select('id', 'name', 'slug')
- ->where('slug', 'like', '%'.$htag.'%')
- ->whereHas('posts')
- ->limit(20)
- ->get();
- if($hashtags->count() > 0) {
- $tags = $hashtags->map(function ($item, $key) {
- return [
- 'count' => $item->posts()->count(),
- 'url' => $item->url(),
- 'type' => 'hashtag',
- 'value' => $item->name,
- 'tokens' => '',
- 'name' => null,
- ];
- });
- $tokens['hashtags'] = $tags;
- }
- return $tokens;
- });
- $users = Profile::select('username', 'name', 'id')
- ->whereNull('status')
- ->where('id', '!=', Auth::user()->profile->id)
- ->where('username', 'like', '%'.$tag.'%')
- ->whereNull('domain')
- //->orWhere('remote_url', $tag)
- ->limit(20)
- ->get();
- if($users->count() > 0) {
- $profiles = $users->map(function ($item, $key) {
- return [
- 'count' => 0,
- 'url' => $item->url(),
- 'type' => 'profile',
- 'value' => $item->username,
- 'tokens' => [$item->username],
- 'name' => $item->name,
- 'avatar' => $item->avatarUrl(),
- 'id' => $item->id,
- 'entity' => [
- 'id' => $item->id,
- 'following' => $item->followedBy(Auth::user()->profile),
- 'thumb' => $item->avatarUrl()
- ]
- ];
- });
- if(isset($tokens['profiles'])) {
- array_push($tokens['profiles'], $profiles);
- } else {
- $tokens['profiles'] = $profiles;
- }
- }
- $posts = Status::select('id', 'profile_id', 'caption', 'created_at')
- ->whereHas('media')
- ->whereNull('in_reply_to_id')
- ->whereNull('reblog_of_id')
- ->whereProfileId(Auth::user()->profile->id)
- ->where('caption', 'like', '%'.$tag.'%')
- ->orWhere('uri', $tag)
- ->latest()
- ->limit(10)
- ->get();
- if($posts->count() > 0) {
- $posts = $posts->map(function($item, $key) {
- return [
- 'count' => 0,
- 'url' => $item->url(),
- 'type' => 'status',
- 'value' => "by {$item->profile->username} <span class='float-right'>{$item->created_at->diffForHumans(null, true, true)}</span>",
- 'tokens' => [$item->caption],
- 'name' => $item->caption,
- 'thumb' => $item->thumb(),
- ];
- });
- $tokens['posts'] = $posts;
- }
- return response()->json($tokens);
- }
- public function results(Request $request)
- {
- $this->validate($request, [
- 'q' => 'required|string|min:1',
- ]);
-
- return view('search.results');
- }
- }
|