Post.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. created() {
  157. this.init();
  158. },
  159. watch: {
  160. '$route': 'init'
  161. },
  162. methods: {
  163. init() {
  164. this.fetchSelf();
  165. },
  166. fetchSelf() {
  167. this.user = window._sharedData.user;
  168. this.isReply = false;
  169. this.fetchPost();
  170. },
  171. fetchPost() {
  172. axios.get('/api/pixelfed/v1/statuses/'+this.$route.params.id)
  173. .then(res => {
  174. if(!res.data || !res.data.hasOwnProperty('id')) {
  175. this.$router.push('/i/web/404');
  176. }
  177. if(!res.data.hasOwnProperty('account') || !res.data.account) {
  178. this.postStateError = true;
  179. return;
  180. }
  181. this.post = res.data;
  182. this.media = this.post.media_attachments;
  183. this.profile = this.post.account;
  184. if(res.data.account && res.data.account.local) {
  185. window.history.pushState({}, '', `/p/${res.data.account.acct}/${res.data.id}`);
  186. }
  187. if(this.post.in_reply_to_id) {
  188. this.fetchReply();
  189. } else {
  190. this.fetchRelationship();
  191. }
  192. }).catch(err => {
  193. switch(err.response.status) {
  194. case 403:
  195. case 404:
  196. this.$router.push('/i/web/404');
  197. break;
  198. }
  199. })
  200. },
  201. fetchReply() {
  202. axios.get('/api/pixelfed/v1/statuses/' + this.post.in_reply_to_id)
  203. .then(res => {
  204. this.reply = res.data;
  205. this.isReply = true;
  206. this.fetchRelationship();
  207. })
  208. .catch(err => {
  209. this.fetchRelationship();
  210. })
  211. },
  212. fetchRelationship() {
  213. if(this.profile.id == this.user.id) {
  214. this.relationship = {};
  215. this.fetchState();
  216. return;
  217. }
  218. axios.get('/api/pixelfed/v1/accounts/relationships', {
  219. params: {
  220. 'id[]': this.profile.id
  221. }
  222. }).then(res => {
  223. this.relationship = res.data[0];
  224. this.fetchState();
  225. });
  226. },
  227. fetchState() {
  228. axios.get('/api/v2/statuses/'+this.post.id+'/state')
  229. .then(res => {
  230. this.post.favourited = res.data.liked;
  231. this.post.reblogged = res.data.shared;
  232. this.post.bookmarked = res.data.bookmarked;
  233. if(!this.post.favourites_count && this.post.favourited) {
  234. this.post.favourites_count = 1;
  235. }
  236. this.isLoaded = true;
  237. }).catch(err => {
  238. this.isLoaded = false;
  239. this.postStateError = true;
  240. })
  241. },
  242. goBack() {
  243. this.$router.push('/i/web');
  244. },
  245. likeStatus() {
  246. let count = this.post.favourites_count;
  247. this.post.favourites_count = count + 1;
  248. this.post.favourited = !this.post.favourited;
  249. axios.post('/api/v1/statuses/' + this.post.id + '/favourite')
  250. .then(res => {
  251. //
  252. }).catch(err => {
  253. this.post.favourites_count = count;
  254. this.post.favourited = false;
  255. })
  256. },
  257. unlikeStatus() {
  258. let count = this.post.favourites_count;
  259. this.post.favourites_count = count - 1;
  260. this.post.favourited = !this.post.favourited;
  261. axios.post('/api/v1/statuses/' + this.post.id + '/unfavourite')
  262. .then(res => {
  263. //
  264. }).catch(err => {
  265. this.post.favourites_count = count;
  266. this.post.favourited = false;
  267. })
  268. },
  269. shareStatus() {
  270. let count = this.post.reblogs_count;
  271. this.post.reblogs_count = count + 1;
  272. this.post.reblogged = !this.post.reblogged;
  273. axios.post('/api/v1/statuses/' + this.post.id + '/reblog')
  274. .then(res => {
  275. //
  276. }).catch(err => {
  277. this.post.reblogs_count = count;
  278. this.post.reblogged = false;
  279. })
  280. },
  281. unshareStatus() {
  282. let count = this.post.reblogs_count;
  283. this.post.reblogs_count = count - 1;
  284. this.post.reblogged = !this.post.reblogged;
  285. axios.post('/api/v1/statuses/' + this.post.id + '/unreblog')
  286. .then(res => {
  287. //
  288. }).catch(err => {
  289. this.post.reblogs_count = count;
  290. this.post.reblogged = false;
  291. })
  292. },
  293. follow() {
  294. axios.post('/api/v1/accounts/' + this.post.account.id + '/follow')
  295. .then(res => {
  296. this.$store.commit('updateRelationship', [res.data]);
  297. this.user.following_count++;
  298. this.post.account.followers_count++;
  299. }).catch(err => {
  300. swal('Oops!', 'An error occurred when attempting to follow this account.', 'error');
  301. this.post.relationship.following = false;
  302. });
  303. },
  304. unfollow() {
  305. axios.post('/api/v1/accounts/' + this.post.account.id + '/unfollow')
  306. .then(res => {
  307. this.$store.commit('updateRelationship', [res.data]);
  308. this.user.following_count--;
  309. this.post.account.followers_count--;
  310. }).catch(err => {
  311. swal('Oops!', 'An error occurred when attempting to unfollow this account.', 'error');
  312. this.post.relationship.following = true;
  313. });
  314. },
  315. openContextMenu() {
  316. this.$nextTick(() => {
  317. this.$refs.contextMenu.open();
  318. });
  319. },
  320. openLikesModal() {
  321. this.showLikesModal = true;
  322. this.$nextTick(() => {
  323. this.$refs.likesModal.open();
  324. });
  325. },
  326. openSharesModal() {
  327. this.showSharesModal = true;
  328. this.$nextTick(() => {
  329. this.$refs.sharesModal.open();
  330. });
  331. },
  332. deletePost() {
  333. this.$router.push('/i/web');
  334. },
  335. goToPost(post) {
  336. this.$router.push({
  337. name: 'post',
  338. path: `/i/web/post/${post.id}`,
  339. params: {
  340. id: post.id,
  341. cachedStatus: post,
  342. cachedProfile: this.user
  343. }
  344. })
  345. },
  346. goToProfile(account) {
  347. this.$router.push({
  348. name: 'profile',
  349. path: `/i/web/profile/${account.id}`,
  350. params: {
  351. id: account.id,
  352. cachedProfile: account,
  353. cachedUser: this.user
  354. }
  355. })
  356. },
  357. handleBookmark() {
  358. axios.post('/i/bookmark', {
  359. item: this.post.id
  360. })
  361. .then(res => {
  362. this.post.bookmarked = !this.post.bookmarked;
  363. })
  364. .catch(err => {
  365. this.$bvToast.toast('Cannot bookmark post at this time.', {
  366. title: 'Bookmark Error',
  367. variant: 'danger',
  368. autoHideDelay: 5000
  369. });
  370. });
  371. },
  372. handleReport() {
  373. this.$nextTick(() => {
  374. this.$refs.reportModal.open();
  375. });
  376. },
  377. counterChange(type) {
  378. switch(type) {
  379. case 'comment-increment':
  380. this.post.reply_count = this.post.reply_count + 1;
  381. break;
  382. case 'comment-decrement':
  383. this.post.reply_count = this.post.reply_count - 1;
  384. break;
  385. }
  386. },
  387. handleEdit(status) {
  388. this.$refs.editModal.show(status);
  389. },
  390. mergeUpdatedPost(post) {
  391. this.post = post;
  392. this.$nextTick(() => {
  393. this.forceUpdateIdx++;
  394. });
  395. }
  396. }
  397. }
  398. </script>