Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit fd08e74

Browse files
authored
Avoid error when DMing oneself (#11754)
* Avoid error when DMing oneself Signed-off-by: Michael Telatynski <[email protected]> * Update comment Signed-off-by: Michael Telatynski <[email protected]> * Add test Signed-off-by: Michael Telatynski <[email protected]> --------- Signed-off-by: Michael Telatynski <[email protected]>
1 parent 7f37699 commit fd08e74

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/createRoom.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ export default async function createRoom(client: MatrixClient, opts: IOpts): Pro
119119
const createOpts: ICreateRoomOpts = opts.createOpts || {};
120120
createOpts.preset = createOpts.preset || defaultPreset;
121121
createOpts.visibility = createOpts.visibility || Visibility.Private;
122-
if (opts.dmUserId && createOpts.invite === undefined) {
122+
123+
// We allow UX of DMing ourselves as a form of creating a personal room but the server throws
124+
// an error when a user tries to invite themselves so we filter it out
125+
if (opts.dmUserId && opts.dmUserId !== client.getUserId() && createOpts.invite === undefined) {
123126
switch (getAddressType(opts.dmUserId)) {
124127
case "mx-user-id":
125128
createOpts.invite = [opts.dmUserId];

test/createRoom-test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,15 @@ describe("createRoom", () => {
142142
}),
143143
);
144144
});
145+
146+
it("should strip self-invite", async () => {
147+
await createRoom(client, { dmUserId: client.getSafeUserId() });
148+
expect(client.createRoom).toHaveBeenCalledWith(
149+
expect.not.objectContaining({
150+
invite: expect.any(Array),
151+
}),
152+
);
153+
});
145154
});
146155

147156
describe("canEncryptToAllUsers", () => {

0 commit comments

Comments
 (0)