PostComponent.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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"></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"></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"></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. </div>
  223. </template>
  224. <script>
  225. pixelfed.postComponent = {};
  226. export default {
  227. props: ['status-id', 'status-username', 'status-template', 'status-url', 'status-profile-url', 'status-avatar'],
  228. data() {
  229. return {
  230. status: false,
  231. media: {},
  232. user: false,
  233. reactions: {
  234. liked: false,
  235. shared: false
  236. },
  237. likes: [],
  238. likesPage: 1,
  239. shares: [],
  240. sharesPage: 1,
  241. }
  242. },
  243. mounted() {
  244. this.fetchData();
  245. this.authCheck();
  246. let token = $('meta[name="csrf-token"]').attr('content');
  247. $('input[name="_token"]').each(function(k, v) {
  248. let el = $(v);
  249. el.val(token);
  250. });
  251. },
  252. updated() {
  253. $('.carousel').carousel();
  254. if(this.reactions) {
  255. if(this.reactions.bookmarked == true) {
  256. $('.postComponent .far.fa-bookmark').removeClass('far').addClass('fas text-warning');
  257. }
  258. if(this.reactions.shared == true) {
  259. $('.postComponent .far.fa-share-square').addClass('text-primary');
  260. }
  261. if(this.reactions.liked == true) {
  262. $('.postComponent .far.fa-heart').removeClass('far text-dark').addClass('fas text-danger');
  263. }
  264. }
  265. if(this.status) {
  266. let title = this.status.account.username + ' posted a photo: ' + this.status.favourites_count + ' likes';
  267. $('head title').text(title);
  268. }
  269. },
  270. methods: {
  271. authCheck() {
  272. let authed = $('body').hasClass('loggedIn');
  273. if(authed == true) {
  274. $('.comment-form-guest').addClass('d-none');
  275. $('.comment-form').removeClass('d-none');
  276. }
  277. },
  278. showMuteBlock() {
  279. let sid = this.status.account.id;
  280. let uid = this.user.id;
  281. if(sid == uid) {
  282. $('.post-actions .menu-author').removeClass('d-none');
  283. } else {
  284. $('.post-actions .menu-user').removeClass('d-none');
  285. }
  286. },
  287. reportUrl() {
  288. return '/i/report?type=post&id=' + this.status.id;
  289. },
  290. editUrl() {
  291. return this.status.url + '/edit';
  292. },
  293. timestampFormat() {
  294. let ts = new Date(this.status.created_at);
  295. return ts.toDateString() + ' ' + ts.toLocaleTimeString();
  296. },
  297. fetchData() {
  298. let loader = this.$loading.show({
  299. 'opacity': 0,
  300. 'background-color': '#f5f8fa'
  301. });
  302. axios.get('/api/v2/profile/'+this.statusUsername+'/status/'+this.statusId)
  303. .then(response => {
  304. let self = this;
  305. self.status = response.data.status;
  306. self.user = response.data.user;
  307. self.media = self.status.media_attachments;
  308. self.reactions = response.data.reactions;
  309. self.likes = response.data.likes;
  310. self.shares = response.data.shares;
  311. self.likesPage = 2;
  312. self.sharesPage = 2;
  313. //this.buildPresenter();
  314. this.showMuteBlock();
  315. loader.hide();
  316. pixelfed.readmore();
  317. $('.postComponent').removeClass('d-none');
  318. $('.postPresenterLoader').addClass('d-none');
  319. $('.postPresenterContainer').removeClass('d-none');
  320. }).catch(error => {
  321. if(!error.response) {
  322. $('.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.');
  323. } else {
  324. switch(error.response.status) {
  325. case 401:
  326. $('.postPresenterLoader .lds-ring')
  327. .attr('style','width:100%')
  328. .addClass('pt-4 font-weight-bold text-muted')
  329. .text('Please login to view.');
  330. break;
  331. default:
  332. $('.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.');
  333. break;
  334. }
  335. }
  336. });
  337. },
  338. commentFocus() {
  339. $('.comment-form input[name="comment"]').focus();
  340. },
  341. likesModal() {
  342. if(this.status.favourites_count == 0 || $('body').hasClass('loggedIn') == false) {
  343. return;
  344. }
  345. this.$refs.likesModal.show();
  346. },
  347. sharesModal() {
  348. if(this.status.reblogs_count == 0 || $('body').hasClass('loggedIn') == false) {
  349. return;
  350. }
  351. this.$refs.sharesModal.show();
  352. },
  353. infiniteLikesHandler($state) {
  354. let api = '/api/v2/likes/profile/'+this.statusUsername+'/status/'+this.statusId;
  355. axios.get(api, {
  356. params: {
  357. page: this.likesPage,
  358. },
  359. }).then(({ data }) => {
  360. if (data.data.length > 0) {
  361. this.likes.push(...data.data);
  362. this.likesPage++;
  363. $state.loaded();
  364. } else {
  365. $state.complete();
  366. }
  367. });
  368. },
  369. infiniteSharesHandler($state) {
  370. axios.get('/api/v2/shares/profile/'+this.statusUsername+'/status/'+this.statusId, {
  371. params: {
  372. page: this.sharesPage,
  373. },
  374. }).then(({ data }) => {
  375. if (data.data.length > 0) {
  376. this.shares.push(...data.data);
  377. this.sharesPage++;
  378. $state.loaded();
  379. } else {
  380. $state.complete();
  381. }
  382. });
  383. },
  384. likeStatus(event) {
  385. if($('body').hasClass('loggedIn') == false) {
  386. return;
  387. }
  388. axios.post('/i/like', {
  389. item: this.status.id
  390. }).then(res => {
  391. this.status.favourites_count = res.data.count;
  392. if(this.reactions.liked == true) {
  393. this.reactions.liked = false;
  394. let user = this.user.id;
  395. this.likes = this.likes.filter(function(like) {
  396. return like.id !== user;
  397. });
  398. } else {
  399. this.reactions.liked = true;
  400. let user = this.user;
  401. this.likes.push(user);
  402. }
  403. }).catch(err => {
  404. console.error(err);
  405. swal('Error', 'Something went wrong, please try again later.', 'error');
  406. });
  407. },
  408. shareStatus() {
  409. if($('body').hasClass('loggedIn') == false) {
  410. return;
  411. }
  412. axios.post('/i/share', {
  413. item: this.status.id
  414. }).then(res => {
  415. this.status.reblogs_count = res.data.count;
  416. if(this.reactions.shared == true) {
  417. this.reactions.shared = false;
  418. let user = this.user.id;
  419. this.shares = this.shares.filter(function(reaction) {
  420. return reaction.id !== user;
  421. });
  422. } else {
  423. this.reactions.shared = true;
  424. let user = this.user;
  425. this.shares.push(user);
  426. }
  427. }).catch(err => {
  428. console.error(err);
  429. swal('Error', 'Something went wrong, please try again later.', 'error');
  430. });
  431. },
  432. bookmarkStatus() {
  433. if($('body').hasClass('loggedIn') == false) {
  434. return;
  435. }
  436. axios.post('/i/bookmark', {
  437. item: this.status.id
  438. }).then(res => {
  439. if(this.reactions.bookmarked == true) {
  440. this.reactions.bookmarked = false;
  441. } else {
  442. this.reactions.bookmarked = true;
  443. }
  444. }).catch(err => {
  445. swal('Error', 'Something went wrong, please try again later.', 'error');
  446. });
  447. },
  448. muteProfile() {
  449. if($('body').hasClass('loggedIn') == false) {
  450. return;
  451. }
  452. axios.post('/i/mute', {
  453. type: 'user',
  454. item: this.status.account.id
  455. }).then(res => {
  456. swal('Success', 'You have successfully muted ' + this.status.account.acct, 'success');
  457. }).catch(err => {
  458. swal('Error', 'Something went wrong. Please try again later.', 'error');
  459. });
  460. },
  461. blockProfile() {
  462. if($('body').hasClass('loggedIn') == false) {
  463. return;
  464. }
  465. axios.post('/i/block', {
  466. type: 'user',
  467. item: this.status.account.id
  468. }).then(res => {
  469. swal('Success', 'You have successfully blocked ' + this.status.account.acct, 'success');
  470. }).catch(err => {
  471. swal('Error', 'Something went wrong. Please try again later.', 'error');
  472. });
  473. },
  474. deletePost(status) {
  475. var result = confirm('Are you sure you want to delete this post?');
  476. if (result) {
  477. if($('body').hasClass('loggedIn') == false) {
  478. return;
  479. }
  480. axios.post('/i/delete', {
  481. type: 'status',
  482. item: this.status.id
  483. }).then(res => {
  484. swal('Success', 'You have successfully deleted this post', 'success');
  485. window.location.href = '/';
  486. }).catch(err => {
  487. swal('Error', 'Something went wrong. Please try again later.', 'error');
  488. });
  489. }
  490. },
  491. owner() {
  492. return this.user.id === this.status.account.id;
  493. },
  494. admin() {
  495. return this.user.is_admin == true;
  496. },
  497. ownerOrAdmin() {
  498. return this.owner() || this.admin();
  499. }
  500. },
  501. }
  502. </script>