store friends/followers in the global user repository

This commit is contained in:
taehoon 2019-04-10 13:49:39 -04:00
parent 936eb23bdf
commit e7010d73ac
3 changed files with 30 additions and 47 deletions

View file

@ -9,8 +9,8 @@ import withList from '../../hocs/with_list/with_list'
const FollowerList = compose( const FollowerList = compose(
withLoadMore({ withLoadMore({
fetch: (props, $store) => $store.dispatch('addFollowers', props.userId), fetch: (props, $store) => $store.dispatch('fetchFollowers', props.userId),
select: (props, $store) => get($store.getters.findUser(props.userId), 'followers', []), select: (props, $store) => get($store.getters.findUser(props.userId), 'followerIds', []).map(id => $store.getters.findUser(id)),
destory: (props, $store) => $store.dispatch('clearFollowers', props.userId), destory: (props, $store) => $store.dispatch('clearFollowers', props.userId),
childPropName: 'entries', childPropName: 'entries',
additionalPropNames: ['userId'] additionalPropNames: ['userId']
@ -20,8 +20,8 @@ const FollowerList = compose(
const FriendList = compose( const FriendList = compose(
withLoadMore({ withLoadMore({
fetch: (props, $store) => $store.dispatch('addFriends', props.userId), fetch: (props, $store) => $store.dispatch('fetchFriends', props.userId),
select: (props, $store) => get($store.getters.findUser(props.userId), 'friends', []), select: (props, $store) => get($store.getters.findUser(props.userId), 'friendIds', []).map(id => $store.getters.findUser(id)),
destory: (props, $store) => $store.dispatch('clearFriends', props.userId), destory: (props, $store) => $store.dispatch('clearFriends', props.userId),
childPropName: 'entries', childPropName: 'entries',
additionalPropNames: ['userId'] additionalPropNames: ['userId']

View file

@ -1,5 +1,5 @@
import backendInteractorService from '../services/backend_interactor_service/backend_interactor_service.js' import backendInteractorService from '../services/backend_interactor_service/backend_interactor_service.js'
import { compact, map, each, merge, find, last } from 'lodash' import { compact, map, each, merge, last, concat, uniq } from 'lodash'
import { set } from 'vue' import { set } from 'vue'
import { registerPushNotifications, unregisterPushNotifications } from '../services/push/push.js' import { registerPushNotifications, unregisterPushNotifications } from '../services/push/push.js'
import oauthApi from '../services/new_api/oauth' import oauthApi from '../services/new_api/oauth'
@ -73,42 +73,27 @@ export const mutations = {
endLogin (state) { endLogin (state) {
state.loggingIn = false state.loggingIn = false
}, },
// TODO Clean after ourselves? saveFriendIds (state, { id, friendIds }) {
addFriends (state, { id, friends }) {
const user = state.usersObject[id] const user = state.usersObject[id]
each(friends, friend => { user.friendIds = uniq(concat(user.friendIds, friendIds))
if (!find(user.friends, { id: friend.id })) {
user.friends.push(friend)
}
})
user.lastFriendId = last(user.friends).id
}, },
addFollowers (state, { id, followers }) { saveFollowerIds (state, { id, followerIds }) {
const user = state.usersObject[id] const user = state.usersObject[id]
each(followers, follower => { user.followerIds = uniq(concat(user.followerIds, followerIds))
if (!find(user.followers, { id: follower.id })) {
user.followers.push(follower)
}
})
user.lastFollowerId = last(user.followers).id
}, },
// Because frontend doesn't have a reason to keep these stuff in memory // Because frontend doesn't have a reason to keep these stuff in memory
// outside of viewing someones user profile. // outside of viewing someones user profile.
clearFriends (state, userId) { clearFriends (state, userId) {
const user = state.usersObject[userId] const user = state.usersObject[userId]
if (!user) { if (user) {
return set(user, 'friendIds', [])
} }
user.friends = []
user.lastFriendId = null
}, },
clearFollowers (state, userId) { clearFollowers (state, userId) {
const user = state.usersObject[userId] const user = state.usersObject[userId]
if (!user) { if (user) {
return set(user, 'followerIds', [])
} }
user.followers = []
user.lastFollowerId = null
}, },
addNewUsers (state, users) { addNewUsers (state, users) {
each(users, (user) => mergeOrAdd(state.users, state.usersObject, user)) each(users, (user) => mergeOrAdd(state.users, state.usersObject, user))
@ -240,25 +225,23 @@ const users = {
return store.rootState.api.backendInteractor.unmuteUser(id) return store.rootState.api.backendInteractor.unmuteUser(id)
.then((relationship) => store.commit('updateUserRelationship', [relationship])) .then((relationship) => store.commit('updateUserRelationship', [relationship]))
}, },
addFriends ({ rootState, commit }, fetchBy) { fetchFriends ({ rootState, commit }, id) {
return new Promise((resolve, reject) => { const user = rootState.users.usersObject[id]
const user = rootState.users.usersObject[fetchBy] const maxId = last(user.friendIds)
const maxId = user.lastFriendId return rootState.api.backendInteractor.fetchFriends({ id, maxId })
rootState.api.backendInteractor.fetchFriends({ id: user.id, maxId }) .then((friends) => {
.then((friends) => { commit('addNewUsers', friends)
commit('addFriends', { id: user.id, friends }) commit('saveFriendIds', { id, friendIds: map(friends, 'id') })
resolve(friends) return friends
}).catch(() => { })
reject()
})
})
}, },
addFollowers ({ rootState, commit }, fetchBy) { fetchFollowers ({ rootState, commit }, id) {
const user = rootState.users.usersObject[fetchBy] const user = rootState.users.usersObject[id]
const maxId = user.lastFollowerId const maxId = last(user.followerIds)
return rootState.api.backendInteractor.fetchFollowers({ id: user.id, maxId }) return rootState.api.backendInteractor.fetchFollowers({ id, maxId })
.then((followers) => { .then((followers) => {
commit('addFollowers', { id: user.id, followers }) commit('addNewUsers', followers)
commit('saveFollowerIds', { id, followerIds: map(followers, 'id') })
return followers return followers
}) })
}, },

View file

@ -129,8 +129,8 @@ export const parseUser = (data) => {
output.locked = data.locked output.locked = data.locked
output.followers_count = data.followers_count output.followers_count = data.followers_count
output.statuses_count = data.statuses_count output.statuses_count = data.statuses_count
output.friends = [] output.friendIds = []
output.followers = [] output.followerIds = []
if (data.pleroma) { if (data.pleroma) {
output.follow_request_count = data.pleroma.follow_request_count output.follow_request_count = data.pleroma.follow_request_count
} }