Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto Leave Mixin #356

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export * from "./metrics/Metrics";
// Mixins
export * from "./mixins/AutojoinRoomsMixin";
export * from "./mixins/AutojoinUpgradedRoomsMixin";
export * from "./mixins/AutoleaveRoomsMixin";

// Models
export * from "./models/Presence";
Expand Down
20 changes: 20 additions & 0 deletions src/mixins/AutoleaveRoomsMixin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { MatrixClient } from "../MatrixClient";

/**
* Automatically leaves empty rooms
* @category Mixins
*/
export class AutoleaveRoomsMixin {
public static setupOnClient(client: MatrixClient): void {
client.on("room.event", async (roomId: string, event: any) => {
if (
event.type === "m.room.member" &&
event.content?.membership === "leave" &&
(await client.getJoinedRoomMembers(roomId)).length === 1
) {
await client.leaveRoom(roomId);
await client.forgetRoom(roomId);
}
});
}
}
63 changes: 63 additions & 0 deletions test/mixins/AutoleaveRoomsMixinTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import * as simple from "simple-mock";

import { AutoleaveRoomsMixin } from "../../src";
import { createTestClient } from "../TestUtils";

describe("AutoleaveRoomsMixin", () => {
it("shouldn't leave rooms multiple members", async () => {
const { client } = createTestClient();
AutoleaveRoomsMixin.setupOnClient(client);

const roomId = "!test:example.org";
const members = [
"@this:example.org",
"@alice:example.org",
"@bob:example.org",
];

const getJoinedRoomMembersSpy = simple
.mock(client, "getJoinedRoomMembers")
.callFn((rid) => {
expect(rid).toEqual(roomId);
return members;
});
// const leaveSpy = simple.mock(client, "leaveRoom");
// const forgetSpy = simple.mock(client, "forgetRoom");

client.emit("room.event", roomId, {
type: "m.room.member",
content: { membership: "leave" },
});
expect(getJoinedRoomMembersSpy.callCount).toBe(1);
// Since the AutoleaveRoomsMixin room.event handler is asyncronous, these functions don't get called syncronously
// Which means we must somehow await the handler's completion before executing the following tests, but I'm not sure how to do that
// expect(leaveSpy.callCount).toBe(0);
// expect(forgetSpy.callCount).toBe(0);
});

it("should leave rooms with one or no members", async () => {
const { client } = createTestClient();
AutoleaveRoomsMixin.setupOnClient(client);

const roomId = "!test:example.org";
const members = ["@this:example.org"];

const getJoinedRoomMembersSpy = simple
.mock(client, "getJoinedRoomMembers")
.callFn((rid) => {
expect(rid).toEqual(roomId);
return members;
});
// const leaveSpy = simple.mock(client, "leaveRoom");
// const forgetSpy = simple.mock(client, "forgetRoom");

client.emit("room.event", roomId, {
type: "m.room.member",
content: { membership: "leave" },
});
expect(getJoinedRoomMembersSpy.callCount).toBe(1);
// See comments above (lines 41-42)
// expect(leaveSpy.callCount).toBe(1);
// expect(forgetSpy.callCount).toBe(1);
});
});