feat(client-federation): user-view cache for cross-instance DM render
Fixes a render bug where a federated user (e.g. axel@nova) appeared with the federation globe icon and a broken avatar when viewed on his own home instance. Root cause: `populateFromReady` is first-wins by federatedId and discards the entire skipped DM payload — including its `members` array — so when a sibling instance's ready arrived first, the home instance's view of every shared user was dropped on the floor. Adds a render-only `userViews` cache that mirrors the `dmAlternatives` philosophy: information from skipped ready payloads is preserved for rendering. Every wire surface that delivers a User upserts into the cache regardless of dedup outcome; render sites read through a Zustand selector hook to surface the home view when one is loaded. The DM channel ingestion race is left untouched — the existing no-flapping invariant on origin reconnect is intentional and load-bearing for failover. Layered changes: - `identity.ts`: `normalizeOriginToHost`, `canonicalUserKey`, `isDeliveryFromHome`, `isFederationGlobeApplicable` — single helpers for origin/host normalization and the home/stub tier decision. - `spaceStore.ts`: `userViews` Map, `UserViewEntry` type, `upsertUserView` action with the home-wins preference rule, prune by `deliveredBy` in `removeInstanceSpaces` (mirrors `dmAlternatives` cleanup), `reset` clears. - `userViewLookup.ts`: `useCanonicalUserView` (Zustand selector hook for React) + `getCanonicalUserView` (sync getter for non-React paths). Render reactivity is structural via the selector, not coincidence on legacy update paths. - `populateFromReady` upsert pass runs BEFORE the federatedId dedup so members of skipped DMs still reach the cache. - WS handlers (dm_message_*, message_*, user_updated, member_joined, friend_request_*, dm_channel_created, dm_member_added) and REST hydrators (socialStore, discoverStore, mutuals) feed the cache with their delivering origin. - Render-site routing through `useCanonicalUserView` at every audited user-rendering site (sidebar, header, search, message bubble, reply chips, profile popout/modal, group settings, voice tiles, mention chips, member lists, friends, invites). Self-rendering sites compose alongside via existing `isSelf`/`resolveDisplayIdentity`. - Globe predicate hoisted to `isFederationGlobeApplicable` and applied at three sites, gating on `domain !== window.location.host` so we never show the globe for users whose home IS our own. Tests: 31 new unit tests across `identity`, `userViews` store, and `userViewLookup`. Full suite 276/276. Docs: `client-federation.md` §3 gains a "User View Cache" section parallel to "DM Origin Failover"; `dm-system.md` notes the new store action and WS handler upserts. Bug 3 (federation profile-sync gap — orbit's stale profile data on nova-Axel after a clear/color-change on nova never propagated) remains open. The user-view cache routes around it for the common case (home instance is connected), but the underlying S2S relay gap is its own diagnosis and follows in a separate branch.
This commit is contained in:
@@ -2,7 +2,7 @@ import { create } from 'zustand';
|
||||
import type { Space, Channel, ChannelCategory, MemberWithUser, SpaceWithChannelsAndMembers, Role, SpaceFolder, SpaceLayoutItem, DmChannel, User, UpdateSpaceRequest, CreateSpaceRequest } from '@backspace/shared';
|
||||
import { api, BackspaceApiClient } from '../api/client';
|
||||
import { resolveAssetUrl, normalizeUserAssets } from '../utils/assetUrls';
|
||||
import { isSelf } from '../utils/identity';
|
||||
import { isSelf, canonicalUserKey, isDeliveryFromHome } from '../utils/identity';
|
||||
import { sortDmChannels } from '../utils/dmSorting';
|
||||
import {
|
||||
getApiForOrigin,
|
||||
@@ -29,6 +29,31 @@ export class NotConnectedError extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
// ─── User-view cache types ────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* A single cached view of a user, populated from one delivering origin.
|
||||
*
|
||||
* The userViews cache stores the best-known view of each canonical identity
|
||||
* across every connected instance, regardless of whether the carrying channel
|
||||
* survived dedup. Mirrors `dmAlternatives` philosophy: information from
|
||||
* skipped ready payloads is still load-bearing for rendering.
|
||||
*
|
||||
* - `deliveredBy`: the origin string used at insert time. Required for
|
||||
* lifecycle pruning (drop entries whose delivering origin is removed from
|
||||
* Connections) — the user's declared `homeInstance` is NOT a substitute,
|
||||
* because a stub view delivered by orbit has homeInstance=nova.
|
||||
* - `isHome`: cached at insert time so the preference rule does not need to
|
||||
* re-normalize on every write.
|
||||
* - `updatedAt`: same-tier freshness tiebreaker.
|
||||
*/
|
||||
export interface UserViewEntry {
|
||||
user: User;
|
||||
deliveredBy: string;
|
||||
isHome: boolean;
|
||||
updatedAt: number;
|
||||
}
|
||||
|
||||
// ─── Store interface ──────────────────────────────────────────────────────────
|
||||
|
||||
interface SpaceState {
|
||||
@@ -50,6 +75,16 @@ interface SpaceState {
|
||||
categoryOriginMap: Map<string, string>; // categoryId → instance origin ('' = home)
|
||||
/** federatedId → (origin → localChannelId). Every DM from every origin's ready payload is recorded here regardless of dedup outcome, so failover can re-point to an alternate origin's local channel ID. */
|
||||
dmAlternatives: Map<string, Map<string, string>>;
|
||||
/**
|
||||
* canonicalUserKey → best-known view of that user. Populated from every wire
|
||||
* surface that delivers a User object (DM members, message authors, friends,
|
||||
* space members, profile updates). Pruned only on full instance removal
|
||||
* (`removeInstanceSpaces`) and `reset`, never on transient WS disconnect —
|
||||
* mirrors `dmAlternatives`' no-flapping invariant. Render sites read through
|
||||
* `getCanonicalUserView` / `useCanonicalUserView` to surface the home view
|
||||
* even when the carrying channel was deduped away.
|
||||
*/
|
||||
userViews: Map<string, UserViewEntry>;
|
||||
loadingSpaceId: string | null; // non-null while loadSpaceDetail is fetching
|
||||
_layoutUpdatedAt: number;
|
||||
setSpaces: (spaces: TaggedSpace[]) => void;
|
||||
@@ -90,6 +125,16 @@ interface SpaceState {
|
||||
setSpaceLayout: (layout: SpaceLayoutItem[] | null) => void;
|
||||
updateSpaceLayout: (items: SpaceLayoutItem[], folders: Record<string, { name: string | null; color: string | null; spaceIds: string[] }>) => Promise<void>;
|
||||
populateFromReady: (origin: string, spaces: SpaceWithChannelsAndMembers[], folders?: SpaceFolder[], dmChannels?: DmChannel[], spaceLayout?: SpaceLayoutItem[] | null, layoutUpdatedAt?: number) => void;
|
||||
/**
|
||||
* Upsert a User into the userViews cache under the preference rule:
|
||||
* - if no entry: insert
|
||||
* - if existing is home view and incoming is stub: ignore
|
||||
* - if existing is stub and incoming is home view: overwrite (upgrade)
|
||||
* - same tier (both home or both stub): freshness wins (incoming overwrites)
|
||||
* Origin is REQUIRED to derive the home/stub tier and to enable pruning by
|
||||
* delivering origin on instance removal.
|
||||
*/
|
||||
upsertUserView: (user: User, deliveringOrigin: string) => void;
|
||||
addSpaceFromReady: (origin: string, space: SpaceWithChannelsAndMembers) => void;
|
||||
removeInstanceSpaces: (origin: string) => void;
|
||||
transferOwnership: (spaceId: string, newOwnerId: string) => Promise<void>;
|
||||
@@ -142,6 +187,7 @@ export const useSpaceStore = create<SpaceState>((set, get) => ({
|
||||
voiceChannelIds: new Set(),
|
||||
categoryOriginMap: new Map(),
|
||||
dmAlternatives: new Map(),
|
||||
userViews: new Map(),
|
||||
loadingSpaceId: null,
|
||||
_layoutUpdatedAt: 0,
|
||||
|
||||
@@ -165,6 +211,7 @@ export const useSpaceStore = create<SpaceState>((set, get) => ({
|
||||
voiceChannelIds: new Set(),
|
||||
categoryOriginMap: new Map(),
|
||||
dmAlternatives: new Map(),
|
||||
userViews: new Map(),
|
||||
loadingSpaceId: null,
|
||||
_layoutUpdatedAt: 0,
|
||||
});
|
||||
@@ -189,6 +236,24 @@ export const useSpaceStore = create<SpaceState>((set, get) => ({
|
||||
};
|
||||
}),
|
||||
|
||||
upsertUserView: (user, deliveringOrigin) => set((state) => {
|
||||
const key = canonicalUserKey(user);
|
||||
const incomingIsHome = isDeliveryFromHome(user, deliveringOrigin);
|
||||
const existing = state.userViews.get(key);
|
||||
|
||||
// Stub view never overwrites a home view.
|
||||
if (existing && existing.isHome && !incomingIsHome) return state;
|
||||
|
||||
const next = new Map(state.userViews);
|
||||
next.set(key, {
|
||||
user,
|
||||
deliveredBy: deliveringOrigin,
|
||||
isHome: incomingIsHome,
|
||||
updatedAt: Date.now(),
|
||||
});
|
||||
return { userViews: next };
|
||||
}),
|
||||
|
||||
removeDmChannel: (id) => {
|
||||
set((state) => ({
|
||||
dmChannels: state.dmChannels.filter(c => c.id !== id)
|
||||
@@ -265,6 +330,11 @@ export const useSpaceStore = create<SpaceState>((set, get) => ({
|
||||
normalizeUserAssets(member.user, origin);
|
||||
}
|
||||
}
|
||||
// Upsert every member into the userViews cache (home or remote).
|
||||
// Assets are already normalized above for the remote case.
|
||||
for (const member of detail.members) {
|
||||
get().upsertUserView(member.user, origin);
|
||||
}
|
||||
|
||||
// Populate permission maps from REST response
|
||||
const spacePermissions = new Map(get().spacePermissions);
|
||||
@@ -662,6 +732,15 @@ export const useSpaceStore = create<SpaceState>((set, get) => ({
|
||||
categoryOriginMap.set(cat.id, origin);
|
||||
}
|
||||
}
|
||||
// Upsert every space member into the userViews cache. Assets for remote
|
||||
// origins were normalized by the ready handler in useWebSocket before
|
||||
// populateFromReady was called, so the user objects are already clean here.
|
||||
if (srv.members) {
|
||||
const { upsertUserView } = get();
|
||||
for (const member of srv.members) {
|
||||
upsertUserView(member.user, origin);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Accept DMs from all origins. Each instance serves its own DM data.
|
||||
@@ -676,6 +755,19 @@ export const useSpaceStore = create<SpaceState>((set, get) => ({
|
||||
}
|
||||
}
|
||||
|
||||
// Upsert every DM member from every origin into the userViews cache.
|
||||
// This runs unconditionally (home + remote) and BEFORE the dedup pass so
|
||||
// members of DMs that are about to be discarded still land in the cache.
|
||||
// Assets are already normalized above for the remote case.
|
||||
{
|
||||
const { upsertUserView } = get();
|
||||
for (const dm of incomingDms) {
|
||||
for (const member of dm.members) {
|
||||
upsertUserView(member, origin);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Build a set of existing federatedIds for dedup (only from OTHER origins —
|
||||
// DMs from the reconnecting origin will be replaced, not deduplicated)
|
||||
const existingFederatedIds = new Map<string, string>(); // federatedId → dmChannelId
|
||||
@@ -890,6 +982,16 @@ export const useSpaceStore = create<SpaceState>((set, get) => ({
|
||||
if (nextInner.size > 0) dmAlternatives.set(fid, nextInner);
|
||||
}
|
||||
|
||||
// Prune userViews: drop entries delivered by this origin. Symmetrical
|
||||
// with dmAlternatives — full removal evicts; transient disconnect leaves
|
||||
// the last-known view in place. If the surviving cache no longer holds
|
||||
// a home view for some user, render falls back to whatever the carrying
|
||||
// payload supplies (no crash; just degrades to stub view).
|
||||
const userViews = new Map<string, UserViewEntry>();
|
||||
for (const [key, entry] of state.userViews) {
|
||||
if (entry.deliveredBy !== origin) userViews.set(key, entry);
|
||||
}
|
||||
|
||||
return {
|
||||
spaces: remainingSpaces,
|
||||
channelToSpaceMap,
|
||||
@@ -898,6 +1000,7 @@ export const useSpaceStore = create<SpaceState>((set, get) => ({
|
||||
channelOriginMap,
|
||||
spacePermissions,
|
||||
dmAlternatives,
|
||||
userViews,
|
||||
currentSpaceId: remainingSpaces.find(s => s.id === state.currentSpaceId)
|
||||
? state.currentSpaceId
|
||||
: null,
|
||||
|
||||
Reference in New Issue
Block a user