ProfileCarousel.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <template>
  2. <div class="profile-carousel-component">
  3. <template v-if="showSplash">
  4. <SplashScreen />
  5. </template>
  6. <template v-else>
  7. <template v-if="emptyFeed">
  8. <div class="bg-dark d-flex justify-content-center align-items-center w-100 h-100">
  9. <div>
  10. <h2 class="text-light">Oops! This account hasn't posted yet or is private.</h2>
  11. <a href="/" class="font-weight-bold text-muted">Go back home</a>
  12. </div>
  13. </div>
  14. </template>
  15. <template v-else>
  16. <FullscreenCarousel
  17. :feed="feed"
  18. :withLinks="withLinks"
  19. :withOverlay="withOverlay"
  20. :autoPlay="autoPlay"
  21. :autoPlayInterval="autoPlayInterval"
  22. :canLoadMore="hasMoreData"
  23. @load-more="loadMoreData"
  24. />
  25. </template>
  26. </template>
  27. </div>
  28. </template>
  29. <script>
  30. import SplashScreen from './SplashScreen.vue';
  31. import FullscreenCarousel from './FullscreenCarousel.vue'
  32. export default {
  33. props: ['profile-id'],
  34. components: {
  35. SplashScreen,
  36. FullscreenCarousel
  37. },
  38. data() {
  39. return {
  40. showSplash: true,
  41. profile: {},
  42. feed: [],
  43. emptyFeed: false,
  44. hasMoreData: false,
  45. withLinks: true,
  46. withOverlay: true,
  47. autoPlay: false,
  48. autoPlayInterval: 5000,
  49. maxId: null
  50. }
  51. },
  52. mounted() {
  53. const url = new URL(window.location.href);
  54. const params = url.searchParams;
  55. if(params.has('linkless') == true) {
  56. this.withLinks = false;
  57. }
  58. if(params.has('clean') == true) {
  59. this.withOverlay = false;
  60. }
  61. if(params.has('interval') == true) {
  62. const val = parseInt(params.get('interval'));
  63. const valid = this.validateIntegerRange(val, { min: 1000, max: 30000 })
  64. if(valid) {
  65. this.autoPlayInterval = val;
  66. }
  67. }
  68. if(params.has('autoplay') == true) {
  69. this.autoPlay = true;
  70. }
  71. this.init();
  72. },
  73. methods: {
  74. async init() {
  75. await axios.get(`/api/pixelfed/v1/accounts/${this.profileId}/statuses?media_type=photo&limit=10`)
  76. .then(res => {
  77. if(!res || !res.data || !res.data.length) {
  78. this.emptyFeed = true;
  79. return;
  80. }
  81. this.maxId = this.arrayMinId(res.data);
  82. const posts = res.data.flatMap(post =>
  83. post.media_attachments.filter(media => {
  84. return ['image/jpeg','image/png', 'image/jpg', 'image/webp'].includes(media.mime)
  85. }).map(media => ({
  86. media_url: media.url,
  87. id: post.id,
  88. caption: post.content_text,
  89. created_at: post.created_at,
  90. url: post.url,
  91. account: {
  92. username: post.account.username,
  93. url: post.account.url
  94. }
  95. }))
  96. );
  97. this.feed = posts;
  98. this.hasMoreData = res.data.length === 10;
  99. setTimeout(() => {
  100. this.showSplash = false;
  101. }, 3000);
  102. })
  103. },
  104. async fetchMore() {
  105. await axios.get(`/api/pixelfed/v1/accounts/${this.profileId}/statuses?media_type=photo&limit=10&max_id=${this.maxId}`)
  106. .then(res => {
  107. this.maxId = this.arrayMinId(res.data);
  108. const posts = res.data.flatMap(post =>
  109. post.media_attachments.filter(media => {
  110. return ['image/jpeg','image/png', 'image/jpg', 'image/webp'].includes(media.mime)
  111. }).map(media => ({
  112. media_url: media.url,
  113. id: post.id,
  114. caption: post.content_text,
  115. created_at: post.created_at,
  116. url: post.url,
  117. account: {
  118. username: post.account.username,
  119. url: post.account.url
  120. }
  121. }))
  122. );
  123. this.feed.push(...posts);
  124. this.hasMoreData = res.data.length === 10;
  125. })
  126. },
  127. arrayMinId(arr) {
  128. if (arr.length === 0) return null;
  129. let smallest = BigInt(arr[0].id);
  130. for (let i = 1; i < arr.length; i++) {
  131. const current = BigInt(arr[i].id);
  132. if (current < smallest) {
  133. smallest = current;
  134. }
  135. }
  136. return smallest.toString();
  137. },
  138. loadMoreData() {
  139. this.fetchMore();
  140. },
  141. validateIntegerRange(value, options = {}) {
  142. if (typeof value !== 'number' || !Number.isInteger(value)) {
  143. return false;
  144. }
  145. const {
  146. min = Number.MIN_SAFE_INTEGER,
  147. max = Number.MAX_SAFE_INTEGER,
  148. inclusiveMin = true,
  149. inclusiveMax = true
  150. } = options;
  151. if (min !== undefined && !Number.isInteger(min)) {
  152. return false;
  153. }
  154. if (max !== undefined && !Number.isInteger(max)) {
  155. return false;
  156. }
  157. if (min > max) {
  158. return false;
  159. }
  160. const aboveMin = inclusiveMin ? value >= min : value > min;
  161. const belowMax = inclusiveMax ? value <= max : value < max;
  162. return aboveMin && belowMax;
  163. }
  164. }
  165. }
  166. </script>
  167. <style type="text/css">
  168. .profile-carousel-component {
  169. display: block;
  170. width: 100dvw;
  171. height: 100dvh;
  172. z-index: 2;
  173. background: #000;
  174. }
  175. </style>