fix(federation): persist remote instance_name from /peer/initiate handshake response
Mirrors the previous performHandshake fix for the admin-initiated path. /peer/initiate now parses the remote's instanceName from the /peer/accept response body and writes it alongside status='active'.
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
||||||
import Fastify, { type FastifyInstance } from 'fastify';
|
import Fastify, { type FastifyInstance } from 'fastify';
|
||||||
import Database from 'better-sqlite3';
|
import Database from 'better-sqlite3';
|
||||||
import { drizzle } from 'drizzle-orm/better-sqlite3';
|
import { drizzle } from 'drizzle-orm/better-sqlite3';
|
||||||
@@ -298,3 +298,60 @@ describe('POST /api/federation/peer/accept — response body carries instanceNam
|
|||||||
expect(body.instanceName).toBeNull();
|
expect(body.instanceName).toBeNull();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('POST /api/federation/peer/initiate — persists remote instanceName from handshake response', () => {
|
||||||
|
let app: FastifyInstance;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
sqlite = new Database(':memory:');
|
||||||
|
testDb = drizzle(sqlite, { schema });
|
||||||
|
applyMigrations(sqlite);
|
||||||
|
seedInstanceSettings('Local Backspace');
|
||||||
|
app = await buildApp();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
vi.unstubAllGlobals();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('writes remote.instanceName when remote /peer/accept succeeds', async () => {
|
||||||
|
vi.stubGlobal('fetch', vi.fn(async () =>
|
||||||
|
new Response(JSON.stringify({ accepted: true, instanceName: 'Remote Backspace' }), {
|
||||||
|
status: 200,
|
||||||
|
headers: { 'content-type': 'application/json' },
|
||||||
|
}),
|
||||||
|
));
|
||||||
|
|
||||||
|
const response = await app.inject({
|
||||||
|
method: 'POST',
|
||||||
|
url: '/api/federation/peer/initiate',
|
||||||
|
payload: { remoteOrigin: 'https://remote.example' },
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.statusCode).toBe(200);
|
||||||
|
const row = testDb.select().from(schema.federationPeers)
|
||||||
|
.where(eq(schema.federationPeers.origin, 'https://remote.example')).get();
|
||||||
|
expect(row?.status).toBe('active');
|
||||||
|
expect(row?.instanceName).toBe('Remote Backspace');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('writes null instanceName when remote response omits the field', async () => {
|
||||||
|
vi.stubGlobal('fetch', vi.fn(async () =>
|
||||||
|
new Response(JSON.stringify({ accepted: true }), {
|
||||||
|
status: 200,
|
||||||
|
headers: { 'content-type': 'application/json' },
|
||||||
|
}),
|
||||||
|
));
|
||||||
|
|
||||||
|
const response = await app.inject({
|
||||||
|
method: 'POST',
|
||||||
|
url: '/api/federation/peer/initiate',
|
||||||
|
payload: { remoteOrigin: 'https://remote.example' },
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.statusCode).toBe(200);
|
||||||
|
const row = testDb.select().from(schema.federationPeers)
|
||||||
|
.where(eq(schema.federationPeers.origin, 'https://remote.example')).get();
|
||||||
|
expect(row?.instanceName).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
@@ -351,9 +351,21 @@ export async function federationRoutes(app: FastifyInstance): Promise<void> {
|
|||||||
return reply.code(502).send({ error: errorMessage, statusCode: 502 });
|
return reply.code(502).send({ error: errorMessage, statusCode: 502 });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remote accepted — activate the peer
|
// Remote accepted — activate the peer. Parse the remote's instanceName
|
||||||
|
// from the response body so the federation panel renders a friendly
|
||||||
|
// label. Tolerate omission and non-JSON bodies.
|
||||||
|
let remoteInstanceName: string | null = null;
|
||||||
|
try {
|
||||||
|
const body = (await response.json()) as { instanceName?: string | null };
|
||||||
|
if (typeof body?.instanceName === 'string' && body.instanceName.length > 0) {
|
||||||
|
remoteInstanceName = body.instanceName;
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// Non-JSON body — leave null.
|
||||||
|
}
|
||||||
|
|
||||||
db.update(schema.federationPeers)
|
db.update(schema.federationPeers)
|
||||||
.set({ status: 'active', lastSeenAt: Date.now() })
|
.set({ status: 'active', lastSeenAt: Date.now(), instanceName: remoteInstanceName })
|
||||||
.where(eq(schema.federationPeers.id, peerId))
|
.where(eq(schema.federationPeers.id, peerId))
|
||||||
.run();
|
.run();
|
||||||
connectionManager.sendToAdmins({ type: 'federation_peers_changed' as const });
|
connectionManager.sendToAdmins({ type: 'federation_peers_changed' as const });
|
||||||
|
|||||||
Reference in New Issue
Block a user