[IMPROVE] Subscribe to permissions (#2993)

* [CHORE] Subscribe to permissions

* add redux action for update

* Minor tweaks

Co-authored-by: Gerzon Z <gerzonc@icloud.com>
Co-authored-by: Gerzon Z <gerzonzcanario@gmail.com>
Co-authored-by: Diego Mello <diegolmello@gmail.com>
This commit is contained in:
Gung Wah 2021-07-01 21:57:23 +08:00 committed by GitHub
parent a6ded9546b
commit 30487c392f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 2 deletions

View File

@ -71,4 +71,4 @@ export const APP_STATE = createRequestTypes('APP_STATE', ['FOREGROUND', 'BACKGRO
export const ENTERPRISE_MODULES = createRequestTypes('ENTERPRISE_MODULES', ['CLEAR', 'SET']);
export const ENCRYPTION = createRequestTypes('ENCRYPTION', ['INIT', 'STOP', 'DECODE_KEY', 'SET', 'SET_BANNER']);
export const PERMISSIONS = createRequestTypes('PERMISSIONS', ['SET']);
export const PERMISSIONS = createRequestTypes('PERMISSIONS', ['SET', 'UPDATE']);

View File

@ -6,3 +6,10 @@ export function setPermissions(permissions) {
permissions
};
}
export function updatePermission(id, roles) {
return {
type: types.PERMISSIONS.UPDATE,
payload: { id, roles }
};
}

View File

@ -6,6 +6,7 @@ import { compareServerVersion, methods } from '../utils';
import database from '../database';
import log from '../../utils/log';
import reduxStore from '../createStore';
import RocketChat from '../rocketchat';
import protectedFunction from './helpers/protectedFunction';
import { setPermissions as setPermissionsAction } from '../../actions/permissions';
@ -128,7 +129,7 @@ export function getPermissions() {
const db = database.active;
const permissionsCollection = db.get('permissions');
const allRecords = await permissionsCollection.query().fetch();
RocketChat.subscribe('stream-notify-logged', 'permissions-changed');
// if server version is lower than 0.73.0, fetches from old api
if (compareServerVersion(serverVersion, '0.73.0', methods.lowerThan)) {
// RC 0.66.0

View File

@ -63,6 +63,7 @@ import UserPreferences from './userPreferences';
import { Encryption } from './encryption';
import EventEmitter from '../utils/events';
import { sanitizeLikeString } from './database/utils';
import { updatePermission } from '../actions/permissions';
import { TEAM_TYPE } from '../definition/ITeam';
const TOKEN_KEY = 'reactnativemeteor_usertoken';
@ -310,6 +311,21 @@ const RocketChat = {
} catch {
// We can't create a new record since we don't receive the user._id
}
} else if (/permissions-changed/.test(eventName)) {
const { _id, roles } = ddpMessage.fields.args[1];
const db = database.active;
const permissionsCollection = db.get('permissions');
try {
const permissionsRecord = await permissionsCollection.find(_id);
await db.action(async() => {
await permissionsRecord.update((u) => {
u.roles = roles;
});
});
reduxStore.dispatch(updatePermission(_id, roles));
} catch (err) {
//
}
} else if (/Users:NameChanged/.test(eventName)) {
const userNameChanged = ddpMessage.fields.args[0];
const db = database.active;

View File

@ -8,6 +8,11 @@ export default function permissions(state = initialState, action) {
switch (action.type) {
case PERMISSIONS.SET:
return action.permissions;
case PERMISSIONS.UPDATE:
return {
...state,
[action.payload.id]: action.payload.roles
};
default:
return state;
}