SearchController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. $remote = Helpers::fetchFromUrl($tag);
  36. if(isset($remote['type']) && in_array($remote['type'], ['Note', 'Person']) == true) {
  37. $type = $remote['type'];
  38. if($type == 'Person') {
  39. $item = Helpers::profileFirstOrNew($tag);
  40. $tokens['profiles'] = [[
  41. 'count' => 1,
  42. 'url' => $item->url(),
  43. 'type' => 'profile',
  44. 'value' => $item->username,
  45. 'tokens' => [$item->username],
  46. 'name' => $item->name,
  47. 'entity' => [
  48. 'id' => $item->id,
  49. 'following' => $item->followedBy(Auth::user()->profile),
  50. 'thumb' => $item->avatarUrl()
  51. ]
  52. ]];
  53. } else if ($type == 'Note') {
  54. $item = Helpers::statusFirstOrFetch($tag, false);
  55. $tokens['posts'] = [[
  56. 'count' => 0,
  57. 'url' => $item->url(),
  58. 'type' => 'status',
  59. 'value' => "by {$item->profile->username} <span class='float-right'>{$item->created_at->diffForHumans(null, true, true)}</span>",
  60. 'tokens' => [$item->caption],
  61. 'name' => $item->caption,
  62. 'thumb' => $item->thumb(),
  63. ]];
  64. }
  65. }
  66. }
  67. $htag = Str::startsWith($tag, '#') == true ? mb_substr($tag, 1) : $tag;
  68. $hashtags = Hashtag::select('id', 'name', 'slug')
  69. ->where('slug', 'like', '%'.$htag.'%')
  70. ->whereHas('posts')
  71. ->limit(20)
  72. ->get();
  73. if($hashtags->count() > 0) {
  74. $tags = $hashtags->map(function ($item, $key) {
  75. return [
  76. 'count' => $item->posts()->count(),
  77. 'url' => $item->url(),
  78. 'type' => 'hashtag',
  79. 'value' => $item->name,
  80. 'tokens' => '',
  81. 'name' => null,
  82. ];
  83. });
  84. $tokens['hashtags'] = $tags;
  85. }
  86. return $tokens;
  87. });
  88. $users = Profile::select('username', 'name', 'id')
  89. ->whereNull('status')
  90. ->whereNull('domain')
  91. ->where('id', '!=', Auth::user()->profile->id)
  92. ->where('username', 'like', '%'.$tag.'%')
  93. //->orWhere('remote_url', $tag)
  94. ->limit(20)
  95. ->get();
  96. if($users->count() > 0) {
  97. $profiles = $users->map(function ($item, $key) {
  98. return [
  99. 'count' => 0,
  100. 'url' => $item->url(),
  101. 'type' => 'profile',
  102. 'value' => $item->username,
  103. 'tokens' => [$item->username],
  104. 'name' => $item->name,
  105. 'avatar' => $item->avatarUrl(),
  106. 'id' => $item->id,
  107. 'entity' => [
  108. 'id' => $item->id,
  109. 'following' => $item->followedBy(Auth::user()->profile),
  110. 'thumb' => $item->avatarUrl()
  111. ]
  112. ];
  113. });
  114. if(isset($tokens['profiles'])) {
  115. array_push($tokens['profiles'], $profiles);
  116. } else {
  117. $tokens['profiles'] = $profiles;
  118. }
  119. }
  120. $posts = Status::select('id', 'profile_id', 'caption', 'created_at')
  121. ->whereHas('media')
  122. ->whereNull('in_reply_to_id')
  123. ->whereNull('reblog_of_id')
  124. ->whereProfileId(Auth::user()->profile->id)
  125. ->where('caption', 'like', '%'.$tag.'%')
  126. ->latest()
  127. ->limit(10)
  128. ->get();
  129. if($posts->count() > 0) {
  130. $posts = $posts->map(function($item, $key) {
  131. return [
  132. 'count' => 0,
  133. 'url' => $item->url(),
  134. 'type' => 'status',
  135. 'value' => "by {$item->profile->username} <span class='float-right'>{$item->created_at->diffForHumans(null, true, true)}</span>",
  136. 'tokens' => [$item->caption],
  137. 'name' => $item->caption,
  138. 'thumb' => $item->thumb(),
  139. ];
  140. });
  141. $tokens['posts'] = $posts;
  142. }
  143. return response()->json($tokens);
  144. }
  145. public function results(Request $request)
  146. {
  147. $this->validate($request, [
  148. 'q' => 'required|string|min:1',
  149. ]);
  150. return view('search.results');
  151. }
  152. }