2015-11-26 16:45:04 +03:00
|
|
|
/*
|
2016-01-07 07:06:39 +03:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2015-11-26 16:45:04 +03: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.
|
|
|
|
*/
|
|
|
|
|
2021-06-17 16:06:03 +03:00
|
|
|
import { RoomMember } from "matrix-js-sdk/src/models/room-member";
|
|
|
|
import { User } from "matrix-js-sdk/src/models/user";
|
|
|
|
import { Room } from "matrix-js-sdk/src/models/room";
|
2021-06-18 17:31:12 +03:00
|
|
|
import { ResizeMethod } from "matrix-js-sdk/src/@types/partials";
|
2021-07-18 01:00:46 +03:00
|
|
|
import { split } from "lodash";
|
2020-10-13 19:38:33 +03:00
|
|
|
|
2019-05-20 16:33:26 +03:00
|
|
|
import DMRoomMap from './utils/DMRoomMap';
|
2021-06-17 16:06:03 +03:00
|
|
|
import { mediaFromMxc } from "./customisations/Media";
|
2021-07-15 10:26:49 +03:00
|
|
|
import SpaceStore from "./stores/SpaceStore";
|
2020-10-13 19:38:33 +03:00
|
|
|
|
2020-05-26 15:33:47 +03:00
|
|
|
// Not to be used for BaseAvatar urls as that has similar default avatar fallback already
|
2021-06-17 16:06:03 +03:00
|
|
|
export function avatarUrlForMember(
|
|
|
|
member: RoomMember,
|
|
|
|
width: number,
|
|
|
|
height: number,
|
|
|
|
resizeMethod: ResizeMethod,
|
|
|
|
): string {
|
2020-10-13 19:38:33 +03:00
|
|
|
let url: string;
|
2021-03-09 03:04:00 +03:00
|
|
|
if (member?.getMxcAvatarUrl()) {
|
2021-04-26 20:25:49 +03:00
|
|
|
url = mediaFromMxc(member.getMxcAvatarUrl()).getThumbnailOfSourceHttp(width, height, resizeMethod);
|
2020-01-13 21:19:41 +03:00
|
|
|
}
|
2019-12-20 03:45:24 +03: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.
|
2020-01-13 23:28:33 +03:00
|
|
|
url = defaultAvatarUrlForString(member ? member.userId : '');
|
2019-12-20 03:45:24 +03:00
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
2015-11-26 16:45:04 +03:00
|
|
|
|
2021-06-17 16:06:03 +03:00
|
|
|
export function avatarUrlForUser(
|
|
|
|
user: Pick<User, "avatarUrl">,
|
|
|
|
width: number,
|
|
|
|
height: number,
|
|
|
|
resizeMethod?: ResizeMethod,
|
|
|
|
): string | null {
|
2021-03-06 04:45:09 +03:00
|
|
|
if (!user.avatarUrl) return null;
|
2021-04-26 20:25:49 +03:00
|
|
|
return mediaFromMxc(user.avatarUrl).getThumbnailOfSourceHttp(width, height, resizeMethod);
|
2019-12-20 03:45:24 +03:00
|
|
|
}
|
2016-01-15 20:31:32 +03:00
|
|
|
|
2020-10-13 19:38:33 +03:00
|
|
|
function isValidHexColor(color: string): boolean {
|
2020-04-28 11:59:10 +03:00
|
|
|
return typeof color === "string" &&
|
2020-10-13 19:38:33 +03:00
|
|
|
(color.length === 7 || color.length === 9) &&
|
2020-04-28 11:59:10 +03:00
|
|
|
color.charAt(0) === "#" &&
|
|
|
|
!color.substr(1).split("").some(c => isNaN(parseInt(c, 16)));
|
|
|
|
}
|
|
|
|
|
2020-10-13 19:38:33 +03:00
|
|
|
function urlForColor(color: string): string {
|
2020-04-27 20:35:38 +03:00
|
|
|
const size = 40;
|
|
|
|
const canvas = document.createElement("canvas");
|
|
|
|
canvas.width = size;
|
|
|
|
canvas.height = size;
|
|
|
|
const ctx = canvas.getContext("2d");
|
2020-04-28 11:41:35 +03:00
|
|
|
// bail out when using jsdom in unit tests
|
|
|
|
if (!ctx) {
|
|
|
|
return "";
|
|
|
|
}
|
2020-04-27 20:35:38 +03:00
|
|
|
ctx.fillStyle = color;
|
|
|
|
ctx.fillRect(0, 0, size, size);
|
|
|
|
return canvas.toDataURL();
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: Ideally we'd clear this cache when the theme changes
|
|
|
|
// but since this function is at global scope, it's a bit
|
|
|
|
// hard to install a listener here, even if there were a clear event to listen to
|
2020-10-13 19:38:33 +03:00
|
|
|
const colorToDataURLCache = new Map<string, string>();
|
2020-04-27 20:35:38 +03:00
|
|
|
|
2020-10-13 19:38:33 +03:00
|
|
|
export function defaultAvatarUrlForString(s: string): string {
|
2020-10-02 18:46:27 +03:00
|
|
|
if (!s) return ""; // XXX: should never happen but empirically does by evidence of a rageshake
|
2020-07-14 21:36:27 +03:00
|
|
|
const defaultColors = ['#0DBD8B', '#368bd6', '#ac3ba8'];
|
2019-12-20 03:45:24 +03:00
|
|
|
let total = 0;
|
|
|
|
for (let i = 0; i < s.length; ++i) {
|
|
|
|
total += s.charCodeAt(i);
|
|
|
|
}
|
2020-04-27 20:35:38 +03:00
|
|
|
const colorIndex = total % defaultColors.length;
|
|
|
|
// overwritten color value in custom themes
|
2020-04-27 20:38:27 +03:00
|
|
|
const cssVariable = `--avatar-background-colors_${colorIndex}`;
|
|
|
|
const cssValue = document.body.style.getPropertyValue(cssVariable);
|
|
|
|
const color = cssValue || defaultColors[colorIndex];
|
2020-04-27 20:35:38 +03:00
|
|
|
let dataUrl = colorToDataURLCache.get(color);
|
|
|
|
if (!dataUrl) {
|
2020-04-28 11:59:10 +03:00
|
|
|
// validate color as this can come from account_data
|
|
|
|
// with custom theming
|
|
|
|
if (isValidHexColor(color)) {
|
|
|
|
dataUrl = urlForColor(color);
|
|
|
|
colorToDataURLCache.set(color, dataUrl);
|
|
|
|
} else {
|
|
|
|
dataUrl = "";
|
|
|
|
}
|
2020-04-27 20:35:38 +03:00
|
|
|
}
|
|
|
|
return dataUrl;
|
2019-12-20 03:45:24 +03:00
|
|
|
}
|
2019-05-20 15:20:36 +03:00
|
|
|
|
2019-12-20 03:45:24 +03:00
|
|
|
/**
|
|
|
|
* returns the first (non-sigil) character of 'name',
|
|
|
|
* converted to uppercase
|
|
|
|
* @param {string} name
|
|
|
|
* @return {string} the first letter
|
|
|
|
*/
|
2020-10-13 19:38:33 +03:00
|
|
|
export function getInitialLetter(name: string): string {
|
2019-12-20 03:45:24 +03: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) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2019-05-20 15:20:36 +03:00
|
|
|
|
2019-12-20 03:45:24 +03:00
|
|
|
const initial = name[0];
|
|
|
|
if ((initial === '@' || initial === '#' || initial === '+') && name[1]) {
|
2021-07-18 01:00:46 +03:00
|
|
|
name = name.substring(1);
|
2019-12-20 03:45:24 +03:00
|
|
|
}
|
2019-05-20 15:20:36 +03:00
|
|
|
|
2021-07-18 01:00:46 +03:00
|
|
|
// rely on the grapheme cluster splitter in lodash so that we don't break apart compound emojis
|
2021-07-19 19:48:44 +03:00
|
|
|
return split(name, "", 1)[0].toUpperCase();
|
2019-12-20 03:45:24 +03:00
|
|
|
}
|
2019-05-20 15:20:36 +03:00
|
|
|
|
2020-10-13 20:48:02 +03:00
|
|
|
export function avatarUrlForRoom(room: Room, width: number, height: number, resizeMethod?: ResizeMethod) {
|
2020-02-21 17:14:24 +03:00
|
|
|
if (!room) return null; // null-guard
|
|
|
|
|
2021-03-09 03:04:00 +03:00
|
|
|
if (room.getMxcAvatarUrl()) {
|
|
|
|
return mediaFromMxc(room.getMxcAvatarUrl()).getThumbnailOfSourceHttp(width, height, resizeMethod);
|
2019-12-20 03:45:24 +03:00
|
|
|
}
|
2019-05-20 16:33:26 +03:00
|
|
|
|
2021-02-19 17:20:57 +03:00
|
|
|
// space rooms cannot be DMs so skip the rest
|
2021-07-15 10:26:49 +03:00
|
|
|
if (SpaceStore.spacesEnabled && room.isSpaceRoom()) return null;
|
2021-02-19 17:20:57 +03:00
|
|
|
|
2019-12-20 03:45:24 +03:00
|
|
|
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();
|
|
|
|
}
|
2021-03-09 03:04:00 +03:00
|
|
|
if (otherMember?.getMxcAvatarUrl()) {
|
|
|
|
return mediaFromMxc(otherMember.getMxcAvatarUrl()).getThumbnailOfSourceHttp(width, height, resizeMethod);
|
2019-12-20 03:45:24 +03:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|