1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2026-01-03 23:22:30 +03:00

add getFriendlyDisplayName for completeness

This commit is contained in:
Matthew Hodgson
2015-05-19 17:11:36 +01:00
parent 32051c319c
commit 9e6394d1f8

View File

@@ -6,6 +6,8 @@ TODO:
- Internal: rate limiting
- Identity server: linkEmail, authEmail, bindEmail, lookup3pid
- uploadContent (?)
- Need a nice way to callback the app for arbitrary events like displayname changes
due to ambiguity (or should this be on a chat-specific layer)?
*/
// wrap in a closure for browsers
@@ -108,7 +110,10 @@ var init = function(exports){
};
exports.MatrixInMemoryStore = MatrixInMemoryStore;
// XXX: this is currently quite procedural - we could possibly pass back
// models of Rooms, Users, Events, etc instead.
MatrixInMemoryStore.prototype = {
/*
* Add an array of one or more state MatrixEvents into the store, overwriting
* any existing state with the same {room, type, stateKey} tuple.
@@ -209,7 +214,7 @@ var init = function(exports){
/*
* Get the timeline of events for a given room
* TODO: ordering?
* TODO: ordering!
*/
getEvents: function(roomId) {
return this.room[roomId].timeline;
@@ -252,14 +257,15 @@ var init = function(exports){
// Higher level APIs
// =================
// stuff to handle:
// TODO: stuff to handle:
// local echo
// disambiguating display names in a room
// event dup suppression? - apparently we should still be doing so
// tracking current display name / avatar per-message
// pagination
// reconnection - DONE
// re-sending
/*
* Helper method for retrieving the name of a room suitable for display in the UI
* TODO: in future, this should be being generated serverside.
@@ -306,6 +312,38 @@ var init = function(exports){
}
},
/*
* Helper method for retrieving the name of a user suitable for display in the UI
* in the context of a room - i.e. disambiguating from any other users in the room.
* XXX: This could perhaps also be generated serverside, perhaps by just passing
* a 'disambiguate' flag down on membership entries which have ambiguous displaynames?
*/
getFriendlyDisplayName: function(userId, roomId) {
// we need a store to track the inputs for calculating display names
if (!this.store) return userId;
var displayName;
var memberEvent = this.store.getStateEvent(roomId, 'm.room.member', userId);
if (memberEvent) {
displayName = memberEvent.event.content.displayname;
}
else {
return userId;
}
var members = this.store.getStateEvents(roomId, 'm.room.member')
.filter(function(event) {
return event.event.content.displayname === displayName;
});
if (members.length > 1) {
return displayName + " (" + userId + ")";
}
else {
return displayName;
}
},
/*
* High level helper method to call initialSync, emit the resulting events,
* and then start polling the eventStream for new events.