Skip to content

Commit

Permalink
Merge pull request #517 from internxt/fix/2-1-0-release
Browse files Browse the repository at this point in the history
Bug Resolution
  • Loading branch information
ArceDanielShok authored Sep 30, 2024
2 parents 706f357 + 310b92b commit d21008f
Show file tree
Hide file tree
Showing 17 changed files with 366 additions and 74 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "internxt-drive",
"version": "2.1.2",
"version": "2.1.3",
"author": "Internxt <[email protected]>",
"description": "Internxt Drive client UI",
"license": "AGPL-3.0",
Expand Down
2 changes: 1 addition & 1 deletion release/app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "internxt-drive",
"version": "2.1.2",
"version": "2.1.3",
"description": "Internxt Drive client UI",
"main": "./dist/main/main.js",
"author": "Internxt <[email protected]>",
Expand Down
2 changes: 1 addition & 1 deletion src/apps/main/auth/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ ipcMain.on('user-logged-in', async (_, data: AccessResponse) => {
if (!canHisConfigBeRestored(data.user.uuid)) {
await setupRootFolder();
}
await await clearRootVirtualDrive();
await clearRootVirtualDrive();

setIsLoggedIn(true);
eventBus.emit('USER_LOGGED_IN');
Expand Down
14 changes: 11 additions & 3 deletions src/apps/main/background-processes/sync-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import eventBus from '../event-bus';
import nodeSchedule from 'node-schedule';
import * as Sentry from '@sentry/electron/main';
import { checkSyncEngineInProcess } from '../remote-sync/handlers';
import { clearRootVirtualDrive } from '../virtual-root-folder/service';

let worker: BrowserWindow | null = null;
let workerIsRunning = false;
let startingWorker = false;
let healthCheckSchedule: nodeSchedule.Job | null = null;
let syncSchedule: nodeSchedule.Job | null = null;
let attemptsAlreadyStarting = 0;

ipcMain.once('SYNC_ENGINE_PROCESS_SETUP_SUCCESSFUL', () => {
Expand Down Expand Up @@ -82,6 +82,15 @@ function scheduleHeathCheck() {
}
});
}
function scheduleSync() {
if (syncSchedule) {
syncSchedule.cancel(false);
}

syncSchedule = nodeSchedule.scheduleJob('0 0 */2 * * *', async () => {
eventBus.emit('RECEIVED_REMOTE_CHANGES');
});
}

function spawnSyncEngineWorker() {
if (startingWorker) {
Expand Down Expand Up @@ -113,6 +122,7 @@ function spawnSyncEngineWorker() {
.then(() => {
Logger.info('[MAIN] Sync engine worker loaded');
scheduleHeathCheck();
scheduleSync();
})
.catch((err) => {
Logger.error('[MAIN] Error loading sync engine worker', err);
Expand Down Expand Up @@ -189,7 +199,6 @@ async function stopAndClearSyncEngineWatcher() {
Logger.info('[MAIN] WORKER WAS NOT RUNNING');
worker?.destroy();
worker = null;
await clearRootVirtualDrive();

return;
}
Expand Down Expand Up @@ -232,7 +241,6 @@ async function stopAndClearSyncEngineWatcher() {
worker?.destroy();
workerIsRunning = false;
worker = null;
await clearRootVirtualDrive();
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/apps/main/database/adapters/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,12 @@ export interface DatabaseCollectionAdapter<DatabaseItemType> {
success: boolean;
result: DatabaseItemType | null;
}>;

/**
* Gets items from partial data
*/

searchPartialBy(
partialData: Partial<DatabaseItemType>
): Promise<{ success: boolean; result: DatabaseItemType[] }>;
}
22 changes: 22 additions & 0 deletions src/apps/main/database/collections/DriveFileCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,26 @@ export class DriveFilesCollection
};
}
}

async searchPartialBy(
partialData: Partial<DriveFile>
): Promise<{ success: boolean; result: DriveFile[] }> {
try {
Logger.info('Searching partial by', partialData);
const result = await this.repository.find({
where: partialData,
});
return {
success: true,
result,
};
} catch (error) {
Sentry.captureException(error);
Logger.error('Error fetching drive folders:', error);
return {
success: false,
result: [],
};
}
}
}
21 changes: 21 additions & 0 deletions src/apps/main/database/collections/DriveFolderCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,25 @@ export class DriveFoldersCollection
};
}
}

async searchPartialBy(
partialData: Partial<DriveFolder>
): Promise<{ success: boolean; result: DriveFolder[] }> {
try {
const result = await this.repository.find({
where: partialData,
});
return {
success: true,
result,
};
} catch (error) {
Sentry.captureException(error);
Logger.error('Error fetching drive folders:', error);
return {
success: false,
result: [],
};
}
}
}
7 changes: 3 additions & 4 deletions src/apps/main/remote-sync/RemoteSyncManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import {
RemoteSyncedFolder,
RemoteSyncedFile,
SyncConfig,
FIVETEEN_MINUTES_IN_MILLISECONDS,
rewind,
WAITING_AFTER_SYNCING_DEFAULT,
FIVETEEN_MINUTES_IN_MILLISECONDS,
} from './helpers';
import { reportError } from '../bug-report/service';

