refactor(federation): extract normalizeIconForWire helper
This commit is contained in:
@@ -44,6 +44,7 @@ import {
|
|||||||
isFederationRelayEnabled,
|
isFederationRelayEnabled,
|
||||||
computeFederatedId,
|
computeFederatedId,
|
||||||
sendTypingRelay,
|
sendTypingRelay,
|
||||||
|
normalizeIconForWire,
|
||||||
} from '../utils/federationOutbox.js';
|
} from '../utils/federationOutbox.js';
|
||||||
import { getOurOrigin } from '../utils/federationAuth.js';
|
import { getOurOrigin } from '../utils/federationAuth.js';
|
||||||
import type { FederationRelayEvent } from '@backspace/shared';
|
import type { FederationRelayEvent } from '@backspace/shared';
|
||||||
@@ -1301,11 +1302,7 @@ export async function dmRoutes(app: FastifyInstance): Promise<void> {
|
|||||||
// relay) stay correct without touching this site.
|
// relay) stay correct without touching this site.
|
||||||
const channelRow = db.select().from(schema.dmChannels)
|
const channelRow = db.select().from(schema.dmChannels)
|
||||||
.where(eq(schema.dmChannels.id, dmChannelId)).get();
|
.where(eq(schema.dmChannels.id, dmChannelId)).get();
|
||||||
const wireIcon = channelRow?.icon
|
const wireIcon = normalizeIconForWire(channelRow?.icon ?? null, domainOrigin);
|
||||||
? (channelRow.icon.startsWith('http://') || channelRow.icon.startsWith('https://')
|
|
||||||
? channelRow.icon
|
|
||||||
: `${domainOrigin}/api/uploads/${channelRow.icon}`)
|
|
||||||
: null;
|
|
||||||
|
|
||||||
for (const targetUser of targetUsers) {
|
for (const targetUser of targetUsers) {
|
||||||
if (!targetUser.homeInstance || targetUser.homeInstance === domainOrigin) continue;
|
if (!targetUser.homeInstance || targetUser.homeInstance === domainOrigin) continue;
|
||||||
@@ -1864,11 +1861,7 @@ export async function dmRoutes(app: FastifyInstance): Promise<void> {
|
|||||||
// patched between the start of this handler and now.
|
// patched between the start of this handler and now.
|
||||||
const channelRow = db.select().from(schema.dmChannels)
|
const channelRow = db.select().from(schema.dmChannels)
|
||||||
.where(eq(schema.dmChannels.id, id)).get();
|
.where(eq(schema.dmChannels.id, id)).get();
|
||||||
const wireIcon = channelRow?.icon
|
const wireIcon = normalizeIconForWire(channelRow?.icon ?? null, domainOrigin);
|
||||||
? (channelRow.icon.startsWith('http://') || channelRow.icon.startsWith('https://')
|
|
||||||
? channelRow.icon
|
|
||||||
: `${domainOrigin}/api/uploads/${channelRow.icon}`)
|
|
||||||
: null;
|
|
||||||
|
|
||||||
const memberAddPayload: FederationRelayEvent = {
|
const memberAddPayload: FederationRelayEvent = {
|
||||||
eventType: 'member_add',
|
eventType: 'member_add',
|
||||||
|
|||||||
@@ -47,6 +47,23 @@ function fetchSettings(): CachedSettings {
|
|||||||
|
|
||||||
// ─── Public API ──────────────────────────────────────────────────────────────
|
// ─── Public API ──────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Normalize a stored icon value (bare filename, absolute http(s) URL, or null)
|
||||||
|
* to an absolute URL suitable for federation wire payloads. Bare filenames are
|
||||||
|
* resolved against `ourOrigin`; absolute URLs pass through unchanged; null
|
||||||
|
* short-circuits.
|
||||||
|
*
|
||||||
|
* @param icon Either a bare filename (e.g. "1234567890.png"), an
|
||||||
|
* already-absolute http(s) URL, or null.
|
||||||
|
* @param ourOrigin Owner-instance origin (e.g. "https://nova.ddns.net").
|
||||||
|
* @returns Absolute URL or null.
|
||||||
|
*/
|
||||||
|
export function normalizeIconForWire(icon: string | null, ourOrigin: string): string | null {
|
||||||
|
if (!icon) return null;
|
||||||
|
if (icon.startsWith('http://') || icon.startsWith('https://')) return icon;
|
||||||
|
return `${ourOrigin}/api/uploads/${icon}`;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the federation relay feature is enabled in instance settings.
|
* Returns true if the federation relay feature is enabled in instance settings.
|
||||||
* Result is cached for 30 seconds to avoid repeated DB reads.
|
* Result is cached for 30 seconds to avoid repeated DB reads.
|
||||||
@@ -884,9 +901,7 @@ export function queueGroupMetadataRelay(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const ourOrigin = getOurOrigin();
|
const ourOrigin = getOurOrigin();
|
||||||
const wireIcon = payload.icon
|
const wireIcon = normalizeIconForWire(payload.icon, ourOrigin);
|
||||||
? ((payload.icon.startsWith('http://') || payload.icon.startsWith('https://')) ? payload.icon : `${ourOrigin}/api/uploads/${payload.icon}`)
|
|
||||||
: null;
|
|
||||||
|
|
||||||
// Resolve actor's full FederationRelayParticipant. Mirrors the participant
|
// Resolve actor's full FederationRelayParticipant. Mirrors the participant
|
||||||
// construction in getDmParticipants() / buildRelayPayload() so receivers
|
// construction in getDmParticipants() / buildRelayPayload() so receivers
|
||||||
|
|||||||
Reference in New Issue
Block a user