2021-07-28 15:15:52 +02:00
|
|
|
import initMatrix from '../client/initMatrix';
|
|
|
|
|
|
|
|
const WELL_KNOWN_URI = '/.well-known/matrix/client';
|
|
|
|
|
|
|
|
async function getBaseUrl(homeserver) {
|
|
|
|
const serverDiscoveryUrl = `https://${homeserver}${WELL_KNOWN_URI}`;
|
|
|
|
try {
|
|
|
|
const result = await fetch(serverDiscoveryUrl, { method: 'GET' });
|
|
|
|
const data = await result.json();
|
|
|
|
|
|
|
|
return data?.['m.homeserver']?.base_url;
|
|
|
|
} catch (e) {
|
|
|
|
throw new Error('Homeserver not found');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 myUserId = initMatrix.matrixClient.getUserId();
|
|
|
|
const myServer = myUserId.slice(myUserId.indexOf(':') + 1);
|
|
|
|
const result = await initMatrix.matrixClient.resolveRoomAlias(alias);
|
|
|
|
const aliasIsRegisteredOnMyServer = typeof result.servers.find((server) => server === myServer) === 'string';
|
|
|
|
|
|
|
|
if (aliasIsRegisteredOnMyServer) return false;
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
if (e.errcode === 'M_NOT_FOUND') return true;
|
|
|
|
if (e.errcode === 'M_INVALID_PARAM') throw new Error(e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function doesRoomHaveUnread(room) {
|
|
|
|
const userId = initMatrix.matrixClient.getUserId();
|
|
|
|
const readUpToId = room.getEventReadUpTo(userId);
|
2021-08-26 07:06:41 +02:00
|
|
|
const supportEvents = ['m.room.message', 'm.room.encrypted', 'm.sticker'];
|
2021-07-28 15:15:52 +02:00
|
|
|
|
|
|
|
if (room.timeline.length
|
|
|
|
&& room.timeline[room.timeline.length - 1].sender
|
|
|
|
&& room.timeline[room.timeline.length - 1].sender.userId === userId
|
|
|
|
&& room.timeline[room.timeline.length - 1].getType() !== 'm.room.member') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = room.timeline.length - 1; i >= 0; i -= 1) {
|
|
|
|
const event = room.timeline[i];
|
|
|
|
|
|
|
|
if (event.getId() === readUpToId) return false;
|
2021-08-26 07:06:41 +02:00
|
|
|
|
|
|
|
if (supportEvents.includes(event.getType())) {
|
|
|
|
return true;
|
|
|
|
}
|
2021-07-28 15:15:52 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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-07-28 15:15:52 +02:00
|
|
|
export {
|
2021-08-25 10:36:13 +02:00
|
|
|
getBaseUrl, getUsername, getUsernameOfRoomMember,
|
2021-10-18 17:25:52 +02:00
|
|
|
isRoomAliasAvailable, doesRoomHaveUnread, getPowerLabel,
|
2021-07-28 15:15:52 +02:00
|
|
|
};
|