Skip to content

Commit

Permalink
[IMPROVE] Subscribe to settings (#3222)
Browse files Browse the repository at this point in the history
* Add action and reducer

* Add streamNotifyAll listener

* Minor tweak

* Minor tweak

* Fix update not taking in consideration other type columnns

Co-authored-by: Diego Mello <diegolmello@gmail.com>
  • Loading branch information
gerzonc and diegolmello authored Jul 1, 2021
1 parent 88191b9 commit d547b61
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/actions/actionsTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const INVITE_LINKS = createRequestTypes('INVITE_LINKS', [
'CLEAR',
...defaultTypes
]);
export const SETTINGS = createRequestTypes('SETTINGS', ['CLEAR', 'ADD']);
export const SETTINGS = createRequestTypes('SETTINGS', ['CLEAR', 'ADD', 'UPDATE']);
export const APP_STATE = createRequestTypes('APP_STATE', ['FOREGROUND', 'BACKGROUND']);
export const ENTERPRISE_MODULES = createRequestTypes('ENTERPRISE_MODULES', ['CLEAR', 'SET']);
export const ENCRYPTION = createRequestTypes('ENCRYPTION', ['INIT', 'STOP', 'DECODE_KEY', 'SET', 'SET_BANNER']);
Expand Down
7 changes: 7 additions & 0 deletions app/actions/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ export function addSettings(settings) {
};
}

export function updateSettings(id, value) {
return {
type: SETTINGS.UPDATE,
payload: { id, value }
};
}

export function clearSettings() {
return {
type: SETTINGS.CLEAR
Expand Down
1 change: 1 addition & 0 deletions app/lib/methods/getSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export default async function() {
const filteredSettingsIds = filteredSettings.map(s => s._id);

reduxStore.dispatch(addSettings(this.parseSettings(filteredSettings)));
RocketChat.subscribe('stream-notify-all', 'public-settings-changed');

// filter server info
const serverInfo = filteredSettings.filter(i1 => serverInfoKeys.includes(i1._id));
Expand Down
32 changes: 32 additions & 0 deletions app/lib/rocketchat.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import EventEmitter from '../utils/events';
import { sanitizeLikeString } from './database/utils';
import { updatePermission } from '../actions/permissions';
import { TEAM_TYPE } from '../definition/ITeam';
import { updateSettings } from '../actions/settings';

const TOKEN_KEY = 'reactnativemeteor_usertoken';
const CURRENT_SERVER = 'currentServer';
Expand Down Expand Up @@ -226,6 +227,14 @@ const RocketChat = {
this.usersListener.then(this.stopListener);
}

if (this.notifyAllListener) {
this.notifyAllListener.then(this.stopListener);
}

if (this.rolesListener) {
this.rolesListener.then(this.stopListener);
}

if (this.notifyLoggedListener) {
this.notifyLoggedListener.then(this.stopListener);
}
Expand Down Expand Up @@ -275,6 +284,29 @@ const RocketChat = {

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

this.notifyAllListener = this.sdk.onStreamData('stream-notify-all', protectedFunction(async(ddpMessage) => {
const { eventName } = ddpMessage.fields;
if (/public-settings-changed/.test(eventName)) {
const { _id, value } = ddpMessage.fields.args[1];
const db = database.active;
const settingsCollection = db.get('settings');
try {
const settingsRecord = await settingsCollection.find(_id);
const { type } = defaultSettings[_id];
if (type) {
await db.action(async() => {
await settingsRecord.update((u) => {
u[type] = value;
});
});
}
reduxStore.dispatch(updateSettings(_id, value));
} catch (e) {
log(e);
}
}
}));

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

this.notifyLoggedListener = this.sdk.onStreamData('stream-notify-logged', protectedFunction(async(ddpMessage) => {
Expand Down
5 changes: 5 additions & 0 deletions app/reducers/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ export default (state = initialState, action) => {
...state,
...action.payload
};
case SETTINGS.UPDATE:
return {
...state,
[action.payload.id]: action.payload.value
};
case SETTINGS.CLEAR:
return initialState;
default:
Expand Down

0 comments on commit d547b61

Please sign in to comment.