Skip to content

Commit

Permalink
[native] add handler for staff to attempt backup restore
Browse files Browse the repository at this point in the history
Summary:
[ENG-9711](https://linear.app/comm/issue/ENG-9711/add-handler-for-staff-to-attempt-restore-after-attempting-backup).

Depends on D14059

Test Plan: Update `millisecondsPerDay` to be lower value and test if this works.

Reviewers: bartek, tomek

Reviewed By: bartek

Subscribers: ashoat

Differential Revision: https://phab.comm.dev/D14060
  • Loading branch information
xsanm committed Dec 3, 2024
1 parent 6340aab commit 618bdb0
Showing 1 changed file with 75 additions and 3 deletions.
78 changes: 75 additions & 3 deletions native/backup/backup-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ import { getMessageForException } from 'lib/utils/errors.js';
import { useDispatchActionPromise } from 'lib/utils/redux-promise-utils.js';

import { useClientBackup } from './use-client-backup.js';
import { useGetBackupSecretForLoggedInUser } from './use-get-backup-secret.js';
import { commCoreModule } from '../native-modules.js';
import { useSelector } from '../redux/redux-utils.js';
import { useStaffCanSee } from '../utils/staff-utils.js';

const millisecondsPerDay = 24 * 60 * 60 * 1000;

function BackupHandler(): null {
const loggedIn = useSelector(isLoggedIn);
const staffCanSee = useStaffCanSee();
Expand All @@ -27,7 +30,9 @@ function BackupHandler(): null {
state => state.backupStore.latestBackupInfo,
);
const dispatchActionPromise = useDispatchActionPromise();
const { createUserKeysBackup } = useClientBackup();
const { createUserKeysBackup, retrieveLatestBackupInfo, getBackupUserKeys } =
useClientBackup();
const getBackupSecret = useGetBackupSecretForLoggedInUser();
const backupUploadInProgress = React.useRef<boolean>(false);
const [handlerStarted, setHandlerStarted] = React.useState(false);

Expand Down Expand Up @@ -72,13 +77,62 @@ function BackupHandler(): null {
staffCanSee,
]);

const testUserKeysRestore = React.useCallback(async () => {
if (!latestBackupInfo?.backupID) {
return;
}
const {
latestBackupInfo: { backupID },
userIdentifier,
} = await retrieveLatestBackupInfo();

const backupSecret = await getBackupSecret();
const [
{
backupDataKey: backupDataKeyFromBackup,
backupLogDataKey: backupLogDataKeyFromBackup,
},
{
backupID: localBackupID,
backupDataKey: localBackupDataKey,
backupLogDataKey: localBackupLogDataKey,
},
] = await Promise.all([
getBackupUserKeys(userIdentifier, backupSecret, backupID),
commCoreModule.getQRAuthBackupData(),
]);

const backupIDCheck =
latestBackupInfo.backupID === backupID && backupID === localBackupID;
const keysCheck =
backupDataKeyFromBackup === localBackupDataKey &&
backupLogDataKeyFromBackup === localBackupLogDataKey;

if (!backupIDCheck || !keysCheck) {
throw new Error(
'\n' +
`backupID: ${backupID}\n` +
`latestBackupInfo.backupID: ${latestBackupInfo.backupID}\n` +
`localBackupID: ${localBackupID}\n` +
`backupDataKeyFromBackup: ${backupDataKeyFromBackup}\n` +
`backupLogDataKeyFromBackup: ${backupLogDataKeyFromBackup}\n` +
`localBackupDataKey: ${localBackupDataKey}\n` +
`localBackupLogDataKey: ${localBackupLogDataKey}\n`,
);
}
}, [
getBackupSecret,
getBackupUserKeys,
latestBackupInfo?.backupID,
retrieveLatestBackupInfo,
]);

React.useEffect(() => {
if (
!staffCanSee ||
!canPerformBackupOperation ||
!handlerStarted ||
backupUploadInProgress.current ||
!!latestBackupInfo
backupUploadInProgress.current
) {
return;
}
Expand All @@ -91,6 +145,23 @@ function BackupHandler(): null {
return;
}

if (latestBackupInfo) {
const timestamp = latestBackupInfo.timestamp;
// If last upload one less than 24h ago ignore it
if (timestamp >= Date.now() - millisecondsPerDay) {
backupUploadInProgress.current = false;
return;
}

try {
await testUserKeysRestore();
} catch (err) {
const message = getMessageForException(err) ?? 'unknown error';
showAlertToStaff('Error restoring User Keys backup', message);
console.log('Error restoring User Keys backup:', message);
}
}

try {
const promise = (async () => {
const backupID = await createUserKeysBackup();
Expand All @@ -117,6 +188,7 @@ function BackupHandler(): null {
latestBackupInfo,
showAlertToStaff,
staffCanSee,
testUserKeysRestore,
]);

return null;
Expand Down

0 comments on commit 618bdb0

Please sign in to comment.