Update fetch permissions api (#850)

This commit is contained in:
Diego Mello 2019-04-26 17:54:58 -03:00 committed by GitHub
parent c15fab7c84
commit c340829638
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 69 additions and 69 deletions

View File

@ -1,17 +0,0 @@
export default [
'add-user-to-any-c-room',
'add-user-to-any-p-room',
'add-user-to-joined-room',
'archive-room',
'delete-c',
'delete-message',
'delete-p',
'edit-message',
'edit-room',
'force-delete-message',
'mute-user',
'set-react-when-readonly',
'set-readonly',
'unarchive-room',
'view-broadcast-member-list'
];

View File

@ -1,34 +1,75 @@
import { InteractionManager } from 'react-native'; import { InteractionManager } from 'react-native';
import semver from 'semver';
import database from '../realm'; import database from '../realm';
import log from '../../utils/log'; import log from '../../utils/log';
import defaultPermissions from '../../constants/permissions'; 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) {
log('getPermissions create', e);
}
});
}
};
export default async function() { export default async function() {
try { try {
// RC 0.66.0 const serverVersion = reduxStore.getState().server.version;
const result = await this.sdk.get('permissions.list');
if (!result.success) { // if server version is lower than 0.73.0, fetches from old api
return; if (semver.lt(serverVersion, '0.73.0')) {
} // RC 0.66.0
const permissions = result.permissions.filter(permission => defaultPermissions.includes(permission._id)); const result = await this.sdk.get('permissions.list');
permissions if (!result.success) {
.map((permission) => { return;
permission._updatedAt = new Date(); }
permission.roles = permission.roles.map(role => ({ value: role })); InteractionManager.runAfterInteractions(() => {
return permission; database.write(() => {
create(result.permissions);
});
}); });
} else {
const params = {};
const updatedSince = getUpdatedSince();
if (updatedSince) {
params.updatedSince = updatedSince;
}
// RC 0.73.0
const result = await this.sdk.get('permissions.listAll', params);
InteractionManager.runAfterInteractions( if (!result.success) {
() => database.write(() => permissions.forEach((permission) => { return;
try { }
database.create('permissions', permission, true);
} catch (e) { InteractionManager.runAfterInteractions(
log('getPermissions create', e); () => database.write(() => {
} create(result.update);
}))
); 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) {
log('getPermissions delete', e);
}
});
}
})
);
}
} catch (e) { } catch (e) {
log('getPermissions', e); log('getPermissions', e);
} }

View File

