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 roles #2992

Merged
merged 21 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions app/actions/actionsTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,4 @@ export const ENTERPRISE_MODULES = createRequestTypes('ENTERPRISE_MODULES', ['CLE
export const ENCRYPTION = createRequestTypes('ENCRYPTION', ['INIT', 'STOP', 'DECODE_KEY', 'SET', 'SET_BANNER']);

export const PERMISSIONS = createRequestTypes('PERMISSIONS', ['SET']);
export const ROLES = createRequestTypes('ROLES', ['SET', 'UPDATE', 'ADD', 'REMOVE']);
26 changes: 26 additions & 0 deletions app/actions/roles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as types from './actionsTypes';

export function setRoles(roles) {
return {
type: types.ROLES.SET,
roles
};
}
export function updateRoles(id, desc) {
return {
type: types.ROLES.UPDATE,
payload: { id, desc }
};
}
export function addRoles(id, desc) {
return {
type: types.ROLES.ADD,
payload: { id, desc }
};
}
export function removeRoles(id) {
return {
type: types.ROLES.REMOVE,
payload: { id }
};
}
52 changes: 51 additions & 1 deletion app/lib/methods/getRoles.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,58 @@ import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';

import database from '../database';
import log from '../../utils/log';
import reduxStore from '../createStore';
import protectedFunction from './helpers/protectedFunction';
import {
addRoles, removeRoles, setRoles as setRolesAction, updateRoles
} from '../../actions/roles';

export default function() {
export async function setRoles() {
const db = database.active;
const rolesCollection = db.collections.get('roles');
const allRoles = await rolesCollection.query().fetch();
const parsed = allRoles.reduce((acc, item) => ({ ...acc, [item.id]: item.description || item.id }), {});
reduxStore.dispatch(setRolesAction(parsed));
}

export async function subscribeRoles(ddpMessage) {
gerzonc marked this conversation as resolved.
Show resolved Hide resolved
gerzonc marked this conversation as resolved.
Show resolved Hide resolved
const { type, _id, description } = ddpMessage.fields.args[0];
if (/changed/.test(type)) {
const db = database.active;
const rolesCollection = db.get('roles');
try {
const rolesRecord = await rolesCollection.find(_id);
await db.action(async() => {
await rolesRecord.update((u) => {
u.description = description;
});
});
reduxStore.dispatch(updateRoles(_id, description));
} catch (err) {
await db.action(async() => {
await rolesCollection.create((post) => {
gerzonc marked this conversation as resolved.
Show resolved Hide resolved
post._raw = sanitizedRaw({ id: _id, description }, rolesCollection.schema);
});
});
reduxStore.dispatch(addRoles(_id, description || _id));
}
}
if (/removed/.test(type)) {
const db = database.active;
const rolesCollection = db.get('roles');
try {
const rolesRecord = await rolesCollection.find(_id);
await db.action(async() => {
await rolesRecord.destroyPermanently();
});
reduxStore.dispatch(removeRoles(_id));
} catch (err) {
console.log(err);
}
}
}

export function getRoles() {
const db = database.active;
return new Promise(async(resolve) => {
try {
Expand Down Expand Up @@ -50,6 +99,7 @@ export default function() {
} catch (e) {
log(e);
}
setRoles();
return allRecords.length;
});
return resolve();
Expand Down
5 changes: 4 additions & 1 deletion app/lib/rocketchat.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
getEnterpriseModules, setEnterpriseModules, hasLicense, isOmnichannelModuleAvailable
} from './methods/enterpriseModules';
import getSlashCommands from './methods/getSlashCommands';
import getRoles from './methods/getRoles';
import { getRoles, setRoles, subscribeRoles } from './methods/getRoles';
import canOpenRoom from './methods/canOpenRoom';
import triggerBlockAction, { triggerSubmitView, triggerCancel } from './methods/actions';

Expand Down Expand Up @@ -257,6 +257,8 @@ const RocketChat = {

this.usersListener = this.sdk.onStreamData('users', protectedFunction(ddpMessage => RocketChat._setUser(ddpMessage)));

this.rolesListener = this.sdk.onStreamData('stream-roles', protectedFunction(ddpMessage => subscribeRoles(ddpMessage)));

this.notifyLoggedListener = this.sdk.onStreamData('stream-notify-logged', protectedFunction(async(ddpMessage) => {
const { eventName } = ddpMessage.fields;
if (/user-status/.test(eventName)) {
Expand Down Expand Up @@ -756,6 +758,7 @@ const RocketChat = {
isOmnichannelModuleAvailable,
getSlashCommands,
getRoles,
setRoles,
parseSettings: settings => settings.reduce((ret, item) => {
ret[item._id] = defaultSettings[item._id] && item[defaultSettings[item._id].type];
if (item._id === 'Hide_System_Messages') {
Expand Down
4 changes: 3 additions & 1 deletion app/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import createDiscussion from './createDiscussion';
import enterpriseModules from './enterpriseModules';
import encryption from './encryption';
import permissions from './permissions';
import roles from './roles';

import inquiry from '../ee/omnichannel/reducers/inquiry';

Expand All @@ -43,5 +44,6 @@ export default combineReducers({
inquiry,
enterpriseModules,
encryption,
permissions
permissions,
roles
});
28 changes: 28 additions & 0 deletions app/reducers/roles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ROLES } from '../actions/actionsTypes';

const initialState = {
roles: {}
};

export default function permissions(state = initialState, action) {
switch (action.type) {
case ROLES.SET:
return action.roles;
case ROLES.UPDATE:
return {
...state,
[action.payload.id]: action.payload.desc || action.payload.id
};
case ROLES.ADD:
gerzonc marked this conversation as resolved.
Show resolved Hide resolved
return {
...state,
...{ [action.payload.id]: action.payload.desc }
};
case ROLES.REMOVE:
delete state[action.payload.id];
gerzonc marked this conversation as resolved.
Show resolved Hide resolved
return state;

default:
return state;
}
}
1 change: 1 addition & 0 deletions app/sagas/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ const fetchCustomEmojis = function* fetchCustomEmojis() {
};

const fetchRoles = function* fetchRoles() {
RocketChat.subscribe('stream-roles', 'roles');
yield RocketChat.getRoles();
};

Expand Down
22 changes: 7 additions & 15 deletions app/views/RoomInfoView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { connect } from 'react-redux';
import UAParser from 'ua-parser-js';
import isEmpty from 'lodash/isEmpty';

import database from '../../lib/database';
import { CustomIcon } from '../../lib/Icons';
import Status from '../../containers/Status';
import Avatar from '../../containers/Avatar';
Expand Down Expand Up @@ -55,7 +54,8 @@ class RoomInfoView extends React.Component {
theme: PropTypes.string,
isMasterDetail: PropTypes.bool,
jitsiEnabled: PropTypes.bool,
editRoomPermission: PropTypes.array
editRoomPermission: PropTypes.array,
roles: PropTypes.object
}

constructor(props) {
Expand Down Expand Up @@ -133,18 +133,9 @@ class RoomInfoView extends React.Component {
return room.t === 'l';
}

getRoleDescription = async(id) => {
const db = database.active;
try {
const rolesCollection = db.get('roles');
const role = await rolesCollection.find(id);
if (role) {
return role.description;
}
return null;
} catch (e) {
return null;
}
getRoleDescription = (id) => {
const { roles } = this.props;
return roles[id];
};

loadVisitor = async() => {
Expand Down Expand Up @@ -378,7 +369,8 @@ const mapStateToProps = state => ({
rooms: state.room.rooms,
isMasterDetail: state.app.isMasterDetail,
jitsiEnabled: state.settings.Jitsi_Enabled || false,
editRoomPermission: state.permissions['edit-room']
editRoomPermission: state.permissions['edit-room'],
roles: state.roles
});

export default connect(mapStateToProps)(withTheme(RoomInfoView));