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

Commit

Permalink
Avoid error when DMing oneself (#11754)
Browse files Browse the repository at this point in the history
* Avoid error when DMing oneself

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

* Update comment

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

* Add test

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

---------

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
  • Loading branch information
t3chguy committed Oct 17, 2023
1 parent 7f37699 commit fd08e74
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/createRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ export default async function createRoom(client: MatrixClient, opts: IOpts): Pro
const createOpts: ICreateRoomOpts = opts.createOpts || {};
createOpts.preset = createOpts.preset || defaultPreset;
createOpts.visibility = createOpts.visibility || Visibility.Private;
if (opts.dmUserId && createOpts.invite === undefined) {

// We allow UX of DMing ourselves as a form of creating a personal room but the server throws
// an error when a user tries to invite themselves so we filter it out
if (opts.dmUserId && opts.dmUserId !== client.getUserId() && createOpts.invite === undefined) {
switch (getAddressType(opts.dmUserId)) {
case "mx-user-id":
createOpts.invite = [opts.dmUserId];
Expand Down
9 changes: 9 additions & 0 deletions test/createRoom-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,15 @@ describe("createRoom", () => {
}),
);
});

it("should strip self-invite", async () => {
await createRoom(client, { dmUserId: client.getSafeUserId() });
expect(client.createRoom).toHaveBeenCalledWith(
expect.not.objectContaining({
invite: expect.any(Array),
}),
);
});
});

describe("canEncryptToAllUsers", () => {
Expand Down

0 comments on commit fd08e74

Please sign in to comment.