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

Raise room.join with an event ID; fix room.leave to check membership #319

Merged
merged 1 commit into from
May 13, 2023
Merged
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
15 changes: 10 additions & 5 deletions src/MatrixClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,9 @@ export class MatrixClient extends EventEmitter {
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;
Expand Down Expand Up @@ -855,11 +858,6 @@ export class MatrixClient extends EventEmitter {

// Process rooms we've joined and their events
for (const roomId in joinedRooms) {
if (this.lastJoinedRoomIds.indexOf(roomId) === -1) {
await emitFn("room.join", roomId);
this.lastJoinedRoomIds.push(roomId);
}

const room = joinedRooms[roomId];

if (room['account_data'] && room['account_data']['events']) {
Expand All @@ -871,6 +869,13 @@ export class MatrixClient extends EventEmitter {
if (!room['timeline'] || !room['timeline']['events']) continue;

for (let event of room['timeline']['events']) {
if (event['type'] === "m.room.member" && event['state_key'] === await this.getUserId()) {
if (event['content']?.['membership'] === "join" && this.lastJoinedRoomIds.indexOf(roomId) === -1) {
await emitFn("room.join", roomId, await this.processEvent(event));
this.lastJoinedRoomIds.push(roomId);
}
}

event = await this.processEvent(event);
if (event['type'] === 'm.room.encrypted' && await this.crypto?.isRoomEncrypted(roomId)) {
await emitFn("room.encrypted_event", roomId, event);
Expand Down
60 changes: 52 additions & 8 deletions test/MatrixClientTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1441,6 +1441,7 @@ describe('MatrixClient', () => {
type: "m.room.member",
state_key: userId,
unsigned: { age: 0 },
content: { membership: "leave" },
},
];

Expand Down Expand Up @@ -1498,16 +1499,19 @@ describe('MatrixClient', () => {
type: "m.room.member",
state_key: userId,
unsigned: { age: 2 },
content: { membership: "leave" },
},
{
type: "m.room.member",
state_key: userId,
unsigned: { age: 1 },
content: { membership: "leave" },
},
{
type: "m.room.member",
state_key: userId,
unsigned: { age: 3 },
content: { membership: "leave" },
},
];

Expand Down Expand Up @@ -1536,16 +1540,19 @@ describe('MatrixClient', () => {
type: "m.room.not_member",
state_key: userId,
unsigned: { age: 1 },
content: { membership: "leave" },
},
{
type: "m.room.member",
state_key: userId,
unsigned: { age: 1 },
content: { membership: "leave" },
},
{
type: "m.room.member",
state_key: userId + "_wrong_member",
unsigned: { age: 1 },
content: { membership: "leave" },
},
];

Expand Down Expand Up @@ -1580,6 +1587,7 @@ describe('MatrixClient', () => {
// type: "m.room.member",
// state_key: userId,
// unsigned: {age: 1},
// content: { membership: "leave" },
// },
{
type: "m.room.member",
Expand Down Expand Up @@ -1612,6 +1620,7 @@ describe('MatrixClient', () => {
{
type: "m.room.member",
state_key: userId,
content: { membership: "leave" },
},
];

Expand Down Expand Up @@ -1793,7 +1802,6 @@ describe('MatrixClient', () => {
const userId = "@syncing:example.org";
const roomId = "!testing:example.org";
const events = [
// TODO: Surely the 'invite' membership should be in some sort of content field?
{
type: "m.room.member",
state_key: userId,
Expand Down Expand Up @@ -1822,16 +1830,25 @@ describe('MatrixClient', () => {

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

client.userId = userId;

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

const roomsObj = {};
roomsObj[roomId] = {};
roomsObj[roomId] = { timeline: { events: events } };
await client.processSync({ rooms: { join: roomsObj } });
expect(spy.callCount).toBe(1);
});
Expand Down Expand Up @@ -1871,16 +1888,25 @@ describe('MatrixClient', () => {

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

client.userId = userId;

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

const roomsObj = {};
roomsObj[roomId] = {};
roomsObj[roomId] = { timeline: { events: events } };
await client.processSync({ rooms: { join: roomsObj } });
expect(spy.callCount).toBe(1);
await client.processSync({ rooms: { join: roomsObj } });
Expand Down Expand Up @@ -1926,6 +1952,12 @@ describe('MatrixClient', () => {
const userId = "@syncing:example.org";
const roomId = "!testing:example.org";
const events = [
{
type: "m.room.member",
state_key: userId,
unsigned: { age: 0 },
content: { membership: "join" },
},
{
type: "m.room.not_message",
content: { body: "hello world 1" },
Expand Down Expand Up @@ -1971,7 +2003,7 @@ describe('MatrixClient', () => {
expect(inviteSpy.callCount).toBe(0);
expect(leaveSpy.callCount).toBe(0);
expect(messageSpy.callCount).toBe(2);
expect(eventSpy.callCount).toBe(4);
expect(eventSpy.callCount).toBe(5);
});

it('should process tombstone events', async () => {
Expand All @@ -1981,6 +2013,12 @@ describe('MatrixClient', () => {
const userId = "@syncing:example.org";
const roomId = "!testing:example.org";
const events = [
{
type: "m.room.member",
state_key: userId,
unsigned: { age: 0 },
content: { membership: "join" },
},
{
type: "m.room.tombstone",
content: { body: "hello world 1" },
Expand Down Expand Up @@ -2020,7 +2058,7 @@ describe('MatrixClient', () => {
expect(inviteSpy.callCount).toBe(0);
expect(leaveSpy.callCount).toBe(0);
expect(archiveSpy.callCount).toBe(1);
expect(eventSpy.callCount).toBe(2);
expect(eventSpy.callCount).toBe(3);
});

it('should process create events with a predecessor', async () => {
Expand All @@ -2030,6 +2068,12 @@ describe('MatrixClient', () => {
const userId = "@syncing:example.org";
const roomId = "!testing:example.org";
const events = [
{
type: "m.room.member",
state_key: userId,
unsigned: { age: 0 },
content: { membership: "join" },
},
{
type: "m.room.tombstone",
content: { body: "hello world 1" },
Expand Down Expand Up @@ -2069,7 +2113,7 @@ describe('MatrixClient', () => {
expect(inviteSpy.callCount).toBe(0);
expect(leaveSpy.callCount).toBe(0);
expect(upgradedSpy.callCount).toBe(1);
expect(eventSpy.callCount).toBe(2);
expect(eventSpy.callCount).toBe(3);
});

it('should send events through a processor', async () => {
Expand Down
Loading