Post.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. <template>
  2. <div class="post-timeline-component web-wrapper">
  3. <div v-if="isLoaded" class="container-fluid mt-3">
  4. <div class="row">
  5. <div class="col-md-4 col-lg-3 d-md-block">
  6. <sidebar :user="user" />
  7. </div>
  8. <div class="col-md-8 col-lg-6">
  9. <div v-if="isReply" class="p-3 rounded-top mb-n3" style="background-color: var(--card-header-accent)">
  10. <p>
  11. <i class="fal fa-reply mr-1"></i> In reply to
  12. <a
  13. :href="'/i/web/profile/' + reply.account.id"
  14. class="font-weight-bold primary"
  15. @click.prevent="goToProfile(reply.account)">
  16. &commat;{{ reply.account.acct }}
  17. </a>
  18. <button
  19. @click.prevent="goToPost(reply)"
  20. class="btn btn-primary font-weight-bold btn-sm px-3 float-right rounded-pill">
  21. View Post
  22. </button>
  23. </p>
  24. </div>
  25. <status
  26. :key="post.id + ':fui:' + forceUpdateIdx"
  27. :status="post"
  28. :profile="user"
  29. v-on:menu="openContextMenu()"
  30. v-on:like="likeStatus()"
  31. v-on:unlike="unlikeStatus()"
  32. v-on:likes-modal="openLikesModal()"
  33. v-on:shares-modal="openSharesModal()"
  34. v-on:bookmark="handleBookmark()"
  35. v-on:share="shareStatus()"
  36. v-on:unshare="unshareStatus()"
  37. v-on:follow="follow()"
  38. v-on:unfollow="unfollow()"
  39. v-on:counter-change="counterChange"
  40. />
  41. </div>
  42. <div class="d-none d-lg-block col-lg-3">
  43. <rightbar />
  44. </div>
  45. </div>
  46. </div>
  47. <div v-if="postStateError" class="container-fluid mt-3">
  48. <div class="row">
  49. <div class="col-md-4 col-lg-3 d-md-block">
  50. <sidebar :user="user" />
  51. </div>
  52. <div class="col-md-8 col-lg-6">
  53. <div class="card card-body shadow-none border">
  54. <div class="d-flex align-self-center flex-column" style="max-width: 500px;">
  55. <p class="text-center">
  56. <i class="far fa-exclamation-triangle fa-3x text-lighter"></i>
  57. </p>
  58. <p class="text-center lead font-weight-bold">Error displaying post</p>
  59. <p class="mb-0">This can happen for a few reasons:</p>
  60. <ul class="text-lighter">
  61. <li>The url is invalid or has a typo</li>
  62. <li>The page has been flagged for review by our automated abuse detection systems</li>
  63. <li>The content may have been deleted</li>
  64. <li>You do not have permission to view this content</li>
  65. </ul>
  66. </div>
  67. </div>
  68. </div>
  69. <div class="d-none d-lg-block col-lg-3">
  70. <rightbar />
  71. </div>
  72. </div>
  73. </div>
  74. <context-menu
  75. v-if="isLoaded"
  76. ref="contextMenu"
  77. :status="post"
  78. :profile="user"
  79. @report-modal="handleReport()"
  80. @delete="deletePost()"
  81. v-on:edit="handleEdit"
  82. />
  83. <likes-modal
  84. v-if="showLikesModal"
  85. ref="likesModal"
  86. :status="post"
  87. :profile="user"
  88. />
  89. <shares-modal
  90. v-if="showSharesModal"
  91. ref="sharesModal"
  92. :status="post"
  93. :profile="profile"
  94. />
  95. <report-modal
  96. v-if="post"
  97. ref="reportModal"
  98. :status="post"
  99. />
  100. <post-edit-modal
  101. ref="editModal"
  102. v-on:update="mergeUpdatedPost"
  103. />
  104. <drawer />
  105. </div>
  106. </template>
  107. <script type="text/javascript">
  108. import Drawer from './partials/drawer.vue';
  109. import Rightbar from './partials/rightbar.vue';
  110. import Sidebar from './partials/sidebar.vue';
  111. import Status from './partials/TimelineStatus.vue';
  112. import ContextMenu from './partials/post/ContextMenu.vue';
  113. import MediaContainer from './partials/post/MediaContainer.vue';
  114. import LikesModal from './partials/post/LikeModal.vue';
  115. import SharesModal from './partials/post/ShareModal.vue';
  116. import ReportModal from './partials/modal/ReportPost.vue';
  117. import PostEditModal from './partials/post/PostEditModal.vue';
  118. export default {
  119. props: {
  120. cachedStatus: {
  121. type: Object
  122. },
  123. cachedProfile: {
  124. type: Object
  125. }
  126. },
  127. components: {
  128. "drawer": Drawer,
  129. "sidebar": Sidebar,
  130. "status": Status,
  131. "context-menu": ContextMenu,
  132. "media-container": MediaContainer,
  133. "likes-modal": LikesModal,
  134. "shares-modal": SharesModal,
  135. "rightbar": Rightbar,
  136. "report-modal": ReportModal,
  137. "post-edit-modal": PostEditModal
  138. },
  139. data() {
  140. return {
  141. isLoaded: false,
  142. user: undefined,
  143. profile: undefined,
  144. post: undefined,
  145. relationship: {},
  146. media: undefined,
  147. mediaIndex: 0,
  148. showLikesModal: false,
  149. isReply: false,
  150. reply: {},
  151. showSharesModal: false,
  152. postStateError: false,
  153. forceUpdateIdx: 0
  154. }
  155. },
  156. beforeMount() {
  157. this.init();
  158. },
  159. watch: {
  160. '$route': 'init'
  161. },
  162. methods: {
  163. init() {
  164. if(this.cachedStatus && this.cachedProfile) {
  165. this.post = this.cachedStatus;
  166. this.media = this.post.media_attachments;
  167. this.profile = this.post.account;
  168. this.user = this.cachedProfile;
  169. if(this.post.in_reply_to_id) {
  170. this.fetchReply();
  171. } else {
  172. this.isReply = false;
  173. this.fetchRelationship();
  174. }
  175. } else {
  176. this.fetchSelf();
  177. }
  178. },
  179. fetchSelf() {
  180. this.user = window._sharedData.user;
  181. this.fetchPost();
  182. },
  183. fetchPost() {
  184. axios.get('/api/pixelfed/v1/statuses/'+this.$route.params.id)
  185. .then(res => {
  186. if(!res.data || !res.data.hasOwnProperty('id')) {
  187. this.$router.push('/i/web/404');
  188. }
  189. if(!res.data.hasOwnProperty('account') || !res.data.account) {
  190. this.postStateError = true;
  191. return;
  192. }
  193. this.post = res.data;
  194. this.media = this.post.media_attachments;
  195. this.profile = this.post.account;
  196. if(this.post.in_reply_to_id) {
  197. this.fetchReply();
  198. } else {
  199. this.fetchRelationship();
  200. }
  201. }).catch(err => {
  202. switch(err.response.status) {
  203. case 403:
  204. case 404:
  205. this.$router.push('/i/web/404');
  206. break;
  207. }
  208. })
  209. },
  210. fetchReply() {
  211. axios.get('/api/pixelfed/v1/statuses/' + this.post.in_reply_to_id)
  212. .then(res => {
  213. this.reply = res.data;
  214. this.isReply = true;
  215. this.fetchRelationship();
  216. })
  217. .catch(err => {
  218. this.fetchRelationship();
  219. })
  220. },
  221. fetchRelationship() {
  222. if(this.profile.id == this.user.id) {
  223. this.relationship = {};
  224. this.fetchState();
  225. return;
  226. }
  227. axios.get('/api/pixelfed/v1/accounts/relationships', {
  228. params: {
  229. 'id[]': this.profile.id
  230. }
  231. }).then(res => {
  232. this.relationship = res.data[0];
  233. this.fetchState();
  234. });
  235. },
  236. fetchState() {
  237. axios.get('/api/v2/statuses/'+this.post.id+'/state')
  238. .then(res => {
  239. this.post.favourited = res.data.liked;
  240. this.post.reblogged = res.data.shared;
  241. this.post.bookmarked = res.data.bookmarked;
  242. if(!this.post.favourites_count && this.post.favourited) {
  243. this.post.favourites_count = 1;
  244. }
  245. this.isLoaded = true;
  246. }).catch(err => {
  247. this.isLoaded = false;
  248. this.postStateError = true;
  249. })
  250. },
  251. goBack() {
  252. this.$router.push('/i/web');
  253. },
  254. likeStatus() {
  255. let count = this.post.favourites_count;
  256. this.post.favourites_count = count + 1;
  257. this.post.favourited = !this.post.favourited;
  258. axios.post('/api/v1/statuses/' + this.post.id + '/favourite')
  259. .then(res => {
  260. //
  261. }).catch(err => {
  262. this.post.favourites_count = count;
  263. this.post.favourited = false;
  264. })
  265. },
  266. unlikeStatus() {
  267. let count = this.post.favourites_count;
  268. this.post.favourites_count = count - 1;
  269. this.post.favourited = !this.post.favourited;
  270. axios.post('/api/v1/statuses/' + this.post.id + '/unfavourite')
  271. .then(res => {
  272. //
  273. }).catch(err => {
  274. this.post.favourites_count = count;
  275. this.post.favourited = false;
  276. })
  277. },
  278. shareStatus() {
  279. let count = this.post.reblogs_count;
  280. this.post.reblogs_count = count + 1;
  281. this.post.reblogged = !this.post.reblogged;
  282. axios.post('/api/v1/statuses/' + this.post.id + '/reblog')
  283. .then(res => {
  284. //
  285. }).catch(err => {
  286. this.post.reblogs_count = count;
  287. this.post.reblogged = false;
  288. })
  289. },
  290. unshareStatus() {
  291. let count = this.post.reblogs_count;
  292. this.post.reblogs_count = count - 1;
  293. this.post.reblogged = !this.post.reblogged;
  294. axios.post('/api/v1/statuses/' + this.post.id + '/unreblog')
  295. .then(res => {
  296. //
  297. }).catch(err => {
  298. this.post.reblogs_count = count;
  299. this.post.reblogged = false;
  300. })
  301. },
  302. follow() {
  303. axios.post('/api/v1/accounts/' + this.post.account.id + '/follow')
  304. .then(res => {
  305. this.$store.commit('updateRelationship', [res.data]);
  306. this.user.following_count++;
  307. this.post.account.followers_count++;
  308. }).catch(err => {
  309. swal('Oops!', 'An error occurred when attempting to follow this account.', 'error');
  310. this.post.relationship.following = false;
  311. });
  312. },
  313. unfollow() {
  314. axios.post('/api/v1/accounts/' + this.post.account.id + '/unfollow')
  315. .then(res => {
  316. this.$store.commit('updateRelationship', [res.data]);
  317. this.user.following_count--;
  318. this.post.account.followers_count--;
  319. }).catch(err => {
  320. swal('Oops!', 'An error occurred when attempting to unfollow this account.', 'error');
  321. this.post.relationship.following = true;
  322. });
  323. },
  324. openContextMenu() {
  325. this.$nextTick(() => {
  326. this.$refs.contextMenu.open();
  327. });
  328. },
  329. openLikesModal() {
  330. this.showLikesModal = true;
  331. this.$nextTick(() => {
  332. this.$refs.likesModal.open();
  333. });
  334. },
  335. openSharesModal() {
  336. this.showSharesModal = true;
  337. this.$nextTick(() => {
  338. this.$refs.sharesModal.open();
  339. });
  340. },
  341. deletePost() {
  342. this.$router.push('/i/web');
  343. },
  344. goToPost(post) {
  345. this.$router.push({
  346. name: 'post',
  347. path: `/i/web/post/${post.id}`,
  348. params: {
  349. id: post.id,
  350. cachedStatus: post,
  351. cachedProfile: this.user
  352. }
  353. })
  354. },
  355. goToProfile(account) {
  356. this.$router.push({
  357. name: 'profile',
  358. path: `/i/web/profile/${account.id}`,
  359. params: {
  360. id: account.id,
  361. cachedProfile: account,
  362. cachedUser: this.user
  363. }
  364. })
  365. },
  366. handleBookmark() {
  367. axios.post('/i/bookmark', {
  368. item: this.post.id
  369. })
  370. .then(res => {
  371. this.post.bookmarked = !this.post.bookmarked;
  372. })
  373. .catch(err => {
  374. this.$bvToast.toast('Cannot bookmark post at this time.', {
  375. title: 'Bookmark Error',
  376. variant: 'danger',
  377. autoHideDelay: 5000
  378. });
  379. });
  380. },
  381. handleReport() {
  382. this.$nextTick(() => {
  383. this.$refs.reportModal.open();
  384. });
  385. },
  386. counterChange(type) {
  387. switch(type) {
  388. case 'comment-increment':
  389. this.post.reply_count = this.post.reply_count + 1;
  390. break;
  391. case 'comment-decrement':
  392. this.post.reply_count = this.post.reply_count - 1;
  393. break;
  394. }
  395. },
  396. handleEdit(status) {
  397. this.$refs.editModal.show(status);
  398. },
  399. mergeUpdatedPost(post) {
  400. this.post = post;
  401. this.$nextTick(() => {
  402. this.forceUpdateIdx++;
  403. });
  404. }
  405. }
  406. }
  407. </script>