2019-09-16 20:26:32 +00:00
|
|
|
import { Q } from '@nozbe/watermelondb';
|
2022-02-18 14:49:47 +00:00
|
|
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
2018-05-18 17:55:08 +00:00
|
|
|
|
2022-04-07 16:53:07 +00:00
|
|
|
import { addSettings, clearSettings } from '../../actions/settings';
|
|
|
|
import { DEFAULT_AUTO_LOCK, defaultSettings } from '../constants';
|
|
|
|
import { IPreparedSettings, ISettingsIcon } from '../../definitions';
|
2022-06-06 14:17:51 +00:00
|
|
|
import fetch from './helpers/fetch';
|
|
|
|
import log from './helpers/log';
|
2022-04-07 16:53:07 +00:00
|
|
|
import { store as reduxStore } from '../store/auxStore';
|
|
|
|
import database from '../database';
|
2022-04-04 19:15:29 +00:00
|
|
|
import sdk from '../services/sdk';
|
2022-04-07 16:53:07 +00:00
|
|
|
import protectedFunction from './helpers/protectedFunction';
|
2022-04-28 20:37:25 +00:00
|
|
|
import { parseSettings, _prepareSettings } from './parseSettings';
|
2023-02-14 13:47:56 +00:00
|
|
|
import { setPresenceCap } from './getUsersPresence';
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2020-09-11 14:31:38 +00:00
|
|
|
const serverInfoKeys = [
|
|
|
|
'Site_Name',
|
|
|
|
'UI_Use_Real_Name',
|
|
|
|
'FileUpload_MediaTypeWhiteList',
|
|
|
|
'FileUpload_MaxFileSize',
|
|
|
|
'Force_Screen_Lock',
|
|
|
|
'Force_Screen_Lock_After',
|
|
|
|
'uniqueID',
|
2023-09-12 13:07:11 +00:00
|
|
|
'E2E_Enable',
|
|
|
|
'E2E_Enabled_Default_PrivateRooms'
|
2020-09-11 14:31:38 +00:00
|
|
|
];
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2020-04-09 05:26:28 +00:00
|
|
|
// these settings are used only on onboarding process
|
|
|
|
const loginSettings = [
|
|
|
|
'API_Gitlab_URL',
|
|
|
|
'CAS_enabled',
|
|
|
|
'CAS_login_url',
|
|
|
|
'Accounts_EmailVerification',
|
|
|
|
'Accounts_ManuallyApproveNewUsers',
|
|
|
|
'Accounts_ShowFormLogin',
|
|
|
|
'Site_Url',
|
|
|
|
'Accounts_RegistrationForm',
|
|
|
|
'Accounts_RegistrationForm_LinkReplacementText',
|
|
|
|
'Accounts_EmailOrUsernamePlaceholder',
|
|
|
|
'Accounts_PasswordPlaceholder',
|
2020-06-17 20:12:21 +00:00
|
|
|
'Accounts_PasswordReset',
|
|
|
|
'Accounts_iframe_enabled',
|
|
|
|
'Accounts_Iframe_api_url',
|
|
|
|
'Accounts_Iframe_api_method'
|
2020-04-09 05:26:28 +00:00
|
|
|
];
|
|
|
|
|
2022-02-18 14:49:47 +00:00
|
|
|
const serverInfoUpdate = async (serverInfo: IPreparedSettings[], iconSetting: ISettingsIcon) => {
|
2019-09-16 20:26:32 +00:00
|
|
|
const serversDB = database.servers;
|
|
|
|
const serverId = reduxStore.getState().server.server;
|
2021-02-26 16:25:51 +00:00
|
|
|
const serversCollection = serversDB.get('servers');
|
2020-05-08 17:04:37 +00:00
|
|
|
const server = await serversCollection.find(serverId);
|
2019-09-16 20:26:32 +00:00
|
|
|
|
|
|
|
let info = serverInfo.reduce((allSettings, setting) => {
|
|
|
|
if (setting._id === 'Site_Name') {
|
|
|
|
return { ...allSettings, name: setting.valueAsString };
|
|
|
|
}
|
|
|
|
if (setting._id === 'UI_Use_Real_Name') {
|
|
|
|
return { ...allSettings, useRealName: setting.valueAsBoolean };
|
|
|
|
}
|
|
|
|
if (setting._id === 'FileUpload_MediaTypeWhiteList') {
|
|
|
|
return { ...allSettings, FileUpload_MediaTypeWhiteList: setting.valueAsString };
|
|
|
|
}
|
|
|
|
if (setting._id === 'FileUpload_MaxFileSize') {
|
|
|
|
return { ...allSettings, FileUpload_MaxFileSize: setting.valueAsNumber };
|
|
|
|
}
|
2020-05-08 17:04:37 +00:00
|
|
|
if (setting._id === 'Force_Screen_Lock') {
|
|
|
|
// if this was disabled on server side we must keep this enabled on app
|
|
|
|
const autoLock = server.autoLock || setting.valueAsBoolean;
|
|
|
|
return { ...allSettings, autoLock };
|
|
|
|
}
|
|
|
|
if (setting._id === 'Force_Screen_Lock_After') {
|
2020-05-22 18:04:43 +00:00
|
|
|
const forceScreenLock = serverInfo.find(s => s._id === 'Force_Screen_Lock')?.valueAsBoolean;
|
|
|
|
|
2020-05-08 17:04:37 +00:00
|
|
|
// if Force_Screen_Lock_After === 0 and autoLockTime is null, set app's default value
|
|
|
|
if (setting.valueAsNumber === 0 && !server.autoLockTime) {
|
|
|
|
return { ...allSettings, autoLockTime: DEFAULT_AUTO_LOCK };
|
|
|
|
}
|
2020-05-22 18:04:43 +00:00
|
|
|
// if Force_Screen_Lock_After > 0 and forceScreenLock is enabled, use it
|
2022-02-18 14:49:47 +00:00
|
|
|
if (setting.valueAsNumber && setting.valueAsNumber > 0 && forceScreenLock) {
|
2020-05-08 17:04:37 +00:00
|
|
|
return { ...allSettings, autoLockTime: setting.valueAsNumber };
|
|
|
|
}
|
|
|
|
}
|
2020-07-30 17:25:52 +00:00
|
|
|
if (setting._id === 'uniqueID') {
|
|
|
|
return { ...allSettings, uniqueID: setting.valueAsString };
|
|
|
|
}
|
2020-09-11 14:31:38 +00:00
|
|
|
if (setting._id === 'E2E_Enable') {
|
|
|
|
return { ...allSettings, E2E_Enable: setting.valueAsBoolean };
|
|
|
|
}
|
2023-09-12 13:07:11 +00:00
|
|
|
if (setting._id === 'E2E_Enabled_Default_PrivateRooms') {
|
|
|
|
return { ...allSettings, E2E_Enabled_Default_PrivateRooms: setting.valueAsBoolean };
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
return allSettings;
|
|
|
|
}, {});
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
if (iconSetting) {
|
2021-09-13 20:41:05 +00:00
|
|
|
const iconURL = `${serverId}/${iconSetting.value.url || iconSetting.value.defaultUrl}`;
|
2019-09-16 20:26:32 +00:00
|
|
|
info = { ...info, iconURL };
|
|
|
|
}
|
|
|
|
|
2022-02-18 14:49:47 +00:00
|
|
|
await serversDB.write(async () => {
|
2019-02-25 16:23:17 +00:00
|
|
|
try {
|
2021-09-13 20:41:05 +00:00
|
|
|
await server.update(record => {
|
2019-09-16 20:26:32 +00:00
|
|
|
Object.assign(record, info);
|
|
|
|
});
|
2019-02-25 16:23:17 +00:00
|
|
|
} catch (e) {
|
2019-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2019-02-25 16:23:17 +00:00
|
|
|
}
|
2018-08-31 16:46:33 +00:00
|
|
|
});
|
2019-09-16 20:26:32 +00:00
|
|
|
};
|
2018-08-31 16:46:33 +00:00
|
|
|
|
2022-02-18 14:49:47 +00:00
|
|
|
export async function getLoginSettings({ server }: { server: string }): Promise<void> {
|
2020-03-03 20:53:48 +00:00
|
|
|
try {
|
2020-04-09 05:26:28 +00:00
|
|
|
const settingsParams = JSON.stringify(loginSettings);
|
2021-09-13 20:41:05 +00:00
|
|
|
const result = await fetch(`${server}/api/v1/settings.public?query={"_id":{"$in":${settingsParams}}}`).then(response =>
|
|
|
|
response.json()
|
|
|
|
);
|
2020-02-20 20:08:20 +00:00
|
|
|
|
2020-03-03 20:53:48 +00:00
|
|
|
if (result.success && result.settings.length) {
|
2020-04-01 19:24:42 +00:00
|
|
|
reduxStore.dispatch(clearSettings());
|
2022-04-07 16:53:07 +00:00
|
|
|
reduxStore.dispatch(addSettings(parseSettings(_prepareSettings(result.settings))));
|
2020-02-20 20:08:20 +00:00
|
|
|
}
|
2020-03-03 20:53:48 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
2020-02-20 20:08:20 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 14:49:47 +00:00
|
|
|
export async function setSettings(): Promise<void> {
|
2019-10-28 13:37:13 +00:00
|
|
|
const db = database.active;
|
2021-02-26 16:25:51 +00:00
|
|
|
const settingsCollection = db.get('settings');
|
2019-10-28 13:37:13 +00:00
|
|
|
const settingsRecords = await settingsCollection.query().fetch();
|
|
|
|
const parsed = Object.values(settingsRecords).map(item => ({
|
|
|
|
_id: item.id,
|
|
|
|
valueAsString: item.valueAsString,
|
|
|
|
valueAsBoolean: item.valueAsBoolean,
|
|
|
|
valueAsNumber: item.valueAsNumber,
|
2020-02-20 20:43:56 +00:00
|
|
|
valueAsArray: item.valueAsArray,
|
2019-10-28 13:37:13 +00:00
|
|
|
_updatedAt: item._updatedAt
|
|
|
|
}));
|
2022-04-07 16:53:07 +00:00
|
|
|
reduxStore.dispatch(addSettings(parseSettings(parsed.slice(0, parsed.length))));
|
2019-10-28 13:37:13 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 14:49:47 +00:00
|
|
|
export function subscribeSettings(): void {
|
2022-04-04 19:15:29 +00:00
|
|
|
return sdk.subscribe('stream-notify-all', 'public-settings-changed');
|
2021-07-06 16:22:38 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 14:49:47 +00:00
|
|
|
type IData = ISettingsIcon | IPreparedSettings;
|
|
|
|
|
2022-04-04 19:15:29 +00:00
|
|
|
export async function getSettings(): Promise<void> {
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
2019-09-16 20:26:32 +00:00
|
|
|
const db = database.active;
|
2022-04-07 14:10:03 +00:00
|
|
|
const settingsParams = Object.keys(defaultSettings).filter(key => !loginSettings.includes(key));
|
2018-12-21 10:55:35 +00:00
|
|
|
// RC 0.60.0
|
2021-09-13 20:41:05 +00:00
|
|
|
const result = await fetch(
|
2022-02-18 14:49:47 +00:00
|
|
|
`${sdk.current.client.host}/api/v1/settings.public?query={"_id":{"$in":${JSON.stringify(settingsParams)}}}&count=${
|
2021-09-13 20:41:05 +00:00
|
|
|
settingsParams.length
|
|
|
|
}`
|
|
|
|
).then(response => response.json());
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
if (!result.success) {
|
|
|
|
return;
|
|
|
|
}
|
2022-02-18 14:49:47 +00:00
|
|
|
const data: IData[] = result.settings || [];
|
2022-04-07 16:53:07 +00:00
|
|
|
const filteredSettings: IPreparedSettings[] = _prepareSettings(data);
|
2019-09-16 20:26:32 +00:00
|
|
|
const filteredSettingsIds = filteredSettings.map(s => s._id);
|
2023-02-14 13:47:56 +00:00
|
|
|
const parsedSettings = parseSettings(filteredSettings);
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2023-02-14 13:47:56 +00:00
|
|
|
reduxStore.dispatch(addSettings(parsedSettings));
|
|
|
|
|
|
|
|
setPresenceCap(parsedSettings.Presence_broadcast_disabled);
|
2020-07-09 13:33:04 +00:00
|
|
|
|
|
|
|
// filter server info
|
|
|
|
const serverInfo = filteredSettings.filter(i1 => serverInfoKeys.includes(i1._id));
|
2022-02-18 14:49:47 +00:00
|
|
|
const iconSetting = data.find(icon => icon._id === 'Assets_favicon_512');
|
2020-09-23 17:16:04 +00:00
|
|
|
try {
|
2022-02-18 14:49:47 +00:00
|
|
|
await serverInfoUpdate(serverInfo, iconSetting as ISettingsIcon);
|
2020-09-23 17:16:04 +00:00
|
|
|
} catch {
|
|
|
|
// Server not found
|
|
|
|
}
|
2020-07-09 13:33:04 +00:00
|
|
|
|
2022-02-18 14:49:47 +00:00
|
|
|
await db.write(async () => {
|
2021-02-26 16:25:51 +00:00
|
|
|
const settingsCollection = db.get('settings');
|
2021-09-13 20:41:05 +00:00
|
|
|
const allSettingsRecords = await settingsCollection.query(Q.where('id', Q.oneOf(filteredSettingsIds))).fetch();
|
2020-07-09 13:33:04 +00:00
|
|
|
|
|
|
|
// filter settings
|
2022-02-18 14:49:47 +00:00
|
|
|
const settingsToCreate = filteredSettings.filter(i1 => !allSettingsRecords.find(i2 => i1._id === i2.id));
|
|
|
|
const settingsToUpdate = allSettingsRecords.filter(i1 => filteredSettings.find(i2 => i1.id === i2._id));
|
2020-07-09 13:33:04 +00:00
|
|
|
// Create
|
2022-02-18 14:49:47 +00:00
|
|
|
const settingsToCreateMapped = settingsToCreate.map(setting =>
|
2021-09-13 20:41:05 +00:00
|
|
|
settingsCollection.prepareCreate(
|
2022-02-18 14:49:47 +00:00
|
|
|
protectedFunction((s: any) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
s._raw = sanitizedRaw({ id: setting._id }, settingsCollection.schema);
|
|
|
|
Object.assign(s, setting);
|
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
2020-07-09 13:33:04 +00:00
|
|
|
|
|
|
|
// Update
|
2022-02-18 14:49:47 +00:00
|
|
|
const settingsToUpdateMapped = settingsToUpdate.map(setting => {
|
2020-07-09 13:33:04 +00:00
|
|
|
const newSetting = filteredSettings.find(s => s._id === setting.id);
|
2021-09-13 20:41:05 +00:00
|
|
|
return setting.prepareUpdate(
|
2022-02-18 14:49:47 +00:00
|
|
|
protectedFunction((s: any) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
Object.assign(s, newSetting);
|
|
|
|
})
|
|
|
|
);
|
2019-09-16 20:26:32 +00:00
|
|
|
});
|
2020-07-09 13:33:04 +00:00
|
|
|
|
2022-02-18 14:49:47 +00:00
|
|
|
const allRecords = [...settingsToCreateMapped, ...settingsToUpdateMapped];
|
2020-07-09 13:33:04 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
await db.batch(...allRecords);
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
return allRecords.length;
|
2019-09-16 20:26:32 +00:00
|
|
|
});
|
2018-05-18 17:55:08 +00:00
|
|
|
} catch (e) {
|
2019-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2018-05-18 17:55:08 +00:00
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|