better message alert handling

This commit is contained in:
2023-10-23 18:06:00 -04:00
parent cd2032227c
commit edb649bb71
10 changed files with 41 additions and 36 deletions

View File

@@ -33,7 +33,7 @@
message: commitment
});
} else {
alertQueue.enqueue('Error getting Identity');
alertQueue.enqueue('Error getting Identity', "error");
}
}

View File

@@ -14,13 +14,14 @@
inviteCode(code)
.then(({ acceptedRoomNames, err }) => {
if (err) {
alertQueue.enqueue(err);
alertQueue.enqueue(err, 'error');
} else {
acceptedRoomNames = acceptedRoomNames;
}
})
.catch((err) => {
alertQueue.enqueue(err);
console.log(err);
alertQueue.enqueue(err, 'error');
})
.finally(() => {
loading = false;

View File

@@ -45,7 +45,8 @@
$identityKeyStore = id;
alertQueue.enqueue(
`Identity restored from backup file with identity commitment:
${$identityKeyStore._commitment}`
${$identityKeyStore._commitment}`,
'success'
);
} else if ($lockStateStore == 'locked') {
alertQueue.enqueue('Please 🔑 UNLOCK before restoring your identity', 'warning');
@@ -62,7 +63,7 @@
console.debug(`Backup/recovery file type detected as ${f?.type}`);
let unverifiedBackup: any;
if (!f) {
alertQueue.enqueue('No file selected');
alertQueue.enqueue('No file selected', 'warning');
return;
}
if (f.type == 'application/json' || f.type == 'text/plain') {
@@ -72,7 +73,8 @@
});
} else {
alertQueue.enqueue(
'Invalid file type, must be a JSON object with the _nullifier, _trapdoor, _secret, and _commitment as stringified bigints'
'Invalid file type, must be a JSON object with the _nullifier, _trapdoor, _secret, and _commitment as stringified bigints',
'warning'
);
console.warn('Invalid file type');
}
@@ -82,7 +84,7 @@
const textBox = document.getElementById('jsonRecovery') as HTMLInputElement;
const json = textBox.value;
if (!json || json == '') {
alertQueue.enqueue('No JSON detected');
alertQueue.enqueue('No JSON detected', 'warning');
return;
} else {
restoreBackup(json);

View File

@@ -17,6 +17,7 @@ function getSalt(): Uint8Array {
// Generate new salt if salt is not set
if (saltFromStore === '') {
console.debug('Making new salt');
window.crypto.getRandomValues(salt);
// Convert to hexadecimal string
@@ -26,6 +27,7 @@ function getSalt(): Uint8Array {
saltStore.set(saltString);
} else {
console.debug(`Restoring Salt: ${saltFromStore}`);
for (let i = 0; i < salt.length; i++) {
salt[i] = parseInt(saltFromStore.substring(i * 2, i * 2 + 2), 16);
}
@@ -86,11 +88,11 @@ export async function encrypt(plainText: string, key: CryptoKey): Promise<string
return btoa(String.fromCharCode(...encryptedBytes));
} else if (get(configStore).hashedPwd) {
alertQueue.enqueue('Unlock your identity with /unlock or click on the lock');
alertQueue.enqueue('Unlock your identity with /unlock or click on the lock', 'warning');
console.error('Unlock your identity with /unlock or click on the lock');
return null;
} else {
alertQueue.enqueue('No Password Set, please set a password with /setpassword');
alertQueue.enqueue('No Password Set, please set a password with /setpassword', 'warning');
console.error('No password set, please set a password first');
return null;
}
@@ -115,11 +117,11 @@ export async function decrypt(cipherText: string, key: CryptoKey): Promise<strin
return decoder.decode(decryptedContent);
} else if (get(configStore).hashedPwd) {
alertQueue.enqueue('Unlock your identity with /unlock or click on the lock');
alertQueue.enqueue('Unlock your identity with /unlock or click on the lock', 'warning');
console.error('Unlock your identity with /unlock or click on the lock');
return null;
} else {
alertQueue.enqueue('No Password Set, please set a password with /setpassword');
alertQueue.enqueue('No Password Set, please set a password with /setpassword', 'warning');
console.error('No password set, please set a password first');
return null;
}

View File

@@ -11,7 +11,7 @@ export async function inviteCode(newCode: string) {
try {
const idc = getCommitment();
if (!idc) {
alertQueue.enqueue('No identity commitment found');
alertQueue.enqueue('No identity commitment found', 'warning');
throw new Error('No identity commitment found');
}
const result = (await postInviteCode(server, {

View File

@@ -151,7 +151,7 @@ declare function queueService(): {
update: (callBack: (value: any[]) => any[]) => void;
enqueue: (
value: any,
type?: 'primary' | 'secondary' | 'tertiary' | 'success' | 'warning' | 'error'
type: 'primary' | 'secondary' | 'tertiary' | 'success' | 'warning' | 'error'
) => void;
dequeue: () => any;
};

View File

@@ -6,20 +6,18 @@ import {
identityStore,
identityExists,
keyStore,
lockStateStore
lockStateStore,
passwordSet
} from '../stores';
import { Identity } from '@semaphore-protocol/identity';
import { IdentityStoreE, type IdentityStoreI } from '$lib/types';
export function createIdentity(regenerate = false): 'created' | 'exists' | 'unsafe' | 'error' {
const identityStatus = get(identityExists);
console.log(identityStatus);
if (!get(identityExists) || regenerate) {
console.debug('Creating identity');
const identity = new Identity() as unknown as IdentityStoreI;
const config = get(configStore);
const lockState = get(lockStateStore);
if (config.hashedPwd && config.hashedPwd.length > 0) {
if (get(passwordSet)) {
if (lockState === 'unlocked') {
try {
identityKeyStore.set(identity);
@@ -28,23 +26,24 @@ export function createIdentity(regenerate = false): 'created' | 'exists' | 'unsa
state.identityStore = IdentityStoreE.localStorageEncrypted;
return state;
});
alertQueue.enqueue('Identity Created! Congrats on your new journey');
alertQueue.enqueue('Identity Created! Congrats on your new journey', 'success');
return 'created';
} else {
alertQueue.enqueue('Error creating identity');
alertQueue.enqueue('Error creating identity', 'error');
return 'error';
}
} catch (e) {
alertQueue.enqueue(`Error creating identity: ${e}`);
alertQueue.enqueue(`Error creating identity: ${e}`, 'error');
return 'error';
}
} else {
alertQueue.enqueue('Unlock your account by clicking on the lock');
alertQueue.enqueue('Unlock your account by clicking on the lock', 'warning');
return 'error';
}
} else {
alertQueue.enqueue(
'For your security please set a password with /password or click on the lock in the corner'
'For your security please set a password with /password or click on the lock in the corner',
'warning'
);
identityStore.set(identity);
configStore.update((state) => {
@@ -54,7 +53,7 @@ export function createIdentity(regenerate = false): 'created' | 'exists' | 'unsa
return 'unsafe';
}
} else {
alertQueue.enqueue('Identity already exists');
alertQueue.enqueue('Identity already exists', 'warning');
return 'exists';
}
}
@@ -63,15 +62,14 @@ export function getIdentity(): IdentityStoreI | null {
const decryptedIdentity = get(identityKeyStore) as unknown as IdentityStoreI;
if (decryptedIdentity._commitment) {
console.log(decryptedIdentity);
return decryptedIdentity;
} else {
const identity = get(identityStore);
if (identity._commitment?.length > 0) {
alertQueue.enqueue('Identity not encrypted, set a password!');
alertQueue.enqueue('Identity not encrypted, set a password!', 'warning');
return identity;
} else {
alertQueue.enqueue('Identity not created, create an identity!');
alertQueue.enqueue('Identity not created, create an identity!', 'warning');
}
}
return null;

View File

@@ -73,7 +73,7 @@ export async function unlockPadlock(password: string) {
roomKeyStore.read();
});
} else {
alertQueue.enqueue('Incorrect Password');
alertQueue.enqueue('Incorrect Password', 'warning');
keyStore.set(null);
}
}

View File

@@ -45,7 +45,7 @@ async function getRoomIdsIfEmpty(server: string, roomIds: string[]): Promise<str
if (roomIds.length < 1) {
const idc = getCommitment();
if (!idc) {
alertQueue.enqueue('No identity commitment found');
alertQueue.enqueue('No identity commitment found', 'warning');
throw new Error('No identity commitment found');
}
return await getRoomIdsByIdentityCommitment(server, idc);

View File

@@ -38,25 +38,26 @@
function checkStatus(): boolean {
if (!connected) {
alertQueue.enqueue('NOT CONNECTED TO CHAT SERVER');
alertQueue.enqueue('NOT CONNECTED TO CHAT SERVER', 'error');
sendingMessage = false;
return false;
}
if (messageText.length < 1) {
alertQueue.enqueue('MESSAGE IS EMPTY');
alertQueue.enqueue('MESSAGE IS EMPTY', 'warning');
sendingMessage = false;
return false;
}
if (messageText.length > 2000) {
alertQueue.enqueue(
'MESSAGE IS TOO LONG, SENDING MAY FAIL UNDER NETWORK CONSTRAINED CONDITIONS'
'MESSAGE IS TOO LONG, SENDING MAY FAIL UNDER NETWORK CONSTRAINED CONDITIONS',
'warning'
);
sendingMessage = false;
return false;
}
// This is 100% thanks to Violet for spamming the chat with spaces
if (messageText.replaceAll(' ', '') == '') {
alertQueue.enqueue('MESSAGE IS EMPTY');
alertQueue.enqueue('MESSAGE IS EMPTY', 'warning');
sendingMessage = false;
return false;
}
@@ -64,7 +65,7 @@
}
function help() {
alertQueue.enqueue('Commands: /clear, /help');
alertQueue.enqueue('Commands: /clear, /help', 'tertiary');
}
function processCommand(value: string) {
@@ -163,10 +164,11 @@
console.error('Error sending message: ', err);
if (err.message.includes('Merkle Proof')) {
alertQueue.enqueue(
"Couldn't generate Merkle Proof. Maybe you don't belong in the room or don't have an updated member list."
"Couldn't generate Merkle Proof. Maybe you don't belong in the room or don't have an updated member list.",
'warning'
);
} else {
alertQueue.enqueue(err as string);
alertQueue.enqueue(err as string, 'error');
}
} finally {
sendingMessage = false;