SearchResults.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <template>
  2. <div class="container">
  3. <div v-if="loading" class="pt-5 text-center">
  4. <div class="spinner-border" role="status">
  5. <span class="sr-only">Loading…</span>
  6. </div>
  7. </div>
  8. <div v-if="networkError" class="pt-5 text-center">
  9. <p class="lead font-weight-lighter">An error occured, results could not be loaded.<br> Please try again later.</p>
  10. </div>
  11. <div v-if="!loading && !networkError" class="mt-5">
  12. <div v-if="analysis == 'all'" class="row">
  13. <div class="col-12 mb-5">
  14. <p class="h5 font-weight-bold text-dark">Showing results for <i>{{query}}</i></p>
  15. <hr>
  16. </div>
  17. <div class="col-md-3">
  18. <div class="mb-4">
  19. <p class="text-secondary small font-weight-bold">HASHTAGS <span class="pl-1 text-lighter">({{results.hashtags.length}})</span></p>
  20. </div>
  21. <div v-if="results.hashtags.length">
  22. <a v-for="(hashtag, index) in results.hashtags" class="mb-2 result-card" :href="buildUrl('hashtag', hashtag)">
  23. <div class="pb-3">
  24. <div class="media align-items-center py-2 pr-3">
  25. <span class="d-inline-flex align-items-center justify-content-center border rounded-circle mr-3" style="width: 50px;height: 50px;">
  26. <i class="fas fa-hashtag text-muted"></i>
  27. </span>
  28. <div class="media-body text-truncate">
  29. <p class="mb-0 text-truncate text-dark font-weight-bold" data-toggle="tooltip" :title="hashtag.value">
  30. #{{hashtag.value}}
  31. </p>
  32. <p v-if="hashtag.count > 2" class="mb-0 small font-weight-bold text-muted text-uppercase">
  33. {{hashtag.count}} posts
  34. </p>
  35. </div>
  36. </div>
  37. </div>
  38. </a>
  39. </div>
  40. <div v-else>
  41. <div class="border py-3 text-center font-weight-bold">No results found</div>
  42. </div>
  43. </div>
  44. <div class="col-md-5">
  45. <div class="mb-4">
  46. <p class="text-secondary small font-weight-bold">PROFILES <span class="pl-1 text-lighter">({{results.profiles.length}})</span></p>
  47. </div>
  48. <div v-if="results.profiles.length">
  49. <a v-for="(profile, index) in results.profiles" class="mb-2 result-card" :href="buildUrl('profile', profile)">
  50. <div class="pb-3">
  51. <div class="media align-items-center py-2 pr-3">
  52. <img class="mr-3 rounded-circle border" :src="profile.avatar" width="50px" height="50px">
  53. <div class="media-body">
  54. <p class="mb-0 text-truncate text-dark font-weight-bold" data-toggle="tooltip" :title="profile.value">
  55. {{profile.value}}
  56. </p>
  57. <p class="mb-0 small font-weight-bold text-muted text-uppercase">
  58. {{profile.entity.post_count}} Posts
  59. </p>
  60. </div>
  61. <div class="ml-3">
  62. <a v-if="profile.entity.following" class="btn btn-primary btn-sm font-weight-bold text-uppercase py-0" :href="buildUrl('profile', profile)">Following</a>
  63. <a v-else class="btn btn-outline-primary btn-sm font-weight-bold text-uppercase py-0" :href="buildUrl('profile', profile)">View</a>
  64. </div>
  65. </div>
  66. </div>
  67. </a>
  68. </div>
  69. <div v-else>
  70. <div class="border py-3 text-center font-weight-bold">No results found</div>
  71. </div>
  72. </div>
  73. <div class="col-md-4">
  74. <div class="mb-4">
  75. <p class="text-secondary small font-weight-bold">STATUSES <span class="pl-1 text-lighter">({{results.statuses.length}})</span></p>
  76. </div>
  77. <div v-if="results.statuses.length">
  78. <a v-for="(status, index) in results.statuses" class="mr-2 result-card" :href="buildUrl('status', status)">
  79. <img :src="status.thumb" width="90px" height="90px" class="mb-2">
  80. </a>
  81. </div>
  82. <div v-else>
  83. <div class="border py-3 text-center font-weight-bold">No results found</div>
  84. </div>
  85. </div>
  86. </div>
  87. <div v-else-if="analysis == 'hashtag'" class="row">
  88. <div class="col-12 mb-5">
  89. <p class="h5 font-weight-bold text-dark">Showing results for <i>{{query}}</i></p>
  90. <hr>
  91. </div>
  92. <div class="col-md-6 offset-md-3">
  93. <div class="mb-4">
  94. <p class="text-secondary small font-weight-bold">HASHTAGS <span class="pl-1 text-lighter">({{results.hashtags.length}})</span></p>
  95. </div>
  96. <div v-if="results.hashtags.length">
  97. <a v-for="(hashtag, index) in results.hashtags" class="mb-2 result-card" :href="buildUrl('hashtag', hashtag)">
  98. <div class="pb-3">
  99. <div class="media align-items-center py-2 pr-3">
  100. <span class="d-inline-flex align-items-center justify-content-center border rounded-circle mr-3" style="width: 50px;height: 50px;">
  101. <i class="fas fa-hashtag text-muted"></i>
  102. </span>
  103. <div class="media-body">
  104. <p class="mb-0 text-truncate text-dark font-weight-bold" data-toggle="tooltip" :title="hashtag.value">
  105. #{{hashtag.value}}
  106. </p>
  107. <p v-if="hashtag.count > 2" class="mb-0 small font-weight-bold text-muted text-uppercase">
  108. {{hashtag.count}} posts
  109. </p>
  110. </div>
  111. </div>
  112. </div>
  113. </a>
  114. </div>
  115. <div v-else>
  116. <div class="border py-3 text-center font-weight-bold">No results found</div>
  117. </div>
  118. </div>
  119. </div>
  120. <div v-else-if="analysis == 'profile'" class="row">
  121. <div class="col-12 mb-5">
  122. <p class="h5 font-weight-bold text-dark">Showing results for <i>{{query}}</i></p>
  123. <hr>
  124. </div>
  125. <div class="col-md-6 offset-md-3">
  126. <div class="mb-4">
  127. <p class="text-secondary small font-weight-bold">PROFILES <span class="pl-1 text-lighter">({{results.profiles.length}})</span></p>
  128. </div>
  129. <div v-if="results.profiles.length">
  130. <div v-for="(profile, index) in results.profiles" class="card mb-4">
  131. <div class="card-header p-0 m-0">
  132. <div style="width: 100%;height: 140px;background: #0070b7"></div>
  133. </div>
  134. <div class="card-body">
  135. <div class="text-center mt-n5 mb-4">
  136. <img class="rounded-circle p-1 border mt-n4 bg-white shadow" :src="profile.entity.thumb" width="90px" height="90px;" onerror="this.onerror=null;this.src='/storage/avatars/default.png';">
  137. </div>
  138. <p class="text-center lead font-weight-bold mb-1">{{profile.value}}</p>
  139. <p class="text-center text-muted small text-uppercase mb-4"><!-- 2 followers --></p>
  140. <div class="d-flex justify-content-center">
  141. <button v-if="profile.entity.following" type="button" class="btn btn-outline-secondary btn-sm py-1 px-4 text-uppercase font-weight-bold mr-3" style="font-weight: 500">Following</button>
  142. <a class="btn btn-primary btn-sm py-1 px-4 text-uppercase font-weight-bold" :href="buildUrl('profile',profile)" style="font-weight: 500">View Profile</a>
  143. </div>
  144. </div>
  145. </div>
  146. </div>
  147. <div v-else>
  148. <div class="border py-3 text-center font-weight-bold">No results found</div>
  149. </div>
  150. </div>
  151. </div>
  152. <div v-else-if="analysis == 'webfinger'" class="row">
  153. <div class="col-12 mb-5">
  154. <p class="h5 font-weight-bold text-dark">Showing results for <i>{{query}}</i></p>
  155. <hr>
  156. <div class="col-md-6 offset-md-3">
  157. <div v-for="(profile, index) in results.profiles" class="card mb-2">
  158. <div class="card-header p-0 m-0">
  159. <div style="width: 100%;height: 140px;background: #0070b7"></div>
  160. </div>
  161. <div class="card-body">
  162. <div class="text-center mt-n5 mb-4">
  163. <img class="rounded-circle p-1 border mt-n4 bg-white shadow" :src="profile.entity.thumb" width="90px" height="90px;" onerror="this.onerror=null;this.src='/storage/avatars/default.png';">
  164. </div>
  165. <p class="text-center lead font-weight-bold mb-1">{{profile.value}}</p>
  166. <p class="text-center text-muted small text-uppercase mb-4"><!-- 2 followers --></p>
  167. <div class="d-flex justify-content-center">
  168. <!-- <button v-if="profile.entity.following" type="button" class="btn btn-outline-secondary btn-sm py-1 px-4 text-uppercase font-weight-bold mr-3" style="font-weight: 500">Unfollow</button> -->
  169. <!-- <button v-else type="button" class="btn btn-primary btn-sm py-1 px-4 text-uppercase font-weight-bold mr-3" style="font-weight: 500">Follow</button> -->
  170. <a class="btn btn-primary btn-sm py-1 px-4 text-uppercase font-weight-bold" :href="'/i/web/profile/_/' + profile.entity.id" style="font-weight: 500">View Profile</a>
  171. </div>
  172. </div>
  173. </div>
  174. </div>
  175. </div>
  176. </div>
  177. <div v-else class="col-12">
  178. <p class="text-center text-muted lead font-weight-bold">No results found</p>
  179. </div>
  180. </div>
  181. </div>
  182. </template>
  183. <style type="text/css" scoped>
  184. .result-card {
  185. text-decoration: none;
  186. }
  187. .result-card .media:hover {
  188. background: #EDF2F7;
  189. }
  190. @media (min-width: 1200px) {
  191. .container {
  192. max-width: 995px;
  193. }
  194. }
  195. </style>
  196. <script type="text/javascript">
  197. export default {
  198. props: ['query', 'profileId'],
  199. data() {
  200. return {
  201. loading: true,
  202. networkError: false,
  203. results: {
  204. hashtags: [],
  205. profiles: [],
  206. statuses: []
  207. },
  208. filters: {
  209. hashtags: true,
  210. profiles: true,
  211. statuses: true
  212. },
  213. analysis: 'profile',
  214. }
  215. },
  216. beforeMount() {
  217. this.bootSearch();
  218. },
  219. mounted() {
  220. $('.search-bar input').val(this.query);
  221. },
  222. methods: {
  223. bootSearch() {
  224. let lexer = this.searchLexer();
  225. this.analysis = lexer;
  226. this.fetchSearchResults();
  227. },
  228. fetchSearchResults() {
  229. this.searchContext(this.analysis);
  230. },
  231. followProfile(profile, index) {
  232. this.loading = true;
  233. axios.post('/i/follow', {
  234. item: profile.entity.id
  235. }).then(res => {
  236. if(profile.entity.local == true) {
  237. this.fetchSearchResults();
  238. return;
  239. } else {
  240. this.loading = false;
  241. this.results.profiles[index].entity.follow_request = true;
  242. return;
  243. }
  244. }).catch(err => {
  245. if(err.response.data.message) {
  246. swal('Error', err.response.data.message, 'error');
  247. }
  248. });
  249. },
  250. searchLexer() {
  251. let q = this.query;
  252. if(q.startsWith('#')) {
  253. return 'hashtag';
  254. }
  255. if((q.match(/@/g) || []).length == 2) {
  256. return 'webfinger';
  257. }
  258. if(q.startsWith('@') || q.search('@') != -1) {
  259. return 'profile';
  260. }
  261. if(q.startsWith('https://')) {
  262. return 'remote';
  263. }
  264. return 'all';
  265. },
  266. buildUrl(type = 'hashtag', obj) {
  267. switch(type) {
  268. case 'hashtag':
  269. return obj.url + '?src=search';
  270. break;
  271. case 'profile':
  272. if(obj.entity.local == true) {
  273. return obj.url;
  274. }
  275. return '/i/web/profile/_/' + obj.entity.id;
  276. break;
  277. default:
  278. return obj.url + '?src=search';
  279. break;
  280. }
  281. },
  282. searchContext(type) {
  283. switch(type) {
  284. case 'all':
  285. axios.get('/api/search', {
  286. params: {
  287. 'q': this.query,
  288. 'src': 'metro',
  289. 'v': 1,
  290. 'scope': 'all'
  291. }
  292. }).then(res => {
  293. let results = res.data;
  294. this.results.hashtags = results.hashtags ? results.hashtags : [];
  295. this.results.profiles = results.profiles ? results.profiles : [];
  296. this.results.statuses = results.posts ? results.posts : [];
  297. this.loading = false;
  298. }).catch(err => {
  299. this.loading = false;
  300. console.log(err);
  301. this.networkError = true;
  302. });
  303. break;
  304. case 'hashtag':
  305. axios.get('/api/search', {
  306. params: {
  307. 'q': this.query.slice(1),
  308. 'src': 'metro',
  309. 'v': 1,
  310. 'scope': 'hashtag'
  311. }
  312. }).then(res => {
  313. let results = res.data;
  314. this.results.hashtags = results.hashtags ? results.hashtags : [];
  315. this.results.profiles = results.profiles ? results.profiles : [];
  316. this.results.statuses = results.posts ? results.posts : [];
  317. this.loading = false;
  318. }).catch(err => {
  319. this.loading = false;
  320. console.log(err);
  321. this.networkError = true;
  322. });
  323. break;
  324. case 'profile':
  325. axios.get('/api/search', {
  326. params: {
  327. 'q': this.query,
  328. 'src': 'metro',
  329. 'v': 1,
  330. 'scope': 'profile'
  331. }
  332. }).then(res => {
  333. let results = res.data;
  334. this.results.hashtags = results.hashtags ? results.hashtags : [];
  335. this.results.profiles = results.profiles ? results.profiles : [];
  336. this.results.statuses = results.posts ? results.posts : [];
  337. this.loading = false;
  338. }).catch(err => {
  339. this.loading = false;
  340. console.log(err);
  341. this.networkError = true;
  342. });
  343. break;
  344. case 'webfinger':
  345. axios.get('/api/search', {
  346. params: {
  347. 'q': this.query,
  348. 'src': 'metro',
  349. 'v': 1,
  350. 'scope': 'webfinger'
  351. }
  352. }).then(res => {
  353. let results = res.data;
  354. this.results.hashtags = [];
  355. this.results.profiles = results.profiles;
  356. this.results.statuses = [];
  357. this.loading = false;
  358. }).catch(err => {
  359. this.loading = false;
  360. console.log(err);
  361. this.networkError = true;
  362. });
  363. break;
  364. default:
  365. this.loading = false;
  366. this.networkError = true;
  367. break;
  368. }
  369. }
  370. }
  371. }
  372. </script>