Skip to content

Commit

Permalink
Fetch roles from rest api (#853)
Browse files Browse the repository at this point in the history
* Fetch roles from rest api

* Fix RoomInfoView role get

* Remove roles from redux
  • Loading branch information
diegolmello committed Apr 26, 2019
1 parent 5c1be71 commit 75e4b86
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 68 deletions.
1 change: 0 additions & 1 deletion app/actions/actionsTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ export const SERVER = createRequestTypes('SERVER', [
export const METEOR = createRequestTypes('METEOR_CONNECT', [...defaultTypes, 'DISCONNECT']);
export const LOGOUT = 'LOGOUT'; // logout is always success
export const ACTIVE_USERS = createRequestTypes('ACTIVE_USERS', ['SET']);
export const ROLES = createRequestTypes('ROLES', ['SET']);
export const SNIPPETED_MESSAGES = createRequestTypes('SNIPPETED_MESSAGES', ['OPEN', 'READY', 'CLOSE', 'MESSAGES_RECEIVED']);
export const DEEP_LINKING = createRequestTypes('DEEP_LINKING', ['OPEN']);
export const SORT_PREFERENCES = createRequestTypes('SORT_PREFERENCES', ['SET_ALL', 'SET']);
8 changes: 0 additions & 8 deletions app/actions/roles.js

This file was deleted.

31 changes: 31 additions & 0 deletions app/lib/methods/getRoles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { InteractionManager } from 'react-native';

import database from '../realm';
import log from '../../utils/log';

export default async function() {
try {
// RC 0.70.0
const result = await this.sdk.get('roles.list');

if (!result.success) {
return;
}

const { roles } = result;

if (roles && roles.length) {
InteractionManager.runAfterInteractions(() => {
database.write(() => roles.forEach((role) => {
try {
database.create('roles', role, true);
} catch (e) {
log('getRoles create', e);
}
}));
});
}
} catch (e) {
log('getRoles', e);
}
}
34 changes: 4 additions & 30 deletions app/lib/rocketchat.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { AsyncStorage, InteractionManager } from 'react-native';
import foreach from 'lodash/forEach';
import semver from 'semver';
import { Rocketchat as RocketchatClient } from '@rocket.chat/sdk';

Expand All @@ -16,7 +15,6 @@ import {
} from '../actions/login';
import { disconnect, connectSuccess, connectRequest } from '../actions/connect';
import { setActiveUser } from '../actions/activeUsers';
import { setRoles } from '../actions/roles';

import subscribeRooms from './methods/subscriptions/rooms';
import subscribeRoom from './methods/subscriptions/room';
Expand All @@ -28,6 +26,7 @@ import getSettings from './methods/getSettings';
import getRooms from './methods/getRooms';
import getPermissions from './methods/getPermissions';
import getCustomEmoji from './methods/getCustomEmojis';
import getRoles from './methods/getRoles';
import canOpenRoom from './methods/canOpenRoom';

import loadMessagesForRoom from './methods/loadMessagesForRoom';
Expand All @@ -43,7 +42,7 @@ import { roomsRequest } from '../actions/rooms';
const TOKEN_KEY = 'reactnativemeteor_usertoken';
const SORT_PREFS_KEY = 'RC_SORT_PREFS_KEY';
const returnAnArray = obj => obj || [];
const MIN_ROCKETCHAT_VERSION = '0.66.0';
const MIN_ROCKETCHAT_VERSION = '0.70.0';

const RocketChat = {
TOKEN_KEY,
Expand Down Expand Up @@ -144,9 +143,9 @@ const RocketChat = {
}
this.roomsSub = await this.subscribeRooms();

this.sdk.subscribe('roles');
this.getPermissions();
this.getCustomEmoji();
this.getRoles();
this.registerPushToken().catch(e => console.log(e));

if (this.activeUsersSubTimeout) {
Expand Down Expand Up @@ -200,32 +199,6 @@ const RocketChat = {
});

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

this.sdk.onStreamData('rocketchat_roles', protectedFunction((ddpMessage) => {
this.roles = this.roles || {};

if (this.roleTimer) {
clearTimeout(this.roleTimer);
this.roleTimer = null;
}
this.roleTimer = setTimeout(() => {
reduxStore.dispatch(setRoles(this.roles));

database.write(() => {
foreach(this.roles, (description, _id) => {
try {
database.create('roles', { _id, description }, true);
} catch (e) {
log('create roles', e);
}
});
});

this.roleTimer = null;
return this.roles = {};
}, 1000);
this.roles[ddpMessage.id] = (ddpMessage.fields && ddpMessage.fields.description) || undefined;
}));
},

register(credentials) {
Expand Down Expand Up @@ -467,6 +440,7 @@ const RocketChat = {
getSettings,
getPermissions,
getCustomEmoji,
getRoles,
parseSettings: settings => settings.reduce((ret, item) => {
ret[item._id] = item[defaultSettings[item._id].type];
return ret;
Expand Down
2 changes: 0 additions & 2 deletions app/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import createChannel from './createChannel';
import app from './app';
import customEmojis from './customEmojis';
import activeUsers from './activeUsers';
import roles from './roles';
import sortPreferences from './sortPreferences';

export default combineReducers({
Expand All @@ -25,6 +24,5 @@ export default combineReducers({
rooms,
customEmojis,
activeUsers,
roles,
sortPreferences
});
15 changes: 0 additions & 15 deletions app/reducers/roles.js

This file was deleted.

6 changes: 0 additions & 6 deletions app/sagas/selectServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import Navigation from '../lib/Navigation';
import { SERVER } from '../actions/actionsTypes';
import * as actions from '../actions';
import { serverFailure, selectServerRequest, selectServerSuccess } from '../actions/server';
import { setRoles } from '../actions/roles';
import { setUser } from '../actions/login';
import RocketChat from '../lib/rocketchat';
import database from '../lib/realm';
Expand Down Expand Up @@ -54,11 +53,6 @@ const handleSelectServer = function* handleSelectServer({ server, version, fetch
yield put(actions.setAllSettings(RocketChat.parseSettings(settings.slice(0, settings.length))));
const emojis = database.objects('customEmojis');
yield put(actions.setCustomEmojis(RocketChat.parseEmojis(emojis.slice(0, emojis.length))));
const roles = database.objects('roles');
yield put(setRoles(roles.reduce((result, role) => {
result[role._id] = role.description;
return result;
}, {})));

yield put(selectServerSuccess(server, fetchVersion ? serverInfo && serverInfo.version : version));
} catch (e) {
Expand Down
17 changes: 11 additions & 6 deletions app/views/RoomInfoView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ const getRoomTitle = room => (room.t === 'd'
token: state.login.user && state.login.user.token
},
activeUsers: state.activeUsers, // TODO: remove it
Message_TimeFormat: state.settings.Message_TimeFormat,
allRoles: state.roles
Message_TimeFormat: state.settings.Message_TimeFormat
}))
/** @extends React.Component */
export default class RoomInfoView extends LoggedView {
Expand All @@ -67,8 +66,7 @@ export default class RoomInfoView extends LoggedView {
}),
baseUrl: PropTypes.string,
activeUsers: PropTypes.object,
Message_TimeFormat: PropTypes.string,
allRoles: PropTypes.object
Message_TimeFormat: PropTypes.string
}

constructor(props) {
Expand Down Expand Up @@ -161,6 +159,14 @@ export default class RoomInfoView extends LoggedView {
}
}

getRoleDescription = (id) => {
const role = database.objectForPrimaryKey('roles', id);
if (role) {
return role.description;
}
return null;
}

isDirect = () => {
const { room: { t } } = this.state;
return t === 'd';
Expand All @@ -185,7 +191,6 @@ export default class RoomInfoView extends LoggedView {

renderRoles = () => {
const { roles } = this.state;
const { allRoles } = this.props;

return (
roles.length > 0
Expand All @@ -195,7 +200,7 @@ export default class RoomInfoView extends LoggedView {
<View style={styles.rolesContainer}>
{roles.map(role => (
<View style={styles.roleBadge} key={role}>
<Text style={styles.role}>{ allRoles[role] }</Text>
<Text style={styles.role}>{ this.getRoleDescription(role) }</Text>
</View>
))}
</View>
Expand Down

0 comments on commit 75e4b86

Please sign in to comment.