Skip to content

Commit 7bb57ca

Browse files
authored
Raise room.join with an event ID; fix room.leave to check membership (#319)
Fixes #304
1 parent 2f768c4 commit 7bb57ca

File tree

3 files changed

+117
-23
lines changed

3 files changed

+117
-23
lines changed

src/MatrixClient.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,9 @@ export class MatrixClient extends EventEmitter {
808808
if (event['type'] !== 'm.room.member') continue;
809809
if (event['state_key'] !== await this.getUserId()) continue;
810810

811+
const membership = event["content"]?.["membership"];
812+
if (membership !== "leave" && membership !== "ban") continue;
813+
811814
const oldAge = leaveEvent && leaveEvent['unsigned'] && leaveEvent['unsigned']['age'] ? leaveEvent['unsigned']['age'] : 0;
812815
const newAge = event['unsigned'] && event['unsigned']['age'] ? event['unsigned']['age'] : 0;
813816
if (leaveEvent && oldAge < newAge) continue;
@@ -855,11 +858,6 @@ export class MatrixClient extends EventEmitter {
855858

856859
// Process rooms we've joined and their events
857860
for (const roomId in joinedRooms) {
858-
if (this.lastJoinedRoomIds.indexOf(roomId) === -1) {
859-
await emitFn("room.join", roomId);
860-
this.lastJoinedRoomIds.push(roomId);
861-
}
862-
863861
const room = joinedRooms[roomId];
864862

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

873871
for (let event of room['timeline']['events']) {
872+
if (event['type'] === "m.room.member" && event['state_key'] === await this.getUserId()) {
873+
if (event['content']?.['membership'] === "join" && this.lastJoinedRoomIds.indexOf(roomId) === -1) {
874+
await emitFn("room.join", roomId, await this.processEvent(event));
875+
this.lastJoinedRoomIds.push(roomId);
876+
}
877+
}
878+
874879
event = await this.processEvent(event);
875880
if (event['type'] === 'm.room.encrypted' && await this.crypto?.isRoomEncrypted(roomId)) {
876881
await emitFn("room.encrypted_event", roomId, event);

test/MatrixClientTest.ts

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,6 +1441,7 @@ describe('MatrixClient', () => {
14411441
type: "m.room.member",
14421442
state_key: userId,
14431443
unsigned: { age: 0 },
1444+
content: { membership: "leave" },
14441445
},
14451446
];
14461447

@@ -1498,16 +1499,19 @@ describe('MatrixClient', () => {
14981499
type: "m.room.member",
14991500
state_key: userId,
15001501
unsigned: { age: 2 },
1502+
content: { membership: "leave" },
15011503
},
15021504
{
15031505
type: "m.room.member",
15041506
state_key: userId,
15051507
unsigned: { age: 1 },
1508+
content: { membership: "leave" },
15061509
},
15071510
{
15081511
type: "m.room.member",
15091512
state_key: userId,
15101513
unsigned: { age: 3 },
1514+
content: { membership: "leave" },
15111515
},
15121516
];
15131517

@@ -1536,16 +1540,19 @@ describe('MatrixClient', () => {
15361540
type: "m.room.not_member",
15371541
state_key: userId,
15381542
unsigned: { age: 1 },
1543+
content: { membership: "leave" },
15391544
},
15401545
{
15411546
type: "m.room.member",
15421547
state_key: userId,
15431548
unsigned: { age: 1 },
1549+
content: { membership: "leave" },
15441550
},
15451551
{
15461552
type: "m.room.member",
15471553
state_key: userId + "_wrong_member",
15481554
unsigned: { age: 1 },
1555+
content: { membership: "leave" },
15491556
},
15501557
];
15511558

@@ -1580,6 +1587,7 @@ describe('MatrixClient', () => {
15801587
// type: "m.room.member",
15811588
// state_key: userId,
15821589
// unsigned: {age: 1},
1590+
// content: { membership: "leave" },
15831591
// },
15841592
{
15851593
type: "m.room.member",
@@ -1612,6 +1620,7 @@ describe('MatrixClient', () => {
16121620
{
16131621
type: "m.room.member",
16141622
state_key: userId,
1623+
content: { membership: "leave" },
16151624
},
16161625
];
16171626

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

18231831
const userId = "@syncing:example.org";
18241832
const roomId = "!testing:example.org";
1833+
const events = [
1834+
{
1835+
type: "m.room.member",
1836+
state_key: userId,
1837+
unsigned: { age: 0 },
1838+
content: { membership: "join" },
1839+
},
1840+
];
18251841

18261842
client.userId = userId;
18271843

1828-
const spy = simple.stub().callFn((rid) => {
1844+
const spy = simple.stub().callFn((rid, ev) => {
1845+
expect(ev).toMatchObject(events[0]);
18291846
expect(rid).toEqual(roomId);
18301847
});
18311848
realClient.on("room.join", spy);
18321849

18331850
const roomsObj = {};
1834-
roomsObj[roomId] = {};
1851+
roomsObj[roomId] = { timeline: { events: events } };
18351852
await client.processSync({ rooms: { join: roomsObj } });
18361853
expect(spy.callCount).toBe(1);
18371854
});
@@ -1871,16 +1888,25 @@ describe('MatrixClient', () => {
18711888

18721889
const userId = "@syncing:example.org";
18731890
const roomId = "!testing:example.org";
1891+
const events = [
1892+
{
1893+
type: "m.room.member",
1894+
state_key: userId,
1895+
unsigned: { age: 0 },
1896+
content: { membership: "join" },
1897+
},
1898+
];
18741899

18751900
client.userId = userId;
18761901

1877-
const spy = simple.stub().callFn((rid) => {
1902+
const spy = simple.stub().callFn((rid, ev) => {
1903+
expect(ev).toMatchObject(events[0]);
18781904
expect(rid).toEqual(roomId);
18791905
});
18801906
realClient.on("room.join", spy);
18811907

18821908
const roomsObj = {};
1883-
roomsObj[roomId] = {};
1909+
roomsObj[roomId] = { timeline: { events: events } };
18841910
await client.processSync({ rooms: { join: roomsObj } });
18851911
expect(spy.callCount).toBe(1);
18861912
await client.processSync({ rooms: { join: roomsObj } });
@@ -1926,6 +1952,12 @@ describe('MatrixClient', () => {
19261952
const userId = "@syncing:example.org";
19271953
const roomId = "!testing:example.org";
19281954
const events = [
1955+
{
1956+
type: "m.room.member",
1957+
state_key: userId,
1958+
unsigned: { age: 0 },
1959+
content: { membership: "join" },
1960+
},
19291961
{
19301962
type: "m.room.not_message",
19311963
content: { body: "hello world 1" },
@@ -1971,7 +2003,7 @@ describe('MatrixClient', () => {
19712003
expect(inviteSpy.callCount).toBe(0);
19722004
expect(leaveSpy.callCount).toBe(0);
19732005
expect(messageSpy.callCount).toBe(2);
1974-
expect(eventSpy.callCount).toBe(4);
2006+
expect(eventSpy.callCount).toBe(5);
19752007
});
19762008

19772009
it('should process tombstone events', async () => {
@@ -1981,6 +2013,12 @@ describe('MatrixClient', () => {
19812013
const userId = "@syncing:example.org";
19822014
const roomId = "!testing:example.org";
19832015
const events = [
2016+
{
2017+
type: "m.room.member",
2018+
state_key: userId,
2019+
unsigned: { age: 0 },
2020+
content: { membership: "join" },
2021+
},
19842022
{
19852023
type: "m.room.tombstone",
19862024
content: { body: "hello world 1" },
@@ -2020,7 +2058,7 @@ describe('MatrixClient', () => {
20202058
expect(inviteSpy.callCount).toBe(0);
20212059
expect(leaveSpy.callCount).toBe(0);
20222060
expect(archiveSpy.callCount).toBe(1);
2023-
expect(eventSpy.callCount).toBe(2);
2061+
expect(eventSpy.callCount).toBe(3);
20242062
});
20252063

20262064
it('should process create events with a predecessor', async () => {
@@ -2030,6 +2068,12 @@ describe('MatrixClient', () => {
20302068
const userId = "@syncing:example.org";
20312069
const roomId = "!testing:example.org";
20322070
const events = [
2071+
{
2072+
type: "m.room.member",
2073+
state_key: userId,
2074+
unsigned: { age: 0 },
2075+
content: { membership: "join" },
2076+
},
20332077
{
20342078
type: "m.room.tombstone",
20352079
content: { body: "hello world 1" },
@@ -2069,7 +2113,7 @@ describe('MatrixClient', () => {
20692113
expect(inviteSpy.callCount).toBe(0);
20702114
expect(leaveSpy.callCount).toBe(0);
20712115
expect(upgradedSpy.callCount).toBe(1);
2072-
expect(eventSpy.callCount).toBe(2);
2116+
expect(eventSpy.callCount).toBe(3);
20732117
});
20742118

20752119
it('should send events through a processor', async () => {

0 commit comments

Comments
 (0)