Remove unused javascript (#2470)
This commit is contained in:
@@ -1,13 +0,0 @@
|
||||
import cons from '../state/cons';
|
||||
|
||||
export function updateLocalStore(
|
||||
accessToken: string,
|
||||
deviceId: string,
|
||||
userId: string,
|
||||
baseUrl: string
|
||||
) {
|
||||
localStorage.setItem(cons.secretKey.ACCESS_TOKEN, accessToken);
|
||||
localStorage.setItem(cons.secretKey.DEVICE_ID, deviceId);
|
||||
localStorage.setItem(cons.secretKey.USER_ID, userId);
|
||||
localStorage.setItem(cons.secretKey.BASE_URL, baseUrl);
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
import appDispatcher from '../dispatcher';
|
||||
import cons from '../state/cons';
|
||||
|
||||
|
||||
export function openSpaceAddExisting(roomId, spaces = false) {
|
||||
appDispatcher.dispatch({
|
||||
type: cons.actions.navigation.OPEN_SPACE_ADDEXISTING,
|
||||
roomId,
|
||||
spaces,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
export function openCreateRoom(isSpace = false, parentId = null) {
|
||||
appDispatcher.dispatch({
|
||||
type: cons.actions.navigation.OPEN_CREATE_ROOM,
|
||||
isSpace,
|
||||
parentId,
|
||||
});
|
||||
}
|
||||
|
||||
export function openJoinAlias(term) {
|
||||
appDispatcher.dispatch({
|
||||
type: cons.actions.navigation.OPEN_JOIN_ALIAS,
|
||||
term,
|
||||
});
|
||||
}
|
||||
|
||||
export function openInviteUser(roomId, searchTerm) {
|
||||
appDispatcher.dispatch({
|
||||
type: cons.actions.navigation.OPEN_INVITE_USER,
|
||||
roomId,
|
||||
searchTerm,
|
||||
});
|
||||
}
|
||||
|
||||
export function openProfileViewer(userId, roomId) {
|
||||
appDispatcher.dispatch({
|
||||
type: cons.actions.navigation.OPEN_PROFILE_VIEWER,
|
||||
userId,
|
||||
roomId,
|
||||
});
|
||||
}
|
||||
|
||||
export function openSearch(term) {
|
||||
appDispatcher.dispatch({
|
||||
type: cons.actions.navigation.OPEN_SEARCH,
|
||||
term,
|
||||
});
|
||||
}
|
||||
|
||||
export function openReusableContextMenu(placement, cords, render, afterClose) {
|
||||
appDispatcher.dispatch({
|
||||
type: cons.actions.navigation.OPEN_REUSABLE_CONTEXT_MENU,
|
||||
placement,
|
||||
cords,
|
||||
render,
|
||||
afterClose,
|
||||
});
|
||||
}
|
||||
|
||||
export function openReusableDialog(title, render, afterClose) {
|
||||
appDispatcher.dispatch({
|
||||
type: cons.actions.navigation.OPEN_REUSABLE_DIALOG,
|
||||
title,
|
||||
render,
|
||||
afterClose,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
import { MatrixClient, ReceiptType } from 'matrix-js-sdk';
|
||||
|
||||
export async function markAsRead(mx: MatrixClient, roomId: string, privateReceipt: boolean) {
|
||||
const room = mx.getRoom(roomId);
|
||||
if (!room) return;
|
||||
|
||||
const timeline = room.getLiveTimeline().getEvents();
|
||||
const readEventId = room.getEventReadUpTo(mx.getUserId()!);
|
||||
|
||||
const getLatestValidEvent = () => {
|
||||
for (let i = timeline.length - 1; i >= 0; i -= 1) {
|
||||
const latestEvent = timeline[i];
|
||||
if (latestEvent.getId() === readEventId) return null;
|
||||
if (!latestEvent.isSending()) return latestEvent;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
if (timeline.length === 0) return;
|
||||
const latestEvent = getLatestValidEvent();
|
||||
if (latestEvent === null) return;
|
||||
|
||||
await mx.sendReadReceipt(
|
||||
latestEvent,
|
||||
privateReceipt ? ReceiptType.ReadPrivate : ReceiptType.Read
|
||||
);
|
||||
}
|
||||
@@ -1,279 +0,0 @@
|
||||
import { EventTimeline } from 'matrix-js-sdk';
|
||||
import { getIdServer } from '../../util/matrixUtil';
|
||||
|
||||
/**
|
||||
* https://github.com/matrix-org/matrix-react-sdk/blob/1e6c6e9d800890c732d60429449bc280de01a647/src/Rooms.js#L73
|
||||
* @param {MatrixClient} mx Matrix client
|
||||
* @param {string} roomId Id of room to add
|
||||
* @param {string} userId User id to which dm || undefined to remove
|
||||
* @returns {Promise} A promise
|
||||
*/
|
||||
function addRoomToMDirect(mx, roomId, userId) {
|
||||
const mDirectsEvent = mx.getAccountData('m.direct');
|
||||
let userIdToRoomIds = {};
|
||||
|
||||
if (typeof mDirectsEvent !== 'undefined') userIdToRoomIds = structuredClone(mDirectsEvent.getContent());
|
||||
|
||||
// remove it from the lists of any others users
|
||||
// (it can only be a DM room for one person)
|
||||
Object.keys(userIdToRoomIds).forEach((thisUserId) => {
|
||||
const roomIds = userIdToRoomIds[thisUserId];
|
||||
|
||||
if (thisUserId !== userId) {
|
||||
const indexOfRoomId = roomIds.indexOf(roomId);
|
||||
if (indexOfRoomId > -1) {
|
||||
roomIds.splice(indexOfRoomId, 1);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// now add it, if it's not already there
|
||||
if (userId) {
|
||||
const roomIds = userIdToRoomIds[userId] || [];
|
||||
if (roomIds.indexOf(roomId) === -1) {
|
||||
roomIds.push(roomId);
|
||||
}
|
||||
userIdToRoomIds[userId] = roomIds;
|
||||
}
|
||||
|
||||
return mx.setAccountData('m.direct', userIdToRoomIds);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a room, estimate which of its members is likely to
|
||||
* be the target if the room were a DM room and return that user.
|
||||
* https://github.com/matrix-org/matrix-react-sdk/blob/1e6c6e9d800890c732d60429449bc280de01a647/src/Rooms.js#L117
|
||||
*
|
||||
* @param {Object} room Target room
|
||||
* @param {string} myUserId User ID of the current user
|
||||
* @returns {string} User ID of the user that the room is probably a DM with
|
||||
*/
|
||||
function guessDMRoomTargetId(room, myUserId) {
|
||||
let oldestMemberTs;
|
||||
let oldestMember;
|
||||
|
||||
// Pick the joined user who's been here longest (and isn't us),
|
||||
room.getJoinedMembers().forEach((member) => {
|
||||
if (member.userId === myUserId) return;
|
||||
|
||||
if (typeof oldestMemberTs === 'undefined' || (member.events.member && member.events.member.getTs() < oldestMemberTs)) {
|
||||
oldestMember = member;
|
||||
oldestMemberTs = member.events.member.getTs();
|
||||
}
|
||||
});
|
||||
if (oldestMember) return oldestMember.userId;
|
||||
|
||||
// if there are no joined members other than us, use the oldest member
|
||||
room.getLiveTimeline().getState(EventTimeline.FORWARDS)?.getMembers().forEach((member) => {
|
||||
if (member.userId === myUserId) return;
|
||||
|
||||
if (typeof oldestMemberTs === 'undefined' || (member.events.member && member.events.member.getTs() < oldestMemberTs)) {
|
||||
oldestMember = member;
|
||||
oldestMemberTs = member.events.member.getTs();
|
||||
}
|
||||
});
|
||||
|
||||
if (typeof oldestMember === 'undefined') return myUserId;
|
||||
return oldestMember.userId;
|
||||
}
|
||||
|
||||
function convertToDm(mx, roomId) {
|
||||
const room = mx.getRoom(roomId);
|
||||
return addRoomToMDirect(mx, roomId, guessDMRoomTargetId(room, mx.getUserId()));
|
||||
}
|
||||
|
||||
function convertToRoom(mx, roomId) {
|
||||
return addRoomToMDirect(mx, roomId, undefined);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {MatrixClient} mx
|
||||
* @param {string} roomId
|
||||
* @param {boolean} isDM
|
||||
* @param {string[]} via
|
||||
*/
|
||||
async function join(mx, roomIdOrAlias, isDM = false, via = undefined) {
|
||||
try {
|
||||
const resultRoom = await mx.joinRoom(roomIdOrAlias, { viaServers: via });
|
||||
|
||||
if (isDM) {
|
||||
const targetUserId = guessDMRoomTargetId(mx.getRoom(resultRoom.roomId), mx.getUserId());
|
||||
await addRoomToMDirect(mx, resultRoom.roomId, targetUserId);
|
||||
}
|
||||
return resultRoom.roomId;
|
||||
} catch (e) {
|
||||
throw new Error(e);
|
||||
}
|
||||
}
|
||||
|
||||
async function create(mx, options, isDM = false) {
|
||||
try {
|
||||
const result = await mx.createRoom(options);
|
||||
if (isDM && typeof options.invite?.[0] === 'string') {
|
||||
await addRoomToMDirect(mx, result.room_id, options.invite[0]);
|
||||
}
|
||||
return result;
|
||||
} catch (e) {
|
||||
const errcodes = ['M_UNKNOWN', 'M_BAD_JSON', 'M_ROOM_IN_USE', 'M_INVALID_ROOM_STATE', 'M_UNSUPPORTED_ROOM_VERSION'];
|
||||
if (errcodes.includes(e.errcode)) {
|
||||
throw new Error(e);
|
||||
}
|
||||
throw new Error('Something went wrong!');
|
||||
}
|
||||
}
|
||||
|
||||
async function createDM(mx, userIdOrIds, isEncrypted = true) {
|
||||
const options = {
|
||||
is_direct: true,
|
||||
invite: Array.isArray(userIdOrIds) ? userIdOrIds : [userIdOrIds],
|
||||
visibility: 'private',
|
||||
preset: 'trusted_private_chat',
|
||||
initial_state: [],
|
||||
};
|
||||
if (isEncrypted) {
|
||||
options.initial_state.push({
|
||||
type: 'm.room.encryption',
|
||||
state_key: '',
|
||||
content: {
|
||||
algorithm: 'm.megolm.v1.aes-sha2',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const result = await create(mx, options, true);
|
||||
return result;
|
||||
}
|
||||
|
||||
async function createRoom(mx, opts) {
|
||||
// joinRule: 'public' | 'invite' | 'restricted'
|
||||
const { name, topic, joinRule } = opts;
|
||||
const alias = opts.alias ?? undefined;
|
||||
const parentId = opts.parentId ?? undefined;
|
||||
const isSpace = opts.isSpace ?? false;
|
||||
const isEncrypted = opts.isEncrypted ?? false;
|
||||
const powerLevel = opts.powerLevel ?? undefined;
|
||||
const blockFederation = opts.blockFederation ?? false;
|
||||
|
||||
const visibility = joinRule === 'public' ? 'public' : 'private';
|
||||
const options = {
|
||||
creation_content: undefined,
|
||||
name,
|
||||
topic,
|
||||
visibility,
|
||||
room_alias_name: alias,
|
||||
initial_state: [],
|
||||
power_level_content_override: undefined,
|
||||
};
|
||||
if (isSpace) {
|
||||
options.creation_content = { type: 'm.space' };
|
||||
}
|
||||
if (blockFederation) {
|
||||
options.creation_content = { 'm.federate': false };
|
||||
}
|
||||
if (isEncrypted) {
|
||||
options.initial_state.push({
|
||||
type: 'm.room.encryption',
|
||||
state_key: '',
|
||||
content: {
|
||||
algorithm: 'm.megolm.v1.aes-sha2',
|
||||
},
|
||||
});
|
||||
}
|
||||
if (powerLevel) {
|
||||
options.power_level_content_override = {
|
||||
users: {
|
||||
[mx.getUserId()]: powerLevel,
|
||||
},
|
||||
};
|
||||
}
|
||||
if (parentId) {
|
||||
options.initial_state.push({
|
||||
type: 'm.space.parent',
|
||||
state_key: parentId,
|
||||
content: {
|
||||
canonical: true,
|
||||
via: [getIdServer(mx.getUserId())],
|
||||
},
|
||||
});
|
||||
}
|
||||
if (parentId && joinRule === 'restricted') {
|
||||
const caps = await mx.getCapabilities();
|
||||
if (caps['m.room_versions'].available?.['9'] !== 'stable') {
|
||||
throw new Error("ERROR: The server doesn't support restricted rooms");
|
||||
}
|
||||
if (Number(caps['m.room_versions'].default) < 9) {
|
||||
options.room_version = '9';
|
||||
}
|
||||
options.initial_state.push({
|
||||
type: 'm.room.join_rules',
|
||||
content: {
|
||||
join_rule: 'restricted',
|
||||
allow: [{
|
||||
type: 'm.room_membership',
|
||||
room_id: parentId,
|
||||
}],
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const result = await create(mx, options);
|
||||
|
||||
if (parentId) {
|
||||
await mx.sendStateEvent(parentId, 'm.space.child', {
|
||||
auto_join: false,
|
||||
suggested: false,
|
||||
via: [getIdServer(mx.getUserId())],
|
||||
}, result.room_id);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
async function ignore(mx, userIds) {
|
||||
|
||||
let ignoredUsers = mx.getIgnoredUsers().concat(userIds);
|
||||
ignoredUsers = [...new Set(ignoredUsers)];
|
||||
await mx.setIgnoredUsers(ignoredUsers);
|
||||
}
|
||||
|
||||
async function unignore(mx, userIds) {
|
||||
const ignoredUsers = mx.getIgnoredUsers();
|
||||
await mx.setIgnoredUsers(ignoredUsers.filter((id) => !userIds.includes(id)));
|
||||
}
|
||||
|
||||
async function setPowerLevel(mx, roomId, userId, powerLevel) {
|
||||
const result = await mx.setPowerLevel(roomId, userId, powerLevel);
|
||||
return result;
|
||||
}
|
||||
|
||||
async function setMyRoomNick(mx, roomId, nick) {
|
||||
const room = mx.getRoom(roomId);
|
||||
const mEvent = room.getLiveTimeline().getState(EventTimeline.FORWARDS).getStateEvents('m.room.member', mx.getUserId());
|
||||
const content = mEvent?.getContent();
|
||||
if (!content) return;
|
||||
await mx.sendStateEvent(roomId, 'm.room.member', {
|
||||
...content,
|
||||
displayname: nick,
|
||||
}, mx.getUserId());
|
||||
}
|
||||
|
||||
async function setMyRoomAvatar(mx, roomId, mxc) {
|
||||
const room = mx.getRoom(roomId);
|
||||
const mEvent = room.getLiveTimeline().getState(EventTimeline.FORWARDS).getStateEvents('m.room.member', mx.getUserId());
|
||||
const content = mEvent?.getContent();
|
||||
if (!content) return;
|
||||
await mx.sendStateEvent(roomId, 'm.room.member', {
|
||||
...content,
|
||||
avatar_url: mxc,
|
||||
}, mx.getUserId());
|
||||
}
|
||||
|
||||
export {
|
||||
convertToDm,
|
||||
convertToRoom,
|
||||
join,
|
||||
createDM, createRoom,
|
||||
ignore, unignore,
|
||||
setPowerLevel,
|
||||
setMyRoomNick, setMyRoomAvatar,
|
||||
};
|
||||
@@ -1,4 +0,0 @@
|
||||
import { Dispatcher } from 'flux';
|
||||
|
||||
const appDispatcher = new Dispatcher();
|
||||
export default appDispatcher;
|
||||
@@ -1,6 +1,6 @@
|
||||
import { createClient, MatrixClient, IndexedDBStore, IndexedDBCryptoStore } from 'matrix-js-sdk';
|
||||
|
||||
import { cryptoCallbacks } from './state/secretStorageKeys';
|
||||
import { cryptoCallbacks } from './secretStorageKeys';
|
||||
import { clearNavToActivePathStore } from '../app/state/navToActivePath';
|
||||
|
||||
type Session = {
|
||||
|
||||
@@ -7,18 +7,14 @@ export function storePrivateKey(keyId, privateKey) {
|
||||
secretStorageKeys.set(keyId, privateKey);
|
||||
}
|
||||
|
||||
export function hasPrivateKey(keyId) {
|
||||
function hasPrivateKey(keyId) {
|
||||
return secretStorageKeys.get(keyId) instanceof Uint8Array;
|
||||
}
|
||||
|
||||
export function getPrivateKey(keyId) {
|
||||
function getPrivateKey(keyId) {
|
||||
return secretStorageKeys.get(keyId);
|
||||
}
|
||||
|
||||
export function deletePrivateKey(keyId) {
|
||||
delete secretStorageKeys.delete(keyId);
|
||||
}
|
||||
|
||||
export function clearSecretStorageKeys() {
|
||||
secretStorageKeys.clear();
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
import cons from './cons';
|
||||
|
||||
const isAuthenticated = () => localStorage.getItem(cons.secretKey.ACCESS_TOKEN) !== null;
|
||||
|
||||
const getSecret = () => ({
|
||||
accessToken: localStorage.getItem(cons.secretKey.ACCESS_TOKEN),
|
||||
deviceId: localStorage.getItem(cons.secretKey.DEVICE_ID),
|
||||
userId: localStorage.getItem(cons.secretKey.USER_ID),
|
||||
baseUrl: localStorage.getItem(cons.secretKey.BASE_URL),
|
||||
});
|
||||
|
||||
export { isAuthenticated, getSecret };
|
||||
@@ -1,42 +0,0 @@
|
||||
const cons = {
|
||||
version: '4.9.1',
|
||||
secretKey: {
|
||||
ACCESS_TOKEN: 'cinny_access_token',
|
||||
DEVICE_ID: 'cinny_device_id',
|
||||
USER_ID: 'cinny_user_id',
|
||||
BASE_URL: 'cinny_hs_base_url',
|
||||
},
|
||||
status: {
|
||||
PRE_FLIGHT: 'pre-flight',
|
||||
IN_FLIGHT: 'in-flight',
|
||||
SUCCESS: 'success',
|
||||
ERROR: 'error',
|
||||
},
|
||||
actions: {
|
||||
navigation: {
|
||||
OPEN_SPACE_ADDEXISTING: 'OPEN_SPACE_ADDEXISTING',
|
||||
OPEN_CREATE_ROOM: 'OPEN_CREATE_ROOM',
|
||||
OPEN_JOIN_ALIAS: 'OPEN_JOIN_ALIAS',
|
||||
OPEN_INVITE_USER: 'OPEN_INVITE_USER',
|
||||
OPEN_PROFILE_VIEWER: 'OPEN_PROFILE_VIEWER',
|
||||
OPEN_SEARCH: 'OPEN_SEARCH',
|
||||
OPEN_REUSABLE_CONTEXT_MENU: 'OPEN_REUSABLE_CONTEXT_MENU',
|
||||
OPEN_REUSABLE_DIALOG: 'OPEN_REUSABLE_DIALOG',
|
||||
},
|
||||
},
|
||||
events: {
|
||||
navigation: {
|
||||
SPACE_ADDEXISTING_OPENED: 'SPACE_ADDEXISTING_OPENED',
|
||||
CREATE_ROOM_OPENED: 'CREATE_ROOM_OPENED',
|
||||
JOIN_ALIAS_OPENED: 'JOIN_ALIAS_OPENED',
|
||||
INVITE_USER_OPENED: 'INVITE_USER_OPENED',
|
||||
SEARCH_OPENED: 'SEARCH_OPENED',
|
||||
REUSABLE_CONTEXT_MENU_OPENED: 'REUSABLE_CONTEXT_MENU_OPENED',
|
||||
REUSABLE_DIALOG_OPENED: 'REUSABLE_DIALOG_OPENED',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
Object.freeze(cons);
|
||||
|
||||
export default cons;
|
||||
@@ -1,75 +0,0 @@
|
||||
import EventEmitter from 'events';
|
||||
import appDispatcher from '../dispatcher';
|
||||
import cons from './cons';
|
||||
|
||||
class Navigation extends EventEmitter {
|
||||
constructor() {
|
||||
super();
|
||||
this.rawModelStack = [];
|
||||
}
|
||||
|
||||
get isRawModalVisible() {
|
||||
return this.rawModelStack.length > 0;
|
||||
}
|
||||
|
||||
setIsRawModalVisible(visible) {
|
||||
if (visible) this.rawModelStack.push(true);
|
||||
else this.rawModelStack.pop();
|
||||
}
|
||||
|
||||
navigate(action) {
|
||||
const actions = {
|
||||
[cons.actions.navigation.OPEN_SPACE_ADDEXISTING]: () => {
|
||||
this.emit(cons.events.navigation.SPACE_ADDEXISTING_OPENED, action.roomId, action.spaces);
|
||||
},
|
||||
[cons.actions.navigation.OPEN_CREATE_ROOM]: () => {
|
||||
this.emit(
|
||||
cons.events.navigation.CREATE_ROOM_OPENED,
|
||||
action.isSpace,
|
||||
action.parentId,
|
||||
);
|
||||
},
|
||||
[cons.actions.navigation.OPEN_JOIN_ALIAS]: () => {
|
||||
this.emit(
|
||||
cons.events.navigation.JOIN_ALIAS_OPENED,
|
||||
action.term,
|
||||
);
|
||||
},
|
||||
[cons.actions.navigation.OPEN_INVITE_USER]: () => {
|
||||
this.emit(cons.events.navigation.INVITE_USER_OPENED, action.roomId, action.searchTerm);
|
||||
},
|
||||
[cons.actions.navigation.OPEN_PROFILE_VIEWER]: () => {
|
||||
this.emit(cons.events.navigation.PROFILE_VIEWER_OPENED, action.userId, action.roomId);
|
||||
},
|
||||
[cons.actions.navigation.OPEN_SEARCH]: () => {
|
||||
this.emit(
|
||||
cons.events.navigation.SEARCH_OPENED,
|
||||
action.term,
|
||||
);
|
||||
},
|
||||
[cons.actions.navigation.OPEN_REUSABLE_CONTEXT_MENU]: () => {
|
||||
this.emit(
|
||||
cons.events.navigation.REUSABLE_CONTEXT_MENU_OPENED,
|
||||
action.placement,
|
||||
action.cords,
|
||||
action.render,
|
||||
action.afterClose,
|
||||
);
|
||||
},
|
||||
[cons.actions.navigation.OPEN_REUSABLE_DIALOG]: () => {
|
||||
this.emit(
|
||||
cons.events.navigation.REUSABLE_DIALOG_OPENED,
|
||||
action.title,
|
||||
action.render,
|
||||
action.afterClose,
|
||||
);
|
||||
},
|
||||
};
|
||||
actions[action.type]?.();
|
||||
}
|
||||
}
|
||||
|
||||
const navigation = new Navigation();
|
||||
appDispatcher.register(navigation.navigate.bind(navigation));
|
||||
|
||||
export default navigation;
|
||||
Reference in New Issue
Block a user