[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 <diegolmello@gmail.com>
This commit is contained in:
parent
88191b965a
commit
d547b6129f
|
@ -66,7 +66,7 @@ export const INVITE_LINKS = createRequestTypes('INVITE_LINKS', [
|
||||||
'CLEAR',
|
'CLEAR',
|
||||||
...defaultTypes
|
...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 APP_STATE = createRequestTypes('APP_STATE', ['FOREGROUND', 'BACKGROUND']);
|
||||||
export const ENTERPRISE_MODULES = createRequestTypes('ENTERPRISE_MODULES', ['CLEAR', 'SET']);
|
export const ENTERPRISE_MODULES = createRequestTypes('ENTERPRISE_MODULES', ['CLEAR', 'SET']);
|
||||||
export const ENCRYPTION = createRequestTypes('ENCRYPTION', ['INIT', 'STOP', 'DECODE_KEY', 'SET', 'SET_BANNER']);
|
export const ENCRYPTION = createRequestTypes('ENCRYPTION', ['INIT', 'STOP', 'DECODE_KEY', 'SET', 'SET_BANNER']);
|
||||||
|
|
|
@ -7,6 +7,13 @@ export function addSettings(settings) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function updateSettings(id, value) {
|
||||||
|
return {
|
||||||
|
type: SETTINGS.UPDATE,
|
||||||
|
payload: { id, value }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export function clearSettings() {
|
export function clearSettings() {
|
||||||
return {
|
return {
|
||||||
type: SETTINGS.CLEAR
|
type: SETTINGS.CLEAR
|
||||||
|
|
|
@ -146,6 +146,7 @@ export default async function() {
|
||||||
const filteredSettingsIds = filteredSettings.map(s => s._id);
|
const filteredSettingsIds = filteredSettings.map(s => s._id);
|
||||||
|
|
||||||
reduxStore.dispatch(addSettings(this.parseSettings(filteredSettings)));
|
reduxStore.dispatch(addSettings(this.parseSettings(filteredSettings)));
|
||||||
|
RocketChat.subscribe('stream-notify-all', 'public-settings-changed');
|
||||||
|
|
||||||
// filter server info
|
// filter server info
|
||||||
const serverInfo = filteredSettings.filter(i1 => serverInfoKeys.includes(i1._id));
|
const serverInfo = filteredSettings.filter(i1 => serverInfoKeys.includes(i1._id));
|
||||||
|
|
|
@ -65,6 +65,7 @@ import EventEmitter from '../utils/events';
|
||||||
import { sanitizeLikeString } from './database/utils';
|
import { sanitizeLikeString } from './database/utils';
|
||||||
import { updatePermission } from '../actions/permissions';
|
import { updatePermission } from '../actions/permissions';
|
||||||
import { TEAM_TYPE } from '../definition/ITeam';
|
import { TEAM_TYPE } from '../definition/ITeam';
|
||||||
|
import { updateSettings } from '../actions/settings';
|
||||||
|
|
||||||
const TOKEN_KEY = 'reactnativemeteor_usertoken';
|
const TOKEN_KEY = 'reactnativemeteor_usertoken';
|
||||||
const CURRENT_SERVER = 'currentServer';
|
const CURRENT_SERVER = 'currentServer';
|
||||||
|
@ -226,6 +227,14 @@ const RocketChat = {
|
||||||
this.usersListener.then(this.stopListener);
|
this.usersListener.then(this.stopListener);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.notifyAllListener) {
|
||||||
|
this.notifyAllListener.then(this.stopListener);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.rolesListener) {
|
||||||
|
this.rolesListener.then(this.stopListener);
|
||||||
|
}
|
||||||
|
|
||||||
if (this.notifyLoggedListener) {
|
if (this.notifyLoggedListener) {
|
||||||
this.notifyLoggedListener.then(this.stopListener);
|
this.notifyLoggedListener.then(this.stopListener);
|
||||||
}
|
}
|
||||||
|
@ -275,6 +284,29 @@ const RocketChat = {
|
||||||
|
|
||||||
this.usersListener = this.sdk.onStreamData('users', protectedFunction(ddpMessage => RocketChat._setUser(ddpMessage)));
|
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.rolesListener = this.sdk.onStreamData('stream-roles', protectedFunction(ddpMessage => onRolesChanged(ddpMessage)));
|
||||||
|
|
||||||
this.notifyLoggedListener = this.sdk.onStreamData('stream-notify-logged', protectedFunction(async(ddpMessage) => {
|
this.notifyLoggedListener = this.sdk.onStreamData('stream-notify-logged', protectedFunction(async(ddpMessage) => {
|
||||||
|
|
|
@ -9,6 +9,11 @@ export default (state = initialState, action) => {
|
||||||
...state,
|
...state,
|
||||||
...action.payload
|
...action.payload
|
||||||
};
|
};
|
||||||
|
case SETTINGS.UPDATE:
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
[action.payload.id]: action.payload.value
|
||||||
|
};
|
||||||
case SETTINGS.CLEAR:
|
case SETTINGS.CLEAR:
|
||||||
return initialState;
|
return initialState;
|
||||||
default:
|
default:
|
||||||
|
|
Loading…
Reference in New Issue