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 settings #3222

Merged
merged 6 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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