Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refactor] Get room federation options from config #59

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion config.prod.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@
{
"base_url": "https://matrix.agent.intradef.tchap.gouv.fr",
"server_name": "Intradef",
"email_examples": "? ..."
"email_examples": "? ...",
"show_room_federation_option": true,
"room_federation_default": false
},
{
"base_url": "https://matrix.agent.justice.tchap.gouv.fr",
Expand Down
37 changes: 30 additions & 7 deletions src/util/TchapUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { MatrixClientPeg } from 'matrix-react-sdk/src/MatrixClientPeg';
import SdkConfig from 'matrix-react-sdk/src/SdkConfig';

/**
* Tchap utils.
Expand All @@ -13,10 +14,11 @@ export default class TchapUtils {
const cli = MatrixClientPeg.get();
const baseDomain = cli.getDomain();
const domain = baseDomain.split('.tchap.gouv.fr')[0].split('.').reverse().filter(Boolean)[0];

// todo doesn't work for preprod
return this.capitalize(domain) || 'Tchap';
}

static DEFAULT_ROOM_FEDERATION_OPTIONS = { showRoomFederationOption: false, roomFederationDefault: true };
/**
* For the current user, get the room federation options.
*
Expand All @@ -26,13 +28,34 @@ export default class TchapUtils {
const cli = MatrixClientPeg.get();
const baseDomain = cli.getDomain();

// Only show the federate switch to defense users : it's difficult to understand, so we avoid
// displaying it unless it's really necessary.
if (baseDomain === 'agent.intradef.tchap.gouv.fr') {
return { showRoomFederationOption: true, roomFederationDefault: false };
}
const findRoomFederationOptionsInConfig = baseDomain => { // todo untested !
const homeServerList = SdkConfig.get()['homeserver_list'];

const homeServerConfig = homeServerList.find(homeServer => {
return homeServer.base_url.split('https://matrix.')[1] === baseDomain;
});

if (!homeServerConfig) {
return undefined;
}

return { showRoomFederationOption: false, roomFederationDefault: true };
return {
showRoomFederationOption: homeServerConfig.show_room_federation_option,
roomFederationDefault: homeServerConfig.room_federation_default,
};
};

const options = findRoomFederationOptionsInConfig(baseDomain);
if (!options) {
return this.DEFAULT_ROOM_FEDERATION_OPTIONS;
}
if (!options.showRoomFederationOption) {
options.showRoomFederationOption = this.DEFAULT_ROOM_FEDERATION_OPTIONS.showRoomFederationOption;
}
if (!options.roomFederationDefault) {
options.roomFederationDefault = this.DEFAULT_ROOM_FEDERATION_OPTIONS.roomFederationDefault;
}
return options;
}

/**
Expand Down