SearchResults.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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-if="analysis == 'remote'" class="row">
  178. <div class="col-12 mb-5">
  179. <p class="h5 font-weight-bold text-dark">Showing results for <i>{{query}}</i></p>
  180. <hr>
  181. </div>
  182. <div v-if="results.profiles.length" class="col-md-6 offset-3">
  183. <a v-for="(profile, index) in results.profiles" class="mb-2 result-card" :href="buildUrl('profile', profile)">
  184. <div class="pb-3">
  185. <div class="media align-items-center py-2 pr-3">
  186. <img class="mr-3 rounded-circle border" :src="profile.entity.thumb" width="50px" height="50px" onerror="this.onerror=null;this.src='/storage/avatars/default.png';">
  187. <div class="media-body">
  188. <p class="mb-0 text-truncate text-dark font-weight-bold" data-toggle="tooltip" :title="profile.value">
  189. {{profile.value}}
  190. </p>
  191. <p class="mb-0 small font-weight-bold text-muted text-uppercase">
  192. {{profile.entity.post_count}} Posts
  193. </p>
  194. </div>
  195. <div class="ml-3">
  196. <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>
  197. <a v-else class="btn btn-outline-primary btn-sm font-weight-bold text-uppercase py-0" :href="buildUrl('profile', profile)">View</a>
  198. </div>
  199. </div>
  200. </div>
  201. </a>
  202. </div>
  203. <div v-if="results.statuses.length" class="col-md-6 offset-3">
  204. <a v-for="(status, index) in results.statuses" class="mr-2 result-card" :href="buildUrl('status', status)">
  205. <img :src="status.thumb" width="90px" height="90px" class="mb-2" onerror="this.onerror=null;this.src='/storage/no-preview.png';">
  206. </a>
  207. </div>
  208. </div>
  209. <div v-else-if="analysis == 'remotePost'" class="row">
  210. <div class="col-12 mb-5">
  211. <p class="h5 font-weight-bold text-dark">Showing results for <i>{{query}}</i></p>
  212. <hr>
  213. </div>
  214. <div class="col-md-6 offset-md-3">
  215. <div v-if="results.statuses.length">
  216. <div v-for="(status, index) in results.statuses" class="card mb-4 shadow-none border">
  217. <div class="card-header p-0 m-0">
  218. <div style="width: 100%;height: 200px;background: #fff">
  219. <div class="pt-4 text-center">
  220. <img :src="status.thumb" class="img-fluid border" style="max-height: 140px;">
  221. </div>
  222. </div>
  223. </div>
  224. <div class="card-body">
  225. <div class="mt-n4 mb-2">
  226. <div class="media">
  227. <img class="rounded-circle p-1 mr-2 border mt-n3 bg-white shadow" src="/storage/avatars/default.png" width="70px" height="70px;" onerror="this.onerror=null;this.src='/storage/avatars/default.png';">
  228. <div class="media-body pt-3">
  229. <p class="font-weight-bold mb-0">{{status.username}}</p>
  230. </div>
  231. <div class="float-right pt-3">
  232. <p class="small mb-0 text-muted">{{status.timestamp}}</p>
  233. </div>
  234. </div>
  235. </div>
  236. <p class="text-center mb-3 lead" v-html="status.caption"></p>
  237. <!-- <p class="text-center text-muted small text-uppercase mb-4">2 likes</p> -->
  238. <!-- <div class="d-flex justify-content-center">
  239. <a class="btn btn-primary btn-sm py-1 px-4 text-uppercase font-weight-bold" :href="status.url" style="font-weight: 500">View Post</a>
  240. </div> -->
  241. </div>
  242. <div class="card-footer">
  243. <a class="btn btn-primary btn-block font-weight-bold rounded-0" :href="status.url">View Post</a>
  244. </div>
  245. </div>
  246. </div>
  247. <div v-else>
  248. <div class="border py-3 text-center font-weight-bold">No results found</div>
  249. </div>
  250. </div>
  251. </div>
  252. <div v-else class="col-12">
  253. <p class="text-center text-muted lead font-weight-bold">No results found</p>
  254. </div>
  255. </div>
  256. </div>
  257. </template>
  258. <style type="text/css" scoped>
  259. .result-card {
  260. text-decoration: none;
  261. }
  262. .result-card .media:hover {
  263. background: #EDF2F7;
  264. }
  265. @media (min-width: 1200px) {
  266. .container {
  267. max-width: 995px;
  268. }
  269. }
  270. </style>
  271. <script type="text/javascript">
  272. export default {
  273. props: ['query', 'profileId'],
  274. data() {
  275. return {
  276. loading: true,
  277. networkError: false,
  278. results: {
  279. hashtags: [],
  280. profiles: [],
  281. statuses: []
  282. },
  283. filters: {
  284. hashtags: true,
  285. profiles: true,
  286. statuses: true
  287. },
  288. analysis: 'profile',
  289. }
  290. },
  291. beforeMount() {
  292. this.bootSearch();
  293. },
  294. mounted() {
  295. $('.search-bar input').val(this.query);
  296. },
  297. methods: {
  298. bootSearch() {
  299. let lexer = this.searchLexer();
  300. this.analysis = lexer;
  301. this.fetchSearchResults();
  302. },
  303. fetchSearchResults() {
  304. if(this.analysis == 'remote') {
  305. let term = this.query;
  306. let parsed = new URL(term);
  307. if(parsed.host === window.location.host) {
  308. window.location.href = term;
  309. return;
  310. }
  311. }
  312. this.searchContext(this.analysis);
  313. },
  314. followProfile(profile, index) {
  315. this.loading = true;
  316. axios.post('/i/follow', {
  317. item: profile.entity.id
  318. }).then(res => {
  319. if(profile.entity.local == true) {
  320. this.fetchSearchResults();
  321. return;
  322. } else {
  323. this.loading = false;
  324. this.results.profiles[index].entity.follow_request = true;
  325. return;
  326. }
  327. }).catch(err => {
  328. if(err.response.data.message) {
  329. swal('Error', err.response.data.message, 'error');
  330. }
  331. });
  332. },
  333. searchLexer() {
  334. let q = this.query;
  335. if(q.startsWith('#')) {
  336. return 'hashtag';
  337. }
  338. if((q.match(/@/g) || []).length == 2) {
  339. return 'webfinger';
  340. }
  341. if(q.startsWith('@')) {
  342. return 'profile';
  343. }
  344. if(q.startsWith('https://')) {
  345. return 'remote';
  346. }
  347. return 'all';
  348. },
  349. buildUrl(type = 'hashtag', obj) {
  350. switch(type) {
  351. case 'hashtag':
  352. return obj.url + '?src=search';
  353. break;
  354. case 'profile':
  355. if(obj.entity.local == true) {
  356. return obj.url;
  357. }
  358. return '/i/web/profile/_/' + obj.entity.id;
  359. break;
  360. default:
  361. return obj.url + '?src=search';
  362. break;
  363. }
  364. },
  365. searchContext(type) {
  366. switch(type) {
  367. case 'all':
  368. axios.get('/api/search', {
  369. params: {
  370. 'q': this.query,
  371. 'src': 'metro',
  372. 'v': 1,
  373. 'scope': 'all'
  374. }
  375. }).then(res => {
  376. let results = res.data;
  377. this.results.hashtags = results.hashtags ? results.hashtags : [];
  378. this.results.profiles = results.profiles ? results.profiles : [];
  379. this.results.statuses = results.posts ? results.posts : [];
  380. this.loading = false;
  381. }).catch(err => {
  382. this.loading = false;
  383. console.log(err);
  384. this.networkError = true;
  385. });
  386. break;
  387. case 'remote':
  388. axios.get('/api/search', {
  389. params: {
  390. 'q': this.query,
  391. 'src': 'metro',
  392. 'v': 1,
  393. 'scope': 'remote'
  394. }
  395. }).then(res => {
  396. let results = res.data;
  397. this.results.hashtags = results.hashtags ? results.hashtags : [];
  398. this.results.profiles = results.profiles ? results.profiles : [];
  399. this.results.statuses = results.posts ? results.posts : [];
  400. if(this.results.profiles.length) {
  401. this.analysis = 'profile';
  402. }
  403. if(this.results.statuses.length) {
  404. this.analysis = 'remotePost';
  405. }
  406. this.loading = false;
  407. }).catch(err => {
  408. this.loading = false;
  409. console.log(err);
  410. this.networkError = true;
  411. });
  412. break;
  413. case 'hashtag':
  414. axios.get('/api/search', {
  415. params: {
  416. 'q': this.query.slice(1),
  417. 'src': 'metro',
  418. 'v': 1,
  419. 'scope': 'hashtag'
  420. }
  421. }).then(res => {
  422. let results = res.data;
  423. this.results.hashtags = results.hashtags ? results.hashtags : [];
  424. this.results.profiles = results.profiles ? results.profiles : [];
  425. this.results.statuses = results.posts ? results.posts : [];
  426. this.loading = false;
  427. }).catch(err => {
  428. this.loading = false;
  429. console.log(err);
  430. this.networkError = true;
  431. });
  432. break;
  433. case 'profile':
  434. axios.get('/api/search', {
  435. params: {
  436. 'q': this.query,
  437. 'src': 'metro',
  438. 'v': 1,
  439. 'scope': 'profile'
  440. }
  441. }).then(res => {
  442. let results = res.data;
  443. this.results.hashtags = results.hashtags ? results.hashtags : [];
  444. this.results.profiles = results.profiles ? results.profiles : [];
  445. this.results.statuses = results.posts ? results.posts : [];
  446. this.loading = false;
  447. }).catch(err => {
  448. this.loading = false;
  449. console.log(err);
  450. this.networkError = true;
  451. });
  452. break;
  453. case 'webfinger':
  454. axios.get('/api/search', {
  455. params: {
  456. 'q': this.query,
  457. 'src': 'metro',
  458. 'v': 1,
  459. 'scope': 'webfinger'
  460. }
  461. }).then(res => {
  462. let results = res.data;
  463. this.results.hashtags = [];
  464. this.results.profiles = results.profiles;
  465. this.results.statuses = [];
  466. this.loading = false;
  467. }).catch(err => {
  468. this.loading = false;
  469. console.log(err);
  470. this.networkError = true;
  471. });
  472. break;
  473. default:
  474. this.loading = false;
  475. this.networkError = true;
  476. break;
  477. }
  478. }
  479. }
  480. }
  481. </script>