@ -31,11 +31,6 @@ export const merge = (subscription, room) => {
subscription.muted = []; subscription.muted = [];
} }
} }
if (subscription.roles && subscription.roles.length) {
subscription.roles = subscription.roles.map(role => (role.value ? role : { value: role }));
} else {
subscription.roles = [];
}
if (subscription.mobilePushNotifications === 'nothing') { if (subscription.mobilePushNotifications === 'nothing') {
subscription.notifications = true; subscription.notifications = true;

View File

@ -28,20 +28,12 @@ const settingsSchema = {
} }
}; };
const permissionsRolesSchema = {
name: 'permissionsRoles',
primaryKey: 'value',
properties: {
value: 'string'
}
};
const permissionsSchema = { const permissionsSchema = {
name: 'permissions', name: 'permissions',
primaryKey: '_id', primaryKey: '_id',
properties: { properties: {
_id: 'string', _id: 'string',
roles: { type: 'list', objectType: 'permissionsRoles' }, roles: 'string[]',
_updatedAt: { type: 'date', optional: true } _updatedAt: { type: 'date', optional: true }
} }
}; };
@ -55,14 +47,6 @@ const roomsSchema = {
} }
}; };
const subscriptionRolesSchema = {
name: 'subscriptionRolesSchema',
primaryKey: 'value',
properties: {
value: 'string'
}
};
const userMutedInRoomSchema = { const userMutedInRoomSchema = {
name: 'usersMuted', name: 'usersMuted',
primaryKey: 'value', primaryKey: 'value',
@ -85,7 +69,7 @@ const subscriptionSchema = {
rid: { type: 'string', indexed: true }, rid: { type: 'string', indexed: true },
open: { type: 'bool', optional: true }, open: { type: 'bool', optional: true },
alert: { type: 'bool', optional: true }, alert: { type: 'bool', optional: true },
roles: { type: 'list', objectType: 'subscriptionRolesSchema' }, roles: 'string[]',
unread: { type: 'int', optional: true }, unread: { type: 'int', optional: true },
userMentions: { type: 'int', optional: true }, userMentions: { type: 'int', optional: true },
roomUpdatedAt: { type: 'date', optional: true }, roomUpdatedAt: { type: 'date', optional: true },
@ -358,7 +342,6 @@ const usersTypingSchema = {
const schema = [ const schema = [
settingsSchema, settingsSchema,
subscriptionSchema, subscriptionSchema,
subscriptionRolesSchema,
messagesSchema, messagesSchema,
threadsSchema, threadsSchema,
threadMessagesSchema, threadMessagesSchema,
@ -368,7 +351,6 @@ const schema = [
attachmentFields, attachmentFields,
messagesEditedBySchema, messagesEditedBySchema,
permissionsSchema, permissionsSchema,
permissionsRolesSchema,
url, url,
frequentlyUsedEmojiSchema, frequentlyUsedEmojiSchema,
customEmojiAliasesSchema, customEmojiAliasesSchema,

View File

@ -298,7 +298,8 @@ const RocketChat = {
language: result.me.language, language: result.me.language,
status: result.me.status, status: result.me.status,
customFields: result.me.customFields, customFields: result.me.customFields,
emails: result.me.emails emails: result.me.emails,
roles: result.me.roles
}; };
return user; return user;
} catch (e) { } catch (e) {
@ -654,7 +655,7 @@ const RocketChat = {
return this.sdk.methodCall('getSingleMessage', msgId); return this.sdk.methodCall('getSingleMessage', msgId);
}, },
hasPermission(permissions, rid) { hasPermission(permissions, rid) {
let roles = []; let roomRoles = [];
try { try {
// get the room from realm // get the room from realm
const [room] = database.objects('subscriptions').filtered('rid = $0', rid); const [room] = database.objects('subscriptions').filtered('rid = $0', rid);
@ -665,15 +666,13 @@ const RocketChat = {
}, {}); }, {});
} }
// get room roles // get room roles
roles = room.roles; // eslint-disable-line prefer-destructuring roomRoles = room.roles;
} catch (error) { } catch (error) {
console.log('hasPermission -> error', error); console.log('hasPermission -> error', error);
} }
// get permissions from realm // get permissions from realm
const permissionsFiltered = database.objects('permissions') const permissionsFiltered = database.objects('permissions')
.filter(permission => permissions.includes(permission._id)); .filter(permission => permissions.includes(permission._id));
// transform room roles to array
const roomRoles = Array.from(Object.keys(roles), i => roles[i].value);
// get user roles on the server from redux // get user roles on the server from redux
const userRoles = (reduxStore.getState().login.user && reduxStore.getState().login.user.roles) || []; const userRoles = (reduxStore.getState().login.user && reduxStore.getState().login.user.roles) || [];
// merge both roles // merge both roles
@ -685,7 +684,7 @@ const RocketChat = {
result[permission] = false; result[permission] = false;
const permissionFound = permissionsFiltered.find(p => p._id === permission); const permissionFound = permissionsFiltered.find(p => p._id === permission);
if (permissionFound) { if (permissionFound) {
result[permission] = returnAnArray(permissionFound.roles).some(r => mergedRoles.includes(r.value)); result[permission] = returnAnArray(permissionFound.roles).some(r => mergedRoles.includes(r));
} }
return result; return result;
}, {}); }, {});