2021-07-28 15:15:52 +02:00
|
|
|
import initMatrix from '../client/initMatrix';
|
|
|
|
|
2022-02-16 15:22:51 +01: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 15:15:52 +02:00
|
|
|
const WELL_KNOWN_URI = '/.well-known/matrix/client';
|
|
|
|
|
2021-11-06 10:45:35 +01:00
|
|
|
async function getBaseUrl(servername) {
|
|
|
|
let protocol = 'https://';
|
|
|
|
if (servername.match(/^https?:\/\//) !== null) protocol = '';
|
|
|
|
const serverDiscoveryUrl = `${protocol}${servername}${WELL_KNOWN_URI}`;
|
2021-07-28 15:15:52 +02:00
|
|
|
try {
|
2021-11-06 10:45:35 +01:00
|
|
|
const result = await (await fetch(serverDiscoveryUrl, { method: 'GET' })).json();
|
2021-07-28 15:15:52 +02:00
|
|
|
|
2021-11-06 10:45:35 +01:00
|
|
|
const baseUrl = result?.['m.homeserver']?.base_url;
|
|
|
|
if (baseUrl === undefined) throw new Error();
|
|
|
|
return baseUrl;
|
2021-07-28 15:15:52 +02:00
|
|
|
} catch (e) {
|
2021-11-06 10:45:35 +01:00
|
|
|
throw new Error(`${protocol}${servername}`);
|
2021-07-28 15:15:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getUsername(userId) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-08-25 10:36:13 +02:00
|
|
|
function getUsernameOfRoomMember(roomMember) {
|
|
|
|
return roomMember.name || roomMember.userId;
|
|
|
|
}
|
|
|
|
|
2021-07-28 15:15:52 +02:00
|
|
|
async function isRoomAliasAvailable(alias) {
|
|
|
|
try {
|
|
|
|
const result = await initMatrix.matrixClient.resolveRoomAlias(alias);
|
2022-01-01 07:13:35 +01:00
|
|
|
if (result.room_id) return false;
|
|
|
|
return false;
|
2021-07-28 15:15:52 +02:00
|
|
|
} catch (e) {
|
|
|
|
if (e.errcode === 'M_NOT_FOUND') return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 17:25:52 +02:00
|
|
|
function getPowerLabel(powerLevel) {
|
|
|
|
if (powerLevel > 9000) return 'Goku';
|
|
|
|
if (powerLevel > 100) return 'Founder';
|
|
|
|
if (powerLevel === 100) return 'Admin';
|
|
|
|
if (powerLevel >= 50) return 'Mod';
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-12-03 14:02:10 +01:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-02-05 14:55:59 +01:00
|
|
|
function hasDMWith(userId) {
|
|
|
|
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-16 15:22:51 +01:00
|
|
|
function joinRuleToIconSrc(joinRule, isSpace) {
|
|
|
|
return ({
|
|
|
|
restricted: () => (isSpace ? SpaceIC : HashIC),
|
|
|
|
invite: () => (isSpace ? SpaceLockIC : HashLockIC),
|
|
|
|
public: () => (isSpace ? SpaceGlobeIC : HashGlobeIC),
|
|
|
|
}[joinRule]?.() || null);
|
|
|
|
}
|
|
|
|
|
2021-07-28 15:15:52 +02:00
|
|
|
export {
|
2021-08-25 10:36:13 +02:00
|
|
|
getBaseUrl, getUsername, getUsernameOfRoomMember,
|
2021-12-03 14:02:10 +01:00
|
|
|
isRoomAliasAvailable, getPowerLabel, parseReply,
|
2022-02-16 15:22:51 +01:00
|
|
|
hasDMWith, joinRuleToIconSrc,
|
2021-07-28 15:15:52 +02:00
|
|
|
};
|