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
1 changed files with 45 additions and 9 deletions

View File

@ -3,7 +3,7 @@
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth, Cache, Storage, URL;
use Auth, Cache, DB, Storage, URL;
use Carbon\Carbon;
use App\{
Avatar,
@ -45,10 +45,12 @@ use App\Services\MediaBlocklistService;
use App\Services\MediaStorageService;
use App\Services\MediaTagService;
use App\Services\StatusService;
use App\Services\SnowflakeService;
use Illuminate\Support\Str;
use App\Util\Lexer\Autolink;
use App\Util\Lexer\Extractor;
use App\Util\Media\License;
use Image;
class ComposeController extends Controller
{
@ -74,7 +76,7 @@ class ComposeController extends Controller
'file.*' => function() {
return [
'required',
'mimes:' . config_cache('pixelfed.media_types'),
'mimetypes:' . config_cache('pixelfed.media_types'),
'max:' . config_cache('pixelfed.max_photo_size'),
];
},
@ -169,7 +171,7 @@ class ComposeController extends Controller
'file' => function() {
return [
'required',
'mimes:' . config_cache('pixelfed.media_types'),
'mimetypes:' . config_cache('pixelfed.media_types'),
'max:' . config_cache('pixelfed.max_photo_size'),
];
},
@ -313,22 +315,56 @@ class ComposeController extends Controller
$this->validate($request, [
'q' => 'required|string|max:100'
]);
$pid = $request->user()->profile_id;
abort_if(!$pid, 400);
$q = filter_var($request->input('q'), FILTER_SANITIZE_STRING);
$hash = hash('sha256', $q);
$key = 'search:location:id:' . $hash;
$places = Cache::remember($key, now()->addMinutes(15), function() use($q) {
$key = 'pf:search:location:v1:id:' . $hash;
$popular = Cache::remember('pf:search:location:v1:popular', 86400, function() {
if(config('database.default') != 'mysql') {
return [];
}
$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()
->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 Place::where('name', 'like', $q)
->take(80)
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) {
return [
'id' => $r->id,
'name' => $r->name,
'country' => $r->country,
'url' => $r->url()
'url' => url('/discover/places/' . $r->id . '/' . $r->slug)
];
});
})
->values()
->all();
});
return $places;
}