diff --git a/src/app/features/estate/joinEstateRoom.test.ts b/src/app/features/estate/joinEstateRoom.test.ts index bf96a4af..19020db2 100644 --- a/src/app/features/estate/joinEstateRoom.test.ts +++ b/src/app/features/estate/joinEstateRoom.test.ts @@ -51,12 +51,12 @@ describe('activateEstateRoom', () => { ); it('falls back to the error message, then to a generic one', async () => { - const named = recordingClient(vi.fn().mockRejectedValue(new Error('Boom'))); + const named = recordingClient(vi.fn().mockRejectedValue(Object.assign(new Error('Boom'), { httpStatus: 500 }))); expect(await activateEstateRoom(named.mx, room('joinable'))).toEqual({ kind: 'refused', message: 'Boom', }); - const bare = recordingClient(vi.fn().mockRejectedValue({})); + const bare = recordingClient(vi.fn().mockRejectedValue({ httpStatus: 500 })); expect(await activateEstateRoom(bare.mx, room('joinable'))).toEqual({ kind: 'refused', message: 'Could not join', @@ -65,6 +65,7 @@ describe('activateEstateRoom', () => { it('reports the server refusal and does not decide access itself', async () => { const refusal = Object.assign(new Error('M_FORBIDDEN'), { + httpStatus: 403, data: { error: 'You are not invited to this room.' }, }); const { mx, joinRoom } = recordingClient(vi.fn().mockRejectedValue(refusal)); @@ -75,7 +76,7 @@ describe('activateEstateRoom', () => { }); /** A client holding DM invites as the server sends them: the mark is in the invite event only. */ -const dmClient = (opts: { refuse?: boolean; invites: Record }) => { +const dmClient = (opts: { refuse?: boolean; drop?: boolean; invites: Record }) => { let direct: Record | undefined; const writes: Record[] = []; const joined: string[] = []; @@ -102,7 +103,8 @@ const dmClient = (opts: { refuse?: boolean; invites: Record }) = joinRoom: async (roomId: string) => { // the join lands only after m.direct has been written: record what it held joined.push(JSON.stringify(direct ?? {})); - if (opts.refuse) throw new Error('M_FORBIDDEN'); + if (opts.refuse) throw Object.assign(new Error('M_FORBIDDEN'), { httpStatus: 403 }); + if (opts.drop) throw new Error('fetch failed'); return { roomId }; }, }; @@ -135,6 +137,16 @@ describe('activateEstateRoom for a direct message invite', () => { }); }); +describe('activateEstateRoom when the join gets no answer', () => { + it('keeps the room listed and shows a retryable message', async () => { + const c = dmClient({ drop: true, invites: { '!dm1:test': '@friend:test' } }); + const result = await activateEstateRoom(c.mx, tap('!dm1:test')); + expect(result.kind).toBe('refused'); + expect((result as { message: string }).message).toMatch(/tap again to retry/); + expect(c.current()).toEqual({ '@friend:test': ['!dm1:test'] }); + }); +}); + describe('awaitMyJoin', () => { /** A store that gains the room when the test says so, emitting like the SDK. */ const store = () => { diff --git a/src/app/features/estate/joinEstateRoom.ts b/src/app/features/estate/joinEstateRoom.ts index 36a9166e..a3be428b 100644 --- a/src/app/features/estate/joinEstateRoom.ts +++ b/src/app/features/estate/joinEstateRoom.ts @@ -7,6 +7,9 @@ export type EstateActivation = | { kind: 'joined'; roomId: string } | { kind: 'refused'; message: string }; +const LOST_JOIN_RESPONSE = + 'The connection dropped before the server answered. The join may have gone through; tap again to retry.'; + type JoinClient = Parameters[0]; /** @@ -18,7 +21,10 @@ type JoinClient = Parameters[0]; * An invite marked is_direct carries the mark in the invite event the client * already holds, and the join event does not repeat it. So the room is listed * in m.direct under the inviter before the join is sent, and taken out again if - * the join is refused. Nothing needs to recognise the DM after it arrives. + * the join is refused by a server answer. A join that fails without one (lost + * response, dropped connection) may still have been accepted, so the entry + * stays and the tap is retryable. Nothing needs to recognise the DM after it + * arrives. The inbox Accept goes through here too: one owner for joining. */ export const activateEstateRoom = async ( mx: JoinClient, @@ -29,6 +35,7 @@ export const activateEstateRoom = async ( const err = e as { data?: { error?: string }; message?: string }; return { kind: 'refused', message: err.data?.error ?? err.message ?? 'Could not join' }; }; + const answered = (e: unknown) => typeof (e as { httpStatus?: unknown }).httpStatus === 'number'; const invite = room.state === 'invited' ? mx.getRoom(room.roomId) : null; const inviter = isDirectInvite(invite, mx.getSafeUserId()) ? invite?.getMember(mx.getSafeUserId())?.events?.member?.getSender() @@ -42,6 +49,7 @@ export const activateEstateRoom = async ( const joined = await mx.joinRoom(room.roomId, { viaServers: room.via }); return { kind: 'joined', roomId: joined.roomId }; } catch (e) { + if (!answered(e)) return { kind: 'refused', message: LOST_JOIN_RESPONSE }; if (inviter) await removeRoomIdFromMDirect(mx, room.roomId).catch(() => undefined); return refusal(e); } diff --git a/src/app/pages/client/inbox/Invites.tsx b/src/app/pages/client/inbox/Invites.tsx index 20518e56..39dfcc72 100644 --- a/src/app/pages/client/inbox/Invites.tsx +++ b/src/app/pages/client/inbox/Invites.tsx @@ -46,11 +46,10 @@ import { import { nameInitials } from '../../../utils/common'; import { RoomAvatar } from '../../../components/room-avatar'; import { - addRoomIdToMDirect, getMxIdLocalPart, - guessDmRoomUserId, rateLimitedActions, } from '../../../utils/matrix'; +import { activateEstateRoom } from '../../../features/estate/joinEstateRoom'; import { Time } from '../../../components/message'; import { useElementSizeObserver } from '../../../hooks/useElementSizeObserver'; import { onEnterOrSpace, stopPropagation } from '../../../utils/keyboard'; @@ -160,24 +159,20 @@ function InviteCard({ hideAvatar, }: InviteCardProps) { const mx = useMatrixClient(); - const userId = mx.getSafeUserId(); - const [viewTopic, setViewTopic] = useState(false); const closeTopic = () => setViewTopic(false); const openTopic = () => setViewTopic(true); const [joinState, join] = useAsyncCallback( useCallback(async () => { - const dmUserId = isDirectInvite(invite.room, userId) - ? guessDmRoomUserId(invite.room, userId) - : undefined; - - await mx.joinRoom(invite.roomId); - if (dmUserId) { - await addRoomIdToMDirect(mx, invite.roomId, dmUserId); - } + const result = await activateEstateRoom(mx, { + roomId: invite.roomId, + state: 'invited', + via: [], + }); + if (result.kind === 'refused') throw new Error(result.message); onNavigate(invite.roomId, invite.isSpace); - }, [mx, invite, userId, onNavigate]) + }, [mx, invite, onNavigate]) ); const [leaveState, leave] = useAsyncCallback, MatrixError, []>( useCallback(() => mx.leave(invite.roomId), [mx, invite])