pleroma-fe/src/components/user_profile/user_profile.js

203 lines
5.8 KiB
JavaScript
Raw Normal View History

2019-02-26 12:26:04 -05:00
import get from 'lodash/get'
2026-01-08 17:26:52 +02:00
import RichContent from 'src/components/rich_content/rich_content.jsx'
2026-01-06 16:23:17 +02:00
import TabSwitcher from 'src/components/tab_switcher/tab_switcher.jsx'
import { useInstanceStore } from 'src/stores/instance.js'
2019-02-25 04:51:23 -05:00
import withLoadMore from '../../hocs/with_load_more/with_load_more'
2026-01-06 16:23:17 +02:00
import Conversation from '../conversation/conversation.vue'
import FollowCard from '../follow_card/follow_card.vue'
import List from '../list/list.vue'
import Timeline from '../timeline/timeline.vue'
import UserCard from '../user_card/user_card.vue'
2020-10-21 00:01:28 +03:00
2026-01-08 17:26:52 +02:00
import { library } from '@fortawesome/fontawesome-svg-core'
import { faCircleNotch } from '@fortawesome/free-solid-svg-icons'
2026-01-06 16:22:52 +02:00
library.add(faCircleNotch)
2019-02-25 04:51:23 -05:00
2019-04-03 22:28:08 -04:00
const FollowerList = withLoadMore({
fetch: (props, $store) => $store.dispatch('fetchFollowers', props.userId),
2026-01-06 16:22:52 +02:00
select: (props, $store) =>
get($store.getters.findUser(props.userId), 'followerIds', []).map((id) =>
$store.getters.findUser(id),
),
2019-04-09 14:46:47 -04:00
destroy: (props, $store) => $store.dispatch('clearFollowers', props.userId),
2019-04-03 22:28:08 -04:00
childPropName: 'items',
2026-01-06 16:22:52 +02:00
additionalPropNames: ['userId'],
2019-04-03 22:28:08 -04:00
})(List)
2019-02-25 04:51:23 -05:00
2019-04-03 22:28:08 -04:00
const FriendList = withLoadMore({
fetch: (props, $store) => $store.dispatch('fetchFriends', props.userId),
2026-01-06 16:22:52 +02:00
select: (props, $store) =>
get($store.getters.findUser(props.userId), 'friendIds', []).map((id) =>
$store.getters.findUser(id),
),
2019-04-09 14:46:47 -04:00
destroy: (props, $store) => $store.dispatch('clearFriends', props.userId),
2019-04-03 22:28:08 -04:00
childPropName: 'items',
2026-01-06 16:22:52 +02:00
additionalPropNames: ['userId'],
2019-04-03 22:28:08 -04:00
})(List)
2016-11-30 23:32:22 +01:00
const defaultTabKey = 'statuses'
2016-11-30 23:32:22 +01:00
const UserProfile = {
2026-01-06 16:22:52 +02:00
data() {
return {
error: false,
userId: null,
tab: defaultTabKey,
2026-01-06 16:22:52 +02:00
footerRef: null,
}
},
2026-01-06 16:22:52 +02:00
created() {
2019-04-15 11:08:28 -04:00
const routeParams = this.$route.params
this.load({ name: routeParams.name, id: routeParams.id })
this.tab = get(this.$route, 'query.tab', defaultTabKey)
2017-06-12 16:00:46 +02:00
},
2026-01-06 16:22:52 +02:00
unmounted() {
this.stopFetching()
},
2016-11-30 23:32:22 +01:00
computed: {
2026-01-06 16:22:52 +02:00
timeline() {
2018-12-15 06:16:44 +03:00
return this.$store.state.statuses.timelines.user
},
2026-01-06 16:22:52 +02:00
favorites() {
return this.$store.state.statuses.timelines.favorites
},
2026-01-06 16:22:52 +02:00
media() {
2019-01-24 10:48:40 +00:00
return this.$store.state.statuses.timelines.media
},
2026-01-06 16:22:52 +02:00
isUs() {
return (
this.userId &&
this.$store.state.users.currentUser.id &&
this.userId === this.$store.state.users.currentUser.id
2026-01-06 16:22:52 +02:00
)
2019-01-17 22:11:51 +03:00
},
2026-01-06 16:22:52 +02:00
user() {
2019-04-15 11:08:28 -04:00
return this.$store.getters.findUser(this.userId)
2018-12-15 06:16:44 +03:00
},
2026-01-06 16:22:52 +02:00
isExternal() {
2018-12-15 06:16:44 +03:00
return this.$route.name === 'external-user-profile'
2019-02-03 12:52:04 -05:00
},
2026-01-06 16:22:52 +02:00
followsTabVisible() {
2019-02-07 10:53:36 -05:00
return this.isUs || !this.user.hide_follows
2019-02-03 12:52:04 -05:00
},
2026-01-06 16:22:52 +02:00
followersTabVisible() {
2019-02-03 12:52:04 -05:00
return this.isUs || !this.user.hide_followers
},
2026-01-06 16:22:52 +02:00
favoritesTabVisible() {
return (
this.isUs ||
(useInstanceStore().pleromaPublicFavouritesAvailable &&
2026-01-06 16:22:52 +02:00
!this.user.hide_favorites)
)
},
2016-11-30 23:32:22 +01:00
},
methods: {
2026-01-06 16:22:52 +02:00
setFooterRef(el) {
this.footerRef = el
},
2026-01-06 16:22:52 +02:00
load(userNameOrId) {
const startFetchingTimeline = (timeline, userId) => {
// Clear timeline only if load another user's profile
if (userId !== this.$store.state.statuses.timelines[timeline].userId) {
this.$store.commit('clearTimeline', { timeline })
}
this.$store.dispatch('startFetchingTimeline', { timeline, userId })
}
const loadById = (userId) => {
this.userId = userId
startFetchingTimeline('user', userId)
startFetchingTimeline('media', userId)
if (this.isUs) {
startFetchingTimeline('favorites')
} else if (!this.user.hide_favorites) {
startFetchingTimeline('favorites', userId)
}
// Fetch all pinned statuses immediately
this.$store.dispatch('fetchPinnedStatuses', userId)
}
// Reset view
this.userId = null
this.error = false
const maybeId = userNameOrId.id
const maybeName = userNameOrId.name
2019-04-15 11:08:28 -04:00
// Check if user data is already loaded in store
2026-01-06 16:22:52 +02:00
const user = maybeId
? this.$store.getters.findUser(maybeId)
: this.$store.getters.findUserByName(maybeName)
2019-04-15 11:08:28 -04:00
if (user) {
loadById(user.id)
} else {
2026-01-06 16:22:52 +02:00
;(maybeId
? this.$store.dispatch('fetchUser', maybeId)
2026-01-06 16:22:52 +02:00
: this.$store.dispatch('fetchUserByName', maybeName)
)
.then(({ id }) => loadById(id))
2019-04-15 11:08:28 -04:00
.catch((reason) => {
const errorMessage = get(reason, 'error.error')
2026-01-06 16:22:52 +02:00
if (errorMessage === 'No user with such user_id') {
// Known error
2019-04-15 11:08:28 -04:00
this.error = this.$t('user_profile.profile_does_not_exist')
} else if (errorMessage) {
this.error = errorMessage
} else {
this.error = this.$t('user_profile.profile_loading_error')
}
})
}
},
2026-01-06 16:22:52 +02:00
stopFetching() {
this.$store.dispatch('stopFetchingTimeline', 'user')
this.$store.dispatch('stopFetchingTimeline', 'favorites')
this.$store.dispatch('stopFetchingTimeline', 'media')
},
2026-01-06 16:22:52 +02:00
switchUser(userNameOrId) {
this.stopFetching()
this.load(userNameOrId)
},
2026-01-06 16:22:52 +02:00
onTabSwitch(tab) {
this.tab = tab
this.$router.replace({ query: { tab } })
2019-11-16 03:12:16 +09:00
},
2026-01-06 16:22:52 +02:00
linkClicked({ target }) {
2019-11-16 03:12:16 +09:00
if (target.tagName === 'SPAN') {
target = target.parentNode
}
if (target.tagName === 'A') {
window.open(target.href, '_blank')
}
2026-01-06 16:22:52 +02:00
},
},
watch: {
2019-04-15 11:08:28 -04:00
'$route.params.id': function (newVal) {
if (newVal) {
this.switchUser({ id: newVal })
}
2019-03-03 13:38:48 -05:00
},
2019-04-15 11:08:28 -04:00
'$route.params.name': function (newVal) {
if (newVal) {
this.switchUser({ name: newVal })
}
},
'$route.query': function (newVal) {
this.tab = newVal.tab || defaultTabKey
2026-01-06 16:22:52 +02:00
},
},
2016-11-30 23:32:22 +01:00
components: {
2019-03-05 14:01:49 -05:00
UserCard,
Timeline,
2019-02-25 04:51:23 -05:00
FollowerList,
2019-02-18 17:49:32 +03:00
FriendList,
FollowCard,
TabSwitcher,
Conversation,
2026-01-06 16:22:52 +02:00
RichContent,
},
2016-11-30 23:32:22 +01:00
}
export default UserProfile