Skip to content

Commit

Permalink
feat: add desktop app version check
Browse files Browse the repository at this point in the history
  • Loading branch information
ayonshafiul committed Jul 28, 2024
1 parent cd24ac9 commit 630dd48
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
2 changes: 2 additions & 0 deletions assets/constants/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export const SERVER_URL_KEY = 'SERVER_URL';

export const QRCODE_SECRET = '<peyara>';

export const DESKTOP_APP_VERSION = '0.1.1';

export const SERVER_REST_RESPONSE = 'peyara';

// settings constants
Expand Down
10 changes: 4 additions & 6 deletions screens/QRCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@ export default function QRCode({navigation}) {

const handleBarCodeScanned = async ({type, data}) => {
setLoading(true);
let qrCodeAdded = await addServer(data);
console.log(data);
let {error, errorTitle} = await addServer(data);

setLoading(false);
if (!qrCodeAdded) {
Alert.alert(
'Invalid QR code or different wifi network.',
'Make sure your desktop and mobile are connected to the same wifi network. Only scan the QR code shown on Peyara desktop client. ',
);
if (error) {
Alert.alert(errorTitle);
setScanned(true);
} else {
navigation.goBack();
Expand Down
49 changes: 35 additions & 14 deletions utils/servers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
QRCODE_SECRET,
SERVER_REST_RESPONSE,
SERVER_KEY,
DESKTOP_APP_VERSION,
} from '../assets/constants/constants';
import {getValueFor, setValueFor} from './storage';

Expand Down Expand Up @@ -32,29 +33,45 @@ function sleep(time) {
}

export async function addServer(qrCodeValue) {
let errorObject = {
error: false,
errorTitle: '',
};

if (!qrCodeValue) {
return false;
}
let servers = getValueFor(SERVER_KEY);
if (!qrCodeValue.includes(QRCODE_SECRET)) {
return false;
errorObject.error = true;
errorObject.errorTitle = 'No data found';
return errorObject;
}

let qrCodeServers = qrCodeValue.split(',');
if (!(qrCodeServers.length > 2)) {
return false;
errorObject.error = true;
errorObject.errorTitle = 'QRCode seems to be invalid';
return errorObject;
}

let secret = qrCodeServers[0];
let appVersion = qrCodeServers[0]; // first index should be app version
if (appVersion !== DESKTOP_APP_VERSION) {
errorObject.error = true;
errorObject.errorTitle =
'Please update desktop app to the latest version and try again';
return errorObject;
}

let secret = qrCodeServers[1];
if (secret !== QRCODE_SECRET) {
return false;
errorObject.error = true;
errorObject.errorTitle =
'Make sure to scan the QRcode shown only on Peyara Desktop App';
return errorObject;
}

let hostName = qrCodeServers[1];
let hostName = qrCodeServers[2];
let serverEntry = null;
// try all possible servers to see which works

for (let i = 2; i < qrCodeServers.length; i++) {
for (let i = 3; i < qrCodeServers.length; i++) {
let url = qrCodeServers[i];
// Alert.alert(url, 'url');
try {
Expand All @@ -75,23 +92,27 @@ export async function addServer(qrCodeValue) {
}
}
if (!serverEntry) {
return false;
errorObject.error = true;
errorObject.errorTitle = "Couldn't connect to server";
return errorObject;
}

let servers = getValueFor(SERVER_KEY);
if (servers) {
try {
let serversArray = JSON.parse(servers);
serversArray.unshift(serverEntry);
setValueFor(SERVER_KEY, JSON.stringify(serversArray));
} catch (e) {
setValueFor(SERVER_KEY, JSON.stringify([]));
return false;
errorObject.error = true;
errorObject.errorTitle = "Couldn't save the server url";
return errorObject;
}
} else {
// initialize store with server value
setValueFor(SERVER_KEY, JSON.stringify([serverEntry]));
}
return true;
return {error: false};
}

export function setServers(serversArray) {
Expand Down

0 comments on commit 630dd48

Please sign in to comment.