-
Notifications
You must be signed in to change notification settings - Fork 367
Expand file tree
/
Copy pathfederationUtils.js
More file actions
68 lines (64 loc) · 1.97 KB
/
federationUtils.js
File metadata and controls
68 lines (64 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* Returns true if the username looks like a Matrix user ID (@localpart:homeserver.tld).
* @param {string} username
* @returns {boolean}
*/
export const isMatrixUser = (username) => {
if (!username) return false;
return /^@[^:]+:[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(username);
};
/**
* Parses a Matrix user ID into localpart and homeserver.
* @param {string} matrixId e.g. "@alice:matrix.org"
* @returns {{ localpart: string, homeserver: string } | null}
*/
export const parseMatrixUserId = (matrixId) => {
if (!isMatrixUser(matrixId)) return null;
const withoutAt = matrixId.slice(1);
const colonIdx = withoutAt.indexOf(':');
return {
localpart: withoutAt.slice(0, colonIdx),
homeserver: withoutAt.slice(colonIdx + 1),
};
};
/**
* Returns a display-friendly label, truncating long homeservers.
* @param {string} matrixId
* @returns {string}
*/
export const getMatrixDisplayLabel = (matrixId) => {
const parsed = parseMatrixUserId(matrixId);
if (!parsed) return matrixId;
const { localpart, homeserver } = parsed;
const shortHost =
homeserver.length > 20 ? homeserver.slice(0, 18) + '…' : homeserver;
return `@${localpart}:${shortHost}`;
};
/**
* Returns 1-2 char initials for avatar fallback.
* @param {string} matrixId
* @returns {string}
*/
export const getMatrixInitials = (matrixId) => {
const parsed = parseMatrixUserId(matrixId);
if (!parsed) return '?';
return parsed.localpart.slice(0, 2).toUpperCase();
};
/**
* Generates a deterministic background color from a string.
* @param {string} str
* @returns {string}
*/
export const getAvatarColor = (str) => {
const COLORS = [
'#e57373', '#f06292', '#ba68c8', '#9575cd',
'#7986cb', '#64b5f6', '#4fc3f7', '#4dd0e1',
'#4db6ac', '#81c784', '#aed581', '#ffd54f',
'#ffb74d', '#ff8a65', '#a1887f', '#90a4ae',
];
let hash = 0;
for (let i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash);
}
return COLORS[Math.abs(hash) % COLORS.length];
};