PostComponent.vue 23 KB

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