2018-04-24 19:34:03 +00:00
|
|
|
import { InteractionManager } from 'react-native';
|
2018-05-18 17:55:08 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
import reduxStore from '../createStore';
|
|
|
|
import database from '../realm';
|
|
|
|
import * as actions from '../../actions';
|
2018-05-18 17:55:08 +00:00
|
|
|
import log from '../../utils/log';
|
2018-12-05 20:52:08 +00:00
|
|
|
import settings from '../../constants/settings';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-08-31 16:46:33 +00:00
|
|
|
function updateServer(param) {
|
|
|
|
database.databases.serversDB.write(() => {
|
2018-10-15 20:22:42 +00:00
|
|
|
database.databases.serversDB.create('servers', { id: reduxStore.getState().server.server, ...param }, true);
|
2018-08-31 16:46:33 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
export default async function() {
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
2018-12-05 20:52:08 +00:00
|
|
|
const settingsParams = JSON.stringify(Object.keys(settings));
|
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 || [];
|
|
|
|
const filteredSettings = this._prepareSettings(data.filter(item => item._id !== 'Assets_favicon_512'));
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
InteractionManager.runAfterInteractions(
|
|
|
|
() => database.write(
|
|
|
|
() => filteredSettings.forEach((setting) => {
|
2018-08-31 16:46:33 +00:00
|
|
|
database.create('settings', { ...setting, _updatedAt: new Date() }, true);
|
|
|
|
|
|
|
|
if (setting._id === 'Site_Name') {
|
|
|
|
updateServer.call(this, { name: setting.valueAsString });
|
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
2018-05-18 18:32:15 +00:00
|
|
|
reduxStore.dispatch(actions.addSettings(this.parseSettings(filteredSettings)));
|
2018-08-31 16:46:33 +00:00
|
|
|
|
|
|
|
const iconSetting = data.find(item => item._id === 'Assets_favicon_512');
|
|
|
|
if (iconSetting) {
|
2018-10-15 20:22:42 +00:00
|
|
|
const baseUrl = reduxStore.getState().server.server;
|
|
|
|
const iconURL = `${ baseUrl }/${ iconSetting.value.url || iconSetting.value.defaultUrl }`;
|
2018-08-31 16:46:33 +00:00
|
|
|
updateServer.call(this, { iconURL });
|
|
|
|
}
|
2018-05-18 17:55:08 +00:00
|
|
|
} catch (e) {
|
|
|
|
log('getSettings', e);
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|