1
0

PostComponent.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. <style scoped>
  2. .status-comments,
  3. .reactions,
  4. .col-md-4 {
  5. background: #fff;
  6. }
  7. .postPresenterContainer {
  8. background: #fff;
  9. }
  10. @media(min-width: 720px) {
  11. .postPresenterContainer {
  12. min-height: 600px;
  13. }
  14. }
  15. </style>
  16. <template>
  17. <div class="postComponent d-none">
  18. <div class="container px-0">
  19. <div class="card card-md-rounded-0 status-container orientation-unknown">
  20. <div class="row px-0 mx-0">
  21. <div class="d-flex d-md-none align-items-center justify-content-between card-header bg-white w-100">
  22. <a :href="statusProfileUrl" class="d-flex align-items-center status-username text-truncate" data-toggle="tooltip" data-placement="bottom" :title="statusUsername">
  23. <div class="status-avatar mr-2">
  24. <img :src="statusAvatar" width="24px" height="24px" style="border-radius:12px;">
  25. </div>
  26. <div class="username">
  27. <span class="username-link font-weight-bold text-dark">{{ statusUsername }}</span>
  28. </div>
  29. </a>
  30. <div v-if="user != false" class="float-right">
  31. <div class="post-actions">
  32. <div class="dropdown">
  33. <button class="btn btn-link text-dark no-caret dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" title="Post options">
  34. <span class="fas fa-ellipsis-v text-muted"></span>
  35. </button>
  36. <div class="dropdown-menu dropdown-menu-right" aria-labelledby="dropdownMenuButton">
  37. <div v-if="!owner()">
  38. <a class="dropdown-item font-weight-bold" :href="reportUrl()">Report</a>
  39. <a class="dropdown-item font-weight-bold" v-on:click="muteProfile()">Mute Profile</a>
  40. <a class="dropdown-item font-weight-bold" v-on:click="blockProfile()">Block Profile</a>
  41. </div>
  42. <div v-if="ownerOrAdmin()">
  43. <!-- <a class="dropdown-item font-weight-bold" :href="editUrl()">Disable Comments</a> -->
  44. <a class="dropdown-item font-weight-bold" :href="editUrl()">Edit</a>
  45. <a class="dropdown-item font-weight-bold text-danger" v-on:click="deletePost(status)">Delete</a>
  46. </div>
  47. </div>
  48. </div>
  49. </div>
  50. </div>
  51. </div>
  52. <div class="col-12 col-md-8 px-0 mx-0">
  53. <div class="postPresenterLoader text-center">
  54. <div class="lds-ring"><div></div><div></div><div></div><div></div></div>
  55. </div>
  56. <div class="postPresenterContainer d-none d-flex justify-content-center align-items-center">
  57. <div v-if="status.pf_type === 'photo'" class="w-100">
  58. <photo-presenter :status="status" v-on:lightbox="lightbox"></photo-presenter>
  59. </div>
  60. <div v-else-if="status.pf_type === 'video'" class="w-100">
  61. <video-presenter :status="status"></video-presenter>
  62. </div>
  63. <div v-else-if="status.pf_type === 'photo:album'" class="w-100">
  64. <photo-album-presenter :status="status" v-on:lightbox="lightbox"></photo-album-presenter>
  65. </div>
  66. <div v-else-if="status.pf_type === 'video:album'" class="w-100">
  67. <video-album-presenter :status="status"></video-album-presenter>
  68. </div>
  69. <div v-else-if="status.pf_type === 'photo:video:album'" class="w-100">
  70. <mixed-album-presenter :status="status" v-on:lightbox="lightbox"></mixed-album-presenter>
  71. </div>
  72. <div v-else class="w-100">
  73. <p class="text-center p-0 font-weight-bold text-white">Error: Problem rendering preview.</p>
  74. </div>
  75. </div>
  76. </div>
  77. <div class="col-12 col-md-4 px-0 d-flex flex-column border-left border-md-left-0">
  78. <div class="d-md-flex d-none align-items-center justify-content-between card-header py-3 bg-white">
  79. <a :href="statusProfileUrl" class="d-flex align-items-center status-username text-truncate" data-toggle="tooltip" data-placement="bottom" :title="statusUsername">
  80. <div class="status-avatar mr-2">
  81. <img :src="statusAvatar" width="24px" height="24px" style="border-radius:12px;">
  82. </div>
  83. <div class="username">
  84. <span class="username-link font-weight-bold text-dark">{{ statusUsername }}</span>
  85. </div>
  86. </a>
  87. <div class="float-right">
  88. <div class="post-actions">
  89. <div class="dropdown">
  90. <button class="btn btn-link text-dark no-caret dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" title="Post options">
  91. <span class="fas fa-ellipsis-v text-muted"></span>
  92. </button>
  93. <div class="dropdown-menu dropdown-menu-right" aria-labelledby="dropdownMenuButton">
  94. <span class="menu-user d-none">
  95. <a class="dropdown-item font-weight-bold" :href="reportUrl()">Report</a>
  96. <a class="dropdown-item font-weight-bold" v-on:click="muteProfile">Mute Profile</a>
  97. <a class="dropdown-item font-weight-bold" v-on:click="blockProfile">Block Profile</a>
  98. </span>
  99. <span class="menu-author d-none">
  100. <!-- <a class="dropdown-item font-weight-bold" :href="editUrl()">Mute Comments</a>
  101. <a class="dropdown-item font-weight-bold" :href="editUrl()">Disable Comments</a> -->
  102. <a class="dropdown-item font-weight-bold" :href="editUrl()">Edit</a>
  103. <a class="dropdown-item font-weight-bold text-danger" v-on:click="deletePost">Delete</a>
  104. </span>
  105. </div>
  106. </div>
  107. </div>
  108. </div>
  109. </div>
  110. <div class="d-flex flex-md-column flex-column-reverse h-100">
  111. <div class="card-body status-comments">
  112. <div class="status-comment">
  113. <p class="mb-1 read-more" style="overflow: hidden;">
  114. <span class="font-weight-bold pr-1">{{statusUsername}}</span>
  115. <span class="comment-text" :id="status.id + '-status-readmore'" v-html="status.content"></span>
  116. </p>
  117. <post-comments :user="this.user" :post-id="statusId" :post-username="statusUsername"></post-comments>
  118. </div>
  119. </div>
  120. <div class="card-body flex-grow-0 py-1">
  121. <div class="reactions my-1">
  122. <h3 v-bind:class="[reactions.liked ? 'fas fa-heart text-danger pr-3 m-0' : 'far fa-heart pr-3 m-0 like-btn']" title="Like" v-on:click="likeStatus"></h3>
  123. <h3 class="far fa-comment pr-3 m-0" title="Comment" v-on:click="commentFocus"></h3>
  124. <h3 v-bind:class="[reactions.shared ? 'far fa-share-square pr-3 m-0 text-primary' : 'far fa-share-square pr-3 m-0 share-btn']" title="Share" v-on:click="shareStatus"></h3>
  125. <h3 v-bind:class="[reactions.bookmarked ? 'fas fa-bookmark text-warning m-0 float-right' : 'far fa-bookmark m-0 float-right']" title="Bookmark" v-on:click="bookmarkStatus"></h3>
  126. </div>
  127. <div class="reaction-counts font-weight-bold mb-0">
  128. <span style="cursor:pointer;" v-on:click="likesModal">
  129. <span class="like-count">{{status.favourites_count || 0}}</span> likes
  130. </span>
  131. <span class="float-right" style="cursor:pointer;" v-on:click="sharesModal">
  132. <span class="share-count pl-4">{{status.reblogs_count || 0}}</span> shares
  133. </span>
  134. </div>
  135. <div class="timestamp">
  136. <a v-bind:href="statusUrl" class="small text-muted">
  137. {{timestampFormat()}}
  138. </a>
  139. </div>
  140. </div>
  141. </div>
  142. <div class="card-footer bg-white sticky-md-bottom">
  143. <div class="comment-form-guest">
  144. <a href="/login">Login</a> to like or comment.
  145. </div>
  146. <form class="comment-form d-none" method="post" action="/i/comment" :data-id="statusId" data-truncate="false">
  147. <input type="hidden" name="_token" value="">
  148. <input type="hidden" name="item" :value="statusId">
  149. <input class="form-control" name="comment" placeholder="Add a comment…" autocomplete="off">
  150. <input type="submit" value="Send" class="btn btn-primary comment-submit" />
  151. </form>
  152. </div>
  153. </div>
  154. </div>
  155. </div>
  156. </div>
  157. <b-modal ref="likesModal"
  158. id="l-modal"
  159. hide-footer
  160. centered
  161. title="Likes"
  162. body-class="list-group-flush p-0">
  163. <div class="list-group">
  164. <div class="list-group-item border-0" v-for="(user, index) in likes" :key="'modal_likes_'+index">
  165. <div class="media">
  166. <a :href="user.url">
  167. <img class="mr-3 rounded-circle box-shadow" :src="user.avatar" :alt="user.username + '’s avatar'" width="30px">
  168. </a>
  169. <div class="media-body">
  170. <p class="mb-0" style="font-size: 14px">
  171. <a :href="user.url" class="font-weight-bold text-dark">
  172. {{user.username}}
  173. </a>
  174. </p>
  175. <p class="text-muted mb-0" style="font-size: 14px">
  176. {{user.display_name}}
  177. </a>
  178. </p>
  179. </div>
  180. </div>
  181. </div>
  182. <infinite-loading @infinite="infiniteLikesHandler" spinner="spiral">
  183. <div slot="no-more"></div>
  184. <div slot="no-results"></div>
  185. </infinite-loading>
  186. </div>
  187. </b-modal>
  188. <b-modal ref="sharesModal"
  189. id="s-modal"
  190. hide-footer
  191. centered
  192. title="Shares"
  193. body-class="list-group-flush p-0">
  194. <div class="list-group">
  195. <div class="list-group-item border-0" v-for="(user, index) in shares" :key="'modal_shares_'+index">
  196. <div class="media">
  197. <a :href="user.url">
  198. <img class="mr-3 rounded-circle box-shadow" :src="user.avatar" :alt="user.username + '’s avatar'" width="30px">
  199. </a>
  200. <div class="media-body">
  201. <div class="d-inline-block">
  202. <p class="mb-0" style="font-size: 14px">
  203. <a :href="user.url" class="font-weight-bold text-dark">
  204. {{user.username}}
  205. </a>
  206. </p>
  207. <p class="text-muted mb-0" style="font-size: 14px">
  208. {{user.display_name}}
  209. </a>
  210. </p>
  211. </div>
  212. <p class="float-right"><!-- <a class="btn btn-primary font-weight-bold py-1" href="#">Follow</a> --></p>
  213. </div>
  214. </div>
  215. </div>
  216. <infinite-loading @infinite="infiniteSharesHandler" spinner="spiral">
  217. <div slot="no-more"></div>
  218. <div slot="no-results"></div>
  219. </infinite-loading>
  220. </div>
  221. </b-modal>
  222. <b-modal
  223. id="lightbox"
  224. ref="lightboxModal"
  225. :hide-header="true"
  226. :hide-footer="true"
  227. centered
  228. size="lg"
  229. body-class="p-0"
  230. >
  231. <div v-if="lightboxMedia" :class="lightboxMedia.filter_class">
  232. <img :src="lightboxMedia.url" class="img-fluid">
  233. </div>
  234. </b-modal>
  235. </div>
  236. </template>
  237. <script>
  238. pixelfed.postComponent = {};
  239. export default {
  240. props: ['status-id', 'status-username', 'status-template', 'status-url', 'status-profile-url', 'status-avatar'],
  241. data() {
  242. return {
  243. status: false,
  244. media: {},
  245. user: false,
  246. reactions: {
  247. liked: false,
  248. shared: false
  249. },
  250. likes: [],
  251. likesPage: 1,
  252. shares: [],
  253. sharesPage: 1,
  254. lightboxMedia: false
  255. }
  256. },
  257. mounted() {
  258. this.fetchData();
  259. this.authCheck();
  260. let token = $('meta[name="csrf-token"]').attr('content');
  261. $('input[name="_token"]').each(function(k, v) {
  262. let el = $(v);
  263. el.val(token);
  264. });
  265. },
  266. updated() {
  267. $('.carousel').carousel();
  268. if(this.reactions) {
  269. if(this.reactions.bookmarked == true) {
  270. $('.postComponent .far.fa-bookmark').removeClass('far').addClass('fas text-warning');
  271. }
  272. if(this.reactions.shared == true) {
  273. $('.postComponent .far.fa-share-square').addClass('text-primary');
  274. }
  275. if(this.reactions.liked == true) {
  276. $('.postComponent .far.fa-heart').removeClass('far text-dark').addClass('fas text-danger');
  277. }
  278. }
  279. },
  280. methods: {
  281. authCheck() {
  282. let authed = $('body').hasClass('loggedIn');
  283. if(authed == true) {
  284. $('.comment-form-guest').addClass('d-none');
  285. $('.comment-form').removeClass('d-none');
  286. }
  287. },
  288. showMuteBlock() {
  289. let sid = this.status.account.id;
  290. let uid = this.user.id;
  291. if(sid == uid) {
  292. $('.post-actions .menu-author').removeClass('d-none');
  293. } else {
  294. $('.post-actions .menu-user').removeClass('d-none');
  295. }
  296. },
  297. reportUrl() {
  298. return '/i/report?type=post&id=' + this.status.id;
  299. },
  300. editUrl() {
  301. return this.status.url + '/edit';
  302. },
  303. timestampFormat() {
  304. let ts = new Date(this.status.created_at);
  305. return ts.toDateString() + ' ' + ts.toLocaleTimeString();
  306. },
  307. fetchData() {
  308. let loader = this.$loading.show({
  309. 'opacity': 0,
  310. 'background-color': '#f5f8fa'
  311. });
  312. axios.get('/api/v2/profile/'+this.statusUsername+'/status/'+this.statusId)
  313. .then(response => {
  314. let self = this;
  315. self.status = response.data.status;
  316. self.user = response.data.user;
  317. self.media = self.status.media_attachments;
  318. self.reactions = response.data.reactions;
  319. self.likes = response.data.likes;
  320. self.shares = response.data.shares;
  321. self.likesPage = 2;
  322. self.sharesPage = 2;
  323. //this.buildPresenter();
  324. this.showMuteBlock();
  325. loader.hide();
  326. pixelfed.readmore();
  327. $('.postComponent').removeClass('d-none');
  328. $('.postPresenterLoader').addClass('d-none');
  329. $('.postPresenterContainer').removeClass('d-none');
  330. $('head title').text(this.status.account.username + ' posted a photo: ' + this.status.favourites_count + ' likes');
  331. }).catch(error => {
  332. if(!error.response) {
  333. $('.postPresenterLoader .lds-ring').attr('style','width:100%').addClass('pt-4 font-weight-bold text-muted').text('An error occurred, cannot fetch media. Please try again later.');
  334. } else {
  335. switch(error.response.status) {
  336. case 401:
  337. $('.postPresenterLoader .lds-ring')
  338. .attr('style','width:100%')
  339. .addClass('pt-4 font-weight-bold text-muted')
  340. .text('Please login to view.');
  341. break;
  342. default:
  343. $('.postPresenterLoader .lds-ring').attr('style','width:100%').addClass('pt-4 font-weight-bold text-muted').text('An error occurred, cannot fetch media. Please try again later.');
  344. break;
  345. }
  346. }
  347. });
  348. },
  349. commentFocus() {
  350. $('.comment-form input[name="comment"]').focus();
  351. },
  352. likesModal() {
  353. if(this.status.favourites_count == 0 || $('body').hasClass('loggedIn') == false) {
  354. return;
  355. }
  356. this.$refs.likesModal.show();
  357. },
  358. sharesModal() {
  359. if(this.status.reblogs_count == 0 || $('body').hasClass('loggedIn') == false) {
  360. return;
  361. }
  362. this.$refs.sharesModal.show();
  363. },
  364. infiniteLikesHandler($state) {
  365. let api = '/api/v2/likes/profile/'+this.statusUsername+'/status/'+this.statusId;
  366. axios.get(api, {
  367. params: {
  368. page: this.likesPage,
  369. },
  370. }).then(({ data }) => {
  371. if (data.data.length > 0) {
  372. this.likes.push(...data.data);
  373. this.likesPage++;
  374. $state.loaded();
  375. } else {
  376. $state.complete();
  377. }
  378. });
  379. },
  380. infiniteSharesHandler($state) {
  381. axios.get('/api/v2/shares/profile/'+this.statusUsername+'/status/'+this.statusId, {
  382. params: {
  383. page: this.sharesPage,
  384. },
  385. }).then(({ data }) => {
  386. if (data.data.length > 0) {
  387. this.shares.push(...data.data);
  388. this.sharesPage++;
  389. $state.loaded();
  390. } else {
  391. $state.complete();
  392. }
  393. });
  394. },
  395. likeStatus(event) {
  396. if($('body').hasClass('loggedIn') == false) {
  397. return;
  398. }
  399. axios.post('/i/like', {
  400. item: this.status.id
  401. }).then(res => {
  402. this.status.favourites_count = res.data.count;
  403. if(this.reactions.liked == true) {
  404. this.reactions.liked = false;
  405. let user = this.user.id;
  406. this.likes = this.likes.filter(function(like) {
  407. return like.id !== user;
  408. });
  409. } else {
  410. this.reactions.liked = true;
  411. let user = this.user;
  412. this.likes.push(user);
  413. }
  414. }).catch(err => {
  415. console.error(err);
  416. swal('Error', 'Something went wrong, please try again later.', 'error');
  417. });
  418. },
  419. shareStatus() {
  420. if($('body').hasClass('loggedIn') == false) {
  421. return;
  422. }
  423. axios.post('/i/share', {
  424. item: this.status.id
  425. }).then(res => {
  426. this.status.reblogs_count = res.data.count;
  427. if(this.reactions.shared == true) {
  428. this.reactions.shared = false;
  429. let user = this.user.id;
  430. this.shares = this.shares.filter(function(reaction) {
  431. return reaction.id !== user;
  432. });
  433. } else {
  434. this.reactions.shared = true;
  435. let user = this.user;
  436. this.shares.push(user);
  437. }
  438. }).catch(err => {
  439. console.error(err);
  440. swal('Error', 'Something went wrong, please try again later.', 'error');
  441. });
  442. },
  443. bookmarkStatus() {
  444. if($('body').hasClass('loggedIn') == false) {
  445. return;
  446. }
  447. axios.post('/i/bookmark', {
  448. item: this.status.id
  449. }).then(res => {
  450. if(this.reactions.bookmarked == true) {
  451. this.reactions.bookmarked = false;
  452. } else {
  453. this.reactions.bookmarked = true;
  454. }
  455. }).catch(err => {
  456. swal('Error', 'Something went wrong, please try again later.', 'error');
  457. });
  458. },
  459. muteProfile() {
  460. if($('body').hasClass('loggedIn') == false) {
  461. return;
  462. }
  463. axios.post('/i/mute', {
  464. type: 'user',
  465. item: this.status.account.id
  466. }).then(res => {
  467. swal('Success', 'You have successfully muted ' + this.status.account.acct, 'success');
  468. }).catch(err => {
  469. swal('Error', 'Something went wrong. Please try again later.', 'error');
  470. });
  471. },
  472. blockProfile() {
  473. if($('body').hasClass('loggedIn') == false) {
  474. return;
  475. }
  476. axios.post('/i/block', {
  477. type: 'user',
  478. item: this.status.account.id
  479. }).then(res => {
  480. swal('Success', 'You have successfully blocked ' + this.status.account.acct, 'success');
  481. }).catch(err => {
  482. swal('Error', 'Something went wrong. Please try again later.', 'error');
  483. });
  484. },
  485. deletePost(status) {
  486. var result = confirm('Are you sure you want to delete this post?');
  487. if (result) {
  488. if($('body').hasClass('loggedIn') == false) {
  489. return;
  490. }
  491. axios.post('/i/delete', {
  492. type: 'status',
  493. item: this.status.id
  494. }).then(res => {
  495. swal('Success', 'You have successfully deleted this post', 'success');
  496. window.location.href = '/';
  497. }).catch(err => {
  498. swal('Error', 'Something went wrong. Please try again later.', 'error');
  499. });
  500. }
  501. },
  502. owner() {
  503. return this.user.id === this.status.account.id;
  504. },
  505. admin() {
  506. return this.user.is_admin == true;
  507. },
  508. ownerOrAdmin() {
  509. return this.owner() || this.admin();
  510. },
  511. lightbox(src) {
  512. this.lightboxMedia = src;
  513. this.$refs.lightboxModal.show();
  514. }
  515. },
  516. }
  517. </script>