initial commit
This commit is contained in:
commit
026f835a87
176 changed files with 10613 additions and 0 deletions
23
src/util/colorMXID.js
Normal file
23
src/util/colorMXID.js
Normal file
|
@ -0,0 +1,23 @@
|
|||
// https://github.com/cloudrac3r/cadencegq/blob/master/pug/mxid.pug
|
||||
|
||||
const colors = ['#368bd6', '#ac3ba8', '#03b381', '#e64f7a', '#ff812d', '#2dc2c5', '#5c56f5', '#74d12c'];
|
||||
function hashCode(str) {
|
||||
let hash = 0;
|
||||
let i;
|
||||
let chr;
|
||||
if (str.length === 0) {
|
||||
return hash;
|
||||
}
|
||||
for (i = 0; i < str.length; i += 1) {
|
||||
chr = str.charCodeAt(i);
|
||||
// eslint-disable-next-line no-bitwise
|
||||
hash = ((hash << 5) - hash) + chr;
|
||||
// eslint-disable-next-line no-bitwise
|
||||
hash |= 0;
|
||||
}
|
||||
return Math.abs(hash);
|
||||
}
|
||||
export default function colorMXID(userId) {
|
||||
const colorNumber = hashCode(userId) % 8;
|
||||
return colors[colorNumber];
|
||||
}
|
21
src/util/common.js
Normal file
21
src/util/common.js
Normal file
|
@ -0,0 +1,21 @@
|
|||
export function bytesToSize(bytes) {
|
||||
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
|
||||
if (bytes === 0) return 'n/a';
|
||||
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)), 10);
|
||||
if (i === 0) return `${bytes} ${sizes[i]}`;
|
||||
return `${(bytes / (1024 ** i)).toFixed(1)} ${sizes[i]}`;
|
||||
}
|
||||
|
||||
export function diffMinutes(dt2, dt1) {
|
||||
let diff = (dt2.getTime() - dt1.getTime()) / 1000;
|
||||
diff /= 60;
|
||||
return Math.abs(Math.round(diff));
|
||||
}
|
||||
|
||||
export function isNotInSameDay(dt2, dt1) {
|
||||
return (
|
||||
dt2.getDay() !== dt1.getDay()
|
||||
|| dt2.getMonth() !== dt1.getMonth()
|
||||
|| dt2.getYear() !== dt1.getYear()
|
||||
);
|
||||
}
|
67
src/util/matrixUtil.js
Normal file
67
src/util/matrixUtil.js
Normal file
|
@ -0,0 +1,67 @@
|
|||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
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;
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
export {
|
||||
getBaseUrl, getUsername,
|
||||
isRoomAliasAvailable, doesRoomHaveUnread,
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue