Skip to content

Commit

Permalink
fix: Do not try to load deleted users on app load [FS-1607] (#14794)
Browse files Browse the repository at this point in the history
  • Loading branch information
atomrc authored Mar 7, 2023
1 parent 2ae0f84 commit e35708b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/script/main/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ export class App {

await conversationRepository.conversationRoleRepository.loadTeamRoles();

await userRepository.loadUsers();
await userRepository.loadTeamUserAvailabilities();

await eventRepository.connectWebSocket(this.core, ({done, total}) => {
const baseMessage = t('initDecryption');
Expand Down
38 changes: 24 additions & 14 deletions src/script/user/UserRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,22 +161,32 @@ export class UserRepository {
}
};

async loadUsers(): Promise<void> {
if (this.userState.isTeam()) {
const users = await this.userService.loadUserFromDb();

if (users.length) {
this.logger.log(`Loaded state of '${users.length}' users from database`, users);
/**
* Will load the availability status to the team users and subscribe to changes.
*/
async loadTeamUserAvailabilities(): Promise<void> {
const users = this.userState.users();
if (this.userState.isTeam() && users.length) {
const availabilities = await this.userService.loadUserFromDb();

this.logger.log(`Loaded state of '${users.length}' users from database`, users);
/** availabilities we have in the DB that are not matching any loaded users */
const orphanAvailabilities = availabilities.filter(
availability => !users.find(user => matchQualifiedIds(user.qualifiedId, availability)),
);

await Promise.all(
users.map(async user => {
const userEntity = await this.getUserById({domain: user.domain, id: user.id});
userEntity.availability(user.availability);
}),
);
}
// Remove availabilities that are not linked to any loaded users
orphanAvailabilities.forEach(async availability => {
await this.userService.removeUserFromDb({id: availability.id, domain: availability.domain ?? ''});
});

this.userState.users().forEach(userEntity => userEntity.subscribeToChanges());
users.forEach(user => {
const userAvailability = availabilities.find(availability => matchQualifiedIds(availability, user.qualifiedId));
if (userAvailability) {
user.availability(userAvailability.availability);
}
user.subscribeToChanges();
});
}
}

Expand Down
16 changes: 14 additions & 2 deletions src/script/user/UserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ import {StorageSchemata} from '../storage/StorageSchemata';
import {StorageService} from '../storage/StorageService';
import {constructUserPrimaryKey} from '../util/StorageUtil';

type StoredUser = {
availability: number;
id: string;
domain?: string;
};

export class UserService {
private readonly logger: Logger;
private readonly USER_STORE_NAME: string;
Expand All @@ -49,8 +55,14 @@ export class UserService {
* @todo There might be more keys which are returned by this function
* @returns Resolves with all the stored user states
*/
loadUserFromDb(): Promise<{availability: number; domain?: string; id: string}[]> {
return this.storageService.getAll(this.USER_STORE_NAME);
async loadUserFromDb(): Promise<{availability: number; domain: string; id: string}[]> {
const users = await this.storageService.getAll<StoredUser>(this.USER_STORE_NAME);
return users.map(user => ({...user, domain: user.domain ?? ''}));
}

async removeUserFromDb(user: {id: string; domain: string}): Promise<void> {
const primaryKey = constructUserPrimaryKey(user);
await this.storageService.delete(this.USER_STORE_NAME, primaryKey);
}

/**
Expand Down

0 comments on commit e35708b

Please sign in to comment.