final fixes for user lists

This commit is contained in:
Henry Jameson 2026-08-12 16:31:28 +03:00
commit e33a8aba6d
2 changed files with 33 additions and 15 deletions

View file

@ -79,11 +79,11 @@ const UserProfile = {
return useMergedConfigStore().mergedConfig.compactProfiles return useMergedConfigStore().mergedConfig.compactProfiles
}, },
friends() { friends() {
return [...this.user.friendIds.keys()] return [...useUsersStore().relationshipsLists.friends.get(this.user).keys()]
.map((id) => useUsersStore().findUser(id)) .map((id) => useUsersStore().findUser(id))
}, },
followers() { followers() {
return [...this.user.followerIds.keys()] return [...useUsersStore().relationshipsLists.followers.get(this.user).keys()]
.map((id) => useUsersStore().findUser(id)) .map((id) => useUsersStore().findUser(id))
}, },
}, },

View file

@ -71,6 +71,10 @@ export const useUsersStore = defineStore('users', {
usersByName: new Map(), usersByName: new Map(),
usersByURL: new Map(), usersByURL: new Map(),
relationships: new Map(), relationships: new Map(),
relationshipsLists: {
friends: new WeakMap(),
followers: new WeakMap(),
},
timestamps: new WeakMap(), timestamps: new WeakMap(),
fetchesIds: new Map(), fetchesIds: new Map(),
fetchesNames: new Map(), fetchesNames: new Map(),
@ -123,11 +127,13 @@ export const useUsersStore = defineStore('users', {
}, },
saveFriendIds(id, friendIds) { saveFriendIds(id, friendIds) {
const user = this.users.get(id) const user = this.users.get(id)
user.friendIds = new Set([...user.friendIds, ...friendIds]) const list = this.relationshipsLists.friends.get(user)
friendIds.forEach((id) => list.add(id))
}, },
saveFollowerIds(id, followerIds) { saveFollowerIds(id, followerIds) {
const user = this.users.get(id) const user = this.users.get(id)
user.followerIds = new Set([...user.followerIds, ...followerIds]) const list = this.relationshipsLists.followers.get(user)
followersIds.forEach((id) => list.add(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.
@ -154,25 +160,37 @@ export const useUsersStore = defineStore('users', {
// implicit: if oldTimestamp is undefined this will still be false // implicit: if oldTimestamp is undefined this will still be false
if (oldTimestamp > timestamp) return existing // not overwriting old data with new if (oldTimestamp > timestamp) return existing // not overwriting old data with new
// TODO: Optimize
const { relationship: oldRelationship, ...oldUser } = existing const { relationship: oldRelationship, ...oldUser } = existing
const { relationship: newRelationship, ...neuUser } = user const { relationship: newRelationship, ...newUser } = user
const relationship = { ...oldRelationship, ...newRelationship } const relationship = { ...oldRelationship, ...newRelationship }
const newUser = { ...oldUser, ...neuUser, relationship }
this.users.set(user.id, newUser)
this.usersByName.set(user.screen_name.toLowerCase(), newUser)
this.usersByURL.set(user.url.toLowerCase(), newUser)
this.relationships.set(user.id, newRelationship) this.relationships.set(user.id, newRelationship)
existing.relationship = relationship
this.timestamps.set(newUser, timestamp) // Relying on object reactivity to avoid mutating the Map
Object.entries(newUser).forEach(([k, v]) => {
existing[k] = v
})
if (!this.users.has(user.id)) {
this.users.set(user.id, existing)
this.usersByName.set(user.screen_name.toLowerCase(), existing)
this.usersByURL.set(user.url.toLowerCase(), existing)
}
this.timestamps.set(existing, timestamp)
if (user.id === this.currentUser.id) { if (user.id === this.currentUser.id) {
this.currentUser = newUser this.currentUser = newUser
} }
return this.users.get(user.id) const result = this.users.get(user.id)
const { friends, followers } = this.relationshipsLists
if (!friends.has(result)) friends.set(result, new Set())
if (!followers.has(result)) followers.set(result, new Set())
return result
}) })
}, },
updateUserRelationship(relationships) { updateUserRelationship(relationships) {
@ -507,7 +525,7 @@ export const useUsersStore = defineStore('users', {
}, },
fetchFriends(id) { fetchFriends(id) {
const user = this.users.get(id) const user = this.users.get(id)
const maxId = last(user.friendIds) const maxId = last([...this.relationshipsLists.friends.get(user)])
return fetchFriends({ return fetchFriends({
id, id,
maxId, maxId,
@ -520,7 +538,7 @@ export const useUsersStore = defineStore('users', {
}, },
fetchFollowers(id) { fetchFollowers(id) {
const user = this.users.get(id) const user = this.users.get(id)
const maxId = last(user.followerIds) const maxId = last([...this.relationshipsLists.followers.get(user)])
return fetchFollowers({ return fetchFollowers({
id, id,
maxId, maxId,