SearchApiV2Service.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. namespace App\Services;
  3. use Cache;
  4. use Illuminate\Support\Facades\Redis;
  5. use App\{Hashtag, Profile, Status};
  6. use App\Transformer\Api\AccountTransformer;
  7. use App\Transformer\Api\StatusTransformer;
  8. use League\Fractal;
  9. use League\Fractal\Serializer\ArraySerializer;
  10. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  11. use App\Util\ActivityPub\Helpers;
  12. use Illuminate\Support\Str;
  13. use App\Services\AccountService;
  14. use App\Services\HashtagService;
  15. use App\Services\StatusService;
  16. class SearchApiV2Service
  17. {
  18. private $query;
  19. public static function query($query)
  20. {
  21. return (new self)->run($query);
  22. }
  23. protected function run($query)
  24. {
  25. $this->query = $query;
  26. $q = urldecode($query->input('q'));
  27. if($query->has('resolve') &&
  28. $query->resolve == true &&
  29. ( Str::startsWith($q, 'https://') ||
  30. Str::substrCount($q, '@') == 2)
  31. ) {
  32. return $this->resolveQuery();
  33. }
  34. if($query->has('type')) {
  35. switch ($query->input('type')) {
  36. case 'accounts':
  37. return [
  38. 'accounts' => $this->accounts(),
  39. 'hashtags' => [],
  40. 'statuses' => []
  41. ];
  42. break;
  43. case 'hashtags':
  44. return [
  45. 'accounts' => [],
  46. 'hashtags' => $this->hashtags(),
  47. 'statuses' => []
  48. ];
  49. break;
  50. case 'statuses':
  51. return [
  52. 'accounts' => [],
  53. 'hashtags' => [],
  54. 'statuses' => $this->statuses()
  55. ];
  56. break;
  57. }
  58. }
  59. if($query->has('account_id')) {
  60. return [
  61. 'accounts' => [],
  62. 'hashtags' => [],
  63. 'statuses' => $this->statusesById()
  64. ];
  65. }
  66. return [
  67. 'accounts' => $this->accounts(),
  68. 'hashtags' => $this->hashtags(),
  69. 'statuses' => $this->statuses()
  70. ];
  71. }
  72. protected function accounts()
  73. {
  74. $user = request()->user();
  75. $limit = $this->query->input('limit') ?? 20;
  76. $offset = $this->query->input('offset') ?? 0;
  77. $query = '%' . $this->query->input('q') . '%';
  78. $banned = InstanceService::getBannedDomains();
  79. $results = Profile::select('profiles.*', 'followers.profile_id', 'followers.created_at')
  80. ->whereNull('status')
  81. ->leftJoin('followers', function($join) use($user) {
  82. return $join->on('profiles.id', '=', 'followers.following_id')
  83. ->where('followers.profile_id', $user->profile_id);
  84. })
  85. ->where('username', 'like', $query)
  86. ->orderByDesc('profiles.followers_count')
  87. ->orderByDesc('followers.created_at')
  88. ->offset($offset)
  89. ->limit($limit)
  90. ->get()
  91. ->filter(function($profile) use ($banned) {
  92. return in_array($profile->domain, $banned) == false;
  93. })
  94. ->map(function($res) {
  95. return AccountService::get($res['id']);
  96. })
  97. ->filter(function($account) {
  98. return $account && isset($account['id']);
  99. })
  100. ->values();
  101. return $results;
  102. }
  103. protected function hashtags()
  104. {
  105. $limit = $this->query->input('limit') ?? 20;
  106. $offset = $this->query->input('offset') ?? 0;
  107. $query = '%' . $this->query->input('q') . '%';
  108. return Hashtag::whereIsBanned(false)
  109. ->where('name', 'like', $query)
  110. ->offset($offset)
  111. ->limit($limit)
  112. ->get()
  113. ->map(function($tag) {
  114. return [
  115. 'name' => $tag->name,
  116. 'url' => $tag->url(),
  117. 'count' => HashtagService::count($tag->id),
  118. 'history' => []
  119. ];
  120. });
  121. }
  122. protected function statuses()
  123. {
  124. // Removed until we provide more relevent sorting/results
  125. return [];
  126. }
  127. protected function statusesById()
  128. {
  129. $accountId = $this->query->input('account_id');
  130. $limit = $this->query->input('limit', 20);
  131. $query = '%' . $this->query->input('q') . '%';
  132. $results = Status::where('caption', 'like', $query)
  133. ->whereProfileId($accountId)
  134. ->limit($limit)
  135. ->get()
  136. ->map(function($status) {
  137. return StatusService::get($status->id);
  138. })
  139. ->filter(function($status) {
  140. return $status && isset($status['account']);
  141. });
  142. return $results;
  143. }
  144. protected function resolveQuery()
  145. {
  146. $query = urldecode($this->query->input('q'));
  147. if(Helpers::validateLocalUrl($query)) {
  148. if(Str::contains($query, '/p/')) {
  149. return $this->resolveLocalStatus();
  150. } else {
  151. return $this->resolveLocalProfile();
  152. }
  153. } else {
  154. $default = [
  155. 'accounts' => [],
  156. 'hashtags' => [],
  157. 'statuses' => [],
  158. ];
  159. if(!Helpers::validateUrl($query) && strpos($query, '@') == -1) {
  160. return $default;
  161. }
  162. if(Str::substrCount($query, '@') == 2) {
  163. try {
  164. $res = WebfingerService::lookup($query);
  165. } catch (\Exception $e) {
  166. return $default;
  167. }
  168. if($res && isset($res['id'])) {
  169. $default['accounts'][] = $res;
  170. return $default;
  171. } else {
  172. return $default;
  173. }
  174. }
  175. try {
  176. $res = ActivityPubFetchService::get($query);
  177. $banned = InstanceService::getBannedDomains();
  178. if($res) {
  179. $json = json_decode($res, true);
  180. if(!$json || !isset($json['@context']) || !isset($json['type']) || !in_array($json['type'], ['Note', 'Person'])) {
  181. return [
  182. 'accounts' => [],
  183. 'hashtags' => [],
  184. 'statuses' => [],
  185. ];
  186. }
  187. switch($json['type']) {
  188. case 'Note':
  189. $obj = Helpers::statusFetch($query);
  190. if(!$obj || !isset($obj['id'])) {
  191. return $default;
  192. }
  193. $note = StatusService::get($obj['id']);
  194. if(!$note) {
  195. return $default;
  196. }
  197. $default['statuses'][] = $note;
  198. return $default;
  199. break;
  200. case 'Person':
  201. $obj = Helpers::profileFetch($query);
  202. if(!$obj) {
  203. return $default;
  204. }
  205. if(in_array($obj['domain'], $banned)) {
  206. return $default;
  207. }
  208. $default['accounts'][] = AccountService::get($obj['id']);
  209. return $default;
  210. break;
  211. default:
  212. return [
  213. 'accounts' => [],
  214. 'hashtags' => [],
  215. 'statuses' => [],
  216. ];
  217. break;
  218. }
  219. }
  220. } catch (\Exception $e) {
  221. return [
  222. 'accounts' => [],
  223. 'hashtags' => [],
  224. 'statuses' => [],
  225. ];
  226. }
  227. return $default;
  228. }
  229. }
  230. protected function resolveLocalStatus()
  231. {
  232. $query = urldecode($this->query->input('q'));
  233. $query = last(explode('/', $query));
  234. $status = Status::whereNull('uri')
  235. ->whereScope('public')
  236. ->find($query);
  237. if(!$status) {
  238. return [
  239. 'accounts' => [],
  240. 'hashtags' => [],
  241. 'statuses' => []
  242. ];
  243. }
  244. $fractal = new Fractal\Manager();
  245. $fractal->setSerializer(new ArraySerializer());
  246. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  247. return [
  248. 'accounts' => [],
  249. 'hashtags' => [],
  250. 'statuses' => $fractal->createData($resource)->toArray()
  251. ];
  252. }
  253. protected function resolveLocalProfile()
  254. {
  255. $query = urldecode($this->query->input('q'));
  256. $query = last(explode('/', $query));
  257. $profile = Profile::whereNull('status')
  258. ->whereNull('domain')
  259. ->whereUsername($query)
  260. ->first();
  261. if(!$profile) {
  262. return [
  263. 'accounts' => [],
  264. 'hashtags' => [],
  265. 'statuses' => []
  266. ];
  267. }
  268. $fractal = new Fractal\Manager();
  269. $fractal->setSerializer(new ArraySerializer());
  270. $resource = new Fractal\Resource\Item($profile, new AccountTransformer());
  271. return [
  272. 'accounts' => $fractal->createData($resource)->toArray(),
  273. 'hashtags' => [],
  274. 'statuses' => []
  275. ];
  276. }
  277. }