Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add room.getAliases() and room.getCanonicalAlias() #140

Merged
merged 2 commits into from
Jun 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Changes in [?](?) (?)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs to contain the word 'unreleased' to avoid confusing the changelog generator.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot I'd written this. fixed on develop.

=====================

* 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
50 changes: 42 additions & 8 deletions lib/models/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,43 @@ 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 i = 0; i < alias_events.length; ++i) {
var alias_event = alias_events[i];
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 +1381,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