2015-11-26 14:45:04 +01:00
|
|
|
/*
|
2023-02-02 11:22:19 +01:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2015-11-26 14:45:04 +01: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.
|
|
|
|
*/
|
|
|
|
|
2023-08-04 09:36:16 +02:00
|
|
|
import { RoomMember, User, Room, ResizeMethod } from "matrix-js-sdk/src/matrix";
|
2023-08-30 13:47:35 +02:00
|
|
|
import { useIdColorHash } from "@vector-im/compound-web";
|
2020-10-13 18:38:33 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
import DMRoomMap from "./utils/DMRoomMap";
|
2021-06-17 15:06:03 +02:00
|
|
|
import { mediaFromMxc } from "./customisations/Media";
|
2022-07-20 09:26:25 +02:00
|
|
|
import { isLocalRoom } from "./utils/localRoom/isLocalRoom";
|
2023-05-25 10:32:20 +02:00
|
|
|
import { getFirstGrapheme } from "./utils/strings";
|
2015-11-26 14:45:04 +01:00
|
|
|
|
2023-08-30 13:47:35 +02:00
|
|
|
/**
|
|
|
|
* Hardcoded from the Compound colors.
|
|
|
|
* Shade for background as defined in the compound web implementation
|
|
|
|
* https://github.com/vector-im/compound-web/blob/main/src/components/Avatar
|
|
|
|
*/
|
|
|
|
const AVATAR_BG_COLORS = ["#e9f2ff", "#faeefb", "#e3f7ed", "#ffecf0", "#ffefe4", "#e3f5f8", "#f1efff", "#e0f8d9"];
|
|
|
|
const AVATAR_TEXT_COLORS = ["#043894", "#671481", "#004933", "#7e0642", "#850000", "#004077", "#4c05b5", "#004b00"];
|
|
|
|
|
2020-05-26 14:33:47 +02:00
|
|
|
// Not to be used for BaseAvatar urls as that has similar default avatar fallback already
|
2021-06-17 15:06:03 +02:00
|
|
|
export function avatarUrlForMember(
|
2023-04-21 12:50:42 +02:00
|
|
|
member: RoomMember | undefined,
|
2021-06-17 15:06:03 +02:00
|
|
|
width: number,
|
|
|
|
height: number,
|
|
|
|
resizeMethod: ResizeMethod,
|
|
|
|
): string {
|
2023-02-03 16:27:47 +01:00
|
|
|
let url: string | null | undefined;
|
2023-02-02 11:22:19 +01:00
|
|
|
if (member?.getMxcAvatarUrl()) {
|
|
|
|
url = mediaFromMxc(member.getMxcAvatarUrl()).getThumbnailOfSourceHttp(width, height, resizeMethod);
|
2020-01-13 19:19:41 +01:00
|
|
|
}
|
2019-12-20 01:45:24 +01: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.
|
2022-12-12 12:24:14 +01:00
|
|
|
url = defaultAvatarUrlForString(member ? member.userId : "");
|
2019-12-20 01:45:24 +01:00
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
2015-11-26 14:45:04 +01:00
|
|
|
|
2023-08-30 13:47:35 +02:00
|
|
|
/**
|
|
|
|
* Determines the HEX color to use in the avatar pills
|
|
|
|
* @param id the user or room ID
|
|
|
|
* @returns the text color to use on the avatar
|
|
|
|
*/
|
|
|
|
export function getAvatarTextColor(id: string): string {
|
|
|
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
|
|
const index = useIdColorHash(id);
|
|
|
|
|
|
|
|
return AVATAR_TEXT_COLORS[index - 1];
|
|
|
|
}
|
|
|
|
|
2021-06-17 15:06:03 +02:00
|
|
|
export function avatarUrlForUser(
|
|
|
|
user: Pick<User, "avatarUrl">,
|
|
|
|
width: number,
|
|
|
|
height: number,
|
|
|
|
resizeMethod?: ResizeMethod,
|
|
|
|
): string | null {
|
2021-03-06 02:45:09 +01:00
|
|
|
if (!user.avatarUrl) return null;
|
2021-04-26 19:25:49 +02:00
|
|
|
return mediaFromMxc(user.avatarUrl).getThumbnailOfSourceHttp(width, height, resizeMethod);
|
2019-12-20 01:45:24 +01:00
|
|
|
}
|
2016-01-15 18:31:32 +01:00
|
|
|
|
2020-10-13 18:38:33 +02:00
|
|
|
function isValidHexColor(color: string): boolean {
|
2022-12-12 12:24:14 +01:00
|
|
|
return (
|
|
|
|
typeof color === "string" &&
|
2020-10-13 18:38:33 +02:00
|
|
|
(color.length === 7 || color.length === 9) &&
|
2020-04-28 10:59:10 +02:00
|
|
|
color.charAt(0) === "#" &&
|
2022-12-12 12:24:14 +01:00
|
|
|
!color
|
|
|
|
.slice(1)
|
|
|
|
.split("")
|
|
|
|
.some((c) => isNaN(parseInt(c, 16)))
|
|
|
|
);
|
2020-04-28 10:59:10 +02:00
|
|
|
}
|
|
|
|
|
2020-10-13 18:38:33 +02:00
|
|
|
function urlForColor(color: string): string {
|
2020-04-27 19:35:38 +02:00
|
|
|
const size = 40;
|
|
|
|
const canvas = document.createElement("canvas");
|
|
|
|
canvas.width = size;
|
|
|
|
canvas.height = size;
|
|
|
|
const ctx = canvas.getContext("2d");
|
2020-04-28 10:41:35 +02:00
|
|
|
// bail out when using jsdom in unit tests
|
|
|
|
if (!ctx) {
|
|
|
|
return "";
|
|
|
|
}
|
2020-04-27 19:35:38 +02: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 18:38:33 +02:00
|
|
|
const colorToDataURLCache = new Map<string, string>();
|
2020-04-27 19:35:38 +02:00
|
|
|
|
2023-02-02 11:22:19 +01:00
|
|
|
export function defaultAvatarUrlForString(s: string): string {
|
2020-10-02 17:46:27 +02:00
|
|
|
if (!s) return ""; // XXX: should never happen but empirically does by evidence of a rageshake
|
2023-08-30 13:47:35 +02:00
|
|
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
|
|
const colorIndex = useIdColorHash(s);
|
2023-02-02 11:22:19 +01:00
|
|
|
// overwritten color value in custom themes
|
|
|
|
const cssVariable = `--avatar-background-colors_${colorIndex}`;
|
2023-04-14 15:38:54 +02:00
|
|
|
const cssValue = getComputedStyle(document.body).getPropertyValue(cssVariable);
|
2023-08-30 13:47:35 +02:00
|
|
|
const color = cssValue || AVATAR_BG_COLORS[colorIndex - 1];
|
2020-04-27 19:35:38 +02:00
|
|
|
let dataUrl = colorToDataURLCache.get(color);
|
|
|
|
if (!dataUrl) {
|
2020-04-28 10:59:10 +02: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 19:35:38 +02:00
|
|
|
}
|
|
|
|
return dataUrl;
|
2019-12-20 01:45:24 +01:00
|
|
|
}
|
2019-05-20 14:20:36 +02:00
|
|
|
|
2019-12-20 01:45:24 +01:00
|
|
|
/**
|
|
|
|
* returns the first (non-sigil) character of 'name',
|
|
|
|
* converted to uppercase
|
|
|
|
* @param {string} name
|
|
|
|
* @return {string} the first letter
|
|
|
|
*/
|
2023-02-03 16:27:47 +01:00
|
|
|
export function getInitialLetter(name: string): string | undefined {
|
2019-12-20 01:45:24 +01: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 14:20:36 +02:00
|
|
|
|
2019-12-20 01:45:24 +01:00
|
|
|
const initial = name[0];
|
2022-12-12 12:24:14 +01:00
|
|
|
if ((initial === "@" || initial === "#" || initial === "+") && name[1]) {
|
2021-07-18 00:00:46 +02:00
|
|
|
name = name.substring(1);
|
2019-12-20 01:45:24 +01:00
|
|
|
}
|
|
|
|
|
2023-05-25 10:32:20 +02:00
|
|
|
return getFirstGrapheme(name).toUpperCase();
|
2019-12-20 01:45:24 +01:00
|
|
|
}
|
2019-05-20 14:20:36 +02:00
|
|
|
|
2023-01-12 14:25:14 +01:00
|
|
|
export function avatarUrlForRoom(
|
2023-02-13 18:01:43 +01:00
|
|
|
room: Room | null,
|
2023-02-28 11:25:36 +01:00
|
|
|
width?: number,
|
|
|
|
height?: number,
|
2023-01-12 14:25:14 +01:00
|
|
|
resizeMethod?: ResizeMethod,
|
|
|
|
): string | null {
|
2020-02-21 15:14:24 +01:00
|
|
|
if (!room) return null; // null-guard
|
|
|
|
|
2023-02-02 11:22:19 +01:00
|
|
|
if (room.getMxcAvatarUrl()) {
|
2023-02-28 11:25:36 +01:00
|
|
|
const media = mediaFromMxc(room.getMxcAvatarUrl() ?? undefined);
|
|
|
|
if (width !== undefined && height !== undefined) {
|
|
|
|
return media.getThumbnailOfSourceHttp(width, height, resizeMethod);
|
|
|
|
}
|
|
|
|
return media.srcHttp;
|
2019-12-20 01:45:24 +01:00
|
|
|
}
|
2019-05-20 15:33:26 +02:00
|
|
|
|
2021-02-19 15:20:57 +01:00
|
|
|
// space rooms cannot be DMs so skip the rest
|
2022-03-23 00:07:37 +01:00
|
|
|
if (room.isSpaceRoom()) return null;
|
2021-02-19 15:20:57 +01:00
|
|
|
|
2021-06-05 08:34:32 +02:00
|
|
|
// If the room is not a DM don't fallback to a member avatar
|
2022-12-12 12:24:14 +01:00
|
|
|
if (!DMRoomMap.shared().getUserIdForRoomId(room.roomId) && !isLocalRoom(room)) {
|
2022-07-20 09:26:25 +02:00
|
|
|
return null;
|
|
|
|
}
|
2021-06-05 08:22:43 +02:00
|
|
|
|
|
|
|
// If there are only two members in the DM use the avatar of the other member
|
2021-06-05 08:33:14 +02:00
|
|
|
const otherMember = room.getAvatarFallbackMember();
|
2023-02-02 11:22:19 +01:00
|
|
|
if (otherMember?.getMxcAvatarUrl()) {
|
2023-02-28 11:25:36 +01:00
|
|
|
const media = mediaFromMxc(otherMember.getMxcAvatarUrl());
|
|
|
|
if (width !== undefined && height !== undefined) {
|
|
|
|
return media.getThumbnailOfSourceHttp(width, height, resizeMethod);
|
|
|
|
}
|
|
|
|
return media.srcHttp;
|
2019-12-20 01:45:24 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|