feat: normalize remote asset URLs and route UI actions by instance origin
Phase 5 of multi-instance federation. Adds a two-layer fix: Layer 1 — Data ingestion normalization: Remote instance user avatars, server icons, and attachment filenames are rewritten to absolute URLs when entering the app (via WebSocket events or API responses), so all downstream components render them correctly without changes. Layer 2 — Outbound action routing: wsSend calls (voice join/leave/status, typing) and file uploads in UI components now route through the correct instance based on the active channel's origin.
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
import { getApiForOrigin } from '../stores/serverStore';
|
||||
|
||||
/**
|
||||
* Resolve a relative asset filename to an absolute URL for remote origins.
|
||||
* Home-origin filenames are returned as-is (components handle the /api/uploads/ prefix).
|
||||
* Already-absolute URLs (starting with 'http') pass through unchanged.
|
||||
*/
|
||||
export function resolveAssetUrl(filename: string | null | undefined, origin: string): typeof filename {
|
||||
if (!filename || !origin || filename.startsWith('http')) return filename;
|
||||
return getApiForOrigin(origin).uploads.url(filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rewrite the avatar field on a user-like object for remote origins.
|
||||
* Mutates in-place for efficiency (called on arrays of members/messages).
|
||||
*/
|
||||
export function normalizeUserAssets<T extends { avatar?: string | null }>(user: T, origin: string): T {
|
||||
if (origin && user.avatar) {
|
||||
user.avatar = resolveAssetUrl(user.avatar, origin) ?? user.avatar;
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rewrite user.avatar and attachment filenames on a message for remote origins.
|
||||
* Mutates in-place.
|
||||
*/
|
||||
export function normalizeMessageAssets<T extends { user: { avatar?: string | null }; attachments?: { filename: string }[] }>(
|
||||
message: T,
|
||||
origin: string,
|
||||
): T {
|
||||
if (!origin) return message;
|
||||
normalizeUserAssets(message.user, origin);
|
||||
if (message.attachments) {
|
||||
for (const att of message.attachments) {
|
||||
att.filename = resolveAssetUrl(att.filename, origin) ?? att.filename;
|
||||
}
|
||||
}
|
||||
return message;
|
||||
}
|
||||
Reference in New Issue
Block a user