Expand Down Expand Up @@ -128,7 +128,6 @@ export class RemoteSyncManager {
await this.db.folders.connect();

Logger.info('Starting RemoteSyncManager');
this.changeStatus('SYNCING');
try {
const syncOptions = {
retry: 1,
Expand All @@ -143,11 +142,11 @@ export class RemoteSyncManager {
? this.syncRemoteFoldersByFolder(syncOptions, folderId)
: this.syncRemoteFolders(syncOptions);

const [_files, folders] = await Promise.all([
const [files, folders] = await Promise.all([
await syncFilesPromise,
await syncFoldersPromise,
]);
return { files: _files, folders };
return { files, folders };
} catch (error) {
this.changeStatus('SYNC_FAILED');
reportError(error as Error);
Expand Down
117 changes: 112 additions & 5 deletions src/apps/main/remote-sync/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
import { debounce } from 'lodash';
import configStore from '../config';
import { setTrayStatus } from '../tray/tray';
import { FilePlaceholderId } from '../../../context/virtual-drive/files/domain/PlaceholderId';
import { FolderPlaceholderId } from '../../../context/virtual-drive/folders/domain/FolderPlaceholderId';

const SYNC_DEBOUNCE_DELAY = 500;

Expand Down Expand Up @@ -146,16 +148,17 @@ export async function updateRemoteSync(): Promise<void> {
// that we received the notification, but if we check
// for new data we don't receive it
Logger.info('Updating remote sync');
const isSyncing = await checkSyncEngineInProcess(2_000);
if (isSyncing) {
Logger.info('Remote sync is already running');
return;
}

const userData = configStore.get('userData');
const lastFilesSyncAt = await remoteSyncManager.getFileCheckpoint();
Logger.info('Last files sync at', lastFilesSyncAt);
const folderId = lastFilesSyncAt ? undefined : userData?.root_folder_id;
await startRemoteSync(folderId);
const isSyncing = await checkSyncEngineInProcess(2_000);
if (isSyncing) {
Logger.info('Remote sync is already running');
return;
}
updateSyncEngine();
}
export async function fallbackRemoteSync(): Promise<void> {
Expand Down Expand Up @@ -242,3 +245,107 @@ export async function checkSyncInProgress(milliSeconds: number) {
ipcMain.handle('CHECK_SYNC_IN_PROGRESS', async (_, milliSeconds: number) => {
return await checkSyncInProgress(milliSeconds);
});
// ipcMain.handle(
// 'DELETE_ITEM_DRIVE',
// async (_, itemId: FilePlaceholderId | FolderPlaceholderId) => {
// try {
// const [type, id] = itemId
// .replace(
// // eslint-disable-next-line no-control-regex
// /[\x00-\x1F\x7F-\x9F]/g,
// ''
// )
// .normalize()
// .split(':');

// Logger.info('Deleting item in handler', itemId);
// Logger.info('Type and id', type, id);

// const isFolder = type === 'FOLDER';
// let result;
// if (isFolder) {
// result = await driveFoldersCollection.update(id, { status: 'TRASHED' });
// } else {
// const item = await driveFilesCollection.searchPartialBy({
// fileId: id,
// });
// Logger.info('Item to delete', item);
// if (!item.result.length) return false;
// result = await driveFilesCollection.update(item.result[0].uuid, {
// status: 'TRASHED',
// });
// }

// Logger.info('Result deleting item in handler', result);
// return true;
// } catch (error) {
// Logger.error('Error deleting item in handler', error);
// return false;
// }
// }
// );

function parseItemId(itemId: string) {
const [type, id] = itemId
.replace(
// eslint-disable-next-line no-control-regex
/[\x00-\x1F\x7F-\x9F]/g,
''
)
.normalize()
.split(':');
if (!type || !id) {
throw new Error(`Invalid itemId format: ${itemId}`);
}
return { type, id };
}

async function deleteFolder(folderId: string): Promise<boolean> {
try {
const result = await driveFoldersCollection.update(folderId, {
status: 'TRASHED',
});
return result.success;
} catch (error) {
Logger.error('Error deleting folder', { folderId, error });
throw error;
}
}

async function deleteFile(fileId: string): Promise<boolean> {
try {
const item = await driveFilesCollection.searchPartialBy({ fileId });
if (!item.result.length) {
Logger.warn('File not found', { fileId });
return false;
}
const result = await driveFilesCollection.update(item.result[0].uuid, {
status: 'TRASHED',
});
return result.success;
} catch (error) {
Logger.error('Error deleting file', { fileId, error });
throw error;
}
}

ipcMain.handle(
'DELETE_ITEM_DRIVE',
async (
_,
itemId: FilePlaceholderId | FolderPlaceholderId
): Promise<boolean> => {
try {
const { type, id } = parseItemId(itemId);
Logger.info('Deleting item in handler', { type, id });

const isFolder = type === 'FOLDER';
const result = isFolder ? await deleteFolder(id) : await deleteFile(id);

return result;
} catch (error) {
Logger.error('Error deleting item in handler', { error });
return false;
}
}
);
6 changes: 3 additions & 3 deletions src/apps/renderer/pages/Widget/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ export default function Widget() {
const [isLogoutModalOpen, setIsLogoutModalOpen] = useState<boolean>(false);

const handleRetrySync = () => {
// window.electron.startRemoteSync().catch((err) => {
// reportError(err);
// });
window.electron.startRemoteSync().catch((err) => {
reportError(err);
});
};

const displayErrorInWidget = syncStatus && syncStatus === 'FAILED';
Expand Down
Loading

0 comments on commit d21008f

Please sign in to comment.