You've already forked matrix-js-sdk
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:
@@ -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"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user