Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMPROVE] Subscribe to permissions #2993

Merged
merged 7 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/actions/actionsTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
7 changes: 7 additions & 0 deletions app/actions/permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@ export function setPermissions(permissions) {
permissions
};
}

export function updatePermission(id, roles) {
return {
type: types.PERMISSIONS.UPDATE,
payload: { id, roles }
};
}
3 changes: 2 additions & 1 deletion app/lib/methods/getPermissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions app/lib/rocketchat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions app/reducers/permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down