Skip to content

Commit

Permalink
refactor: refresh method returning updated protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrykBuniX committed Jul 26, 2023
1 parent b734549 commit a6a74c2
Showing 1 changed file with 27 additions and 23 deletions.
50 changes: 27 additions & 23 deletions src/script/self/SelfRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {TIME_IN_MILLIS} from 'Util/TimeUtil';
import {SelfService} from './SelfService';

import {ClientRepository} from '../client';
import {User} from '../entity/User';
import {isMLSSupportedByEnvironment} from '../mls/isMLSSupportedByEnvironment';
import {MLSMigrationStatus} from '../mls/MLSMigration/migrationStatus';
import {TeamRepository} from '../team/TeamRepository';
Expand Down Expand Up @@ -137,43 +136,41 @@ export class SelfRepository {
* It will send a request to the backend to change the supported protocols and then update the user in the local state.
* @param supportedProtocols - an array of new supported protocols
*/
private async updateSelfSupportedProtocols(supportedProtocols: ConversationProtocol[]): Promise<User> {
private async updateSelfSupportedProtocols(
supportedProtocols: ConversationProtocol[],
): Promise<ConversationProtocol[]> {
this.logger.info('Supported protocols will get updated to:', supportedProtocols);
await this.selfService.putSupportedProtocols(supportedProtocols);
return this.userRepository.updateUserSupportedProtocols(this.userState.self().qualifiedId, supportedProtocols);
await this.userRepository.updateUserSupportedProtocols(this.userState.self().qualifiedId, supportedProtocols);
return supportedProtocols;
}

/**
* Will re-evaluate self supported protocols and update them if necessary.
* It will send a request to the backend to change the supported protocols and then update the user in the local state.
* @param supportedProtocols - an array of new supported protocols
*/
public async refreshSelfSupportedProtocols(): Promise<void> {
public async refreshSelfSupportedProtocols(): Promise<ConversationProtocol[]> {
const selfUser = this.userState.self();
const localSupportedProtocols = selfUser.supportedProtocols();

this.logger.info('Evaluating self supported protocols, currently supported protocols:', localSupportedProtocols);
const refreshedSupportedProtocols = await this.evaluateSelfSupportedProtocols();

try {
const refreshedSupportedProtocols = await this.evaluateSelfSupportedProtocols();

if (!localSupportedProtocols) {
return void this.updateSelfSupportedProtocols(refreshedSupportedProtocols);
}

const hasSupportedProtocolsChanged = !(
localSupportedProtocols.length === refreshedSupportedProtocols.length &&
[...localSupportedProtocols].every(protocol => refreshedSupportedProtocols.includes(protocol))
);
if (!localSupportedProtocols) {
return this.updateSelfSupportedProtocols(refreshedSupportedProtocols);
}

if (!hasSupportedProtocolsChanged) {
return;
}
const hasSupportedProtocolsChanged = !(
localSupportedProtocols.length === refreshedSupportedProtocols.length &&
[...localSupportedProtocols].every(protocol => refreshedSupportedProtocols.includes(protocol))
);

return void this.updateSelfSupportedProtocols(refreshedSupportedProtocols);
} catch (error) {
this.logger.error('Failed to update self supported protocols, will retry after 24h. Error: ', error);
if (!hasSupportedProtocolsChanged) {
return localSupportedProtocols;
}

return this.updateSelfSupportedProtocols(refreshedSupportedProtocols);
}

/**
Expand All @@ -186,11 +183,18 @@ export class SelfRepository {
*/
public async initialisePeriodicSelfSupportedProtocolsCheck() {
// We update supported protocols of self user on initial app load and then in 24 hours intervals
await this.refreshSelfSupportedProtocols();
const refreshProtocolsTask = async () => {
await this.refreshSelfSupportedProtocols();
try {
} catch (error) {
this.logger.error('Failed to update self supported protocols, will retry after 24h. Error: ', error);
}
};
await refreshProtocolsTask();

return registerRecurringTask({
every: TIME_IN_MILLIS.DAY,
task: () => this.refreshSelfSupportedProtocols(),
task: refreshProtocolsTask,
key: SELF_SUPPORTED_PROTOCOLS_CHECK_KEY,
});
}
Expand Down

0 comments on commit a6a74c2

Please sign in to comment.