
* Fix AvatarField to work with authenticated media * Fix ViewMediaButton to work for user's media tab and reported events * remove console.log * cleanup AvatarField * use correct thumbnail size * fix AvatarField.test * ignore postgres data for watchman * fix new avatar preview * watchman should ignore testdata completely, instead of specific subdirs * update README * change user's media icon in sidebar - use the same icon as the media tab * Add preview for user media files if mimeType is image/* * Add new line in user media Dialog
100 lines
3.3 KiB
TypeScript
100 lines
3.3 KiB
TypeScript
import { Identifier, fetchUtils } from "react-admin";
|
|
|
|
import storage from "../storage";
|
|
|
|
export const splitMxid = mxid => {
|
|
const re = /^@(?<name>[a-zA-Z0-9._=\-/]+):(?<domain>[a-zA-Z0-9\-.]+\.[a-zA-Z]+)$/;
|
|
return re.exec(mxid)?.groups;
|
|
};
|
|
|
|
export const isValidBaseUrl = baseUrl => /^(http|https):\/\/[a-zA-Z0-9\-.]+(:\d{1,5})?$/.test(baseUrl);
|
|
|
|
/**
|
|
* Resolve the homeserver URL using the well-known lookup
|
|
* @param domain the domain part of an MXID
|
|
* @returns homeserver base URL
|
|
*/
|
|
export const getWellKnownUrl = async domain => {
|
|
const wellKnownUrl = `https://${domain}/.well-known/matrix/client`;
|
|
try {
|
|
const response = await fetchUtils.fetchJson(wellKnownUrl, { method: "GET" });
|
|
return response.json["m.homeserver"].base_url;
|
|
} catch {
|
|
// if there is no .well-known entry, return the domain itself
|
|
return `https://${domain}`;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Get synapse server version
|
|
* @param base_url the base URL of the homeserver
|
|
* @returns server version
|
|
*/
|
|
export const getServerVersion = async baseUrl => {
|
|
const versionUrl = `${baseUrl}/_synapse/admin/v1/server_version`;
|
|
const response = await fetchUtils.fetchJson(versionUrl, { method: "GET" });
|
|
return response.json.server_version;
|
|
};
|
|
|
|
/** Get supported Matrix features */
|
|
export const getSupportedFeatures = async baseUrl => {
|
|
const versionUrl = `${baseUrl}/_matrix/client/versions`;
|
|
const response = await fetchUtils.fetchJson(versionUrl, { method: "GET" });
|
|
return response.json;
|
|
};
|
|
|
|
/**
|
|
* Get supported login flows
|
|
* @param baseUrl the base URL of the homeserver
|
|
* @returns array of supported login flows
|
|
*/
|
|
export const getSupportedLoginFlows = async baseUrl => {
|
|
const loginFlowsUrl = `${baseUrl}/_matrix/client/r0/login`;
|
|
const response = await fetchUtils.fetchJson(loginFlowsUrl, { method: "GET" });
|
|
return response.json.flows;
|
|
};
|
|
|
|
/**
|
|
* Generate a random MXID for current homeserver
|
|
* @returns full MXID as string
|
|
*/
|
|
export function generateRandomMxId(): string {
|
|
const homeserver = storage.getItem("home_server");
|
|
const characters = "0123456789abcdefghijklmnopqrstuvwxyz";
|
|
const localpart = Array.from(crypto.getRandomValues(new Uint32Array(8)))
|
|
.map(x => characters[x % characters.length])
|
|
.join("");
|
|
return `@${localpart}:${homeserver}`;
|
|
}
|
|
|
|
/**
|
|
* Return the full MXID from an arbitrary input
|
|
* @param input the input string
|
|
* @returns full MXID as string
|
|
*/
|
|
export function returnMXID(input: string | Identifier): string {
|
|
const homeserver = storage.getItem("home_server");
|
|
|
|
// Check if the input already looks like a valid MXID (i.e., starts with "@" and contains ":")
|
|
const mxidPattern = /^@[^@:]+:[^@:]+$/;
|
|
if (typeof input === 'string' && mxidPattern.test(input)) {
|
|
return input; // Already a valid MXID
|
|
}
|
|
|
|
// If input is not a valid MXID, assume it's a localpart and construct the MXID
|
|
const localpart = typeof input === 'string' && input.startsWith('@') ? input.slice(1) : input;
|
|
return `@${localpart}:${homeserver}`;
|
|
}
|
|
|
|
|
|
/**
|
|
* Generate a random user password
|
|
* @returns a new random password as string
|
|
*/
|
|
export function generateRandomPassword(length = 20): string {
|
|
const characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz~!@-#$";
|
|
return Array.from(crypto.getRandomValues(new Uint32Array(length)))
|
|
.map(x => characters[x % characters.length])
|
|
.join("");
|
|
}
|