Skip to content

Commit

Permalink
Implemented the changes for ticket PB-2692. Integrated the new endpoi…
Browse files Browse the repository at this point in the history
…nt to retrieve folder metadata during creation. This update ensures that when a folder is created, the correct metadata is fetched and stored, addressing the issues related to folder persistence and synchronization.
  • Loading branch information
ArceDanielShok committed Sep 4, 2024
1 parent 67cb45e commit 3dfd35f
Show file tree
Hide file tree
Showing 22 changed files with 157 additions and 36 deletions.
4 changes: 2 additions & 2 deletions src/apps/main/auth/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { applicationOpened } from '../analytics/service';
import eventBus from '../event-bus';
import { setupRootFolder } from '../virtual-root-folder/service';
import { getWidget } from '../windows/widget';
import { createTokenSchedule } from './refresh-token';
import { checkUserData, createTokenSchedule } from './refresh-token';
import {
canHisConfigBeRestored,
encryptToken,
Expand Down Expand Up @@ -79,7 +79,7 @@ eventBus.on('APP_IS_READY', async () => {
if (!isLoggedIn) {
return;
}

await checkUserData();
encryptToken();
applicationOpened();
await createTokenSchedule();
Expand Down
39 changes: 38 additions & 1 deletion src/apps/main/auth/refresh-token.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import Logger from 'electron-log';

import { getClient } from '../../shared/HttpClient/main-process-client';
import {
getClient,
getNewTokenClient,
} from '../../shared/HttpClient/main-process-client';
import { TokenScheduler } from '../token-scheduler/TokenScheduler';
import { onUserUnauthorized } from './handlers';
import {
getUser,
obtainTokens as obtainStoredTokens,
setUser,
updateCredentials,
} from './service';
import axios from 'axios';

const authorizedClient = getClient();
const newAuthorizedClient = getNewTokenClient();

async function obtainTokens() {
try {
Expand Down Expand Up @@ -48,3 +55,33 @@ export async function createTokenSchedule(refreshedTokens?: Array<string>) {
createTokenSchedule(await refreshToken());
}
}

async function getRootFolderMetadata(rootFolderid: number) {
try {
const res = await newAuthorizedClient.get(
`${process.env.NEW_DRIVE_URL}/drive/folders/${rootFolderid}/metadata`
);

Logger.info('[AUTH] Got root folder metadata', res.data);
return res.data;
} catch (err) {
Logger.error('[AUTH] Could not get root folder metadata', err);
if (axios.isAxiosError(err)) {
Logger.error('[Is Axios Error]', err.response?.data);
}
return null;
}
}

export async function checkUserData(): Promise<void> {
const user = getUser();
if (user?.root_folder_id && !user?.rootFolderId) {
const rootFolderMetadata = await getRootFolderMetadata(user.root_folder_id);
if (rootFolderMetadata) {
setUser({
...user,
rootFolderId: rootFolderMetadata.uuid,
});
}
}
}
7 changes: 4 additions & 3 deletions src/apps/main/auth/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ export function setCredentials(
ConfigStore.set('newTokenEncrypted', isSafeStorageAvailable);
}

export function setUser(userData: User) {
ConfigStore.set('userData', userData);
}

export function updateCredentials(
bearerToken: string,
newBearerToken?: string
Expand Down Expand Up @@ -146,7 +150,6 @@ export function getUser(): User | null {
return user && Object.keys(user).length ? user : null;
}


export function obtainTokens(): Array<string> {
return tokensKeys.map(obtainToken);
}
Expand Down Expand Up @@ -220,5 +223,3 @@ export function logout() {
resetCredentials();
Logger.info('[AUTH] User logged out');
}


7 changes: 7 additions & 0 deletions src/apps/main/preload.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ declare interface Window {

pathChanged(path: string): void;

logger: {
info: (...message: unknown[]) => void;
error: (...message: unknown[]) => void;
warn: (...message: unknown[]) => void;
debug: (...message: unknown[]) => void;
};

getGeneralIssues: () => Promise<
import('../../apps/shared/types').GeneralIssue[]
>;
Expand Down
7 changes: 7 additions & 0 deletions src/apps/main/preload.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { contextBridge, ipcRenderer } = require('electron');
const path = require('path');
const Logger = require('electron-log');

contextBridge.exposeInMainWorld('electron', {
getConfigKey(key) {
Expand All @@ -16,6 +17,12 @@ contextBridge.exposeInMainWorld('electron', {
return () => ipcRenderer.removeListener(eventName, callback);
},

logger: {
info: (...message) => Logger.info(message),
error: (...message) => Logger.error(message),
warn: (...message) => Logger.warn(message),
},

pathChanged(pathname) {
ipcRenderer.send('path-changed', pathname);
},
Expand Down
4 changes: 2 additions & 2 deletions src/apps/main/remote-sync/RemoteSyncManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ export class RemoteSyncManager {
);
Logger.info(
`Fetching item ${type} response: ${JSON.stringify(
response.data[0],
response.data?.length,
null,
2
)}`
Expand All @@ -700,7 +700,7 @@ export class RemoteSyncManager {
);
Logger.info(
`Fetching by folder ${type} by folder response: ${JSON.stringify(
response.data.result[0],
response.data.result?.length,
null,
2
)}`
Expand Down
1 change: 1 addition & 0 deletions src/apps/main/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type User = {
registerCompleted: boolean;
revocateKey: string;
root_folder_id: number;
rootFolderId: string;
sharedWorkspace: boolean;
teams: boolean;
userId: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export class DependencyInjectionTraverserProvider {
const traverser = Traverser.existingItems(
crypt,
ipcRendererSyncEngine,
user.root_folder_id
user.root_folder_id,
user.rootFolderId
);

DependencyInjectionTraverserProvider.traverser = traverser;
Expand Down
14 changes: 7 additions & 7 deletions src/apps/sync-engine/dependency-injection/folders/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { RetrieveAllFolders } from '../../../../context/virtual-drive/folders/ap
import { SynchronizeOfflineModifications } from '../../../../context/virtual-drive/folders/application/SynchronizeOfflineModifications';
import { SynchronizeOfflineModificationsOnFolderCreated } from '../../../../context/virtual-drive/folders/application/SynchronizeOfflineModificationsOnFolderCreated';
import { FolderPlaceholderUpdater } from '../../../../context/virtual-drive/folders/application/UpdatePlaceholderFolder';
import { HttpRemoteFileSystem } from '../../../../context/virtual-drive/folders/infrastructure/HttpRemoteFileSystem';
import { HttpRemoteFolderSystem } from '../../../../context/virtual-drive/folders/infrastructure/HttpRemoteFolderSystem';
import { InMemoryFolderRepository } from '../../../../context/virtual-drive/folders/infrastructure/InMemoryFolderRepository';
import { InMemoryOfflineFolderRepository } from '../../../../context/virtual-drive/folders/infrastructure/InMemoryOfflineFolderRepository';
import { ipcRendererSyncEngine } from '../../ipcRendererSyncEngine';
Expand Down Expand Up @@ -48,7 +48,7 @@ export async function buildFoldersContainer(
virtualDrive,
shredContainer.relativePathToAbsoluteConverter
);
const remoteFileSystem = new HttpRemoteFileSystem(
const remoteFolderSystem = new HttpRemoteFolderSystem(
clients.drive,
clients.newDrive
);
Expand All @@ -67,7 +67,7 @@ export async function buildFoldersContainer(

const folderDeleter = new FolderDeleter(
repository,
remoteFileSystem,
remoteFolderSystem,
localFileSystem,
allParentFoldersStatusIsExists
);
Expand All @@ -76,20 +76,20 @@ export async function buildFoldersContainer(

const folderCreator = new FolderCreator(
repository,
remoteFileSystem,
remoteFolderSystem,
ipcRendererSyncEngine,
eventBus,
folderPlaceholderConverter
);

const folderMover = new FolderMover(
repository,
remoteFileSystem,
remoteFolderSystem,
folderFinder
);
const folderRenamer = new FolderRenamer(
repository,
remoteFileSystem,
remoteFolderSystem,
ipcRendererSyncEngine
);

Expand Down Expand Up @@ -144,7 +144,7 @@ export async function buildFoldersContainer(

const folderPlaceholderDeleter = new FolderPlaceholderDeleter(
shredContainer.relativePathToAbsoluteConverter,
remoteFileSystem,
remoteFolderSystem,
localFileSystem
);

Expand Down
6 changes: 4 additions & 2 deletions src/apps/sync-engine/dependency-injection/items/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ export function buildItemsContainer(): ItemsContainer {
const existingItemsTraverser = Traverser.existingItems(
nameDecryptor,
ipcRendererSyncEngine,
user.root_folder_id
user.root_folder_id,
user.rootFolderId
);
const allStatusesTraverser = Traverser.allItems(
nameDecryptor,
ipcRendererSyncEngine,
user.root_folder_id
user.root_folder_id,
user.rootFolderId
);
const treeBuilder = new TreeBuilder(
remoteItemsGenerator,
Expand Down
1 change: 1 addition & 0 deletions src/context/shared/domain/ServerFolder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export type ServerFolder = {
plain_name: string | null;
status: ServerFolderStatus;
uuid: string;
removed: boolean;
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { EventBus } from '../../shared/domain/EventBus';
import { Folder } from '../domain/Folder';
import { FolderRepository } from '../domain/FolderRepository';
import { OfflineFolder } from '../domain/OfflineFolder';
import { RemoteFileSystem } from '../domain/file-systems/RemoteFileSystem';
import { RemoteFolderSystem } from '../domain/file-systems/RemoteFolderSystem';
import { FolderPlaceholderConverter } from './FolderPlaceholderConverter';

export class FolderCreator {
constructor(
private readonly repository: FolderRepository,
private readonly remote: RemoteFileSystem,
private readonly remote: RemoteFolderSystem,
private readonly ipc: SyncEngineIpc,
private readonly eventBus: EventBus,
private readonly folderPlaceholderConverter: FolderPlaceholderConverter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ActionNotPermittedError } from '../domain/errors/ActionNotPermittedErro
import { FolderNotFoundError } from '../domain/errors/FolderNotFoundError';
import { AllParentFoldersStatusIsExists } from './AllParentFoldersStatusIsExists';
import { FolderRepository } from '../domain/FolderRepository';
import { RemoteFileSystem } from '../domain/file-systems/RemoteFileSystem';
import { RemoteFileSystem } from '../domain/file-systems/RemoteFolderSystem';
import { LocalFolderSystem } from '../domain/file-systems/LocalFolderSystem';

export class FolderDeleter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FolderPath } from '../domain/FolderPath';
import { Folder } from '../domain/Folder';
import { FolderFinder } from './FolderFinder';
import { FolderRepository } from '../domain/FolderRepository';
import { RemoteFileSystem } from '../domain/file-systems/RemoteFileSystem';
import { RemoteFileSystem } from '../domain/file-systems/RemoteFolderSystem';

export class FolderMover {
constructor(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { RelativePathToAbsoluteConverter } from '../../shared/application/RelativePathToAbsoluteConverter';
import { Folder } from '../domain/Folder';
import { FolderStatuses } from '../domain/FolderStatus';
import { RemoteFileSystem } from '../domain/file-systems/RemoteFileSystem';
import { RemoteFileSystem } from '../domain/file-systems/RemoteFolderSystem';
import { LocalFolderSystem } from '../domain/file-systems/LocalFolderSystem';
import Logger from 'electron-log';
import { sleep } from '../../../../apps/main/util';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FolderPath } from '../domain/FolderPath';
import { Folder } from '../domain/Folder';
import { FolderRepository } from '../domain/FolderRepository';
import { RemoteFileSystem } from '../domain/file-systems/RemoteFileSystem';
import { RemoteFileSystem } from '../domain/file-systems/RemoteFolderSystem';
import { SyncEngineIpc } from '../../../../apps/sync-engine/ipcRendererSyncEngine';

export class FolderRenamer {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Logger from 'electron-log';
import { FolderPath } from '../../domain/FolderPath';
import { OfflineFolder } from '../../domain/OfflineFolder';
import { OfflineFolderRepository } from '../../domain/OfflineFolderRepository';
Expand Down Expand Up @@ -26,7 +27,11 @@ export class OfflineFolderCreator {

const parent = this.folderFinder.run(folderPath.dirname());

const folder = OfflineFolder.create(folderPath, parent.id);
Logger.debug('[DEBUG IN OFFLINEFOLDERCREATOR STEEP 1]');

Logger.debug(parent);

const folder = OfflineFolder.create(folderPath, parent.id, parent.uuid);

this.offlineRepository.update(folder);

Expand Down
19 changes: 18 additions & 1 deletion src/context/virtual-drive/folders/domain/OfflineFolder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { FolderRenamedDomainEvent } from './events/FolderRenamedDomainEvent';
export type OfflineFolderAttributes = {
uuid: string;
parentId: number;
parentUuid: string;
path: string;
updatedAt: string;
createdAt: string;
Expand All @@ -19,6 +20,7 @@ export class OfflineFolder extends AggregateRoot {
private _uuid: FolderUuid,
private _path: FolderPath,
private _parentId: number,
private _parentUuid: FolderUuid,
public createdAt: Date,
public updatedAt: Date,
private _status: FolderStatus
Expand Down Expand Up @@ -50,6 +52,10 @@ export class OfflineFolder extends AggregateRoot {
return this._parentId;
}

public get parentUuid() {
return this._parentUuid.value;
}

public get status() {
return this._status;
}
Expand All @@ -64,25 +70,35 @@ export class OfflineFolder extends AggregateRoot {
new FolderUuid(attributes.uuid),
new FolderPath(attributes.path),
attributes.parentId,
attributes.parentUuid
? new FolderUuid(attributes.parentUuid)
: FolderUuid.random(),
new Date(attributes.updatedAt),
new Date(attributes.createdAt),
FolderStatus.fromValue(attributes.status)
);
}

static create(path: FolderPath, parentId: number): OfflineFolder {
static create(
path: FolderPath,
parentId: number,
parentUuid: string
): OfflineFolder {
return new OfflineFolder(
FolderUuid.random(),
path,
parentId,
new FolderUuid(parentUuid),
new Date(),

new Date(),
FolderStatus.Exists
);
}

moveTo(destinationFolder: Folder) {
this._parentId = destinationFolder.id;
this._parentUuid = new FolderUuid(destinationFolder.uuid);
}

rename(destination: FolderPath) {
Expand Down Expand Up @@ -116,6 +132,7 @@ export class OfflineFolder extends AggregateRoot {
const attributes: OfflineFolderAttributes = {
uuid: this.uuid,
parentId: this._parentId,
parentUuid: this._parentUuid.value,
path: this._path.value,
updatedAt: this.updatedAt.toISOString(),
createdAt: this.createdAt.toISOString(),
Expand Down
Loading

0 comments on commit 3dfd35f

Please sign in to comment.