2018-04-24 19:34:03 +00:00
|
|
|
import { InteractionManager } from 'react-native';
|
2019-09-16 20:26:32 +00:00
|
|
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
|
|
|
import { Q } from '@nozbe/watermelondb';
|
2018-05-18 17:55:08 +00:00
|
|
|
|
2020-04-01 19:24:42 +00:00
|
|
|
import { addSettings, clearSettings } from '../../actions/settings';
|
2019-10-28 13:37:13 +00:00
|
|
|
import RocketChat from '../rocketchat';
|
2018-04-24 19:34:03 +00:00
|
|
|
import reduxStore from '../createStore';
|
2018-12-05 20:52:08 +00:00
|
|
|
import settings from '../../constants/settings';
|
2019-09-16 20:26:32 +00:00
|
|
|
import log from '../../utils/log';
|
|
|
|
import database from '../database';
|
|
|
|
import protectedFunction from './helpers/protectedFunction';
|
2019-10-18 16:20:01 +00:00
|
|
|
import fetch from '../../utils/fetch';
|
2020-05-18 17:35:21 +00:00
|
|
|
import { DEFAULT_AUTO_LOCK } from '../../constants/localAuthentication';
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2020-05-08 17:04:37 +00:00
|
|
|
const serverInfoKeys = ['Site_Name', 'UI_Use_Real_Name', 'FileUpload_MediaTypeWhiteList', 'FileUpload_MaxFileSize', 'Force_Screen_Lock', 'Force_Screen_Lock_After'];
|
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',
|
|
|
|
'Accounts_PasswordReset'
|
|
|
|
];
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
const serverInfoUpdate = async(serverInfo, iconSetting) => {
|
|
|
|
const serversDB = database.servers;
|
|
|
|
const serverId = reduxStore.getState().server.server;
|
2020-05-08 17:04:37 +00:00
|
|
|
const serversCollection = serversDB.collections.get('servers');
|
|
|
|
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') {
|
|
|
|
// 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 };
|
|
|
|
}
|
|
|
|
// if Force_Screen_Lock_After > 0, use it
|
|
|
|
if (setting.valueAsNumber > 0) {
|
|
|
|
return { ...allSettings, autoLockTime: setting.valueAsNumber };
|
|
|
|
}
|
|
|
|
}
|
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) {
|
|
|
|
const iconURL = `${ serverId }/${ iconSetting.value.url || iconSetting.value.defaultUrl }`;
|
|
|
|
info = { ...info, iconURL };
|
|
|
|
}
|
|
|
|
|
|
|
|
await serversDB.action(async() => {
|
2019-02-25 16:23:17 +00:00
|
|
|
try {
|
2019-09-16 20:26:32 +00:00
|
|
|
await server.update((record) => {
|
|
|
|
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
|
|
|
|
2020-03-03 20:53:48 +00:00
|
|
|
export async function getLoginSettings({ server }) {
|
|
|
|
try {
|
2020-04-09 05:26:28 +00:00
|
|
|
const settingsParams = JSON.stringify(loginSettings);
|
2020-03-03 20:53:48 +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());
|
|
|
|
reduxStore.dispatch(addSettings(this.parseSettings(this._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
|
|
|
}
|
|
|
|
|
2019-10-28 13:37:13 +00:00
|
|
|
export async function setSettings() {
|
|
|
|
const db = database.active;
|
|
|
|
const settingsCollection = db.collections.get('settings');
|
|
|
|
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
|
|
|
|
}));
|
2020-04-01 19:24:42 +00:00
|
|
|
reduxStore.dispatch(addSettings(RocketChat.parseSettings(parsed.slice(0, parsed.length))));
|
2019-10-28 13:37:13 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
export default async function() {
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
2019-09-16 20:26:32 +00:00
|
|
|
const db = database.active;
|
2020-04-09 05:26:28 +00:00
|
|
|
const settingsParams = JSON.stringify(Object.keys(settings).filter(key => !loginSettings.includes(key)));
|
2018-12-21 10:55:35 +00:00
|
|
|
// RC 0.60.0
|
2019-02-07 15:48:10 +00:00
|
|
|
const result = await fetch(`${ this.sdk.client.host }/api/v1/settings.public?query={"_id":{"$in":${ settingsParams }}}`).then(response => response.json());
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
if (!result.success) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const data = result.settings || [];
|
2020-03-30 19:20:50 +00:00
|
|
|
const filteredSettings = this._prepareSettings(data);
|
2019-09-16 20:26:32 +00:00
|
|
|
const filteredSettingsIds = filteredSettings.map(s => s._id);
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2020-04-01 19:24:42 +00:00
|
|
|
reduxStore.dispatch(addSettings(this.parseSettings(filteredSettings)));
|
2019-09-16 20:26:32 +00:00
|
|
|
InteractionManager.runAfterInteractions(async() => {
|
|
|
|
// filter server info
|
|
|
|
const serverInfo = filteredSettings.filter(i1 => serverInfoKeys.includes(i1._id));
|
|
|
|
const iconSetting = data.find(item => item._id === 'Assets_favicon_512');
|
|
|
|
await serverInfoUpdate(serverInfo, iconSetting);
|
2018-08-31 16:46:33 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
await db.action(async() => {
|
|
|
|
const settingsCollection = db.collections.get('settings');
|
|
|
|
const allSettingsRecords = await settingsCollection
|
|
|
|
.query(Q.where('id', Q.oneOf(filteredSettingsIds)))
|
|
|
|
.fetch();
|
|
|
|
|
|
|
|
// filter settings
|
|
|
|
let settingsToCreate = filteredSettings.filter(i1 => !allSettingsRecords.find(i2 => i1._id === i2.id));
|
|
|
|
let settingsToUpdate = allSettingsRecords.filter(i1 => filteredSettings.find(i2 => i1.id === i2._id));
|
|
|
|
|
|
|
|
// Create
|
|
|
|
settingsToCreate = settingsToCreate.map(setting => settingsCollection.prepareCreate(protectedFunction((s) => {
|
|
|
|
s._raw = sanitizedRaw({ id: setting._id }, settingsCollection.schema);
|
|
|
|
Object.assign(s, setting);
|
|
|
|
})));
|
|
|
|
|
|
|
|
// Update
|
|
|
|
settingsToUpdate = settingsToUpdate.map((setting) => {
|
|
|
|
const newSetting = filteredSettings.find(s => s._id === setting.id);
|
|
|
|
return setting.prepareUpdate(protectedFunction((s) => {
|
|
|
|
Object.assign(s, newSetting);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
const allRecords = [
|
|
|
|
...settingsToCreate,
|
|
|
|
...settingsToUpdate
|
|
|
|
];
|
|
|
|
|
|
|
|
try {
|
|
|
|
await db.batch(...allRecords);
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
return allRecords.length;
|
|
|
|
});
|
|
|
|
});
|
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
|
|
|
}
|