diff --git a/app/actions/actionsTypes.js b/app/actions/actionsTypes.js index 1fd8679bc..6bb169009 100644 --- a/app/actions/actionsTypes.js +++ b/app/actions/actionsTypes.js @@ -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']); diff --git a/app/actions/permissions.js b/app/actions/permissions.js index 88179c34f..444d9f11c 100644 --- a/app/actions/permissions.js +++ b/app/actions/permissions.js @@ -6,3 +6,10 @@ export function setPermissions(permissions) { permissions }; } + +export function updatePermission(id, roles) { + return { + type: types.PERMISSIONS.UPDATE, + payload: { id, roles } + }; +} diff --git a/app/lib/methods/getPermissions.js b/app/lib/methods/getPermissions.js index 2b7da4765..6e68f5c33 100644 --- a/app/lib/methods/getPermissions.js +++ b/app/lib/methods/getPermissions.js @@ -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 diff --git a/app/lib/rocketchat.js b/app/lib/rocketchat.js index 6e7b3bcb2..bcff9e300 100644 --- a/app/lib/rocketchat.js +++ b/app/lib/rocketchat.js @@ -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; diff --git a/app/reducers/permissions.js b/app/reducers/permissions.js index 1b3a14ec2..dbba0e107 100644 --- a/app/reducers/permissions.js +++ b/app/reducers/permissions.js @@ -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; }