MediaTagController.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Services\MediaTagService;
  5. use App\MediaTag;
  6. use App\Notification;
  7. use App\Profile;
  8. use App\UserFilter;
  9. use App\User;
  10. use Illuminate\Support\Str;
  11. class MediaTagController extends Controller
  12. {
  13. public function __construct()
  14. {
  15. $this->middleware('auth');
  16. }
  17. public function usernameLookup(Request $request)
  18. {
  19. abort_if(!$request->user(), 403);
  20. $this->validate($request, [
  21. 'q' => 'required|string|min:1|max:50'
  22. ]);
  23. $q = $request->input('q');
  24. if(Str::of($q)->startsWith('@')) {
  25. if(strlen($q) < 3) {
  26. return [];
  27. }
  28. $q = mb_substr($q, 1);
  29. }
  30. $blocked = UserFilter::whereFilterableType('App\Profile')
  31. ->whereFilterType('block')
  32. ->whereFilterableId($request->user()->profile_id)
  33. ->pluck('user_id');
  34. $blocked->push($request->user()->profile_id);
  35. $results = Profile::select('id','domain','username')
  36. ->whereNotIn('id', $blocked)
  37. ->whereNull('domain')
  38. ->where('username','like','%'.$q.'%')
  39. ->limit(15)
  40. ->get()
  41. ->map(function($r) {
  42. return [
  43. 'id' => (string) $r->id,
  44. 'name' => $r->username,
  45. 'privacy' => true,
  46. 'avatar' => $r->avatarUrl()
  47. ];
  48. });
  49. return $results;
  50. }
  51. public function untagProfile(Request $request)
  52. {
  53. abort_if(!$request->user(), 403);
  54. $this->validate($request, [
  55. 'status_id' => 'required',
  56. 'profile_id' => 'required'
  57. ]);
  58. $user = $request->user();
  59. $status_id = $request->input('status_id');
  60. $profile_id = (int) $request->input('profile_id');
  61. abort_if((int) $user->profile_id !== $profile_id, 400);
  62. $tag = MediaTag::whereStatusId($status_id)
  63. ->whereProfileId($profile_id)
  64. ->first();
  65. if(!$tag) {
  66. return [];
  67. }
  68. Notification::whereItemType('App\MediaTag')
  69. ->whereItemId($tag->id)
  70. ->whereProfileId($profile_id)
  71. ->whereAction('tagged')
  72. ->delete();
  73. MediaTagService::untag($status_id, $profile_id);
  74. return [200];
  75. }
  76. }