MediaTagController.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\MediaTag;
  5. use App\Profile;
  6. use App\UserFilter;
  7. use App\User;
  8. use Illuminate\Support\Str;
  9. class MediaTagController extends Controller
  10. {
  11. public function usernameLookup(Request $request)
  12. {
  13. abort_if(!$request->user(), 403);
  14. $this->validate($request, [
  15. 'q' => 'required|string|min:1|max:50'
  16. ]);
  17. $q = $request->input('q');
  18. if(Str::of($q)->startsWith('@')) {
  19. if(strlen($q) < 3) {
  20. return [];
  21. }
  22. $q = mb_substr($q, 1);
  23. }
  24. $blocked = UserFilter::whereFilterableType('App\Profile')
  25. ->whereFilterType('block')
  26. ->whereFilterableId($request->user()->profile_id)
  27. ->pluck('user_id');
  28. $blocked->push($request->user()->profile_id);
  29. $results = Profile::select('id','domain','username')
  30. ->whereNotIn('id', $blocked)
  31. ->whereNull('domain')
  32. ->where('username','like','%'.$q.'%')
  33. ->limit(15)
  34. ->get()
  35. ->map(function($r) {
  36. return [
  37. 'id' => (string) $r->id,
  38. 'name' => $r->username,
  39. 'privacy' => true,
  40. 'avatar' => $r->avatarUrl()
  41. ];
  42. });
  43. return $results;
  44. }
  45. }