move most of room avatar handling to Avatar, to reuse in editor pills

pull/21833/head
Bruno Windels 2019-05-20 15:33:26 +02:00
parent 5edfd01cb2
commit 230e53fe2f
2 changed files with 39 additions and 31 deletions

View File

@ -17,6 +17,7 @@ limitations under the License.
'use strict'; 'use strict';
import {ContentRepo} from 'matrix-js-sdk'; import {ContentRepo} from 'matrix-js-sdk';
import MatrixClientPeg from './MatrixClientPeg'; import MatrixClientPeg from './MatrixClientPeg';
import DMRoomMap from './utils/DMRoomMap';
module.exports = { module.exports = {
avatarUrlForMember: function(member, width, height, resizeMethod) { avatarUrlForMember: function(member, width, height, resizeMethod) {
@ -62,6 +63,8 @@ module.exports = {
/** /**
* returns the first (non-sigil) character of 'name', * returns the first (non-sigil) character of 'name',
* converted to uppercase * converted to uppercase
* @param {string} name
* @return {string} the first letter
*/ */
getInitialLetter(name) { getInitialLetter(name) {
if (name.length < 1) { if (name.length < 1) {
@ -90,4 +93,37 @@ module.exports = {
const firstChar = name.substring(idx, idx+chars); const firstChar = name.substring(idx, idx+chars);
return firstChar.toUpperCase(); return firstChar.toUpperCase();
}, },
avatarUrlForRoom(room, width, height, resizeMethod) {
const explicitRoomAvatar = room.getAvatarUrl(
MatrixClientPeg.get().getHomeserverUrl(),
width,
height,
resizeMethod,
false,
);
if (explicitRoomAvatar) {
return explicitRoomAvatar;
}
let otherMember = null;
const otherUserId = DMRoomMap.shared().getUserIdForRoomId(room.roomId);
if (otherUserId) {
otherMember = room.getMember(otherUserId);
} else {
// if the room is not marked as a 1:1, but only has max 2 members
// then still try to show any avatar (pref. other member)
otherMember = room.getAvatarFallbackMember();
}
if (otherMember) {
return otherMember.getAvatarUrl(
MatrixClientPeg.get().getHomeserverUrl(),
width,
height,
resizeMethod,
false,
);
}
return null;
},
}; };

View File

@ -19,7 +19,7 @@ import {ContentRepo} from "matrix-js-sdk";
import MatrixClientPeg from "../../../MatrixClientPeg"; import MatrixClientPeg from "../../../MatrixClientPeg";
import Modal from '../../../Modal'; import Modal from '../../../Modal';
import sdk from "../../../index"; import sdk from "../../../index";
import DMRoomMap from '../../../utils/DMRoomMap'; import Avatar from '../../../Avatar';
module.exports = React.createClass({ module.exports = React.createClass({
displayName: 'RoomAvatar', displayName: 'RoomAvatar',
@ -89,7 +89,6 @@ module.exports = React.createClass({
props.resizeMethod, props.resizeMethod,
), // highest priority ), // highest priority
this.getRoomAvatarUrl(props), this.getRoomAvatarUrl(props),
this.getOneToOneAvatar(props), // lowest priority
].filter(function(url) { ].filter(function(url) {
return (url != null && url != ""); return (url != null && url != "");
}); });
@ -98,41 +97,14 @@ module.exports = React.createClass({
getRoomAvatarUrl: function(props) { getRoomAvatarUrl: function(props) {
if (!props.room) return null; if (!props.room) return null;
return props.room.getAvatarUrl( return Avatar.avatarUrlForRoom(
MatrixClientPeg.get().getHomeserverUrl(), props.room,
Math.floor(props.width * window.devicePixelRatio), Math.floor(props.width * window.devicePixelRatio),
Math.floor(props.height * window.devicePixelRatio), Math.floor(props.height * window.devicePixelRatio),
props.resizeMethod, props.resizeMethod,
false,
); );
}, },
getOneToOneAvatar: function(props) {
const room = props.room;
if (!room) {
return null;
}
let otherMember = null;
const otherUserId = DMRoomMap.shared().getUserIdForRoomId(room.roomId);
if (otherUserId) {
otherMember = room.getMember(otherUserId);
} else {
// if the room is not marked as a 1:1, but only has max 2 members
// then still try to show any avatar (pref. other member)
otherMember = room.getAvatarFallbackMember();
}
if (otherMember) {
return otherMember.getAvatarUrl(
MatrixClientPeg.get().getHomeserverUrl(),
Math.floor(props.width * window.devicePixelRatio),
Math.floor(props.height * window.devicePixelRatio),
props.resizeMethod,
false,
);
}
return null;
},
onRoomAvatarClick: function() { onRoomAvatarClick: function() {
const avatarUrl = this.props.room.getAvatarUrl( const avatarUrl = this.props.room.getAvatarUrl(
MatrixClientPeg.get().getHomeserverUrl(), MatrixClientPeg.get().getHomeserverUrl(),