Files
cinny/src/util/matrixUtil.js
T

146 lines
4.1 KiB
JavaScript
Raw Normal View History

2022-02-16 19:52:51 +05:30
import HashIC from '../../public/res/ic/outlined/hash.svg';
import HashGlobeIC from '../../public/res/ic/outlined/hash-globe.svg';
import HashLockIC from '../../public/res/ic/outlined/hash-lock.svg';
import SpaceIC from '../../public/res/ic/outlined/space.svg';
import SpaceGlobeIC from '../../public/res/ic/outlined/space-globe.svg';
import SpaceLockIC from '../../public/res/ic/outlined/space-lock.svg';
2021-07-28 18:45:52 +05:30
const WELL_KNOWN_URI = '/.well-known/matrix/client';
2022-02-23 19:30:48 +05:30
export async function getBaseUrl(servername) {
let protocol = 'https://';
if (servername.match(/^https?:\/\//) !== null) protocol = '';
const serverDiscoveryUrl = `${protocol}${servername}${WELL_KNOWN_URI}`;
2021-07-28 18:45:52 +05:30
try {
const result = await (await fetch(serverDiscoveryUrl, { method: 'GET' })).json();
2021-07-28 18:45:52 +05:30
const baseUrl = result?.['m.homeserver']?.base_url;
if (baseUrl === undefined) throw new Error();
return baseUrl;
2021-07-28 18:45:52 +05:30
} catch (e) {
2022-07-09 13:58:57 +05:30
return `${protocol}${servername}`;
2021-07-28 18:45:52 +05:30
}
}
2024-07-22 16:17:19 +05:30
export function getUsername(mx, userId) {
2021-07-28 18:45:52 +05:30
const user = mx.getUser(userId);
if (user === null) return userId;
let username = user.displayName;
if (typeof username === 'undefined') {
username = userId;
}
return username;
}
2022-02-23 19:30:48 +05:30
export function getUsernameOfRoomMember(roomMember) {
2021-08-25 14:06:13 +05:30
return roomMember.name || roomMember.userId;
}
2024-07-22 16:17:19 +05:30
export async function isRoomAliasAvailable(mx, alias) {
2021-07-28 18:45:52 +05:30
try {
2024-07-22 16:17:19 +05:30
const result = await mx.getRoomIdForAlias(alias);
2022-01-01 11:43:35 +05:30
if (result.room_id) return false;
return false;
2021-07-28 18:45:52 +05:30
} catch (e) {
if (e.errcode === 'M_NOT_FOUND') return true;
return false;
}
}
2022-02-23 19:30:48 +05:30
export function getPowerLabel(powerLevel) {
2021-10-18 17:25:52 +02:00
if (powerLevel > 9000) return 'Goku';
if (powerLevel > 100) return 'Founder';
if (powerLevel === 100) return 'Admin';
if (powerLevel >= 50) return 'Mod';
return null;
}
2022-02-23 19:30:48 +05:30
export function parseReply(rawBody) {
if (rawBody?.indexOf('>') !== 0) return null;
let body = rawBody.slice(rawBody.indexOf('<') + 1);
const user = body.slice(0, body.indexOf('>'));
body = body.slice(body.indexOf('>') + 2);
const replyBody = body.slice(0, body.indexOf('\n\n'));
body = body.slice(body.indexOf('\n\n') + 2);
if (user === '') return null;
const isUserId = user.match(/^@.+:.+/);
return {
userId: isUserId ? user : null,
displayName: isUserId ? null : user,
replyBody,
body,
};
}
export function trimHTMLReply(html) {
if (!html) return html;
const suffix = '</mx-reply>';
const i = html.indexOf(suffix);
if (i < 0) {
return html;
}
return html.slice(i + suffix.length);
}
2022-02-23 19:30:48 +05:30
export function joinRuleToIconSrc(joinRule, isSpace) {
2022-02-16 19:52:51 +05:30
return ({
restricted: () => (isSpace ? SpaceIC : HashIC),
2022-03-06 17:48:31 +05:30
knock: () => (isSpace ? SpaceLockIC : HashLockIC),
2022-02-16 19:52:51 +05:30
invite: () => (isSpace ? SpaceLockIC : HashLockIC),
public: () => (isSpace ? SpaceGlobeIC : HashGlobeIC),
}[joinRule]?.() || null);
}
2022-02-23 19:30:48 +05:30
export function getIdServer(userId) {
const idParts = userId.split(':');
return idParts[1];
}
2024-07-22 16:17:19 +05:30
export function isCrossVerified(mx, deviceId) {
try {
const crossSignInfo = mx.getStoredCrossSigningForUser(mx.getUserId());
const deviceInfo = mx.getStoredDevice(mx.getUserId(), deviceId);
const deviceTrust = crossSignInfo.checkDeviceTrust(crossSignInfo, deviceInfo, false, true);
return deviceTrust.isCrossSigningVerified();
} catch (e) {
// device does not support encryption
return null;
}
}
2024-07-22 16:17:19 +05:30
export function hasCrossSigningAccountData(mx) {
const masterKeyData = mx.getAccountData('m.cross_signing.master');
return !!masterKeyData;
}
2024-07-22 16:17:19 +05:30
export function getDefaultSSKey(mx) {
try {
return mx.getAccountData('m.secret_storage.default_key').getContent().key;
} catch {
return undefined;
}
}
2024-07-22 16:17:19 +05:30
export function getSSKeyInfo(mx, key) {
try {
return mx.getAccountData(`m.secret_storage.key.${key}`).getContent();
} catch {
return undefined;
}
}
2024-07-22 16:17:19 +05:30
export async function hasDevices(mx, userId) {
try {
const usersDeviceMap = await mx.downloadKeys([userId, mx.getUserId()]);
2022-07-09 13:58:57 +05:30
return Object.values(usersDeviceMap)
.every((userDevices) => (Object.keys(userDevices).length > 0));
} catch (e) {
console.error("Error determining if it's possible to encrypt to all users: ", e);
return false;
}
2022-07-09 13:58:57 +05:30
}