2021-07-28 16:15:52 +03:00
|
|
|
import initMatrix from '../client/initMatrix';
|
|
|
|
|
2022-02-16 16:22:51 +02:00
|
|
|
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 16:15:52 +03:00
|
|
|
const WELL_KNOWN_URI = '/.well-known/matrix/client';
|
|
|
|
|
2022-02-23 16:00:48 +02:00
|
|
|
export async function getBaseUrl(servername) {
|
2021-11-06 11:45:35 +02:00
|
|
|
let protocol = 'https://';
|
|
|
|
if (servername.match(/^https?:\/\//) !== null) protocol = '';
|
|
|
|
const serverDiscoveryUrl = `${protocol}${servername}${WELL_KNOWN_URI}`;
|
2021-07-28 16:15:52 +03:00
|
|
|
try {
|
2021-11-06 11:45:35 +02:00
|
|
|
const result = await (await fetch(serverDiscoveryUrl, { method: 'GET' })).json();
|
2021-07-28 16:15:52 +03:00
|
|
|
|
2021-11-06 11:45:35 +02:00
|
|
|
const baseUrl = result?.['m.homeserver']?.base_url;
|
|
|
|
if (baseUrl === undefined) throw new Error();
|
|
|
|
return baseUrl;
|
2021-07-28 16:15:52 +03:00
|
|
|
} catch (e) {
|
2021-11-06 11:45:35 +02:00
|
|
|
throw new Error(`${protocol}${servername}`);
|
2021-07-28 16:15:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-23 16:00:48 +02:00
|
|
|
export function getUsername(userId) {
|
2021-07-28 16:15:52 +03:00
|
|
|
const mx = initMatrix.matrixClient;
|
|
|
|
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 16:00:48 +02:00
|
|
|
export function getUsernameOfRoomMember(roomMember) {
|
2021-08-25 11:36:13 +03:00
|
|
|
return roomMember.name || roomMember.userId;
|
|
|
|
}
|
|
|
|
|
2022-02-23 16:00:48 +02:00
|
|
|
export async function isRoomAliasAvailable(alias) {
|
2021-07-28 16:15:52 +03:00
|
|
|
try {
|
|
|
|
const result = await initMatrix.matrixClient.resolveRoomAlias(alias);
|
2022-01-01 08:13:35 +02:00
|
|
|
if (result.room_id) return false;
|
|
|
|
return false;
|
2021-07-28 16:15:52 +03:00
|
|
|
} catch (e) {
|
|
|
|
if (e.errcode === 'M_NOT_FOUND') return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-23 16:00:48 +02:00
|
|
|
export function getPowerLabel(powerLevel) {
|
2021-10-18 18:25:52 +03: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 16:00:48 +02:00
|
|
|
export function parseReply(rawBody) {
|
2021-12-03 15:02:10 +02:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-02-23 16:00:48 +02:00
|
|
|
export function hasDMWith(userId) {
|
2022-02-05 15:55:59 +02:00
|
|
|
const mx = initMatrix.matrixClient;
|
|
|
|
const directIds = [...initMatrix.roomList.directs];
|
|
|
|
|
|
|
|
return directIds.find((roomId) => {
|
|
|
|
const dRoom = mx.getRoom(roomId);
|
|
|
|
const roomMembers = dRoom.getMembers();
|
|
|
|
if (roomMembers.length <= 2 && dRoom.getMember(userId)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-23 16:00:48 +02:00
|
|
|
export function joinRuleToIconSrc(joinRule, isSpace) {
|
2022-02-16 16:22:51 +02:00
|
|
|
return ({
|
|
|
|
restricted: () => (isSpace ? SpaceIC : HashIC),
|
|
|
|
invite: () => (isSpace ? SpaceLockIC : HashLockIC),
|
|
|
|
public: () => (isSpace ? SpaceGlobeIC : HashGlobeIC),
|
|
|
|
}[joinRule]?.() || null);
|
|
|
|
}
|
|
|
|
|
2022-02-23 16:00:48 +02:00
|
|
|
// NOTE: it gives userId with minimum power level 50;
|
|
|
|
function getHighestPowerUserId(room) {
|
|
|
|
const userIdToPower = room.currentState.getStateEvents('m.room.power_levels', '')?.getContent().users;
|
|
|
|
let powerUserId = null;
|
|
|
|
if (!userIdToPower) return powerUserId;
|
|
|
|
|
|
|
|
Object.keys(userIdToPower).forEach((userId) => {
|
|
|
|
if (userIdToPower[userId] < 50) return;
|
|
|
|
if (powerUserId === null) {
|
|
|
|
powerUserId = userId;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (userIdToPower[userId] > userIdToPower[powerUserId]) {
|
|
|
|
powerUserId = userId;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return powerUserId;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getIdServer(userId) {
|
|
|
|
const idParts = userId.split(':');
|
|
|
|
return idParts[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getServerToPopulation(room) {
|
|
|
|
const members = room.getMembers();
|
|
|
|
const serverToPop = {};
|
|
|
|
|
|
|
|
members?.forEach((member) => {
|
|
|
|
const { userId } = member;
|
|
|
|
const server = getIdServer(userId);
|
|
|
|
const serverPop = serverToPop[server];
|
|
|
|
if (serverPop === undefined) {
|
|
|
|
serverToPop[server] = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
serverToPop[server] = serverPop + 1;
|
|
|
|
});
|
|
|
|
|
|
|
|
return serverToPop;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function genRoomVia(room) {
|
|
|
|
const via = [];
|
|
|
|
const userId = getHighestPowerUserId(room);
|
|
|
|
if (userId) {
|
|
|
|
const server = getIdServer(userId);
|
|
|
|
if (server) via.push(server);
|
|
|
|
}
|
|
|
|
const serverToPop = getServerToPopulation(room);
|
|
|
|
const sortedServers = Object.keys(serverToPop).sort(
|
|
|
|
(svrA, svrB) => serverToPop[svrB] - serverToPop[svrA],
|
|
|
|
);
|
|
|
|
const mostPop3 = sortedServers.slice(0, 3);
|
|
|
|
if (via.length === 0) return mostPop3;
|
|
|
|
if (mostPop3.includes(via[0])) {
|
|
|
|
mostPop3.splice(mostPop3.indexOf(via[0]), 1);
|
|
|
|
}
|
|
|
|
return via.concat(mostPop3.slice(0, 2));
|
|
|
|
}
|