Skip to content

Commit

Permalink
Add room.getAliases() and room.getCanonicalAlias()
Browse files Browse the repository at this point in the history
  • Loading branch information
dbkr committed Jun 17, 2016
1 parent 0f153fd commit e943bc4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Changes in [?](?) (?)
=====================

* Add room.getAliases() and room.getCanonicalAlias

Changes in [0.5.4](https://github.com/matrix-org/matrix-js-sdk/releases/tag/v0.5.4) (2016-06-02)
================================================================================================
[Full Changelog](https://github.com/matrix-org/matrix-js-sdk/compare/v0.5.3...v0.5.4)
Expand Down
47 changes: 39 additions & 8 deletions lib/models/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,40 @@ Room.prototype.getAvatarUrl = function(baseUrl, width, height, resizeMethod,
return null;
};

/**
* Get the aliases this room has according to the room's state
* The aliases returned by this function may not necessarily
* still point to this room.
* @return {array} The room's alias as an array of strings
*/
Room.prototype.getAliases = function() {
var alias_strings = [];

var alias_events = this.currentState.getStateEvents("m.room.aliases");
if (alias_events) {
for (var alias_event of alias_events) {
if (utils.isArray(alias_event.getContent().aliases)) {
Array.prototype.push.apply(alias_strings, alias_event.getContent().aliases);
}
}
}
return alias_strings;
};

/**
* Get this room's canonical alias
* The alias returned by this function may not necessarily
* still point to this room.
* @return {?string} The room's canonical alias, or null if there is none
*/
Room.prototype.getCanonicalAlias = function() {
var canonicalAlias = this.currentState.getStateEvents("m.room.canonical_alias", "");
if (canonicalAlias) {
return canonicalAlias.getContent().alias;
}
return null;
};

/**
* Get a member from the current room state.
* @param {string} userId The user ID of the member.
Expand Down Expand Up @@ -1344,16 +1378,13 @@ function calculateRoomName(room, userId, ignoreRoomNameEvent) {
}
}

var alias;
var canonicalAlias = room.currentState.getStateEvents("m.room.canonical_alias", "");
if (canonicalAlias) {
alias = canonicalAlias.getContent().alias;
}
var alias = room.getCanonicalAlias();

if (!alias) {
var mRoomAliases = room.currentState.getStateEvents("m.room.aliases")[0];
if (mRoomAliases && utils.isArray(mRoomAliases.getContent().aliases)) {
alias = mRoomAliases.getContent().aliases[0];
var aliases = room.getAliases();

if (aliases.length) {
alias = aliases[0];
}
}
if (alias) {
Expand Down

0 comments on commit e943bc4

Please sign in to comment.