From d547b6129fd1ccf9f1cf5f76c61db988c23fb72e Mon Sep 17 00:00:00 2001 From: Gerzon Z Date: Thu, 1 Jul 2021 15:30:55 -0400 Subject: [PATCH] [IMPROVE] Subscribe to settings (#3222) * 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 --- app/actions/actionsTypes.js | 2 +- app/actions/settings.js | 7 +++++++ app/lib/methods/getSettings.js | 1 + app/lib/rocketchat.js | 32 ++++++++++++++++++++++++++++++++ app/reducers/settings.js | 5 +++++ 5 files changed, 46 insertions(+), 1 deletion(-) diff --git a/app/actions/actionsTypes.js b/app/actions/actionsTypes.js index 7bba8309..0790b7ba 100644 --- a/app/actions/actionsTypes.js +++ b/app/actions/actionsTypes.js @@ -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']); diff --git a/app/actions/settings.js b/app/actions/settings.js index 381958c5..6fae375b 100644 --- a/app/actions/settings.js +++ b/app/actions/settings.js @@ -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 diff --git a/app/lib/methods/getSettings.js b/app/lib/methods/getSettings.js index 6935631e..f68995af 100644 --- a/app/lib/methods/getSettings.js +++ b/app/lib/methods/getSettings.js @@ -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)); diff --git a/app/lib/rocketchat.js b/app/lib/rocketchat.js index 59b85b73..dcb3358c 100644 --- a/app/lib/rocketchat.js +++ b/app/lib/rocketchat.js @@ -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'; @@ -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); } @@ -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) => { diff --git a/app/reducers/settings.js b/app/reducers/settings.js index 9e037b1d..6e9ab500 100644 --- a/app/reducers/settings.js +++ b/app/reducers/settings.js @@ -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: