Avoid error when DMing oneself (#11754)

* 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>
This commit is contained in:
Michael Telatynski 2023-10-17 10:37:08 +01:00 committed by GitHub
parent 7f37699566
commit fd08e74275
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View File

@ -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];

View File

@ -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", () => {