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

Add support for m.room.avatar: refactor avatar URLs

BREAKING CHANGE.

Scope each "getAvatarUrl" to be instance methods on the entity it
relates to (Room and RoomMember respectively). By doing this, we
can actually pull out specific state such as the `m.room.avatar`
event more easily rather than keeping it in the global cesspit
of `MatrixClient`.

This was complicated by `getHttpUriForMxc` and `getIdenticonUri`
which were attached to the HTTP API to pull out the `baseUrl` when
crafting the URL. Pull out this dependency out and explicitly pass
it in when crafting the URL. This is trivial to get from
`MatrixClient.getHomeserverUrl()`.
This commit is contained in:
Kegan Dougal
2015-10-19 14:14:34 +01:00
parent 19721c3f53
commit f26154d0ac
5 changed files with 147 additions and 137 deletions

View File

@@ -17,6 +17,7 @@ var Room = require("./models/room");
var User = require("./models/user");
var webRtcCall = require("./webrtc/call");
var utils = require("./utils");
var contentRepo = require("./content-repo");
var CRYPTO_ENABLED = false;
@@ -1403,67 +1404,6 @@ MatrixClient.prototype.setAvatarUrl = function(url, callback) {
);
};
/**
* Get the avatar URL for a room member. <strong>This method is experimental and
* may change.</strong>
* @param {module:room-member.RoomMember} member
* @param {Number} width The desired width of the thumbnail.
* @param {Number} height The desired height of the thumbnail.
* @param {string} resizeMethod The thumbnail resize method to use, either
* "crop" or "scale".
* @param {Boolean} allowDefault (optional) Passing false causes this method to
* return null if the user has no avatar image. Otherwise, a default image URL
* will be returned.
* @return {?string} the avatar URL or null.
*/
MatrixClient.prototype.getAvatarUrlForMember =
function(member, width, height, resizeMethod, allowDefault) {
if (!member || !member.events.member) {
return null;
}
if (allowDefault === undefined) { allowDefault = true; }
var rawUrl = member.events.member.getContent().avatar_url;
if (rawUrl) {
return this._http.getHttpUriForMxc(rawUrl, width, height, resizeMethod);
} else if (allowDefault) {
return this._http.getIdenticonUri(member.userId, width, height);
}
return null;
};
/**
* Get the avatar URL for a room. <strong>This method is experimental and
* may change.</strong>
* @param {module:room.Room} room
* @param {Number} width The desired width of the thumbnail.
* @param {Number} height The desired height of the thumbnail.
* @param {string} resizeMethod The thumbnail resize method to use, either
* "crop" or "scale".
* @param {Boolean} allowDefault (optional) Passing false causes this method to
* return null if the user has no avatar image. Otherwise, a default image URL
* will be returned.
* @return {?string} the avatar URL or null.
*/
MatrixClient.prototype.getAvatarUrlForRoom =
function(room, width, height, resizeMethod, allowDefault) {
if (!room || !room.currentState || !room.currentState.members) {
return null;
}
var userId = this.credentials.userId;
var members = utils.filter(room.currentState.getMembers(), function(m) {
return (m.membership === "join" && m.userId !== userId);
});
if (members[0]) {
return this.getAvatarUrlForMember(
members[0], width, height, resizeMethod, allowDefault
);
}
return null;
};
/**
* Turn an MXC URL into an HTTP one. <strong>This method is experimental and
* may change.</strong>
@@ -1476,7 +1416,9 @@ MatrixClient.prototype.getAvatarUrlForRoom =
*/
MatrixClient.prototype.mxcUrlToHttp =
function(mxcUrl, width, height, resizeMethod) {
return this._http.getHttpUriForMxc(mxcUrl, width, height, resizeMethod);
return contentRepo.getHttpUriForMxc(
this.baseUrl, mxcUrl, width, height, resizeMethod
);
};
/**