The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Rocket.Chat/packages/rocketchat-lib/lib/settings.js

102 lines
2.9 KiB

import { Meteor } from 'meteor/meteor';
import _ from 'underscore';
9 years ago
/*
* RocketChat.settings holds all packages settings
* @namespace RocketChat.settings
*/
RocketChat.settings = {
callbacks: {},
regexCallbacks: {},
ts: new Date,
get(_id, callback) {
if (callback != null) {
RocketChat.settings.onload(_id, callback);
if (!Meteor.settings) {
return;
}
if (_id === '*') {
return Object.keys(Meteor.settings).forEach((key) => {
9 years ago
const value = Meteor.settings[key];
callback(key, value);
});
}
if (_.isRegExp(_id) && Meteor.settings) {
return Object.keys(Meteor.settings).forEach((key) => {
9 years ago
if (!_id.test(key)) {
return;
}
const value = Meteor.settings[key];
callback(key, value);
});
}
return Meteor.settings[_id] != null && callback(_id, Meteor.settings[_id]);
9 years ago
} else {
if (!Meteor.settings) {
return;
}
if (_.isRegExp(_id)) {
return Object.keys(Meteor.settings).reduce((items, key) => {
const value = Meteor.settings[key];
if (_id.test(key)) {
items.push({
key,
value,
9 years ago
});
}
return items;
}, []);
}
return Meteor.settings && Meteor.settings[_id];
}
},
set(_id, value, callback) {
return Meteor.call('saveSetting', _id, value, callback);
},
batchSet(settings, callback) {
// async -> sync
// http://daemon.co.za/2012/04/simple-async-with-only-underscore/
const save = function(setting) {
return function(callback) {
return Meteor.call('saveSetting', setting._id, setting.value, setting.editor, callback);
};
};
const actions = _.map(settings, (setting) => save(setting));
return _(actions).reduceRight(_.wrap, (err, success) => callback(err, success))();
},
load(key, value, initialLoad) {
['*', key].forEach((item) => {
if (RocketChat.settings.callbacks[item]) {
RocketChat.settings.callbacks[item].forEach((callback) => callback(key, value, initialLoad));
9 years ago
}
});
Object.keys(RocketChat.settings.regexCallbacks).forEach((cbKey) => {
9 years ago
const cbValue = RocketChat.settings.regexCallbacks[cbKey];
if (!cbValue.regex.test(key)) {
return;
}
cbValue.callbacks.forEach((callback) => callback(key, value, initialLoad));
9 years ago
});
},
onload(key, callback) {
// if key is '*'
// for key, value in Meteor.settings
// callback key, value, false
// else if Meteor.settings?[_id]?
// callback key, Meteor.settings[_id], false
const keys = [].concat(key);
keys.forEach((k) => {
9 years ago
if (_.isRegExp(k)) {
RocketChat.settings.regexCallbacks[name = k.source] = RocketChat.settings.regexCallbacks[name = k.source] || {
regex: k,
callbacks: [],
9 years ago
};
RocketChat.settings.regexCallbacks[k.source].callbacks.push(callback);
} else {
RocketChat.settings.callbacks[k] = RocketChat.settings.callbacks[k] || [];
RocketChat.settings.callbacks[k].push(callback);
}
});
},
9 years ago
};