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

Fix room.leave event on MatrixClient emitter to emit when the bot is banned. #52

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
21 changes: 10 additions & 11 deletions src/MatrixClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -827,19 +827,18 @@ export class MatrixClient extends EventEmitter {
for (const event of room['timeline']['events']) {
if (event['type'] !== 'm.room.member') continue;
if (event['state_key'] !== await this.getUserId()) continue;

const membership = event["content"]?.["membership"];
if (membership !== "leave" && membership !== "ban") continue;

const oldAge = leaveEvent && leaveEvent['unsigned'] && leaveEvent['unsigned']['age'] ? leaveEvent['unsigned']['age'] : 0;
const newAge = event['unsigned'] && event['unsigned']['age'] ? event['unsigned']['age'] : 0;
if (leaveEvent && oldAge < newAge) continue;

leaveEvent = event;
switch (event["content"]?.["membership"]) {
case "leave":
case "ban": {
const oldAge = leaveEvent && leaveEvent['unsigned'] && leaveEvent['unsigned']['age'] ? leaveEvent['unsigned']['age'] : 0;
const newAge = event['unsigned'] && event['unsigned']['age'] ? event['unsigned']['age'] : 0;
if (leaveEvent && oldAge < newAge) continue;
leaveEvent = event;
}
}
}

if (!leaveEvent) {
LogService.warn("MatrixClientLite", "Left room " + roomId + " without receiving an event");
LogService.error("MatrixClientLite", "Left room " + roomId + " without receiving an event");
continue;
}

Expand Down
29 changes: 29 additions & 0 deletions test/MatrixClientTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1457,6 +1457,35 @@ describe('MatrixClient', () => {
expect(spy.callCount).toBe(1);
});

it('should process banned rooms as left rooms', async () => {
const { client: realClient } = createTestClient();
const client = <ProcessSyncClient>(<any>realClient);

const userId = "@syncing:example.org";
const roomId = "!testing:example.org";
const events = [
{
type: "m.room.member",
state_key: userId,
unsigned: { age: 0 },
content: { membership: "ban" },
},
];

client.userId = userId;

const spy = simple.stub().callFn((rid, ev) => {
expect(ev).toMatchObject(events[0]);
expect(rid).toEqual(roomId);
});
realClient.on("room.leave", spy);

const roomsObj = {};
roomsObj[roomId] = { timeline: { events: events } };
await client.processSync({ rooms: { leave: roomsObj } });
expect(spy.callCount).toBe(1);
});

it('should process left rooms account data', async () => {
const { client: realClient } = createTestClient();
const client = <ProcessSyncClient>(<any>realClient);
Expand Down
Loading