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

Move rocketchat settings to specific package #13026

Merged
merged 3 commits into from
Jan 7, 2019
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
3 changes: 2 additions & 1 deletion .meteor/packages
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,5 @@ rocketchat:bigbluebutton
rocketchat:mailmessages
juliancwirko:postcss
littledata:synced-cron
rocketchat:utils
rocketchat:utils
rocketchat:settings
1 change: 1 addition & 0 deletions .meteor/versions
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ rocketchat:reactions@0.0.1
rocketchat:retention-policy@0.0.1
rocketchat:sandstorm@0.0.1
rocketchat:search@0.0.1
rocketchat:settings@0.0.1
rocketchat:setup-wizard@0.0.1
rocketchat:slackbridge@0.0.1
rocketchat:slashcommands-archive@0.0.1
Expand Down
102 changes: 2 additions & 100 deletions packages/rocketchat-lib/lib/settings.js
Original file line number Diff line number Diff line change
@@ -1,101 +1,3 @@
import { Meteor } from 'meteor/meteor';
import _ from 'underscore';
import { settings } from 'meteor/rocketchat:settings';

/*
* 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) => {
const value = Meteor.settings[key];
callback(key, value);
});
}
if (_.isRegExp(_id) && Meteor.settings) {
return Object.keys(Meteor.settings).forEach((key) => {
if (!_id.test(key)) {
return;
}
const value = Meteor.settings[key];
callback(key, value);
});
}
return Meteor.settings[_id] != null && callback(_id, Meteor.settings[_id]);
} 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,
});
}
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));
}
});
Object.keys(RocketChat.settings.regexCallbacks).forEach((cbKey) => {
const cbValue = RocketChat.settings.regexCallbacks[cbKey];
if (!cbValue.regex.test(key)) {
return;
}
cbValue.callbacks.forEach((callback) => callback(key, value, initialLoad));
});
},
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) => {
if (_.isRegExp(k)) {
RocketChat.settings.regexCallbacks[name = k.source] = RocketChat.settings.regexCallbacks[name = k.source] || {
regex: k,
callbacks: [],
};
RocketChat.settings.regexCallbacks[k.source].callbacks.push(callback);
} else {
RocketChat.settings.callbacks[k] = RocketChat.settings.callbacks[k] || [];
RocketChat.settings.callbacks[k].push(callback);
}
});
},
};
RocketChat.settings = settings;
1 change: 1 addition & 0 deletions packages/rocketchat-lib/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Package.onUse(function(api) {
api.use('rocketchat:version');
api.use('rocketchat:logger');
api.use('rocketchat:mailer');
api.use('rocketchat:settings');
api.use('mizzao:timesync');
api.use('rocketchat:custom-oauth');
api.use('konecty:multiple-instances-status');
Expand Down
5 changes: 5 additions & 0 deletions packages/rocketchat-settings/client/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { settings } from '../lib/settings';

export {
settings,
};
97 changes: 97 additions & 0 deletions packages/rocketchat-settings/lib/settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { Meteor } from 'meteor/meteor';
import _ from 'underscore';

export const settings = {
callbacks: {},
regexCallbacks: {},
ts: new Date,
get(_id, callback) {
if (callback != null) {
settings.onload(_id, callback);
if (!Meteor.settings) {
return;
}
if (_id === '*') {
return Object.keys(Meteor.settings).forEach((key) => {
const value = Meteor.settings[key];
callback(key, value);
});
}
if (_.isRegExp(_id) && Meteor.settings) {
return Object.keys(Meteor.settings).forEach((key) => {
if (!_id.test(key)) {
return;
}
const value = Meteor.settings[key];
callback(key, value);
});
}
return Meteor.settings[_id] != null && callback(_id, Meteor.settings[_id]);
} 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,
});
}
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 (settings.callbacks[item]) {
settings.callbacks[item].forEach((callback) => callback(key, value, initialLoad));
}
});
Object.keys(settings.regexCallbacks).forEach((cbKey) => {
const cbValue = settings.regexCallbacks[cbKey];
if (!cbValue.regex.test(key)) {
return;
}
cbValue.callbacks.forEach((callback) => callback(key, value, initialLoad));
});
},
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) => {
if (_.isRegExp(k)) {
settings.regexCallbacks[name = k.source] = settings.regexCallbacks[name = k.source] || {
regex: k,
callbacks: [],
};
settings.regexCallbacks[k.source].callbacks.push(callback);
} else {
settings.callbacks[k] = settings.callbacks[k] || [];
settings.callbacks[k].push(callback);
}
});
},
};
14 changes: 14 additions & 0 deletions packages/rocketchat-settings/package.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Package.describe({
name: 'rocketchat:settings',
version: '0.0.1',
summary: '',
git: '',
});

Package.onUse(function(api) {
api.use([
'ecmascript',
]);
api.mainModule('client/index.js', 'client');
api.mainModule('server/index.js', 'server');
});
5 changes: 5 additions & 0 deletions packages/rocketchat-settings/server/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { settings } from '../lib/settings';

export {
settings,
};