SearchController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. ->where('username', 'like', '%'.$tag.'%')
  69. ->orWhere('remote_url', $tag)
  70. ->limit(20)
  71. ->get();
  72. if($users->count() > 0) {
  73. $profiles = $users->map(function ($item, $key) {
  74. return [
  75. 'count' => 0,
  76. 'url' => $item->url(),
  77. 'type' => 'profile',
  78. 'value' => $item->username,
  79. 'tokens' => [$item->username],
  80. 'name' => $item->name,
  81. ];
  82. });
  83. $tokens->push($profiles);
  84. }
  85. return $tokens;
  86. });
  87. $posts = Status::select('id', 'profile_id', 'caption', 'created_at')
  88. ->whereHas('media')
  89. ->whereNull('in_reply_to_id')
  90. ->whereNull('reblog_of_id')
  91. ->whereProfileId(Auth::user()->profile->id)
  92. ->where('caption', 'like', '%'.$tag.'%')
  93. ->orWhere('uri', $tag)
  94. ->orderBy('created_at', 'desc')
  95. ->get();
  96. if($posts->count() > 0) {
  97. $posts = $posts->map(function($item, $key) {
  98. return [
  99. 'count' => 0,
  100. 'url' => $item->url(),
  101. 'type' => 'status',
  102. 'value' => "by {$item->profile->username} <span class='float-right'>{$item->created_at->diffForHumans(null, true, true)}</span>",
  103. 'tokens' => [$item->caption],
  104. 'name' => $item->caption,
  105. 'thumb' => $item->thumb(),
  106. ];
  107. });
  108. $tokens = $tokens->push($posts);
  109. }
  110. if($tokens->count() > 0) {
  111. $tokens = $tokens[0];
  112. }
  113. return response()->json($tokens);
  114. }
  115. }