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 database from '../realm';
|
2018-05-18 17:55:08 +00:00
|
|
|
import log from '../../utils/log';
|
2018-06-18 13:30:36 +00:00
|
|
|
import defaultPermissions from '../../constants/permissions';
|
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('permissions').sorted('_updatedAt', true)[0];
|
|
|
|
return setting && setting._updatedAt;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default async function() {
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
2018-06-18 13:30:36 +00:00
|
|
|
const lastUpdate = getLastUpdate();
|
2018-10-15 20:22:42 +00:00
|
|
|
const result = await (!lastUpdate
|
|
|
|
? SDK.driver.asyncCall('permissions/get')
|
|
|
|
: SDK.driver.asyncCall('permissions/get', new Date(lastUpdate)));
|
2018-06-18 13:30:36 +00:00
|
|
|
const permissions = (result.update || result).filter(permission => defaultPermissions.includes(permission._id));
|
|
|
|
permissions
|
|
|
|
.map((permission) => {
|
|
|
|
permission._updatedAt = new Date();
|
|
|
|
permission.roles = permission.roles.map(role => ({ value: role }));
|
|
|
|
return permission;
|
|
|
|
});
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
InteractionManager.runAfterInteractions(
|
2018-10-15 20:22:42 +00:00
|
|
|
() => database.write(() => permissions.forEach((permission) => {
|
|
|
|
try {
|
|
|
|
database.create('permissions', permission, true);
|
|
|
|
} catch (e) {
|
|
|
|
log('getPermissions create', e);
|
|
|
|
}
|
|
|
|
}))
|
2018-09-25 19:28:42 +00:00
|
|
|
);
|
2018-05-18 17:55:08 +00:00
|
|
|
} catch (e) {
|
|
|
|
log('getPermissions', e);
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|