fix(dm): owner-only group DM ops accept federated target identification

Transferring ownership or kicking a member surfaced "Target user is not a
member of this DM channel" whenever the target was a federated user.

Root cause: the client passed `canonical.id` from `useCanonicalUserView`,
which returns the user's HOME id when the home view is in the userViews
cache. After owner-routing the request to the owner instance, that
instance's `dm_members.userId` (its own local replicated id) never
matched the home id, so `isDmMember` returned false. The same failure
mode applied across any cross-instance scenario where the
channel-serving instance and the owner-serving instance disagree on the
local replicated user id for the same federated user.

Fix: both endpoints now accept federated identification, mirroring the
existing pattern on `POST /api/dm/:id/members`:

- `POST /api/dm/:id/transfer` body: `{ newOwnerId? } | { homeUserId, homeInstance }`.
  Federated args win when both are supplied (strictly more specific).
- `DELETE /api/dm/:id/members/:targetUserId` reads optional
  `?homeInstance=<origin>` query; when present, the URL segment is
  treated as a homeUserId and resolved via `resolveOrCreateReplicatedUser`.

Client `api.dm.kickMember` and `api.dm.transferOwnership` gain an
optional `federated` argument; `DmRosterPanel` and `MobileGroupDmInfo`
pass it whenever the target has `homeUserId` + `homeInstance` populated.

Adds 5 server tests (3 transfer + 2 kick) covering federated targets,
the federated-wins-over-local precedence rule, and federated-non-member
rejection. Updates 2 client routing tests and 2 DmRosterPanel test
assertions for the new signature. Updates `docs/systems/dm-system.md`
and `docs/systems/api.md`.

Server: 965 tests pass (was 960). Web: 362 tests pass (was 360).
This commit is contained in:
Jannis Braun
2026-05-10 22:09:14 +02:00
parent 9279ac78e5
commit b6842c5590
11 changed files with 442 additions and 35 deletions
@@ -355,4 +355,56 @@ describe('DELETE /api/dm/:id/members/:targetUserId — owner kick', () => {
expect(res.statusCode).toBe(404);
expect(res.json().error).toMatch(/not found/i);
});
// Federated-identification path — mirrors POST /api/dm/:id/transfer. The
// client cannot reliably know the OWNER instance's local user id for a
// federated member (the home view surfaced through `useCanonicalUserView`
// carries the home id). The `?homeInstance=...` query string signals the
// path segment is a homeUserId; the server resolves via
// `resolveOrCreateReplicatedUser` before checking membership.
it('kick with federated identity (?homeInstance=...) → resolves to local replicated user and succeeds', async () => {
seedGroupDm({
id: 'dm-kick-fed-1',
ownerId: 'owner-A',
members: ['owner-A', 'member-B', 'remote-D'],
federatedId: 'fed-kick-fed-1',
});
const res = await app.inject({
method: 'DELETE',
url: '/api/dm/dm-kick-fed-1/members/remote-dan?homeInstance=' + encodeURIComponent('https://remote.test'),
});
expect(res.statusCode).toBe(200);
// Federated member's local replicated row is gone from this channel
const remaining = testDb.select().from(schema.dmMembers)
.where(eq(schema.dmMembers.dmChannelId, 'dm-kick-fed-1'))
.all();
expect(remaining.map((m) => m.userId).sort()).toEqual(['member-B', 'owner-A']);
// Outbox event carries the federated user's home identity with reason=kick
const outboxRows = testDb.select().from(schema.federationOutbox).all();
const removeRows = outboxRows.filter((r) => r.eventType === 'member_remove');
expect(removeRows.length).toBe(1);
const wire = JSON.parse(removeRows[0]!.payload);
expect(wire.membership.reason).toBe('kick');
expect(wire.membership.user.homeUserId).toBe('remote-dan');
expect(wire.membership.user.homeInstance).toBe('https://remote.test');
});
// Negative case: federated identity for a user who is NOT a member.
it('kick with federated identity for non-member → 404', async () => {
seedGroupDm({
id: 'dm-kick-fed-2',
ownerId: 'owner-A',
members: ['owner-A', 'member-B'], // remote-D is NOT a member here
});
const res = await app.inject({
method: 'DELETE',
url: '/api/dm/dm-kick-fed-2/members/remote-dan?homeInstance=' + encodeURIComponent('https://remote.test'),
});
expect(res.statusCode).toBe(404);
expect(res.json().error).toMatch(/not a member/i);
});
});