2018-04-24 19:34:03 +00:00
|
|
|
import { InteractionManager } from 'react-native';
|
2019-04-26 20:54:58 +00:00
|
|
|
import semver from 'semver';
|
2018-05-18 17:55:08 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
import database from '../realm';
|
2018-05-18 17:55:08 +00:00
|
|
|
import log from '../../utils/log';
|
2019-04-26 20:54:58 +00:00
|
|
|
import reduxStore from '../createStore';
|
|
|
|
|
|
|
|
const getUpdatedSince = () => {
|
|
|
|
const permissions = database.objects('permissions').sorted('_updatedAt', true)[0];
|
|
|
|
return permissions && permissions._updatedAt.toISOString();
|
|
|
|
};
|
|
|
|
|
|
|
|
const create = (permissions) => {
|
|
|
|
if (permissions && permissions.length) {
|
|
|
|
permissions.forEach((permission) => {
|
|
|
|
try {
|
|
|
|
database.create('permissions', permission, true);
|
|
|
|
} catch (e) {
|
2019-09-03 19:27:57 +00:00
|
|
|
log(e);
|
2019-04-26 20:54:58 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2019-06-17 13:57:07 +00:00
|
|
|
export default function() {
|
|
|
|
return new Promise(async(resolve) => {
|
|
|
|
try {
|
|
|
|
const serverVersion = reduxStore.getState().server.version;
|
2018-12-05 20:52:08 +00:00
|
|
|
|
2019-06-17 13:57:07 +00:00
|
|
|
// if server version is lower than 0.73.0, fetches from old api
|
|
|
|
if (semver.lt(serverVersion, '0.73.0')) {
|
|
|
|
// RC 0.66.0
|
|
|
|
const result = await this.sdk.get('permissions.list');
|
|
|
|
if (!result.success) {
|
|
|
|
return resolve();
|
|
|
|
}
|
|
|
|
InteractionManager.runAfterInteractions(() => {
|
|
|
|
database.write(() => {
|
|
|
|
create(result.permissions);
|
|
|
|
});
|
|
|
|
return resolve();
|
2019-04-26 20:54:58 +00:00
|
|
|
});
|
2019-06-17 13:57:07 +00:00
|
|
|
} else {
|
|
|
|
const params = {};
|
|
|
|
const updatedSince = getUpdatedSince();
|
|
|
|
if (updatedSince) {
|
|
|
|
params.updatedSince = updatedSince;
|
|
|
|
}
|
|
|
|
// RC 0.73.0
|
|
|
|
const result = await this.sdk.get('permissions.listAll', params);
|
2018-06-18 13:30:36 +00:00
|
|
|
|
2019-06-17 13:57:07 +00:00
|
|
|
if (!result.success) {
|
|
|
|
return resolve();
|
|
|
|
}
|
2019-04-26 20:54:58 +00:00
|
|
|
|
2019-06-17 13:57:07 +00:00
|
|
|
InteractionManager.runAfterInteractions(
|
|
|
|
() => database.write(() => {
|
|
|
|
create(result.update);
|
2019-04-26 20:54:58 +00:00
|
|
|
|
2019-06-17 13:57:07 +00:00
|
|
|
if (result.delete && result.delete.length) {
|
|
|
|
result.delete.forEach((p) => {
|
|
|
|
try {
|
|
|
|
const permission = database.objectForPrimaryKey('permissions', p._id);
|
|
|
|
if (permission) {
|
|
|
|
database.delete(permission);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2019-09-03 19:27:57 +00:00
|
|
|
log(e);
|
2019-04-26 20:54:58 +00:00
|
|
|
}
|
2019-06-17 13:57:07 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return resolve();
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2019-09-03 19:27:57 +00:00
|
|
|
log(e);
|
2019-06-17 13:57:07 +00:00
|
|
|
return resolve();
|
2019-04-26 20:54:58 +00:00
|
|
|
}
|
2019-06-17 13:57:07 +00:00
|
|
|
});
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|