SearchController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Auth;
  4. use App\Hashtag;
  5. use App\Profile;
  6. use App\Status;
  7. use Illuminate\Http\Request;
  8. use App\Util\ActivityPub\Helpers;
  9. use Illuminate\Support\Facades\Cache;
  10. use Illuminate\Support\Str;
  11. use App\Transformer\Api\{
  12. AccountTransformer,
  13. HashtagTransformer,
  14. StatusTransformer,
  15. };
  16. class SearchController extends Controller
  17. {
  18. public function __construct()
  19. {
  20. $this->middleware('auth');
  21. }
  22. public function searchAPI(Request $request)
  23. {
  24. $this->validate($request, [
  25. 'q' => 'required|string|min:3|max:120',
  26. 'src' => 'required|string|in:metro',
  27. 'v' => 'required|integer|in:1'
  28. ]);
  29. $tag = $request->input('q');
  30. $tag = e(urldecode($tag));
  31. $hash = hash('sha256', $tag);
  32. $tokens = Cache::remember('api:search:tag:'.$hash, now()->addMinutes(5), function () use ($tag) {
  33. $tokens = [];
  34. if(Helpers::validateUrl($tag) != false && config('federation.activitypub.enabled') == true && config('federation.activitypub.remoteFollow') == true) {
  35. abort_if(Helpers::validateLocalUrl($tag), 404);
  36. $remote = Helpers::fetchFromUrl($tag);
  37. if(isset($remote['type']) && in_array($remote['type'], ['Note', 'Person']) == true) {
  38. $type = $remote['type'];
  39. if($type == 'Person') {
  40. $item = Helpers::profileFirstOrNew($tag);
  41. $tokens['profiles'] = [[
  42. 'count' => 1,
  43. 'url' => $item->url(),
  44. 'type' => 'profile',
  45. 'value' => $item->username,
  46. 'tokens' => [$item->username],
  47. 'name' => $item->name,
  48. 'entity' => [
  49. 'id' => (string) $item->id,
  50. 'following' => $item->followedBy(Auth::user()->profile),
  51. 'follow_request' => $item->hasFollowRequestById(Auth::user()->profile_id),
  52. 'thumb' => $item->avatarUrl()
  53. ]
  54. ]];
  55. } else if ($type == 'Note') {
  56. $item = Helpers::statusFetch($tag);
  57. $tokens['posts'] = [[
  58. 'count' => 0,
  59. 'url' => $item->url(),
  60. 'type' => 'status',
  61. 'value' => "by {$item->profile->username} <span class='float-right'>{$item->created_at->diffForHumans(null, true, true)}</span>",
  62. 'tokens' => [$item->caption],
  63. 'name' => $item->caption,
  64. 'thumb' => $item->thumb(),
  65. ]];
  66. }
  67. }
  68. }
  69. $htag = Str::startsWith($tag, '#') == true ? mb_substr($tag, 1) : $tag;
  70. $hashtags = Hashtag::select('id', 'name', 'slug')
  71. ->where('slug', 'like', '%'.$htag.'%')
  72. ->whereHas('posts')
  73. ->limit(20)
  74. ->get();
  75. if($hashtags->count() > 0) {
  76. $tags = $hashtags->map(function ($item, $key) {
  77. return [
  78. 'count' => $item->posts()->count(),
  79. 'url' => $item->url(),
  80. 'type' => 'hashtag',
  81. 'value' => $item->name,
  82. 'tokens' => '',
  83. 'name' => null,
  84. ];
  85. });
  86. $tokens['hashtags'] = $tags;
  87. }
  88. return $tokens;
  89. });
  90. $users = Profile::select('username', 'name', 'id')
  91. ->whereNull('status')
  92. ->whereNull('domain')
  93. ->where('id', '!=', Auth::user()->profile->id)
  94. ->where('username', 'like', '%'.$tag.'%')
  95. //->orWhere('remote_url', $tag)
  96. ->limit(20)
  97. ->get();
  98. if($users->count() > 0) {
  99. $profiles = $users->map(function ($item, $key) {
  100. return [
  101. 'count' => 0,
  102. 'url' => $item->url(),
  103. 'type' => 'profile',
  104. 'value' => $item->username,
  105. 'tokens' => [$item->username],
  106. 'name' => $item->name,
  107. 'avatar' => $item->avatarUrl(),
  108. 'id' => $item->id,
  109. 'entity' => [
  110. 'id' => $item->id,
  111. 'following' => $item->followedBy(Auth::user()->profile),
  112. 'thumb' => $item->avatarUrl()
  113. ]
  114. ];
  115. });
  116. if(isset($tokens['profiles'])) {
  117. array_push($tokens['profiles'], $profiles);
  118. } else {
  119. $tokens['profiles'] = $profiles;
  120. }
  121. }
  122. $posts = Status::select('id', 'profile_id', 'caption', 'created_at')
  123. ->whereHas('media')
  124. ->whereNull('in_reply_to_id')
  125. ->whereNull('reblog_of_id')
  126. ->whereProfileId(Auth::user()->profile->id)
  127. ->where('caption', 'like', '%'.$tag.'%')
  128. ->latest()
  129. ->limit(10)
  130. ->get();
  131. if($posts->count() > 0) {
  132. $posts = $posts->map(function($item, $key) {
  133. return [
  134. 'count' => 0,
  135. 'url' => $item->url(),
  136. 'type' => 'status',
  137. 'value' => "by {$item->profile->username} <span class='float-right'>{$item->created_at->diffForHumans(null, true, true)}</span>",
  138. 'tokens' => [$item->caption],
  139. 'name' => $item->caption,
  140. 'thumb' => $item->thumb(),
  141. 'filter' => $item->firstMedia()->filter_class
  142. ];
  143. });
  144. $tokens['posts'] = $posts;
  145. }
  146. return response()->json($tokens);
  147. }
  148. public function results(Request $request)
  149. {
  150. $this->validate($request, [
  151. 'q' => 'required|string|min:1',
  152. ]);
  153. return view('search.results');
  154. }
  155. }