1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-08-06 12:02:40 +03:00

move userId into room

This commit is contained in:
Bruno Windels
2018-08-02 11:10:04 +02:00
parent b034f67a0f
commit c0f706a2a2
4 changed files with 71 additions and 57 deletions

View File

@@ -159,7 +159,7 @@ describe("MatrixClient", function() {
describe("joinRoom", function() { describe("joinRoom", function() {
it("should no-op if you've already joined a room", function() { it("should no-op if you've already joined a room", function() {
const roomId = "!foo:bar"; const roomId = "!foo:bar";
const room = new Room(roomId); const room = new Room(roomId, userId);
room.addLiveEvents([ room.addLiveEvents([
utils.mkMembership({ utils.mkMembership({
user: userId, room: roomId, mship: "join", event: true, user: userId, room: roomId, mship: "join", event: true,

View File

@@ -387,7 +387,7 @@ describe("Room", function() {
let events = null; let events = null;
beforeEach(function() { 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 // set events each time to avoid resusing Event objects (which
// doesn't work because they get frozen) // doesn't work because they get frozen)
events = [ events = [
@@ -469,7 +469,7 @@ describe("Room", function() {
describe("compareEventOrdering", function() { describe("compareEventOrdering", function() {
beforeEach(function() { beforeEach(function() {
room = new Room(roomId, {timelineSupport: true}); room = new Room(roomId, null, {timelineSupport: true});
}); });
const events = [ const events = [
@@ -658,7 +658,7 @@ describe("Room", function() {
beforeEach(function() { beforeEach(function() {
// no mocking // no mocking
room = new Room(roomId); room = new Room(roomId, userA);
}); });
describe("Room.recalculate => Stripped State Events", function() { describe("Room.recalculate => Stripped State Events", function() {
@@ -677,7 +677,7 @@ describe("Room", function() {
}, },
]; ];
room.recalculate(userA); room.recalculate();
expect(room.name).toEqual(roomName); expect(room.name).toEqual(roomName);
}); });
@@ -696,7 +696,7 @@ describe("Room", function() {
}, },
]; ];
room.recalculate(userA); room.recalculate();
expect(room.name).toEqual(roomName); expect(room.name).toEqual(roomName);
}); });
}); });
@@ -711,7 +711,7 @@ describe("Room", function() {
"m.heroes": [userB, userC, userD], "m.heroes": [userB, userC, userD],
}); });
room.recalculate(userA); room.recalculate();
expect(room.name).toEqual(`${userB} and 2 others`); expect(room.name).toEqual(`${userB} and 2 others`);
}); });
@@ -721,7 +721,7 @@ describe("Room", function() {
"m.joined_member_count": 2, "m.joined_member_count": 2,
}); });
room.recalculate(userA); room.recalculate();
expect(room.name).toEqual(userB); expect(room.name).toEqual(userB);
}); });
@@ -733,7 +733,7 @@ describe("Room", function() {
"m.heroes": [userB], "m.heroes": [userB],
}); });
room.recalculate(userA); room.recalculate();
expect(room.name).toEqual(name); expect(room.name).toEqual(name);
}); });
@@ -745,7 +745,7 @@ describe("Room", function() {
"m.joined_member_count": 50, "m.joined_member_count": 50,
"m.invited_member_count": 50, "m.invited_member_count": 50,
}); });
room.recalculate(userA); room.recalculate();
expect(room.name).toEqual(`${name} and 98 others`); expect(room.name).toEqual(`${name} and 98 others`);
}); });
@@ -757,7 +757,7 @@ describe("Room", function() {
room.setSummary({ room.setSummary({
"m.heroes": [userB, userC], "m.heroes": [userB, userC],
}); });
room.recalculate(userA); room.recalculate();
expect(room.name).toEqual(`${nameB} and ${nameC}`); expect(room.name).toEqual(`${nameB} and ${nameC}`);
}); });
@@ -768,7 +768,7 @@ describe("Room", function() {
room.setSummary({ room.setSummary({
"m.heroes": [userB], "m.heroes": [userB],
}); });
room.recalculate(userA); room.recalculate();
expect(room.name).toEqual(nameB); expect(room.name).toEqual(nameB);
}); });
@@ -777,7 +777,7 @@ describe("Room", function() {
"m.heroes": [], "m.heroes": [],
"m.invited_member_count": 1, "m.invited_member_count": 1,
}); });
room.recalculate(userA); room.recalculate();
expect(room.name).toEqual("Empty room"); expect(room.name).toEqual("Empty room");
}); });
}); });
@@ -791,7 +791,7 @@ describe("Room", function() {
addMember(userB); addMember(userB);
addMember(userC); addMember(userC);
addMember(userD); addMember(userD);
room.recalculate(userA); room.recalculate();
const name = room.name; const name = room.name;
// we expect at least 1 member to be mentioned // we expect at least 1 member to be mentioned
const others = [userB, userC, userD]; const others = [userB, userC, userD];
@@ -812,7 +812,7 @@ describe("Room", function() {
addMember(userA); addMember(userA);
addMember(userB); addMember(userB);
addMember(userC); addMember(userC);
room.recalculate(userA); room.recalculate();
const name = room.name; const name = room.name;
expect(name.indexOf(userB)).toNotEqual(-1, name); expect(name.indexOf(userB)).toNotEqual(-1, name);
expect(name.indexOf(userC)).toNotEqual(-1, name); expect(name.indexOf(userC)).toNotEqual(-1, name);
@@ -825,7 +825,7 @@ describe("Room", function() {
addMember(userA); addMember(userA);
addMember(userB); addMember(userB);
addMember(userC); addMember(userC);
room.recalculate(userA); room.recalculate();
const name = room.name; const name = room.name;
expect(name.indexOf(userB)).toNotEqual(-1, name); expect(name.indexOf(userB)).toNotEqual(-1, name);
expect(name.indexOf(userC)).toNotEqual(-1, name); expect(name.indexOf(userC)).toNotEqual(-1, name);
@@ -837,7 +837,7 @@ describe("Room", function() {
setJoinRule("public"); setJoinRule("public");
addMember(userA); addMember(userA);
addMember(userB); addMember(userB);
room.recalculate(userA); room.recalculate();
const name = room.name; const name = room.name;
expect(name.indexOf(userB)).toNotEqual(-1, name); expect(name.indexOf(userB)).toNotEqual(-1, name);
}); });
@@ -848,7 +848,7 @@ describe("Room", function() {
setJoinRule("invite"); setJoinRule("invite");
addMember(userA); addMember(userA);
addMember(userB); addMember(userB);
room.recalculate(userA); room.recalculate();
const name = room.name; const name = room.name;
expect(name.indexOf(userB)).toNotEqual(-1, name); expect(name.indexOf(userB)).toNotEqual(-1, name);
}); });
@@ -858,7 +858,7 @@ describe("Room", function() {
setJoinRule("invite"); setJoinRule("invite");
addMember(userA, "invite", {user: userB}); addMember(userA, "invite", {user: userB});
addMember(userB); addMember(userB);
room.recalculate(userA); room.recalculate();
const name = room.name; const name = room.name;
expect(name.indexOf(userB)).toNotEqual(-1, name); expect(name.indexOf(userB)).toNotEqual(-1, name);
}); });
@@ -868,7 +868,7 @@ describe("Room", function() {
const alias = "#room_alias:here"; const alias = "#room_alias:here";
setJoinRule("invite"); setJoinRule("invite");
setAliases([alias, "#another:one"]); setAliases([alias, "#another:one"]);
room.recalculate(userA); room.recalculate();
const name = room.name; const name = room.name;
expect(name).toEqual(alias); expect(name).toEqual(alias);
}); });
@@ -878,7 +878,7 @@ describe("Room", function() {
const alias = "#room_alias:here"; const alias = "#room_alias:here";
setJoinRule("public"); setJoinRule("public");
setAliases([alias, "#another:one"]); setAliases([alias, "#another:one"]);
room.recalculate(userA); room.recalculate();
const name = room.name; const name = room.name;
expect(name).toEqual(alias); expect(name).toEqual(alias);
}); });
@@ -888,7 +888,7 @@ describe("Room", function() {
const roomName = "A mighty name indeed"; const roomName = "A mighty name indeed";
setJoinRule("invite"); setJoinRule("invite");
setRoomName(roomName); setRoomName(roomName);
room.recalculate(userA); room.recalculate();
const name = room.name; const name = room.name;
expect(name).toEqual(roomName); expect(name).toEqual(roomName);
}); });
@@ -898,7 +898,7 @@ describe("Room", function() {
const roomName = "A mighty name indeed"; const roomName = "A mighty name indeed";
setJoinRule("public"); setJoinRule("public");
setRoomName(roomName); setRoomName(roomName);
room.recalculate(userA); room.recalculate();
expect(room.name).toEqual(roomName); expect(room.name).toEqual(roomName);
}); });
@@ -906,7 +906,7 @@ describe("Room", function() {
" a room name and alias don't exist and it is a self-chat.", function() { " a room name and alias don't exist and it is a self-chat.", function() {
setJoinRule("invite"); setJoinRule("invite");
addMember(userA); addMember(userA);
room.recalculate(userA); room.recalculate();
expect(room.name).toEqual("Empty room"); expect(room.name).toEqual("Empty room");
}); });
@@ -914,7 +914,7 @@ describe("Room", function() {
" room name and alias don't exist and it is a self-chat.", function() { " room name and alias don't exist and it is a self-chat.", function() {
setJoinRule("public"); setJoinRule("public");
addMember(userA); addMember(userA);
room.recalculate(userA); room.recalculate();
const name = room.name; const name = room.name;
expect(name).toEqual("Empty room"); expect(name).toEqual("Empty room");
}); });
@@ -922,7 +922,7 @@ describe("Room", function() {
it("should return 'Empty room' if there is no name, " + it("should return 'Empty room' if there is no name, " +
"alias or members in the room.", "alias or members in the room.",
function() { function() {
room.recalculate(userA); room.recalculate();
const name = room.name; const name = room.name;
expect(name).toEqual("Empty room"); expect(name).toEqual("Empty room");
}); });
@@ -931,9 +931,9 @@ describe("Room", function() {
"available", "available",
function() { function() {
setJoinRule("invite"); setJoinRule("invite");
addMember(userA, 'join', {name: "Alice"}); addMember(userB, 'join', {name: "Alice"});
addMember(userB, "invite", {user: userA}); addMember(userA, "invite", {user: userA});
room.recalculate(userB); room.recalculate();
const name = room.name; const name = room.name;
expect(name).toEqual("Alice"); expect(name).toEqual("Alice");
}); });
@@ -941,11 +941,11 @@ describe("Room", function() {
it("should return inviter mxid if display name not available", it("should return inviter mxid if display name not available",
function() { function() {
setJoinRule("invite"); setJoinRule("invite");
addMember(userA); addMember(userB);
addMember(userB, "invite", {user: userA}); addMember(userA, "invite", {user: userA});
room.recalculate(userB); room.recalculate();
const name = room.name; const name = room.name;
expect(name).toEqual(userA); expect(name).toEqual(userB);
}); });
}); });
}); });
@@ -1192,7 +1192,7 @@ describe("Room", function() {
describe("addPendingEvent", function() { describe("addPendingEvent", function() {
it("should add pending events to the pendingEventList if " + it("should add pending events to the pendingEventList if " +
"pendingEventOrdering == 'detached'", function() { "pendingEventOrdering == 'detached'", function() {
const room = new Room(roomId, { const room = new Room(roomId, userA, {
pendingEventOrdering: "detached", pendingEventOrdering: "detached",
}); });
const eventA = utils.mkMessage({ const eventA = utils.mkMessage({
@@ -1218,7 +1218,7 @@ describe("Room", function() {
it("should add pending events to the timeline if " + it("should add pending events to the timeline if " +
"pendingEventOrdering == 'chronological'", function() { "pendingEventOrdering == 'chronological'", function() {
room = new Room(roomId, { room = new Room(roomId, userA, {
pendingEventOrdering: "chronological", pendingEventOrdering: "chronological",
}); });
const eventA = utils.mkMessage({ const eventA = utils.mkMessage({
@@ -1242,7 +1242,7 @@ describe("Room", function() {
describe("updatePendingEvent", function() { describe("updatePendingEvent", function() {
it("should remove cancelled events from the pending list", function() { it("should remove cancelled events from the pending list", function() {
const room = new Room(roomId, { const room = new Room(roomId, userA, {
pendingEventOrdering: "detached", pendingEventOrdering: "detached",
}); });
const eventA = utils.mkMessage({ const eventA = utils.mkMessage({
@@ -1278,7 +1278,7 @@ describe("Room", function() {
it("should remove cancelled events from the timeline", 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({ const eventA = utils.mkMessage({
room: roomId, user: userA, event: true, room: roomId, user: userA, event: true,
}); });
@@ -1318,7 +1318,7 @@ describe("Room", function() {
}); });
it("should apply member events", async 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])); await room.loadOutOfBandMembers(Promise.resolve([memberEvent]));
const memberA = room.getMember("@user_a:bar"); const memberA = room.getMember("@user_a:bar");
expect(memberA.name).toEqual("User A"); expect(memberA.name).toEqual("User A");
@@ -1329,7 +1329,7 @@ describe("Room", function() {
user: "@user_a:bar", mship: "join", user: "@user_a:bar", mship: "join",
room: roomId, event: true, name: "Ms A", room: roomId, event: true, name: "Ms A",
}); });
const room = new Room(roomId); const room = new Room(roomId, null);
const promise2 = Promise.resolve([memberEvent2]); const promise2 = Promise.resolve([memberEvent2]);
const promise1 = promise2.then(() => [memberEvent]); const promise1 = promise2.then(() => [memberEvent]);
@@ -1342,7 +1342,7 @@ describe("Room", function() {
}); });
it("should revert needs loading on error", async function() { it("should revert needs loading on error", async function() {
const room = new Room(roomId); const room = new Room(roomId, null);
let hasThrown = false; let hasThrown = false;
try { try {
await room.loadOutOfBandMembers(Promise.reject(new Error("bugger"))); await room.loadOutOfBandMembers(Promise.reject(new Error("bugger")));
@@ -1353,4 +1353,17 @@ describe("Room", function() {
expect(room.needsOutOfBandMembers()).toEqual(true); 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");
});
});
}); });

View File

@@ -68,6 +68,7 @@ function synthesizeReceipt(userId, event, receiptType) {
* @constructor * @constructor
* @alias module:models/room * @alias module:models/room
* @param {string} roomId Required. The ID of this room. * @param {string} roomId Required. The ID of this room.
* @param {string} myUserId Required. The ID of the syncing user.
* @param {Object=} opts Configuration options * @param {Object=} opts Configuration options
* @param {*} opts.storageToken Optional. The token which a data store can use * @param {*} opts.storageToken Optional. The token which a data store can use
* to remember the state of the room. What this means is dependent on the store * to remember the state of the room. What this means is dependent on the store
@@ -102,7 +103,7 @@ function synthesizeReceipt(userId, event, receiptType) {
* @prop {*} storageToken A token which a data store can use to remember * @prop {*} storageToken A token which a data store can use to remember
* the state of the room. * the state of the room.
*/ */
function Room(roomId, opts) { function Room(roomId, myUserId, opts) {
opts = opts || {}; opts = opts || {};
opts.pendingEventOrdering = opts.pendingEventOrdering || "chronological"; opts.pendingEventOrdering = opts.pendingEventOrdering || "chronological";
@@ -115,6 +116,7 @@ function Room(roomId, opts) {
); );
} }
this.myUserId = myUserId;
this.roomId = roomId; this.roomId = roomId;
this.name = roomId; this.name = roomId;
this.tags = { this.tags = {
@@ -228,9 +230,9 @@ Room.prototype.getLastEventId = function() {
* @param {string} myUserId the user id for the logged in member * @param {string} myUserId the user id for the logged in member
* @return {string} the membership type (join | leave | invite) for the logged in user * @return {string} the membership type (join | leave | invite) for the logged in user
*/ */
Room.prototype.getMyMembership = function(myUserId) { Room.prototype.getMyMembership = function() {
if (myUserId) { if (this.myUserId) {
const me = this.getMember(myUserId); const me = this.getMember(this.myUserId);
if (me) { if (me) {
return me.membership; return me.membership;
} }
@@ -1014,15 +1016,14 @@ Room.prototype.removeEvent = function(eventId) {
* Recalculate various aspects of the room, including the room name and * Recalculate various aspects of the room, including the room name and
* room summary. Call this any time the room's current state is modified. * room summary. Call this any time the room's current state is modified.
* May fire "Room.name" if the room name is updated. * May fire "Room.name" if the room name is updated.
* @param {string} userId The client's user ID.
* @fires module:client~MatrixClient#event:"Room.name" * @fires module:client~MatrixClient#event:"Room.name"
*/ */
Room.prototype.recalculate = function(userId) { Room.prototype.recalculate = function() {
// set fake stripped state events if this is an invite room so logic remains // set fake stripped state events if this is an invite room so logic remains
// consistent elsewhere. // consistent elsewhere.
const self = this; const self = this;
const membershipEvent = this.currentState.getStateEvents( const membershipEvent = this.currentState.getStateEvents(
"m.room.member", userId, "m.room.member", this.myUserId,
); );
if (membershipEvent && membershipEvent.getContent().membership === "invite") { if (membershipEvent && membershipEvent.getContent().membership === "invite") {
const strippedStateEvents = membershipEvent.event.invite_room_state || []; const strippedStateEvents = membershipEvent.event.invite_room_state || [];
@@ -1038,14 +1039,14 @@ Room.prototype.recalculate = function(userId) {
content: strippedEvent.content, content: strippedEvent.content,
event_id: "$fake" + Date.now(), event_id: "$fake" + Date.now(),
room_id: self.roomId, room_id: self.roomId,
user_id: userId, // technically a lie user_id: self.myUserId, // technically a lie
})]); })]);
} }
}); });
} }
const oldName = this.name; const oldName = this.name;
this.name = calculateRoomName(this, userId); this.name = calculateRoomName(this, this.myUserId);
this.summary = new RoomSummary(this.roomId, { this.summary = new RoomSummary(this.roomId, {
title: this.name, title: this.name,
}); });
@@ -1320,7 +1321,7 @@ function calculateRoomName(room, userId, ignoreRoomNameEvent) {
return memberNamesToRoomName(otherNames, inviteJoinCount); return memberNamesToRoomName(otherNames, inviteJoinCount);
} }
const myMembership = room.getMyMembership(userId); const myMembership = room.getMyMembership();
// if I have created a room and invited people throuh // if I have created a room and invited people throuh
// 3rd party invites // 3rd party invites
if (myMembership == 'join') { if (myMembership == 'join') {

View File

@@ -113,7 +113,7 @@ function SyncApi(client, opts) {
*/ */
SyncApi.prototype.createRoom = function(roomId) { SyncApi.prototype.createRoom = function(roomId) {
const client = this.client; const client = this.client;
const room = new Room(roomId, { const room = new Room(roomId, client.getUserId(), {
pendingEventOrdering: this.opts.pendingEventOrdering, pendingEventOrdering: this.opts.pendingEventOrdering,
timelineSupport: client.timelineSupport, timelineSupport: client.timelineSupport,
}); });
@@ -232,7 +232,7 @@ SyncApi.prototype.syncLeftRooms = function() {
self._processRoomEvents(room, stateEvents, timelineEvents); self._processRoomEvents(room, stateEvents, timelineEvents);
room.recalculate(client.credentials.userId); room.recalculate();
client.store.storeRoom(room); client.store.storeRoom(room);
client.emit("Room", room); client.emit("Room", room);
@@ -303,7 +303,7 @@ SyncApi.prototype.peek = function(roomId) {
peekRoom.currentState.setStateEvents(stateEvents); peekRoom.currentState.setStateEvents(stateEvents);
self._resolveInvites(peekRoom); self._resolveInvites(peekRoom);
peekRoom.recalculate(self.client.credentials.userId); peekRoom.recalculate();
// roll backwards to diverge old state. addEventsToTimeline // roll backwards to diverge old state. addEventsToTimeline
// will overwrite the pagination token, so make sure it overwrites // will overwrite the pagination token, so make sure it overwrites
@@ -969,7 +969,7 @@ SyncApi.prototype._processSyncResponse = async function(
self._mapSyncEventsFormat(inviteObj.invite_state, room); self._mapSyncEventsFormat(inviteObj.invite_state, room);
self._processRoomEvents(room, stateEvents); self._processRoomEvents(room, stateEvents);
if (inviteObj.isBrandNewRoom) { if (inviteObj.isBrandNewRoom) {
room.recalculate(client.credentials.userId); room.recalculate();
client.store.storeRoom(room); client.store.storeRoom(room);
client.emit("Room", room); client.emit("Room", room);
} }
@@ -1076,7 +1076,7 @@ SyncApi.prototype._processSyncResponse = async function(
// we deliberately don't add accountData to the timeline // we deliberately don't add accountData to the timeline
room.addAccountData(accountDataEvents); room.addAccountData(accountDataEvents);
room.recalculate(client.credentials.userId); room.recalculate();
if (joinObj.isBrandNewRoom) { if (joinObj.isBrandNewRoom) {
client.store.storeRoom(room); client.store.storeRoom(room);
client.emit("Room", room); client.emit("Room", room);
@@ -1116,7 +1116,7 @@ SyncApi.prototype._processSyncResponse = async function(
self._processRoomEvents(room, stateEvents, timelineEvents); self._processRoomEvents(room, stateEvents, timelineEvents);
room.addAccountData(accountDataEvents); room.addAccountData(accountDataEvents);
room.recalculate(client.credentials.userId); room.recalculate();
if (leaveObj.isBrandNewRoom) { if (leaveObj.isBrandNewRoom) {
client.store.storeRoom(room); client.store.storeRoom(room);
client.emit("Room", room); client.emit("Room", room);
@@ -1406,7 +1406,7 @@ SyncApi.prototype._processRoomEvents = function(room, stateEventList,
// a recalculation (like m.room.name) we won't recalculate until we've // a recalculation (like m.room.name) we won't recalculate until we've
// finished adding all the events, which will cause the notification to have // finished adding all the events, which will cause the notification to have
// the old room name rather than the new one. // the old room name rather than the new one.
room.recalculate(this.client.credentials.userId); room.recalculate();
// If the timeline wasn't empty, we process the state events here: they're // If the timeline wasn't empty, we process the state events here: they're
// defined as updates to the state before the start of the timeline, so this // defined as updates to the state before the start of the timeline, so this