logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://hacktivis.me/git/pleroma-fe.git

user_profile.js (6150B)


  1. import get from 'lodash/get'
  2. import UserCard from '../user_card/user_card.vue'
  3. import FollowCard from '../follow_card/follow_card.vue'
  4. import Timeline from '../timeline/timeline.vue'
  5. import Conversation from '../conversation/conversation.vue'
  6. import TabSwitcher from 'src/components/tab_switcher/tab_switcher.jsx'
  7. import RichContent from 'src/components/rich_content/rich_content.jsx'
  8. import List from '../list/list.vue'
  9. import withLoadMore from '../../hocs/with_load_more/with_load_more'
  10. import localeService from 'src/services/locale/locale.service.js'
  11. import { library } from '@fortawesome/fontawesome-svg-core'
  12. import {
  13. faCircleNotch,
  14. faBirthdayCake
  15. } from '@fortawesome/free-solid-svg-icons'
  16. library.add(
  17. faCircleNotch,
  18. faBirthdayCake
  19. )
  20. const FollowerList = withLoadMore({
  21. fetch: (props, $store) => $store.dispatch('fetchFollowers', props.userId),
  22. select: (props, $store) => get($store.getters.findUser(props.userId), 'followerIds', []).map(id => $store.getters.findUser(id)),
  23. destroy: (props, $store) => $store.dispatch('clearFollowers', props.userId),
  24. childPropName: 'items',
  25. additionalPropNames: ['userId']
  26. })(List)
  27. const FriendList = withLoadMore({
  28. fetch: (props, $store) => $store.dispatch('fetchFriends', props.userId),
  29. select: (props, $store) => get($store.getters.findUser(props.userId), 'friendIds', []).map(id => $store.getters.findUser(id)),
  30. destroy: (props, $store) => $store.dispatch('clearFriends', props.userId),
  31. childPropName: 'items',
  32. additionalPropNames: ['userId']
  33. })(List)
  34. const defaultTabKey = 'statuses'
  35. const UserProfile = {
  36. data () {
  37. return {
  38. error: false,
  39. userId: null,
  40. tab: defaultTabKey,
  41. footerRef: null
  42. }
  43. },
  44. created () {
  45. const routeParams = this.$route.params
  46. this.load({ name: routeParams.name, id: routeParams.id })
  47. this.tab = get(this.$route, 'query.tab', defaultTabKey)
  48. },
  49. unmounted () {
  50. this.stopFetching()
  51. },
  52. computed: {
  53. timeline () {
  54. return this.$store.state.statuses.timelines.user
  55. },
  56. favorites () {
  57. return this.$store.state.statuses.timelines.favorites
  58. },
  59. media () {
  60. return this.$store.state.statuses.timelines.media
  61. },
  62. isUs () {
  63. return this.userId && this.$store.state.users.currentUser.id &&
  64. this.userId === this.$store.state.users.currentUser.id
  65. },
  66. user () {
  67. return this.$store.getters.findUser(this.userId)
  68. },
  69. isExternal () {
  70. return this.$route.name === 'external-user-profile'
  71. },
  72. followsTabVisible () {
  73. return this.isUs || !this.user.hide_follows
  74. },
  75. followersTabVisible () {
  76. return this.isUs || !this.user.hide_followers
  77. },
  78. favoritesTabVisible () {
  79. return this.isUs || !this.user.hide_favorites
  80. },
  81. formattedBirthday () {
  82. const browserLocale = localeService.internalToBrowserLocale(this.$i18n.locale)
  83. return this.user.birthday && new Date(Date.parse(this.user.birthday)).toLocaleDateString(browserLocale, { timeZone: 'UTC', day: 'numeric', month: 'long', year: 'numeric' })
  84. }
  85. },
  86. methods: {
  87. setFooterRef (el) {
  88. this.footerRef = el
  89. },
  90. load (userNameOrId) {
  91. const startFetchingTimeline = (timeline, userId) => {
  92. // Clear timeline only if load another user's profile
  93. if (userId !== this.$store.state.statuses.timelines[timeline].userId) {
  94. this.$store.commit('clearTimeline', { timeline })
  95. }
  96. this.$store.dispatch('startFetchingTimeline', { timeline, userId })
  97. }
  98. const loadById = (userId) => {
  99. this.userId = userId
  100. startFetchingTimeline('user', userId)
  101. startFetchingTimeline('media', userId)
  102. if (this.isUs) {
  103. startFetchingTimeline('favorites')
  104. } else if (!this.user.hide_favorites) {
  105. startFetchingTimeline('favorites', userId)
  106. }
  107. // Fetch all pinned statuses immediately
  108. this.$store.dispatch('fetchPinnedStatuses', userId)
  109. }
  110. // Reset view
  111. this.userId = null
  112. this.error = false
  113. const maybeId = userNameOrId.id
  114. const maybeName = userNameOrId.name
  115. // Check if user data is already loaded in store
  116. const user = maybeId ? this.$store.getters.findUser(maybeId) : this.$store.getters.findUserByName(maybeName)
  117. if (user) {
  118. loadById(user.id)
  119. } else {
  120. (maybeId
  121. ? this.$store.dispatch('fetchUser', maybeId)
  122. : this.$store.dispatch('fetchUserByName', maybeName))
  123. .then(({ id }) => loadById(id))
  124. .catch((reason) => {
  125. const errorMessage = get(reason, 'error.error')
  126. if (errorMessage === 'No user with such user_id') { // Known error
  127. this.error = this.$t('user_profile.profile_does_not_exist')
  128. } else if (errorMessage) {
  129. this.error = errorMessage
  130. } else {
  131. this.error = this.$t('user_profile.profile_loading_error')
  132. }
  133. })
  134. }
  135. },
  136. stopFetching () {
  137. this.$store.dispatch('stopFetchingTimeline', 'user')
  138. this.$store.dispatch('stopFetchingTimeline', 'favorites')
  139. this.$store.dispatch('stopFetchingTimeline', 'media')
  140. },
  141. switchUser (userNameOrId) {
  142. this.stopFetching()
  143. this.load(userNameOrId)
  144. },
  145. onTabSwitch (tab) {
  146. this.tab = tab
  147. this.$router.replace({ query: { tab } })
  148. },
  149. linkClicked ({ target }) {
  150. if (target.tagName === 'SPAN') {
  151. target = target.parentNode
  152. }
  153. if (target.tagName === 'A') {
  154. window.open(target.href, '_blank')
  155. }
  156. }
  157. },
  158. watch: {
  159. '$route.params.id': function (newVal) {
  160. if (newVal) {
  161. this.switchUser({ id: newVal })
  162. }
  163. },
  164. '$route.params.name': function (newVal) {
  165. if (newVal) {
  166. this.switchUser({ name: newVal })
  167. }
  168. },
  169. '$route.query': function (newVal) {
  170. this.tab = newVal.tab || defaultTabKey
  171. }
  172. },
  173. components: {
  174. UserCard,
  175. Timeline,
  176. FollowerList,
  177. FriendList,
  178. FollowCard,
  179. TabSwitcher,
  180. Conversation,
  181. RichContent
  182. }
  183. }
  184. export default UserProfile