estate: the inbox Accept joins through activateEstateRoom; a join with no server answer keeps the DM listed
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
44395328e9
commit
719f062ef1
3 changed files with 33 additions and 18 deletions
|
|
@ -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<string, string> }) => {
|
||||
const dmClient = (opts: { refuse?: boolean; drop?: boolean; invites: Record<string, string> }) => {
|
||||
let direct: Record<string, string[]> | undefined;
|
||||
const writes: Record<string, string[]>[] = [];
|
||||
const joined: string[] = [];
|
||||
|
|
@ -102,7 +103,8 @@ const dmClient = (opts: { refuse?: boolean; invites: Record<string, string> }) =
|
|||
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 = () => {
|
||||
|
|
|
|||
|
|
@ -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<typeof addRoomIdToMDirect>[0];
|
||||
|
||||
/**
|
||||
|
|
@ -18,7 +21,10 @@ type JoinClient = Parameters<typeof addRoomIdToMDirect>[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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<void, MatrixError, []>(
|
||||
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<Record<string, never>, MatrixError, []>(
|
||||
useCallback(() => mx.leave(invite.roomId), [mx, invite])
|
||||
|
|
|
|||
Loading…
Reference in a new issue