PostComponent.vue 20 KB

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