You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-12-04 05:02:41 +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:
@@ -7,6 +7,7 @@ var EventEmitter = require("events").EventEmitter;
|
||||
var RoomState = require("./room-state");
|
||||
var RoomSummary = require("./room-summary");
|
||||
var utils = require("../utils");
|
||||
var contentRepo = require("../content-repo");
|
||||
|
||||
/**
|
||||
* Construct a new Room.
|
||||
@@ -39,6 +40,38 @@ function Room(roomId, storageToken) {
|
||||
}
|
||||
utils.inherits(Room, EventEmitter);
|
||||
|
||||
/**
|
||||
* Get the avatar URL for a room if one was set.
|
||||
* @param {String} baseUrl The homeserver base URL. See {@link MatrixClient#getHomeserverUrl()}.
|
||||
* @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 True to allow an identicon for this room if an
|
||||
* avatar URL wasn't explicitly set. Default: true.
|
||||
* @return {?string} the avatar URL or null.
|
||||
*/
|
||||
Room.prototype.getAvatarUrl = function(baseUrl, width, height, resizeMethod, allowDefault) {
|
||||
var roomAvatarEvent = this.currentState.getStateEvents("m.room.avatar", "");
|
||||
if (!roomAvatarEvent) {
|
||||
return null;
|
||||
}
|
||||
if (allowDefault === undefined) { allowDefault = true; }
|
||||
var mainUrl = roomAvatarEvent.getContent().url;
|
||||
if (mainUrl) {
|
||||
return contentRepo.getHttpUriForMxc(
|
||||
baseUrl, mainUrl, width, height, resizeMethod
|
||||
);
|
||||
}
|
||||
else if (allowDefault) {
|
||||
return contentRepo.getIdenticonUri(
|
||||
baseUrl, member.userId, width, height
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a member from the current room state.
|
||||
* @param {string} userId The user ID of the member.
|
||||
|
||||
Reference in New Issue
Block a user