Bug fixes in emoji verificaiton

This commit is contained in:
Ajay Bura 2022-05-03 12:52:26 +05:30
parent 3f83514427
commit 2ba4d2f2b7
4 changed files with 52 additions and 43 deletions

View file

@ -20,30 +20,14 @@ import CrossIC from '../../../../public/res/ic/outlined/cross.svg';
import { useStore } from '../../hooks/useStore'; import { useStore } from '../../hooks/useStore';
import { accessSecretStorage } from '../settings/SecretStorageAccess'; import { accessSecretStorage } from '../settings/SecretStorageAccess';
function EmojiVerificationContent({ request, requestClose }) { function EmojiVerificationContent({ data, requestClose }) {
const [sas, setSas] = useState(null); const [sas, setSas] = useState(null);
const [process, setProcess] = useState(false); const [process, setProcess] = useState(false);
const { request, targetDevice } = data;
const mx = initMatrix.matrixClient; const mx = initMatrix.matrixClient;
const mountStore = useStore(); const mountStore = useStore();
useEffect(() => { const beginVerification = async () => {
mountStore.setItem(true);
const handleChange = () => {
if (request.done || request.cancelled) requestClose();
};
if (request === null) return null;
const req = request;
req.on('change', handleChange);
return () => {
req.off('change', handleChange);
if (!req.cancelled && !req.done) {
req.cancel();
}
};
}, [request]);
const acceptRequest = async () => {
if (isCrossVerified(mx.deviceId) && !hasPrivateKey(getDefaultSSKey())) { if (isCrossVerified(mx.deviceId) && !hasPrivateKey(getDefaultSSKey())) {
const keyData = await accessSecretStorage('Session verification'); const keyData = await accessSecretStorage('Session verification');
if (!keyData) { if (!keyData) {
@ -54,22 +38,12 @@ function EmojiVerificationContent({ request, requestClose }) {
setProcess(true); setProcess(true);
await request.accept(); await request.accept();
let targetDevice;
try {
targetDevice = request.targetDevice;
} catch {
targetDevice = {
userId: mx.getUserId(),
deviceId: request.channel.devices[0],
};
}
const verifier = request.beginKeyVerification('m.sas.v1', targetDevice); const verifier = request.beginKeyVerification('m.sas.v1', targetDevice);
const handleVerifier = (data) => { const handleVerifier = (sasData) => {
verifier.off('show_sas', handleVerifier); verifier.off('show_sas', handleVerifier);
if (!mountStore.getItem()) return; if (!mountStore.getItem()) return;
setSas(data); setSas(sasData);
setProcess(false); setProcess(false);
}; };
verifier.on('show_sas', handleVerifier); verifier.on('show_sas', handleVerifier);
@ -86,6 +60,26 @@ function EmojiVerificationContent({ request, requestClose }) {
setProcess(true); setProcess(true);
}; };
useEffect(() => {
mountStore.setItem(true);
const handleChange = () => {
if (targetDevice && request.started) {
beginVerification();
}
if (request.done || request.cancelled) requestClose();
};
if (request === null) return null;
const req = request;
req.on('change', handleChange);
return () => {
req.off('change', handleChange);
if (!req.cancelled && !req.done) {
req.cancel();
}
};
}, [request]);
const renderWait = () => ( const renderWait = () => (
<> <>
<Spinner size="small" /> <Spinner size="small" />
@ -118,6 +112,17 @@ function EmojiVerificationContent({ request, requestClose }) {
); );
} }
if (targetDevice) {
return (
<div className="emoji-verification__content">
<Text>Please accept the request from other device.</Text>
<div className="emoji-verification__buttons">
{renderWait()}
</div>
</div>
);
}
return ( return (
<div className="emoji-verification__content"> <div className="emoji-verification__content">
<Text>Click accept to start the verification process</Text> <Text>Click accept to start the verification process</Text>
@ -125,23 +130,25 @@ function EmojiVerificationContent({ request, requestClose }) {
{ {
process process
? renderWait() ? renderWait()
: <Button variant="primary" onClick={acceptRequest}>Accept</Button> : <Button variant="primary" onClick={beginVerification}>Accept</Button>
} }
</div> </div>
</div> </div>
); );
} }
EmojiVerificationContent.propTypes = { EmojiVerificationContent.propTypes = {
request: PropTypes.shape({}).isRequired, data: PropTypes.shape({}).isRequired,
requestClose: PropTypes.func.isRequired, requestClose: PropTypes.func.isRequired,
}; };
function useVisibilityToggle() { function useVisibilityToggle() {
const [request, setRequest] = useState(null); const [data, setData] = useState(null);
const mx = initMatrix.matrixClient; const mx = initMatrix.matrixClient;
useEffect(() => { useEffect(() => {
const handleOpen = (req) => setRequest(req); const handleOpen = (request, targetDevice) => {
setData({ request, targetDevice });
};
navigation.on(cons.events.navigation.EMOJI_VERIFICATION_OPENED, handleOpen); navigation.on(cons.events.navigation.EMOJI_VERIFICATION_OPENED, handleOpen);
mx.on('crypto.verification.request', handleOpen); mx.on('crypto.verification.request', handleOpen);
return () => { return () => {
@ -150,17 +157,17 @@ function useVisibilityToggle() {
}; };
}, []); }, []);
const requestClose = () => setRequest(null); const requestClose = () => setData(null);
return [request, requestClose]; return [data, requestClose];
} }
function EmojiVerification() { function EmojiVerification() {
const [request, requestClose] = useVisibilityToggle(); const [data, requestClose] = useVisibilityToggle();
return ( return (
<Dialog <Dialog
isOpen={request !== null} isOpen={data !== null}
className="emoji-verification" className="emoji-verification"
title={( title={(
<Text variant="s1" weight="medium" primary> <Text variant="s1" weight="medium" primary>
@ -171,8 +178,8 @@ function EmojiVerification() {
onRequestClose={requestClose} onRequestClose={requestClose}
> >
{ {
request !== null data !== null
? <EmojiVerificationContent request={request} requestClose={requestClose} /> ? <EmojiVerificationContent data={data} requestClose={requestClose} />
: <div /> : <div />
} }
</Dialog> </Dialog>

View file

@ -138,7 +138,7 @@ function DeviceManage() {
const verifyWithEmojis = async (deviceId) => { const verifyWithEmojis = async (deviceId) => {
const req = await mx.requestVerification(mx.getUserId(), [deviceId]); const req = await mx.requestVerification(mx.getUserId(), [deviceId]);
openEmojiVerification(req); openEmojiVerification(req, { userId: mx.getUserId(), deviceId });
}; };
const verify = (deviceId, isCurrentDevice) => { const verify = (deviceId, isCurrentDevice) => {

View file

@ -167,9 +167,10 @@ export function openReusableDialog(title, render, afterClose) {
}); });
} }
export function openEmojiVerification(request) { export function openEmojiVerification(request, targetDevice) {
appDispatcher.dispatch({ appDispatcher.dispatch({
type: cons.actions.navigation.OPEN_EMOJI_VERIFICATION, type: cons.actions.navigation.OPEN_EMOJI_VERIFICATION,
request, request,
targetDevice,
}); });
} }

View file

@ -189,6 +189,7 @@ class Navigation extends EventEmitter {
this.emit( this.emit(
cons.events.navigation.EMOJI_VERIFICATION_OPENED, cons.events.navigation.EMOJI_VERIFICATION_OPENED,
action.request, action.request,
action.targetDevice,
); );
}, },
}; };