2015-11-26 13:45:04 +00:00
|
|
|
|
/*
|
2016-01-07 04:06:39 +00:00
|
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2015-11-26 13:45:04 +00:00
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
'use strict';
|
2017-07-01 13:13:32 +00:00
|
|
|
|
import {ContentRepo} from 'matrix-js-sdk';
|
2019-12-20 21:13:46 +00:00
|
|
|
|
import {MatrixClientPeg} from './MatrixClientPeg';
|
2019-05-20 13:33:26 +00:00
|
|
|
|
import DMRoomMap from './utils/DMRoomMap';
|
2015-11-26 13:45:04 +00:00
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
avatarUrlForMember: function(member, width, height, resizeMethod) {
|
2017-07-01 13:13:32 +00:00
|
|
|
|
let url = member.getAvatarUrl(
|
2015-11-26 13:45:04 +00:00
|
|
|
|
MatrixClientPeg.get().getHomeserverUrl(),
|
2017-04-27 10:38:03 +00:00
|
|
|
|
Math.floor(width * window.devicePixelRatio),
|
|
|
|
|
Math.floor(height * window.devicePixelRatio),
|
2015-11-30 14:39:29 +00:00
|
|
|
|
resizeMethod,
|
|
|
|
|
false,
|
2017-07-01 13:13:32 +00:00
|
|
|
|
false,
|
2015-11-26 13:45:04 +00:00
|
|
|
|
);
|
|
|
|
|
if (!url) {
|
|
|
|
|
// member can be null here currently since on invites, the JS SDK
|
|
|
|
|
// does not have enough info to build a RoomMember object for
|
|
|
|
|
// the inviter.
|
|
|
|
|
url = this.defaultAvatarUrlForString(member ? member.userId : '');
|
|
|
|
|
}
|
|
|
|
|
return url;
|
|
|
|
|
},
|
|
|
|
|
|
2016-01-15 17:31:32 +00:00
|
|
|
|
avatarUrlForUser: function(user, width, height, resizeMethod) {
|
2017-07-01 13:13:32 +00:00
|
|
|
|
const url = ContentRepo.getHttpUriForMxc(
|
2016-01-15 17:31:32 +00:00
|
|
|
|
MatrixClientPeg.get().getHomeserverUrl(), user.avatarUrl,
|
2017-04-27 10:38:03 +00:00
|
|
|
|
Math.floor(width * window.devicePixelRatio),
|
|
|
|
|
Math.floor(height * window.devicePixelRatio),
|
2017-07-01 13:13:32 +00:00
|
|
|
|
resizeMethod,
|
2016-01-15 17:31:32 +00:00
|
|
|
|
);
|
|
|
|
|
if (!url || url.length === 0) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return url;
|
|
|
|
|
},
|
|
|
|
|
|
2015-11-26 13:45:04 +00:00
|
|
|
|
defaultAvatarUrlForString: function(s) {
|
2019-02-08 17:34:01 +00:00
|
|
|
|
const images = ['03b381', '368bd6', 'ac3ba8'];
|
2017-07-01 13:13:32 +00:00
|
|
|
|
let total = 0;
|
|
|
|
|
for (let i = 0; i < s.length; ++i) {
|
2015-11-26 13:45:04 +00:00
|
|
|
|
total += s.charCodeAt(i);
|
|
|
|
|
}
|
2019-01-11 01:37:28 +00:00
|
|
|
|
return require('../res/img/' + images[total % images.length] + '.png');
|
2017-07-01 13:13:32 +00:00
|
|
|
|
},
|
2019-05-20 12:20:36 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* returns the first (non-sigil) character of 'name',
|
|
|
|
|
* converted to uppercase
|
2019-05-20 13:33:26 +00:00
|
|
|
|
* @param {string} name
|
|
|
|
|
* @return {string} the first letter
|
2019-05-20 12:20:36 +00:00
|
|
|
|
*/
|
|
|
|
|
getInitialLetter(name) {
|
2019-09-30 15:00:58 +00:00
|
|
|
|
if (!name) {
|
|
|
|
|
// XXX: We should find out what causes the name to sometimes be falsy.
|
|
|
|
|
console.trace("`name` argument to `getInitialLetter` not supplied");
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
if (name.length < 1) {
|
2019-05-20 12:20:36 +00:00
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let idx = 0;
|
|
|
|
|
const initial = name[0];
|
|
|
|
|
if ((initial === '@' || initial === '#' || initial === '+') && name[1]) {
|
|
|
|
|
idx++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// string.codePointAt(0) would do this, but that isn't supported by
|
|
|
|
|
// some browsers (notably PhantomJS).
|
|
|
|
|
let chars = 1;
|
|
|
|
|
const first = name.charCodeAt(idx);
|
|
|
|
|
|
|
|
|
|
// check if it’s the start of a surrogate pair
|
|
|
|
|
if (first >= 0xD800 && first <= 0xDBFF && name[idx+1]) {
|
|
|
|
|
const second = name.charCodeAt(idx+1);
|
|
|
|
|
if (second >= 0xDC00 && second <= 0xDFFF) {
|
|
|
|
|
chars++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const firstChar = name.substring(idx, idx+chars);
|
|
|
|
|
return firstChar.toUpperCase();
|
|
|
|
|
},
|
2019-05-20 13:33:26 +00:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
},
|
2017-01-20 14:22:27 +00:00
|
|
|
|
};
|