SearchController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. class SearchController extends Controller
  11. {
  12. public function __construct()
  13. {
  14. $this->middleware('auth');
  15. }
  16. public function searchAPI(Request $request, $tag)
  17. {
  18. if(mb_strlen($tag) < 3) {
  19. return;
  20. }
  21. $hash = hash('sha256', $tag);
  22. $tokens = Cache::remember('api:search:tag:'.$hash, 5, function () use ($tag) {
  23. $tokens = collect([]);
  24. if(Helpers::validateUrl($tag)) {
  25. $remote = Helpers::fetchFromUrl($tag);
  26. if(isset($remote['type']) && in_array($remote['type'], ['Create', 'Person']) == true) {
  27. $type = $remote['type'];
  28. if($type == 'Person') {
  29. $item = Helpers::profileFirstOrNew($tag);
  30. $tokens->push([[
  31. 'count' => 1,
  32. 'url' => $item->url(),
  33. 'type' => 'profile',
  34. 'value' => $item->username,
  35. 'tokens' => [$item->username],
  36. 'name' => $item->name,
  37. ]]);
  38. } else if ($type == 'Create') {
  39. $item = Helpers::statusFirstOrFetch($tag, false);
  40. $tokens->push([[
  41. 'count' => 0,
  42. 'url' => $item->url(),
  43. 'type' => 'status',
  44. 'value' => "by {$item->profile->username} <span class='float-right'>{$item->created_at->diffForHumans(null, true, true)}</span>",
  45. 'tokens' => [$item->caption],
  46. 'name' => $item->caption,
  47. 'thumb' => $item->thumb(),
  48. ]]);
  49. }
  50. }
  51. }
  52. $hashtags = Hashtag::select('id', 'name', 'slug')->where('slug', 'like', '%'.$tag.'%')->limit(20)->get();
  53. if($hashtags->count() > 0) {
  54. $tags = $hashtags->map(function ($item, $key) {
  55. return [
  56. 'count' => $item->posts()->count(),
  57. 'url' => $item->url(),
  58. 'type' => 'hashtag',
  59. 'value' => $item->name,
  60. 'tokens' => explode('-', $item->name),
  61. 'name' => null,
  62. ];
  63. });
  64. $tokens->push($tags);
  65. }
  66. $users = Profile::select('username', 'name', 'id')
  67. ->whereNull('status')
  68. ->whereNull('domain')
  69. ->where('username', 'like', '%'.$tag.'%')
  70. //->orWhere('remote_url', $tag)
  71. ->limit(20)
  72. ->get();
  73. if($users->count() > 0) {
  74. $profiles = $users->map(function ($item, $key) {
  75. return [
  76. 'count' => 0,
  77. 'url' => $item->url(),
  78. 'type' => 'profile',
  79. 'value' => $item->username,
  80. 'tokens' => [$item->username],
  81. 'name' => $item->name,
  82. ];
  83. });
  84. $tokens->push($profiles);
  85. }
  86. return $tokens;
  87. });
  88. $posts = Status::select('id', 'profile_id', 'caption', 'created_at')
  89. ->whereHas('media')
  90. ->whereNull('in_reply_to_id')
  91. ->whereNull('reblog_of_id')
  92. ->whereProfileId(Auth::user()->profile->id)
  93. ->where('caption', 'like', '%'.$tag.'%')
  94. ->orWhere('uri', $tag)
  95. ->orderBy('created_at', 'desc')
  96. ->get();
  97. if($posts->count() > 0) {
  98. $posts = $posts->map(function($item, $key) {
  99. return [
  100. 'count' => 0,
  101. 'url' => $item->url(),
  102. 'type' => 'status',
  103. 'value' => "by {$item->profile->username} <span class='float-right'>{$item->created_at->diffForHumans(null, true, true)}</span>",
  104. 'tokens' => [$item->caption],
  105. 'name' => $item->caption,
  106. 'thumb' => $item->thumb(),
  107. ];
  108. });
  109. $tokens = $tokens->push($posts);
  110. }
  111. if($tokens->count() > 0) {
  112. $tokens = $tokens[0];
  113. }
  114. return response()->json($tokens);
  115. }
  116. public function results(Request $request)
  117. {
  118. $this->validate($request, [
  119. 'q' => 'required|string|min:1',
  120. ]);
  121. return view('search.results');
  122. }
  123. }