feat(federation): resolveDmChannelId for alternate-origin DM ids

Resolves any raw DM channel ID (primary or alternate-origin local ID)
to its primary dmChannels entry via dmAlternatives federatedId lookup.
Returns null for unknown IDs. Used by the dm_message_created handler
in a later commit to prevent phantom sidebar entries from alternate-
origin deliveries (closes a pre-existing group-DM bug and supports
post-failover routing).
This commit is contained in:
Jannis Braun
2026-04-23 01:06:46 +02:00
parent d66932362a
commit 678790b88b
2 changed files with 84 additions and 0 deletions
+27
View File
@@ -924,6 +924,33 @@ export function getChannelOrigin(channelId: string): string {
return useSpaceStore.getState().channelOriginMap.get(channelId) ?? '';
}
/**
* Resolves a raw DM channel ID to its primary `dmChannels` entry ID.
*
* - If `rawId` is already a primary entry: returns `rawId` unchanged.
* - If `rawId` is recorded in `dmAlternatives` as an alternate-origin local ID
* for a DM whose primary is present in `dmChannels`: returns the primary's ID.
* - Otherwise: returns `null` (unknown ID — caller should no-op).
*
* Used by:
* - `dm_message_created` WS handler to route messages arriving from alternate
* origins to the primary entry (§3.11 of the failover spec).
* - Future DM WS handlers that need to dedup alternate-origin deliveries.
*/
export function resolveDmChannelId(rawId: string): string | null {
const { dmChannels, dmAlternatives } = useSpaceStore.getState();
if (dmChannels.some(dm => dm.id === rawId)) return rawId;
for (const [federatedId, byOrigin] of dmAlternatives) {
for (const localId of byOrigin.values()) {
if (localId !== rawId) continue;
const primary = dmChannels.find(dm => dm.federatedId === federatedId);
return primary ? primary.id : null;
}
}
return null;
}
// ─── API client resolution ────────────────────────────────────────────────────
// The actual resolver is registered by instanceStore on import, avoiding a
// circular dependency (instanceStore → useWebSocket → chatStore → spaceStore).