Skip to content

Commit

Permalink
Merge pull request #677 from matrix-org/bwindels/selfmembershipchecks
Browse files Browse the repository at this point in the history
Lazy loading: don't assume we have our own member available
  • Loading branch information
bwindels authored Aug 3, 2018
2 parents 8b2a339 + 2e7ecfb commit 7f8be3d
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 68 deletions.
6 changes: 3 additions & 3 deletions examples/node/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,9 @@ function printRoomList() {
dateStr = new Date(msg.getTs()).toISOString().replace(
/T/, ' ').replace(/\..+/, '');
}
var me = roomList[i].getMember(myUserId);
if (me) {
fmt = fmts[me.membership];
var myMembership = roomList[i].getMyMembership();
if (myMembership) {
fmt = fmts[myMembership];
}
var roomName = fixWidth(roomList[i].name, 25);
print(
Expand Down
2 changes: 1 addition & 1 deletion spec/integ/matrix-client-methods.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ describe("MatrixClient", function() {
describe("joinRoom", function() {
it("should no-op if you've already joined a room", function() {
const roomId = "!foo:bar";
const room = new Room(roomId);
const room = new Room(roomId, userId);
room.addLiveEvents([
utils.mkMembership({
user: userId, room: roomId, mship: "join", event: true,
Expand Down
92 changes: 53 additions & 39 deletions spec/unit/room.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ describe("Room", function() {
let events = null;

beforeEach(function() {
room = new Room(roomId, {timelineSupport: timelineSupport});
room = new Room(roomId, null, {timelineSupport: timelineSupport});
// set events each time to avoid resusing Event objects (which
// doesn't work because they get frozen)
events = [
Expand Down Expand Up @@ -469,7 +469,7 @@ describe("Room", function() {

describe("compareEventOrdering", function() {
beforeEach(function() {
room = new Room(roomId, {timelineSupport: true});
room = new Room(roomId, null, {timelineSupport: true});
});

const events = [
Expand Down Expand Up @@ -658,7 +658,7 @@ describe("Room", function() {

beforeEach(function() {
// no mocking
room = new Room(roomId);
room = new Room(roomId, userA);
});

describe("Room.recalculate => Stripped State Events", function() {
Expand All @@ -677,7 +677,7 @@ describe("Room", function() {
},
];

room.recalculate(userA);
room.recalculate();
expect(room.name).toEqual(roomName);
});

Expand All @@ -696,7 +696,7 @@ describe("Room", function() {
},
];

room.recalculate(userA);
room.recalculate();
expect(room.name).toEqual(roomName);
});
});
Expand All @@ -711,7 +711,7 @@ describe("Room", function() {
"m.heroes": [userB, userC, userD],
});

room.recalculate(userA);
room.recalculate();
expect(room.name).toEqual(`${userB} and 2 others`);
});

Expand All @@ -721,7 +721,7 @@ describe("Room", function() {
"m.joined_member_count": 2,
});

room.recalculate(userA);
room.recalculate();
expect(room.name).toEqual(userB);
});

Expand All @@ -733,7 +733,7 @@ describe("Room", function() {
"m.heroes": [userB],
});

room.recalculate(userA);
room.recalculate();
expect(room.name).toEqual(name);
});

Expand All @@ -745,7 +745,7 @@ describe("Room", function() {
"m.joined_member_count": 50,
"m.invited_member_count": 50,
});
room.recalculate(userA);
room.recalculate();
expect(room.name).toEqual(`${name} and 98 others`);
});

Expand All @@ -757,7 +757,7 @@ describe("Room", function() {
room.setSummary({
"m.heroes": [userB, userC],
});
room.recalculate(userA);
room.recalculate();
expect(room.name).toEqual(`${nameB} and ${nameC}`);
});

Expand All @@ -768,7 +768,7 @@ describe("Room", function() {
room.setSummary({
"m.heroes": [userB],
});
room.recalculate(userA);
room.recalculate();
expect(room.name).toEqual(nameB);
});

Expand All @@ -777,7 +777,7 @@ describe("Room", function() {
"m.heroes": [],
"m.invited_member_count": 1,
});
room.recalculate(userA);
room.recalculate();
expect(room.name).toEqual("Empty room");
});
});
Expand All @@ -791,7 +791,7 @@ describe("Room", function() {
addMember(userB);
addMember(userC);
addMember(userD);
room.recalculate(userA);
room.recalculate();
const name = room.name;
// we expect at least 1 member to be mentioned
const others = [userB, userC, userD];
Expand All @@ -812,7 +812,7 @@ describe("Room", function() {
addMember(userA);
addMember(userB);
addMember(userC);
room.recalculate(userA);
room.recalculate();
const name = room.name;
expect(name.indexOf(userB)).toNotEqual(-1, name);
expect(name.indexOf(userC)).toNotEqual(-1, name);
Expand All @@ -825,7 +825,7 @@ describe("Room", function() {
addMember(userA);
addMember(userB);
addMember(userC);
room.recalculate(userA);
room.recalculate();
const name = room.name;
expect(name.indexOf(userB)).toNotEqual(-1, name);
expect(name.indexOf(userC)).toNotEqual(-1, name);
Expand All @@ -837,7 +837,7 @@ describe("Room", function() {
setJoinRule("public");
addMember(userA);
addMember(userB);
room.recalculate(userA);
room.recalculate();
const name = room.name;
expect(name.indexOf(userB)).toNotEqual(-1, name);
});
Expand All @@ -848,7 +848,7 @@ describe("Room", function() {
setJoinRule("invite");
addMember(userA);
addMember(userB);
room.recalculate(userA);
room.recalculate();
const name = room.name;
expect(name.indexOf(userB)).toNotEqual(-1, name);
});
Expand All @@ -858,7 +858,7 @@ describe("Room", function() {
setJoinRule("invite");
addMember(userA, "invite", {user: userB});
addMember(userB);
room.recalculate(userA);
room.recalculate();
const name = room.name;
expect(name.indexOf(userB)).toNotEqual(-1, name);
});
Expand All @@ -868,7 +868,7 @@ describe("Room", function() {
const alias = "#room_alias:here";
setJoinRule("invite");
setAliases([alias, "#another:one"]);
room.recalculate(userA);
room.recalculate();
const name = room.name;
expect(name).toEqual(alias);
});
Expand All @@ -878,7 +878,7 @@ describe("Room", function() {
const alias = "#room_alias:here";
setJoinRule("public");
setAliases([alias, "#another:one"]);
room.recalculate(userA);
room.recalculate();
const name = room.name;
expect(name).toEqual(alias);
});
Expand All @@ -888,7 +888,7 @@ describe("Room", function() {
const roomName = "A mighty name indeed";
setJoinRule("invite");
setRoomName(roomName);
room.recalculate(userA);
room.recalculate();
const name = room.name;
expect(name).toEqual(roomName);
});
Expand All @@ -898,31 +898,31 @@ describe("Room", function() {
const roomName = "A mighty name indeed";
setJoinRule("public");
setRoomName(roomName);
room.recalculate(userA);
room.recalculate();
expect(room.name).toEqual(roomName);
});

it("should return 'Empty room' for private (invite join_rules) rooms if" +
" a room name and alias don't exist and it is a self-chat.", function() {
setJoinRule("invite");
addMember(userA);
room.recalculate(userA);
room.recalculate();
expect(room.name).toEqual("Empty room");
});

it("should return 'Empty room' for public (public join_rules) rooms if a" +
" room name and alias don't exist and it is a self-chat.", function() {
setJoinRule("public");
addMember(userA);
room.recalculate(userA);
room.recalculate();
const name = room.name;
expect(name).toEqual("Empty room");
});

it("should return 'Empty room' if there is no name, " +
"alias or members in the room.",
function() {
room.recalculate(userA);
room.recalculate();
const name = room.name;
expect(name).toEqual("Empty room");
});
Expand All @@ -931,21 +931,21 @@ describe("Room", function() {
"available",
function() {
setJoinRule("invite");
addMember(userA, 'join', {name: "Alice"});
addMember(userB, "invite", {user: userA});
room.recalculate(userB);
addMember(userB, 'join', {name: "Alice"});
addMember(userA, "invite", {user: userA});
room.recalculate();
const name = room.name;
expect(name).toEqual("Alice");
});

it("should return inviter mxid if display name not available",
function() {
setJoinRule("invite");
addMember(userA);
addMember(userB, "invite", {user: userA});
room.recalculate(userB);
addMember(userB);
addMember(userA, "invite", {user: userA});
room.recalculate();
const name = room.name;
expect(name).toEqual(userA);
expect(name).toEqual(userB);
});
});
});
Expand Down Expand Up @@ -1192,7 +1192,7 @@ describe("Room", function() {
describe("addPendingEvent", function() {
it("should add pending events to the pendingEventList if " +
"pendingEventOrdering == 'detached'", function() {
const room = new Room(roomId, {
const room = new Room(roomId, userA, {
pendingEventOrdering: "detached",
});
const eventA = utils.mkMessage({
Expand All @@ -1218,7 +1218,7 @@ describe("Room", function() {

it("should add pending events to the timeline if " +
"pendingEventOrdering == 'chronological'", function() {
room = new Room(roomId, {
room = new Room(roomId, userA, {
pendingEventOrdering: "chronological",
});
const eventA = utils.mkMessage({
Expand All @@ -1242,7 +1242,7 @@ describe("Room", function() {

describe("updatePendingEvent", function() {
it("should remove cancelled events from the pending list", function() {
const room = new Room(roomId, {
const room = new Room(roomId, userA, {
pendingEventOrdering: "detached",
});
const eventA = utils.mkMessage({
Expand Down Expand Up @@ -1278,7 +1278,7 @@ describe("Room", function() {


it("should remove cancelled events from the timeline", function() {
const room = new Room(roomId);
const room = new Room(roomId, userA);
const eventA = utils.mkMessage({
room: roomId, user: userA, event: true,
});
Expand Down Expand Up @@ -1318,7 +1318,7 @@ describe("Room", function() {
});

it("should apply member events", async function() {
const room = new Room(roomId);
const room = new Room(roomId, null);
await room.loadOutOfBandMembers(Promise.resolve([memberEvent]));
const memberA = room.getMember("@user_a:bar");
expect(memberA.name).toEqual("User A");
Expand All @@ -1329,7 +1329,7 @@ describe("Room", function() {
user: "@user_a:bar", mship: "join",
room: roomId, event: true, name: "Ms A",
});
const room = new Room(roomId);
const room = new Room(roomId, null);

const promise2 = Promise.resolve([memberEvent2]);
const promise1 = promise2.then(() => [memberEvent]);
Expand All @@ -1342,7 +1342,7 @@ describe("Room", function() {
});

it("should revert needs loading on error", async function() {
const room = new Room(roomId);
const room = new Room(roomId, null);
let hasThrown = false;
try {
await room.loadOutOfBandMembers(Promise.reject(new Error("bugger")));
Expand All @@ -1353,4 +1353,18 @@ describe("Room", function() {
expect(room.needsOutOfBandMembers()).toEqual(true);
});
});

describe("getMyMembership", function() {
it("should return synced membership if membership isn't available yet",
async function() {
const room = new Room(roomId, userA);
room.setSyncedMembership("invite");
expect(room.getMyMembership()).toEqual("invite");
room.addLiveEvents([utils.mkMembership({
user: userA, mship: "join",
room: roomId, event: true,
})]);
expect(room.getMyMembership()).toEqual("join");
});
});
});
10 changes: 2 additions & 8 deletions src/crypto/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1008,14 +1008,8 @@ Crypto.prototype._getE2eRooms = function() {
}

// ignore any rooms which we have left
const me = room.getMember(this._userId);
if (!me || (
me.membership !== "join" && me.membership !== "invite"
)) {
return false;
}

return true;
const myMembership = room.getMyMembership();
return myMembership === "join" || myMembership === "invite";
});
};

Expand Down
Loading

0 comments on commit 7f8be3d

Please sign in to comment.