2018-04-24 19:34:03 +00:00
|
|
|
import { InteractionManager } from 'react-native';
|
2018-10-15 20:22:42 +00:00
|
|
|
import * as SDK from '@rocket.chat/sdk';
|
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-09-11 16:32:52 +00:00
|
|
|
import { settingsUpdatedAt } from '../../constants/settings';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-06-18 13:30:36 +00:00
|
|
|
const getLastUpdate = () => {
|
2018-04-24 19:34:03 +00:00
|
|
|
const [setting] = database.objects('settings').sorted('_updatedAt', true);
|
|
|
|
return setting && setting._updatedAt;
|
|
|
|
};
|
|
|
|
|
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-10-15 20:22:42 +00:00
|
|
|
// if (!SDK.driver.dd) {
|
|
|
|
// // TODO: should implement loop or get from rest?
|
|
|
|
// return;
|
|
|
|
// }
|
2018-09-28 18:57:29 +00:00
|
|
|
|
2018-06-18 13:30:36 +00:00
|
|
|
const lastUpdate = getLastUpdate();
|
2018-09-11 16:32:52 +00:00
|
|
|
const fetchNewSettings = lastUpdate < settingsUpdatedAt;
|
2018-10-15 20:22:42 +00:00
|
|
|
const result = await ((!lastUpdate || fetchNewSettings)
|
|
|
|
? SDK.driver.asyncCall('public-settings/get')
|
|
|
|
: SDK.driver.asyncCall('public-settings/get', new Date(lastUpdate)));
|
2018-08-31 16:46:33 +00:00
|
|
|
const data = result.update || result || [];
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-08-31 16:46:33 +00:00
|
|
|
const filteredSettings = this._prepareSettings(this._filterSettings(data));
|
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
|
|
|
}
|