1
0

CommentReplies.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. <template>
  2. <div class="comment-replies-component">
  3. <div v-if="loading" class="mt-n2">
  4. <div class="ph-item border-0 mb-0 p-0 bg-transparent" style="border-radius:15px;margin-left:-14px;">
  5. <div class="ph-col-12 mb-0">
  6. <div class="ph-row align-items-center mt-0">
  7. <div class="ph-avatar mr-3 d-flex" style="min-width: 40px;width:40px!important;height:40px!important;border-radius: 8px;"></div>
  8. <div class="ph-col-6"></div>
  9. </div>
  10. </div>
  11. </div>
  12. </div>
  13. <template v-else>
  14. <transition-group tag="div" enter-active-class="animate__animated animate__fadeIn" leave-active-class="animate__animated animate__fadeOut" mode="out-in">
  15. <div
  16. v-for="(post, idx) in feed"
  17. :key="'cd:' + post.id + ':' + idx">
  18. <div class="media media-status align-items-top mb-3">
  19. <a href="#l">
  20. <img class="shadow-sm media-avatar border" :src="post.account.avatar" width="40" height="40" draggable="false" onerror="this.onerror=null;this.src='/storage/avatars/default.jpg?v=0';">
  21. </a>
  22. <div class="media-body">
  23. <div class="media-body-wrapper">
  24. <div v-if="!post.media_attachments.length" class="media-body-comment">
  25. <p class="media-body-comment-username">
  26. <a :href="post.account.url" @click.prevent="goToProfile(post.account)">
  27. {{ post.account.acct }}
  28. </a>
  29. </p>
  30. <span v-if="post.sensitive">
  31. <p class="mb-0">
  32. {{ $t('common.sensitiveContentWarning') }}
  33. </p>
  34. <a href="#" class="small font-weight-bold primary" @click.prevent="post.sensitive = false">Show</a>
  35. </span>
  36. <!-- <span v-else v-html="post.content"></span> -->
  37. <read-more v-else :status="post" />
  38. <button
  39. v-if="post.favourites_count"
  40. class="btn btn-link media-body-likes-count shadow-sm"
  41. @click.prevent="showLikesModal(idx)">
  42. <i class="far fa-thumbs-up primary"></i>
  43. <span class="count">{{ prettyCount(post.favourites_count) }}</span>
  44. </button>
  45. </div>
  46. <div v-else>
  47. <p class="media-body-comment-username">
  48. <a :href="post.account.url" @click.prevent="goToProfile(post.account)">
  49. {{ post.account.acct }}
  50. </a>
  51. </p>
  52. <div v-if="post.sensitive" class="bh-comment" @click="post.sensitive = false">
  53. <blur-hash-image
  54. :width="blurhashWidth(post)"
  55. :height="blurhashHeight(post)"
  56. :punch="1"
  57. class="img-fluid border shadow"
  58. :hash="post.media_attachments[0].blurhash"
  59. />
  60. <div class="sensitive-warning">
  61. <p class="mb-0"><i class="far fa-eye-slash fa-lg"></i></p>
  62. <p class="mb-0 small">Click to view</p>
  63. </div>
  64. </div>
  65. <div v-else class="bh-comment">
  66. <div @click="lightbox(post)">
  67. <blur-hash-image
  68. :width="blurhashWidth(post)"
  69. :height="blurhashHeight(post)"
  70. :punch="1"
  71. class="img-fluid border shadow"
  72. :hash="post.media_attachments[0].blurhash"
  73. :src="getMediaSource(post)"
  74. />
  75. </div>
  76. <button
  77. v-if="post.favourites_count"
  78. class="btn btn-link media-body-likes-count shadow-sm"
  79. @click.prevent="showLikesModal(idx)">
  80. <i class="far fa-thumbs-up primary"></i>
  81. <span class="count">{{ prettyCount(post.favourites_count) }}</span>
  82. </button>
  83. </div>
  84. </div>
  85. </div>
  86. <p class="media-body-reactions">
  87. <button
  88. class="btn btn-link font-weight-bold btn-sm p-0"
  89. :class="[ post.favourited ? 'primary' : 'text-muted' ]"
  90. @click="likeComment(idx)">
  91. {{ post.favourited ? 'Liked' : 'Like' }}
  92. </button>
  93. <!-- <span class="mx-1">·</span>
  94. <a class="font-weight-bold text-muted" :href="post.url" @click.prevent="toggleCommentReply(idx)">
  95. Reply
  96. </a> -->
  97. <span class="mx-1">·</span>
  98. <a class="font-weight-bold text-muted" :href="post.url" @click.prevent="goToPost(post)" v-once>
  99. {{ timeago(post.created_at) }}
  100. </a>
  101. <span v-if="profile && post.account.id === profile.id">
  102. <span class="mx-1">·</span>
  103. <a
  104. class="font-weight-bold text-muted"
  105. href="#"
  106. @click.prevent="deleteComment(idx)">
  107. Delete
  108. </a>
  109. </span>
  110. <span v-else>
  111. <span class="mx-1">·</span>
  112. <a
  113. class="font-weight-bold text-muted"
  114. href="#"
  115. @click.prevent="reportComment(idx)">
  116. Report
  117. </a>
  118. </span>
  119. </p>
  120. <!-- <div class="d-flex align-items-top reply-form child-reply-form my-3">
  121. <img class="shadow-sm media-avatar border" :src="profile.avatar" width="40" height="40">
  122. <input
  123. class="form-control bg-light rounded-pill shadow-sm" style="border-color: #e2e8f0 !important;"
  124. placeholder="Write a comment...."
  125. v-model="replyContent"
  126. v-on:keyup.enter="storeComment"
  127. :disabled="isPostingReply" />
  128. <div class="reply-form-input-actions">
  129. <button
  130. class="btn btn-link text-muted px-1 mr-2">
  131. <i class="far fa-image fa-lg"></i>
  132. </button>
  133. <button
  134. class="btn btn-link text-muted px-1 small font-weight-bold py-0 rounded-pill text-decoration-none"
  135. @click="toggleShowReplyOptions">
  136. <i class="far fa-ellipsis-h"></i>
  137. </button>
  138. </div>
  139. </div> -->
  140. </div>
  141. </div>
  142. </div>
  143. </transition-group>
  144. </template>
  145. </div>
  146. </template>
  147. <script type="text/javascript">
  148. import ReadMore from './ReadMore.vue';
  149. export default {
  150. props: {
  151. status: {
  152. type: Object
  153. },
  154. feed: {
  155. type: Array
  156. }
  157. },
  158. components: {
  159. ReadMore
  160. },
  161. data() {
  162. return {
  163. loading: true,
  164. profile: window._sharedData.user,
  165. ids: [],
  166. nextUrl: undefined,
  167. canLoadMore: false,
  168. }
  169. },
  170. watch: {
  171. feed: {
  172. deep: true,
  173. immediate: true,
  174. handler(o, n) {
  175. this.loading = false;
  176. }
  177. }
  178. },
  179. methods: {
  180. fetchContext() {
  181. axios.get('/api/v2/statuses/' + this.status.id + '/replies', {
  182. params: {
  183. limit: 3
  184. }
  185. })
  186. .then(res => {
  187. if(res.data.next) {
  188. this.nextUrl = res.data.next;
  189. this.canLoadMore = true;
  190. }
  191. res.data.data.forEach(post => {
  192. this.ids.push(post.id);
  193. this.feed.push(post);
  194. });
  195. if(!res.data || !res.data.data || !res.data.data.length && this.status.reply_count) {
  196. this.showEmptyRepliesRefresh = true;
  197. }
  198. })
  199. },
  200. fetchMore(limit = 3) {
  201. axios.get(this.nextUrl, {
  202. params: {
  203. limit: limit,
  204. sort: this.sorts[this.sortIndex]
  205. }
  206. }).then(res => {
  207. this.feedLoading = false;
  208. if(!res.data.next) {
  209. this.canLoadMore = false;
  210. }
  211. this.nextUrl = res.data.next;
  212. res.data.data.forEach(post => {
  213. if(this.ids.indexOf(post.id) == -1) {
  214. this.ids.push(post.id);
  215. this.feed.push(post);
  216. }
  217. });
  218. })
  219. },
  220. fetchSortedFeed() {
  221. axios.get('/api/v2/statuses/' + this.status.id + '/replies', {
  222. params: {
  223. limit: 3,
  224. sort: this.sorts[this.sortIndex]
  225. }
  226. })
  227. .then(res => {
  228. this.feed = res.data.data;
  229. this.nextUrl = res.data.next;
  230. this.feedLoading = false;
  231. });
  232. },
  233. forceRefresh() {
  234. axios.get('/api/v2/statuses/' + this.status.id + '/replies', {
  235. params: {
  236. limit: 3,
  237. refresh_cache: true
  238. }
  239. })
  240. .then(res => {
  241. if(res.data.next) {
  242. this.nextUrl = res.data.next;
  243. this.canLoadMore = true;
  244. }
  245. res.data.data.forEach(post => {
  246. this.ids.push(post.id);
  247. this.feed.push(post);
  248. });
  249. this.showEmptyRepliesRefresh = false;
  250. })
  251. },
  252. timeago(ts) {
  253. return App.util.format.timeAgo(ts);
  254. },
  255. prettyCount(val) {
  256. return App.util.format.count(val);
  257. },
  258. goToPost(post) {
  259. this.$router.push({
  260. name: 'post',
  261. path: `/i/web/post/${post.id}`,
  262. params: {
  263. id: post.id,
  264. cachedStatus: post,
  265. cachedProfile: this.profile
  266. }
  267. })
  268. },
  269. goToProfile(account) {
  270. this.$router.push({
  271. name: 'profile',
  272. path: `/i/web/profile/${account.id}`,
  273. params: {
  274. id: account.id,
  275. cachedProfile: account,
  276. cachedUser: this.profile
  277. }
  278. })
  279. },
  280. storeComment() {
  281. this.isPostingReply = true;
  282. axios.post('/api/v1/statuses', {
  283. status: this.replyContent,
  284. in_reply_to_id: this.status.id,
  285. sensitive: this.settings.sensitive
  286. })
  287. .then(res => {
  288. this.replyContent = undefined;
  289. this.isPostingReply = false;
  290. this.ids.push(res.data.id);
  291. this.feed.push(res.data);
  292. this.$emit('new-comment', res.data);
  293. })
  294. },
  295. toggleSort(index) {
  296. this.$refs.sortMenu.hide();
  297. this.feedLoading = true;
  298. this.sortIndex = index;
  299. this.fetchSortedFeed();
  300. },
  301. deleteComment(index) {
  302. event.currentTarget.blur();
  303. if(!window.confirm(this.$t('menu.deletePostConfirm'))) {
  304. return;
  305. }
  306. axios.post('/i/delete', {
  307. type: 'status',
  308. item: this.feed[index].id
  309. })
  310. .then(res => {
  311. this.feed.splice(index, 1);
  312. this.$emit('counter-change', 'comment-decrement');
  313. this.fetchMore(1);
  314. })
  315. .catch(err => {
  316. })
  317. },
  318. showLikesModal(index) {
  319. this.$emit('show-likes', this.feed[index]);
  320. },
  321. reportComment(index) {
  322. // location.href = '/i/report?type=post&id=' + this.feed[index].id;
  323. this.$emit('handle-report', this.feed[index]);
  324. },
  325. likeComment(index) {
  326. event.currentTarget.blur();
  327. let post = this.feed[index];
  328. let count = post.favourites_count;
  329. let state = post.favourited;
  330. this.feed[index].favourited = !this.feed[index].favourited;
  331. this.feed[index].favourites_count = state ? count - 1 : count + 1;
  332. axios.post('/api/v1/statuses/' + post.id + '/' + (state ? 'unfavourite' : 'favourite'))
  333. .then(res => {
  334. })
  335. },
  336. toggleShowReplyOptions() {
  337. event.currentTarget.blur();
  338. this.showReplyOptions = !this.showReplyOptions;
  339. },
  340. replyUpload() {
  341. event.currentTarget.blur();
  342. this.$refs.fileInput.click();
  343. },
  344. handleImageUpload() {
  345. if(!this.$refs.fileInput.files.length) {
  346. return;
  347. }
  348. this.isUploading = true;
  349. let self = this;
  350. let data = new FormData();
  351. data.append('file', this.$refs.fileInput.files[0]);
  352. axios.post('/api/v1/media', data)
  353. .then(res => {
  354. axios.post('/api/v1/statuses', {
  355. media_ids: [ res.data.id ],
  356. in_reply_to_id: this.status.id,
  357. sensitive: this.settings.sensitive
  358. }).then(res => {
  359. this.feed.push(res.data)
  360. })
  361. });
  362. },
  363. lightbox(status) {
  364. this.lightboxStatus = status.media_attachments[0];
  365. this.$refs.lightboxModal.show();
  366. },
  367. hideLightbox() {
  368. this.lightboxStatus = null;
  369. this.$refs.lightboxModal.hide();
  370. },
  371. blurhashWidth(status) {
  372. if(!status.media_attachments[0].meta) {
  373. return 25;
  374. }
  375. let aspect = status.media_attachments[0].meta.original.aspect;
  376. if(aspect == 1) {
  377. return 25;
  378. } else if(aspect > 1) {
  379. return 30;
  380. } else {
  381. return 20;
  382. }
  383. },
  384. blurhashHeight(status) {
  385. if(!status.media_attachments[0].meta) {
  386. return 25;
  387. }
  388. let aspect = status.media_attachments[0].meta.original.aspect;
  389. if(aspect == 1) {
  390. return 25;
  391. } else if(aspect > 1) {
  392. return 20;
  393. } else {
  394. return 30;
  395. }
  396. },
  397. getMediaSource(status) {
  398. let media = status.media_attachments[0];
  399. if(media.preview_url.endsWith('storage/no-preview.png')) {
  400. return media.url;
  401. }
  402. return media.preview_url;
  403. },
  404. toggleReplyExpand() {
  405. event.currentTarget.blur();
  406. this.settings.expanded = !this.settings.expanded;
  407. },
  408. toggleCommentReply(index) {
  409. this.commentReplyIndex = index;
  410. }
  411. }
  412. }
  413. </script>