You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-07-30 04:23:07 +03:00
Add room name UTs and add self-chat room name.
This commit is contained in:
@ -123,7 +123,14 @@ Room.prototype = {
|
|||||||
});
|
});
|
||||||
// TODO: Localisation
|
// TODO: Localisation
|
||||||
if (members.length === 0) {
|
if (members.length === 0) {
|
||||||
return "Unknown";
|
if (this.currentState.getMembers().length === 1) {
|
||||||
|
// we exist, but no one else... self-chat!
|
||||||
|
return userId;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// there really isn't anyone in this room...
|
||||||
|
return "Unknown";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (members.length === 1) {
|
else if (members.length === 1) {
|
||||||
return members[0].name;
|
return members[0].name;
|
||||||
|
@ -7,6 +7,12 @@ var utils = require("../test-utils");
|
|||||||
describe("Room", function() {
|
describe("Room", function() {
|
||||||
var roomId = "!foo:bar";
|
var roomId = "!foo:bar";
|
||||||
var userA = "@alice:bar";
|
var userA = "@alice:bar";
|
||||||
|
var userB = "@bertha:bar";
|
||||||
|
var userC = "@clarissa:bar";
|
||||||
|
var userD = "@dorothy:bar";
|
||||||
|
var userAName = "Alice";
|
||||||
|
var userBName = "Big Bertha";
|
||||||
|
var userCName = "Clarissa Explains";
|
||||||
var room;
|
var room;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
@ -54,4 +60,201 @@ describe("Room", function() {
|
|||||||
expect(room.timeline[1]).toEqual(events[0]);
|
expect(room.timeline[1]).toEqual(events[0]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("calculateRoomName", function() {
|
||||||
|
var stateLookup = {
|
||||||
|
// event.type + "$" event.state_key : MatrixEvent
|
||||||
|
};
|
||||||
|
var mockRoomState = {
|
||||||
|
getStateEvents: function(type, key) {
|
||||||
|
if (key === undefined) {
|
||||||
|
var prefix = type+"$";
|
||||||
|
var list = [];
|
||||||
|
for (var stateBlob in stateLookup) {
|
||||||
|
if (!stateLookup.hasOwnProperty(stateBlob)) { continue; }
|
||||||
|
if (stateBlob.indexOf(prefix) === 0) {
|
||||||
|
list.push(stateLookup[stateBlob]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return stateLookup[type+"$"+key];
|
||||||
|
}
|
||||||
|
},
|
||||||
|
getMembers: function() {
|
||||||
|
var memberEvents = this.getStateEvents("m.room.member");
|
||||||
|
var members = [];
|
||||||
|
for (var i = 0; i < memberEvents.length; i++) {
|
||||||
|
members.push({
|
||||||
|
// not interested in user ID vs display name semantics.
|
||||||
|
// That should be tested in RoomMember UTs.
|
||||||
|
name: memberEvents[i].getSender(),
|
||||||
|
userId: memberEvents[i].getSender()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return members;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var setJoinRule = function(rule) {
|
||||||
|
stateLookup["m.room.join_rules$"] = new MatrixEvent(
|
||||||
|
utils.mkEvent("m.room.join_rules", roomId, userA, {
|
||||||
|
join_rule: rule
|
||||||
|
})
|
||||||
|
);
|
||||||
|
};
|
||||||
|
var setAliases = function(aliases, stateKey) {
|
||||||
|
if (!stateKey) { stateKey = "flibble"; }
|
||||||
|
stateLookup["m.room.aliases$"+stateKey] = new MatrixEvent(
|
||||||
|
utils.mkEvent("m.room.aliases", roomId, stateKey, {
|
||||||
|
aliases: aliases
|
||||||
|
})
|
||||||
|
);
|
||||||
|
};
|
||||||
|
var setRoomName = function(name) {
|
||||||
|
stateLookup["m.room.name$"] = new MatrixEvent(
|
||||||
|
utils.mkEvent("m.room.name", roomId, userA, {
|
||||||
|
name: name
|
||||||
|
})
|
||||||
|
);
|
||||||
|
};
|
||||||
|
var addMember = function(userId, state) {
|
||||||
|
if (!state) { state = "join"; }
|
||||||
|
stateLookup["m.room.member$"+userId] = new MatrixEvent(
|
||||||
|
utils.mkMembership(roomId, state, userId, userId)
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
beforeEach(function() {
|
||||||
|
stateLookup = {};
|
||||||
|
room.currentState = mockRoomState;
|
||||||
|
});
|
||||||
|
|
||||||
|
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);
|
||||||
|
var name = room.calculateRoomName(userA);
|
||||||
|
// 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);
|
||||||
|
});
|
||||||
|
|
||||||
|
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);
|
||||||
|
var name = room.calculateRoomName(userA);
|
||||||
|
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);
|
||||||
|
var name = room.calculateRoomName(userA);
|
||||||
|
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);
|
||||||
|
var name = room.calculateRoomName(userA);
|
||||||
|
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);
|
||||||
|
var name = room.calculateRoomName(userA);
|
||||||
|
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);
|
||||||
|
var name = room.calculateRoomName(userA);
|
||||||
|
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"]);
|
||||||
|
var name = room.calculateRoomName(userA);
|
||||||
|
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"]);
|
||||||
|
var name = room.calculateRoomName(userA);
|
||||||
|
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);
|
||||||
|
var name = room.calculateRoomName(userA);
|
||||||
|
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);
|
||||||
|
var name = room.calculateRoomName(userA);
|
||||||
|
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);
|
||||||
|
var name = room.calculateRoomName(userA);
|
||||||
|
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);
|
||||||
|
var name = room.calculateRoomName(userA);
|
||||||
|
expect(name).toEqual(userA);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user