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

Move getFriendlyRoomName to Room. Add recalculate() function to cache info.

This commit is contained in:
Kegan Dougal
2015-06-08 16:10:23 +01:00
parent 9fa7fa0487
commit 2d00998b61
5 changed files with 89 additions and 67 deletions

View File

@@ -3,6 +3,8 @@
* @module models/room
*/
var RoomState = require("./room-state");
var RoomSummary = require("./room-summary");
var utils = require("../utils");
/**
* Construct a new Room.
@@ -43,6 +45,62 @@ Room.prototype = {
this.timeline.push(events[i]);
}
}
},
/**
* Recalculate various aspects of the room, including the room name and
* room summary. Call this any time the room's current state is modified.
* @param {string} userId The client's user ID.
*/
recalculate: function(userId) {
this.name = this.calculateRoomName(userId);
this.summary = new RoomSummary(this.roomId, {
title: this.name
});
},
/**
* Calculates the name of the room from the current room state.
* @param {string} userId The client's user ID. Used to filter room members
* correctly.
* @return {string} The calculated room name.
*/
calculateRoomName: function(userId) {
// check for an alias, if any. for now, assume first alias is the
// official one.
var alias;
var mRoomAliases = this.currentState.getStateEvents("m.room.aliases")[0];
if (mRoomAliases && utils.isArray(mRoomAliases.getContent().aliases)) {
alias = mRoomAliases.getContent().aliases[0];
}
var mRoomName = this.currentState.getStateEvents('m.room.name', '');
if (mRoomName) {
return mRoomName.getContent().name + (alias ? " (" + alias + ")" : "");
}
else if (alias) {
return alias;
}
else {
var members = this.currentState.getMembers();
if (members.length === 0) {
return "Unknown";
}
else if (members.length == 1) {
return members[0].name;
}
else if (members.length == 2) {
return (
members[0].name + " and " + members[1].name
);
}
else {
return (
members[0].name + " and " + (members.length - 1) + " others"
);
}
}
}
};