pixelfed/app/Http/Controllers/SearchController.php

155 lines
5.8 KiB
PHP
Raw Normal View History

2018-05-26 22:50:36 +00:00
<?php
namespace App\Http\Controllers;
2018-09-27 05:00:45 +00:00
use Auth;
2018-08-28 03:07:36 +00:00
use App\Hashtag;
use App\Profile;
2018-09-27 05:00:45 +00:00
use App\Status;
2018-05-26 22:50:36 +00:00
use Illuminate\Http\Request;
2019-02-07 19:01:31 +00:00
use App\Util\ActivityPub\Helpers;
2018-05-26 22:50:36 +00:00
use Illuminate\Support\Facades\Cache;
2019-04-03 04:55:56 +00:00
use App\Transformer\Api\{
AccountTransformer,
HashtagTransformer,
StatusTransformer,
};
2018-05-26 22:50:36 +00:00
class SearchController extends Controller
{
2018-09-27 05:00:45 +00:00
public function __construct()
{
$this->middleware('auth');
}
2018-05-26 22:50:36 +00:00
public function searchAPI(Request $request, $tag)
{
2018-09-27 05:00:45 +00:00
if(mb_strlen($tag) < 3) {
return;
}
2019-04-03 04:55:56 +00:00
$tag = e(urldecode($tag));
2018-09-27 05:00:45 +00:00
$hash = hash('sha256', $tag);
2019-02-25 18:56:24 +00:00
$tokens = Cache::remember('api:search:tag:'.$hash, now()->addMinutes(5), function () use ($tag) {
2019-04-03 04:55:56 +00:00
$tokens = [];
2019-04-13 04:12:22 +00:00
if(Helpers::validateUrl($tag) != false && config('pixelfed.activitypub_enabled') == true) {
2019-02-07 19:01:31 +00:00
$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);
2019-04-03 04:55:56 +00:00
$tokens['profiles'] = [[
2019-02-07 19:01:31 +00:00
'count' => 1,
'url' => $item->url(),
'type' => 'profile',
'value' => $item->username,
'tokens' => [$item->username],
'name' => $item->name,
2019-04-03 04:55:56 +00:00
'entity' => [
'id' => $item->id,
'following' => $item->followedBy(Auth::user()->profile),
'thumb' => $item->avatarUrl()
]
]];
2019-02-07 19:01:31 +00:00
} else if ($type == 'Create') {
$item = Helpers::statusFirstOrFetch($tag, false);
2019-04-03 04:55:56 +00:00
$tokens['posts'] = [[
2019-02-07 19:01:31 +00:00
'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(),
2019-04-03 04:55:56 +00:00
]];
2019-02-07 19:01:31 +00:00
}
}
}
2019-02-28 04:45:06 +00:00
$hashtags = Hashtag::select('id', 'name', 'slug')->where('slug', 'like', '%'.$tag.'%')->whereHas('posts')->limit(20)->get();
2018-09-27 05:00:45 +00:00
if($hashtags->count() > 0) {
$tags = $hashtags->map(function ($item, $key) {
return [
'count' => $item->posts()->count(),
'url' => $item->url(),
'type' => 'hashtag',
'value' => $item->name,
2019-04-03 04:55:56 +00:00
'tokens' => '',
2018-09-27 05:00:45 +00:00
'name' => null,
];
});
2019-04-03 04:55:56 +00:00
$tokens['hashtags'] = $tags;
2018-09-27 05:00:45 +00:00
}
return $tokens;
2018-05-26 22:50:36 +00:00
});
2019-04-03 04:55:56 +00:00
$users = Profile::select('username', 'name', 'id')
->whereNull('status')
->where('id', '!=', Auth::user()->profile->id)
->where('username', 'like', '%'.$tag.'%')
2019-04-03 09:04:05 +00:00
->whereNull('domain')
//->orWhere('remote_url', $tag)
2019-04-03 04:55:56 +00:00
->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;
}
}
2018-09-27 05:00:45 +00:00
$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.'%')
2019-02-07 19:01:31 +00:00
->orWhere('uri', $tag)
2019-04-03 04:55:56 +00:00
->latest()
->limit(10)
2018-09-27 05:00:45 +00:00
->get();
2018-05-26 22:50:36 +00:00
2018-09-27 05:00:45 +00:00
if($posts->count() > 0) {
$posts = $posts->map(function($item, $key) {
return [
'count' => 0,
'url' => $item->url(),
'type' => 'status',
2018-11-19 03:42:35 +00:00
'value' => "by {$item->profile->username} <span class='float-right'>{$item->created_at->diffForHumans(null, true, true)}</span>",
2018-09-27 05:00:45 +00:00
'tokens' => [$item->caption],
'name' => $item->caption,
'thumb' => $item->thumb(),
];
});
2019-04-03 04:55:56 +00:00
$tokens['posts'] = $posts;
2018-09-27 05:00:45 +00:00
}
2019-04-03 04:55:56 +00:00
2018-09-27 05:00:45 +00:00
return response()->json($tokens);
2018-05-26 22:50:36 +00:00
}
2019-02-22 20:50:52 +00:00
public function results(Request $request)
{
$this->validate($request, [
'q' => 'required|string|min:1',
]);
return view('search.results');
}
2018-05-26 22:50:36 +00:00
}