1
0
Fork 1
mirror of https://github.com/pixelfed/pixelfed.git synced 2024-12-26 01:36:43 +00:00

Update ComposeController, improve location search results ordering by use frequency

This commit is contained in:
Daniel Supernault 2022-02-09 19:14:10 -07:00
parent 927a2a7d04
commit 29c4bd251a
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7

View file

@ -3,7 +3,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Auth, Cache, Storage, URL; use Auth, Cache, DB, Storage, URL;
use Carbon\Carbon; use Carbon\Carbon;
use App\{ use App\{
Avatar, Avatar,
@ -45,10 +45,12 @@ use App\Services\MediaBlocklistService;
use App\Services\MediaStorageService; use App\Services\MediaStorageService;
use App\Services\MediaTagService; use App\Services\MediaTagService;
use App\Services\StatusService; use App\Services\StatusService;
use App\Services\SnowflakeService;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use App\Util\Lexer\Autolink; use App\Util\Lexer\Autolink;
use App\Util\Lexer\Extractor; use App\Util\Lexer\Extractor;
use App\Util\Media\License; use App\Util\Media\License;
use Image;
class ComposeController extends Controller class ComposeController extends Controller
{ {
@ -74,7 +76,7 @@ class ComposeController extends Controller
'file.*' => function() { 'file.*' => function() {
return [ return [
'required', 'required',
'mimes:' . config_cache('pixelfed.media_types'), 'mimetypes:' . config_cache('pixelfed.media_types'),
'max:' . config_cache('pixelfed.max_photo_size'), 'max:' . config_cache('pixelfed.max_photo_size'),
]; ];
}, },
@ -169,7 +171,7 @@ class ComposeController extends Controller
'file' => function() { 'file' => function() {
return [ return [
'required', 'required',
'mimes:' . config_cache('pixelfed.media_types'), 'mimetypes:' . config_cache('pixelfed.media_types'),
'max:' . config_cache('pixelfed.max_photo_size'), 'max:' . config_cache('pixelfed.max_photo_size'),
]; ];
}, },
@ -313,22 +315,56 @@ class ComposeController extends Controller
$this->validate($request, [ $this->validate($request, [
'q' => 'required|string|max:100' 'q' => 'required|string|max:100'
]); ]);
$pid = $request->user()->profile_id;
abort_if(!$pid, 400);
$q = filter_var($request->input('q'), FILTER_SANITIZE_STRING); $q = filter_var($request->input('q'), FILTER_SANITIZE_STRING);
$hash = hash('sha256', $q); $hash = hash('sha256', $q);
$key = 'search:location:id:' . $hash; $key = 'pf:search:location:v1:id:' . $hash;
$places = Cache::remember($key, now()->addMinutes(15), function() use($q) { $popular = Cache::remember('pf:search:location:v1:popular', 86400, function() {
$q = '%' . $q . '%'; if(config('database.default') != 'mysql') {
return Place::where('name', 'like', $q) return [];
->take(80) }
$minId = SnowflakeService::byDate(now()->subDays(290));
return Status::selectRaw('id, place_id, count(place_id) as pc')
->whereNotNull('place_id')
->where('id', '>', $minId)
->groupBy('place_id')
->orderByDesc('pc')
->limit(400)
->get() ->get()
->filter(function($post) {
return $post;
})
->map(function($place) {
return [
'id' => $place->place_id,
'count' => $place->pc
];
});
});
$places = Cache::remember($key, 900, function() use($q, $popular) {
$q = '%' . $q . '%';
return DB::table('places')
->where('name', 'like', $q)
->limit((strlen($q) > 5 ? 360 : 180))
->get()
->sortByDesc(function($place, $key) use($popular) {
return $popular->filter(function($p) use($place) {
return $p['id'] == $place->id;
})->map(function($p) use($place) {
return in_array($place->country, ['Canada', 'USA', 'France', 'Germany', 'United Kingdom']) ? $p['count'] : 1;
})->values();
})
->map(function($r) { ->map(function($r) {
return [ return [
'id' => $r->id, 'id' => $r->id,
'name' => $r->name, 'name' => $r->name,
'country' => $r->country, 'country' => $r->country,
'url' => $r->url() 'url' => url('/discover/places/' . $r->id . '/' . $r->slug)
]; ];
}); })
->values()
->all();
}); });
return $places; return $places;
} }