1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-09-01 21:21:58 +03:00

Merge pull request #26 from matrix-org/invite-room-state

Invite room state
This commit is contained in:
Kegsay
2015-10-19 15:31:45 +01:00
3 changed files with 221 additions and 142 deletions

View File

@@ -1927,17 +1927,21 @@ function doInitialSync(client, historyLen, includeArchived) {
data.rooms[i].state = []; data.rooms[i].state = [];
} }
if (data.rooms[i].membership === "invite") { if (data.rooms[i].membership === "invite") {
// create fake invite state event (v1 sucks) var inviteEvent = data.rooms[i].invite;
data.rooms[i].state.push({ if (!inviteEvent) {
event_id: "$fake_" + room.roomId, // fallback for servers which don't serve the invite key yet
content: { inviteEvent = {
membership: "invite" event_id: "$fake_" + room.roomId,
}, content: {
state_key: client.credentials.userId, membership: "invite"
user_id: data.rooms[i].inviter, },
room_id: room.roomId, state_key: client.credentials.userId,
type: "m.room.member" user_id: data.rooms[i].inviter,
}); room_id: room.roomId,
type: "m.room.member"
};
}
data.rooms[i].state.push(inviteEvent);
} }
_processRoomEvents( _processRoomEvents(

View File

@@ -6,6 +6,7 @@ var EventEmitter = require("events").EventEmitter;
var RoomState = require("./room-state"); var RoomState = require("./room-state");
var RoomSummary = require("./room-summary"); var RoomSummary = require("./room-summary");
var MatrixEvent = require("./event").MatrixEvent;
var utils = require("../utils"); var utils = require("../utils");
/** /**
@@ -231,6 +232,34 @@ Room.prototype.addEvents = function(events, duplicateStrategy) {
* @fires module:client~MatrixClient#event:"Room.name" * @fires module:client~MatrixClient#event:"Room.name"
*/ */
Room.prototype.recalculate = function(userId) { Room.prototype.recalculate = function(userId) {
// set fake stripped state events if this is an invite room so logic remains
// consistent elsewhere.
var self = this;
var membershipEvent = this.currentState.getStateEvents(
"m.room.member", userId
);
if (membershipEvent && membershipEvent.getContent().membership === "invite") {
var strippedStateEvents = membershipEvent.event.invite_room_state || [];
utils.forEach(strippedStateEvents, function(strippedEvent) {
var existingEvent = self.currentState.getStateEvents(
strippedEvent.type, strippedEvent.state_key
);
if (!existingEvent) {
// set the fake stripped event instead
self.currentState.setStateEvents([new MatrixEvent({
type: strippedEvent.type,
state_key: strippedEvent.state_key,
content: strippedEvent.content,
event_id: "$fake" + Date.now(),
room_id: self.roomId,
user_id: userId // technically a lie
})]);
}
});
}
var oldName = this.name; var oldName = this.name;
this.name = calculateRoomName(this, userId); this.name = calculateRoomName(this, userId);
this.summary = new RoomSummary(this.roomId, { this.summary = new RoomSummary(this.roomId, {

View File

@@ -338,7 +338,7 @@ describe("Room", function() {
}); });
}); });
describe("recalculate (Room Name)", function() { describe("recalculate", function() {
var stateLookup = { var stateLookup = {
// event.type + "$" event.state_key : MatrixEvent // event.type + "$" event.state_key : MatrixEvent
}; };
@@ -405,149 +405,195 @@ describe("Room", function() {
}); });
}); });
it("should return the names of members in a private (invite join_rules)" + describe("Room.recalculate => Stripped State Events", function() {
" room if a room name and alias don't exist and there are >3 members.", it("should set stripped state events as actual state events if the " +
function() { "room is an invite room", function() {
setJoinRule("invite"); var roomName = "flibble";
addMember(userA);
addMember(userB); addMember(userA, "invite");
addMember(userC); stateLookup["m.room.member$" + userA].event.invite_room_state = [
addMember(userD); {
room.recalculate(userA); type: "m.room.name",
var name = room.name; state_key: "",
// we expect at least 1 member to be mentioned content: {
var others = [userB, userC, userD]; name: roomName
var found = false; }
for (var i = 0; i < others.length; i++) { }
if (name.indexOf(others[i]) !== -1) { ];
found = true;
break; room.recalculate(userA);
expect(room.currentState.setStateEvents).toHaveBeenCalled();
// first call, first arg (which is an array), first element in array
var fakeEvent = room.currentState.setStateEvents.calls[0].args[0][0];
expect(fakeEvent.getContent()).toEqual({
name: roomName
});
});
it("should not clobber state events if it isn't an invite room", function() {
addMember(userA, "join");
stateLookup["m.room.member$" + userA].event.invite_room_state = [
{
type: "m.room.name",
state_key: "",
content: {
name: "flibble"
}
}
];
room.recalculate(userA);
expect(room.currentState.setStateEvents).not.toHaveBeenCalled();
});
});
describe("Room.recalculate => Room Name", function() {
it("should return the names of members in a private (invite join_rules)" +
" room if a room name and alias don't exist and there are >3 members.",
function() {
setJoinRule("invite");
addMember(userA);
addMember(userB);
addMember(userC);
addMember(userD);
room.recalculate(userA);
var name = room.name;
// we expect at least 1 member to be mentioned
var others = [userB, userC, userD];
var found = false;
for (var i = 0; i < others.length; i++) {
if (name.indexOf(others[i]) !== -1) {
found = true;
break;
}
} }
} expect(found).toEqual(true, name);
expect(found).toEqual(true, name); });
});
it("should return the names of members in a private (invite join_rules)" + it("should return the names of members in a private (invite join_rules)" +
" room if a room name and alias don't exist and there are >2 members.", " room if a room name and alias don't exist and there are >2 members.",
function() { function() {
setJoinRule("invite"); setJoinRule("invite");
addMember(userA); addMember(userA);
addMember(userB); addMember(userB);
addMember(userC); addMember(userC);
room.recalculate(userA); room.recalculate(userA);
var name = room.name; var name = room.name;
expect(name.indexOf(userB)).not.toEqual(-1, name); expect(name.indexOf(userB)).not.toEqual(-1, name);
expect(name.indexOf(userC)).not.toEqual(-1, name); expect(name.indexOf(userC)).not.toEqual(-1, name);
}); });
it("should return the names of members in a public (public join_rules)" + it("should return the names of members in a public (public join_rules)" +
" room if a room name and alias don't exist and there are >2 members.", " room if a room name and alias don't exist and there are >2 members.",
function() { function() {
setJoinRule("public"); setJoinRule("public");
addMember(userA); addMember(userA);
addMember(userB); addMember(userB);
addMember(userC); addMember(userC);
room.recalculate(userA); room.recalculate(userA);
var name = room.name; var name = room.name;
expect(name.indexOf(userB)).not.toEqual(-1, name); expect(name.indexOf(userB)).not.toEqual(-1, name);
expect(name.indexOf(userC)).not.toEqual(-1, name); expect(name.indexOf(userC)).not.toEqual(-1, name);
}); });
it("should show the other user's name for public (public join_rules)" + it("should show the other user's name for public (public join_rules)" +
" rooms if a room name and alias don't exist and it is a 1:1-chat.", " rooms if a room name and alias don't exist and it is a 1:1-chat.",
function() { function() {
setJoinRule("public"); setJoinRule("public");
addMember(userA); addMember(userA);
addMember(userB); addMember(userB);
room.recalculate(userA); room.recalculate(userA);
var name = room.name; var name = room.name;
expect(name.indexOf(userB)).not.toEqual(-1, name); expect(name.indexOf(userB)).not.toEqual(-1, name);
}); });
it("should show the other user's name for private " + it("should show the other user's name for private " +
"(invite join_rules) rooms if a room name and alias don't exist and it" + "(invite join_rules) rooms if a room name and alias don't exist and it" +
" is a 1:1-chat.", function() { " is a 1:1-chat.", function() {
setJoinRule("invite"); setJoinRule("invite");
addMember(userA); addMember(userA);
addMember(userB); addMember(userB);
room.recalculate(userA); room.recalculate(userA);
var name = room.name; var name = room.name;
expect(name.indexOf(userB)).not.toEqual(-1, name); expect(name.indexOf(userB)).not.toEqual(-1, name);
}); });
it("should show the other user's name for private" + it("should show the other user's name for private" +
" (invite join_rules) rooms if you are invited to it.", function() { " (invite join_rules) rooms if you are invited to it.", function() {
setJoinRule("invite"); setJoinRule("invite");
addMember(userA, "invite"); addMember(userA, "invite");
addMember(userB); addMember(userB);
room.recalculate(userA); room.recalculate(userA);
var name = room.name; var name = room.name;
expect(name.indexOf(userB)).not.toEqual(-1, name); expect(name.indexOf(userB)).not.toEqual(-1, name);
}); });
it("should show the room alias if one exists for private " + it("should show the room alias if one exists for private " +
"(invite join_rules) rooms if a room name doesn't exist.", function() { "(invite join_rules) rooms if a room name doesn't exist.", function() {
var alias = "#room_alias:here"; var alias = "#room_alias:here";
setJoinRule("invite"); setJoinRule("invite");
setAliases([alias, "#another:one"]); setAliases([alias, "#another:one"]);
room.recalculate(userA); room.recalculate(userA);
var name = room.name; var name = room.name;
expect(name).toEqual(alias); expect(name).toEqual(alias);
}); });
it("should show the room alias if one exists for public " + it("should show the room alias if one exists for public " +
"(public join_rules) rooms if a room name doesn't exist.", function() { "(public join_rules) rooms if a room name doesn't exist.", function() {
var alias = "#room_alias:here"; var alias = "#room_alias:here";
setJoinRule("public"); setJoinRule("public");
setAliases([alias, "#another:one"]); setAliases([alias, "#another:one"]);
room.recalculate(userA); room.recalculate(userA);
var name = room.name; var name = room.name;
expect(name).toEqual(alias); expect(name).toEqual(alias);
}); });
it("should show the room name if one exists for private " + it("should show the room name if one exists for private " +
"(invite join_rules) rooms.", function() { "(invite join_rules) rooms.", function() {
var roomName = "A mighty name indeed"; var roomName = "A mighty name indeed";
setJoinRule("invite"); setJoinRule("invite");
setRoomName(roomName); setRoomName(roomName);
room.recalculate(userA); room.recalculate(userA);
var name = room.name; var name = room.name;
expect(name).toEqual(roomName); expect(name).toEqual(roomName);
}); });
it("should show the room name if one exists for public " + it("should show the room name if one exists for public " +
"(public join_rules) rooms.", function() { "(public join_rules) rooms.", function() {
var roomName = "A mighty name indeed"; var roomName = "A mighty name indeed";
setJoinRule("public"); setJoinRule("public");
setRoomName(roomName); setRoomName(roomName);
room.recalculate(userA); room.recalculate(userA);
var name = room.name; var name = room.name;
expect(name).toEqual(roomName); expect(name).toEqual(roomName);
}); });
it("should show your name for private (invite join_rules) rooms if" + it("should show your name for private (invite join_rules) rooms if" +
" 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(userA);
var name = room.name; var name = room.name;
expect(name).toEqual(userA); expect(name).toEqual(userA);
}); });
it("should show your name for public (public join_rules) rooms if a" + it("should show your name for public (public join_rules) rooms if a" +
" 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(userA);
var name = room.name; var name = room.name;
expect(name).toEqual(userA); expect(name).toEqual(userA);
}); });
it("should return '?' if there is no name, alias or members in the room.",
function() {
room.recalculate(userA);
var name = room.name;
expect(name).toEqual("?");
});
it("should return '?' if there is no name, alias or members in the room.",
function() {
room.recalculate(userA);
var name = room.name;
expect(name).toEqual("?");
}); });
}); });