1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-09-10 17:31:53 +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 = [];
}
if (data.rooms[i].membership === "invite") {
// create fake invite state event (v1 sucks)
data.rooms[i].state.push({
event_id: "$fake_" + room.roomId,
content: {
membership: "invite"
},
state_key: client.credentials.userId,
user_id: data.rooms[i].inviter,
room_id: room.roomId,
type: "m.room.member"
});
var inviteEvent = data.rooms[i].invite;
if (!inviteEvent) {
// fallback for servers which don't serve the invite key yet
inviteEvent = {
event_id: "$fake_" + room.roomId,
content: {
membership: "invite"
},
state_key: client.credentials.userId,
user_id: data.rooms[i].inviter,
room_id: room.roomId,
type: "m.room.member"
};
}
data.rooms[i].state.push(inviteEvent);
}
_processRoomEvents(

View File

@@ -6,6 +6,7 @@ var EventEmitter = require("events").EventEmitter;
var RoomState = require("./room-state");
var RoomSummary = require("./room-summary");
var MatrixEvent = require("./event").MatrixEvent;
var utils = require("../utils");
/**
@@ -231,6 +232,34 @@ Room.prototype.addEvents = function(events, duplicateStrategy) {
* @fires module:client~MatrixClient#event:"Room.name"
*/
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;
this.name = calculateRoomName(this, userId);
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 = {
// 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)" +
" 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;
describe("Room.recalculate => Stripped State Events", function() {
it("should set stripped state events as actual state events if the " +
"room is an invite room", function() {
var roomName = "flibble";
addMember(userA, "invite");
stateLookup["m.room.member$" + userA].event.invite_room_state = [
{
type: "m.room.name",
state_key: "",
content: {
name: roomName
}
}
];
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)" +
" room if a room name and alias don't exist and there are >2 members.",
function() {
setJoinRule("invite");
addMember(userA);
addMember(userB);
addMember(userC);
room.recalculate(userA);
var name = room.name;
expect(name.indexOf(userB)).not.toEqual(-1, name);
expect(name.indexOf(userC)).not.toEqual(-1, name);
});
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.",
function() {
setJoinRule("invite");
addMember(userA);
addMember(userB);
addMember(userC);
room.recalculate(userA);
var name = room.name;
expect(name.indexOf(userB)).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)" +
" room if a room name and alias don't exist and there are >2 members.",
function() {
setJoinRule("public");
addMember(userA);
addMember(userB);
addMember(userC);
room.recalculate(userA);
var name = room.name;
expect(name.indexOf(userB)).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)" +
" room if a room name and alias don't exist and there are >2 members.",
function() {
setJoinRule("public");
addMember(userA);
addMember(userB);
addMember(userC);
room.recalculate(userA);
var name = room.name;
expect(name.indexOf(userB)).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)" +
" rooms if a room name and alias don't exist and it is a 1:1-chat.",
function() {
setJoinRule("public");
addMember(userA);
addMember(userB);
room.recalculate(userA);
var name = room.name;
expect(name.indexOf(userB)).not.toEqual(-1, name);
});
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.",
function() {
setJoinRule("public");
addMember(userA);
addMember(userB);
room.recalculate(userA);
var name = room.name;
expect(name.indexOf(userB)).not.toEqual(-1, name);
});
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" +
" is a 1:1-chat.", function() {
setJoinRule("invite");
addMember(userA);
addMember(userB);
room.recalculate(userA);
var name = room.name;
expect(name.indexOf(userB)).not.toEqual(-1, name);
});
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" +
" is a 1:1-chat.", function() {
setJoinRule("invite");
addMember(userA);
addMember(userB);
room.recalculate(userA);
var name = room.name;
expect(name.indexOf(userB)).not.toEqual(-1, name);
});
it("should show the other user's name for private" +
" (invite join_rules) rooms if you are invited to it.", function() {
setJoinRule("invite");
addMember(userA, "invite");
addMember(userB);
room.recalculate(userA);
var name = room.name;
expect(name.indexOf(userB)).not.toEqual(-1, name);
});
it("should show the other user's name for private" +
" (invite join_rules) rooms if you are invited to it.", function() {
setJoinRule("invite");
addMember(userA, "invite");
addMember(userB);
room.recalculate(userA);
var name = room.name;
expect(name.indexOf(userB)).not.toEqual(-1, name);
});
it("should show the room alias if one exists for private " +
"(invite join_rules) rooms if a room name doesn't exist.", function() {
var alias = "#room_alias:here";
setJoinRule("invite");
setAliases([alias, "#another:one"]);
room.recalculate(userA);
var name = room.name;
expect(name).toEqual(alias);
});
it("should show the room alias if one exists for private " +
"(invite join_rules) rooms if a room name doesn't exist.", function() {
var alias = "#room_alias:here";
setJoinRule("invite");
setAliases([alias, "#another:one"]);
room.recalculate(userA);
var name = room.name;
expect(name).toEqual(alias);
});
it("should show the room alias if one exists for public " +
"(public join_rules) rooms if a room name doesn't exist.", function() {
var alias = "#room_alias:here";
setJoinRule("public");
setAliases([alias, "#another:one"]);
room.recalculate(userA);
var name = room.name;
expect(name).toEqual(alias);
});
it("should show the room alias if one exists for public " +
"(public join_rules) rooms if a room name doesn't exist.", function() {
var alias = "#room_alias:here";
setJoinRule("public");
setAliases([alias, "#another:one"]);
room.recalculate(userA);
var name = room.name;
expect(name).toEqual(alias);
});
it("should show the room name if one exists for private " +
"(invite join_rules) rooms.", function() {
var roomName = "A mighty name indeed";
setJoinRule("invite");
setRoomName(roomName);
room.recalculate(userA);
var name = room.name;
expect(name).toEqual(roomName);
});
it("should show the room name if one exists for private " +
"(invite join_rules) rooms.", function() {
var roomName = "A mighty name indeed";
setJoinRule("invite");
setRoomName(roomName);
room.recalculate(userA);
var name = room.name;
expect(name).toEqual(roomName);
});
it("should show the room name if one exists for public " +
"(public join_rules) rooms.", function() {
var roomName = "A mighty name indeed";
setJoinRule("public");
setRoomName(roomName);
room.recalculate(userA);
var name = room.name;
expect(name).toEqual(roomName);
});
it("should show the room name if one exists for public " +
"(public join_rules) rooms.", function() {
var roomName = "A mighty name indeed";
setJoinRule("public");
setRoomName(roomName);
room.recalculate(userA);
var name = room.name;
expect(name).toEqual(roomName);
});
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() {
setJoinRule("invite");
addMember(userA);
room.recalculate(userA);
var name = room.name;
expect(name).toEqual(userA);
});
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() {
setJoinRule("invite");
addMember(userA);
room.recalculate(userA);
var name = room.name;
expect(name).toEqual(userA);
});
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() {
setJoinRule("public");
addMember(userA);
room.recalculate(userA);
var name = room.name;
expect(name).toEqual(userA);
});
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() {
setJoinRule("public");
addMember(userA);
room.recalculate(userA);
var name = room.name;
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("?");
});
});