1
0

ShareModal.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <template>
  2. <div>
  3. <b-modal
  4. ref="sharesModal"
  5. centered
  6. size="md"
  7. :scrollable="true"
  8. hide-footer
  9. header-class="py-2"
  10. body-class="p-0"
  11. title-class="w-100 text-center pl-4 font-weight-bold"
  12. title-tag="p"
  13. title="Shared By">
  14. <div v-if="isLoading" class="likes-loader list-group border-top-0" style="max-height: 500px;">
  15. <like-placeholder />
  16. </div>
  17. <div v-else>
  18. <div v-if="!likes.length" class="d-flex justify-content-center align-items-center" style="height: 140px;">
  19. <p class="font-weight-bold mb-0">Nobody has shared this yet!</p>
  20. </div>
  21. <div v-else class="list-group" style="max-height: 500px;">
  22. <div v-for="(account, index) in likes" class="list-group-item border-left-0 border-right-0 px-3" :class="[ index === 0 ? 'border-top-0' : '']">
  23. <div class="media align-items-center">
  24. <img :src="account.avatar" width="40" height="40" style="border-radius: 8px;" class="mr-3 shadow-sm" onerror="this.src='/storage/avatars/default.jpg?v=0';this.onerror=null;">
  25. <div class="media-body">
  26. <p class="mb-0 text-truncate"><a :href="account.url" class="text-dark font-weight-bold text-decoration-none" @click.prevent="goToProfile(account)">{{ getUsername(account) }}</a></p>
  27. <p class="mb-0 mt-n1 text-dark font-weight-bold small text-break">&commat;{{ account.acct }}</p>
  28. </div>
  29. <div>
  30. <button
  31. v-if="account.id == user.id"
  32. class="btn btn-outline-muted rounded-pill btn-sm font-weight-bold"
  33. @click="goToProfile(profile)"
  34. style="width:110px;">
  35. View Profile
  36. </button>
  37. <button
  38. v-else-if="account.follows"
  39. class="btn btn-outline-muted rounded-pill btn-sm font-weight-bold"
  40. :disabled="isUpdatingFollowState"
  41. @click="handleUnfollow(index)"
  42. style="width:110px;">
  43. <span v-if="isUpdatingFollowState && followStateIndex === index">
  44. <b-spinner small />
  45. </span>
  46. <span v-else>Following</span>
  47. </button>
  48. <button
  49. v-else-if="!account.follows"
  50. class="btn btn-primary rounded-pill btn-sm font-weight-bold"
  51. :disabled="isUpdatingFollowState"
  52. @click="handleFollow(index)"
  53. style="width:110px;">
  54. <span v-if="isUpdatingFollowState && followStateIndex === index">
  55. <b-spinner small />
  56. </span>
  57. <span v-else>Follow</span>
  58. </button>
  59. </div>
  60. </div>
  61. </div>
  62. <div v-if="canLoadMore">
  63. <intersect @enter="enterIntersect">
  64. <like-placeholder class="border-top-0" />
  65. </intersect>
  66. <like-placeholder />
  67. </div>
  68. </div>
  69. </div>
  70. </b-modal>
  71. </div>
  72. </template>
  73. <script type="text/javascript">
  74. import Intersect from 'vue-intersect'
  75. import LikePlaceholder from './LikeListPlaceholder.vue';
  76. import { parseLinkHeader } from '@web3-storage/parse-link-header';
  77. export default {
  78. props: {
  79. status: {
  80. type: Object
  81. },
  82. profile: {
  83. type: Object
  84. }
  85. },
  86. components: {
  87. "intersect": Intersect,
  88. "like-placeholder": LikePlaceholder
  89. },
  90. data() {
  91. return {
  92. isOpen: false,
  93. isLoading: true,
  94. canLoadMore: false,
  95. isFetchingMore: false,
  96. likes: [],
  97. ids: [],
  98. cursor: undefined,
  99. isUpdatingFollowState: false,
  100. followStateIndex: undefined,
  101. user: window._sharedData.user
  102. }
  103. },
  104. methods: {
  105. clear() {
  106. this.isOpen = false;
  107. this.isLoading = true;
  108. this.canLoadMore = false;
  109. this.isFetchingMore = false;
  110. this.likes = [];
  111. this.ids = [];
  112. this.cursor = undefined;
  113. },
  114. fetchShares() {
  115. axios.get('/api/v1/statuses/'+this.status.id+'/reblogged_by', {
  116. params: {
  117. limit: 40,
  118. '_pe': 1
  119. }
  120. })
  121. .then(res => {
  122. this.ids = res.data.map(a => a.id);
  123. this.likes = res.data;
  124. if(res.headers && res.headers.link) {
  125. const links = parseLinkHeader(res.headers.link);
  126. if(links.prev) {
  127. this.cursor = links.prev.cursor;
  128. this.canLoadMore = true;
  129. } else {
  130. this.canLoadMore = false;
  131. }
  132. } else {
  133. this.canLoadMore = false;
  134. }
  135. this.isLoading = false;
  136. });
  137. },
  138. open() {
  139. if(this.cursor) {
  140. this.clear();
  141. }
  142. this.isOpen = true;
  143. this.fetchShares();
  144. this.$refs.sharesModal.show();
  145. },
  146. enterIntersect() {
  147. if(this.isFetchingMore) {
  148. return;
  149. }
  150. this.isFetchingMore = true;
  151. axios.get('/api/v1/statuses/'+this.status.id+'/reblogged_by', {
  152. params: {
  153. limit: 10,
  154. cursor: this.cursor,
  155. '_pe': 1
  156. }
  157. }).then(res => {
  158. if(!res.data || !res.data.length) {
  159. this.canLoadMore = false;
  160. this.isFetchingMore = false;
  161. return;
  162. }
  163. res.data.forEach(user => {
  164. if(this.ids.indexOf(user.id) == -1) {
  165. this.ids.push(user.id);
  166. this.likes.push(user);
  167. }
  168. })
  169. if(res.headers && res.headers.link) {
  170. const links = parseLinkHeader(res.headers.link);
  171. if(links.prev) {
  172. this.cursor = links.prev.cursor;
  173. } else {
  174. this.canLoadMore = false;
  175. }
  176. } else {
  177. this.canLoadMore = false;
  178. }
  179. this.isFetchingMore = false;
  180. })
  181. },
  182. getUsername(account) {
  183. return account.display_name ? account.display_name : account.username;
  184. },
  185. goToProfile(account) {
  186. this.$router.push({
  187. name: 'profile',
  188. path: `/i/web/profile/${account.id}`,
  189. params: {
  190. id: account.id,
  191. cachedProfile: account,
  192. cachedUser: this.profile
  193. }
  194. })
  195. },
  196. handleFollow(index) {
  197. event.currentTarget.blur();
  198. this.followStateIndex = index;
  199. this.isUpdatingFollowState = true;
  200. let account = this.likes[index];
  201. axios.post('/api/v1/accounts/' + account.id + '/follow')
  202. .then(res => {
  203. this.likes[index].follows = true;
  204. this.followStateIndex = undefined;
  205. this.isUpdatingFollowState = false;
  206. });
  207. },
  208. handleUnfollow(index) {
  209. event.currentTarget.blur();
  210. this.followStateIndex = index;
  211. this.isUpdatingFollowState = true;
  212. let account = this.likes[index];
  213. axios.post('/api/v1/accounts/' + account.id + '/unfollow')
  214. .then(res => {
  215. this.likes[index].follows = false;
  216. this.followStateIndex = undefined;
  217. this.isUpdatingFollowState = false;
  218. });
  219. }
  220. }
  221. }
  222. </script>