diff --git a/data/newArticles.yaml b/data/newArticles.yaml new file mode 100644 index 00000000..98e16dc7 --- /dev/null +++ b/data/newArticles.yaml @@ -0,0 +1,6 @@ +Calendar: + categoryEN: "Upcoming Events" + title: "#BlogName# Upcoming Events" +Picture: + categoryEN: "Picture" + title: "#BlogName# Picture" diff --git a/model/blog.js b/model/blog.js index 866f2908..fa5dcff9 100644 --- a/model/blog.js +++ b/model/blog.js @@ -379,30 +379,30 @@ function createNewBlog(user, proto, noArticle, callback) { // create an Empty blog and simualte an id != 0 const emptyBlog = exports.create(); emptyBlog.id = -1; + const newArticles = configModule.getConfig("newArticles"); - async.series([ - function createCalendar(cb) { + async.eachOf(newArticles, + function createArticle(value, key, cb) { if (noArticle) return cb(); - articleModule.createNewArticle({ blog: blog.name, categoryEN: "Upcoming Events", title: blog.name + " Upcoming Events" }, cb); + const newArticle = { blog: blog.name }; + for (const k in value) { + newArticle[k] = value[k].replaceAll("#BlogName#", blog.name); + } + articleModule.createNewArticle(newArticle, cb); }, - function createCalendar(cb) { - if (noArticle) return cb(); - articleModule.createNewArticle({ blog: blog.name, categoryEN: "Picture", title: blog.name + " Picture" }, cb); - } - ], - function finalFunction(err) { - if (err) return callback(err); - blog.save(function feedback(err, savedblog) { + function finalFunction(err) { if (err) return callback(err); - emptyBlog.id = savedblog.id; - messageCenter.global.updateBlog(user, emptyBlog, change, function(err) { - if (err) { - return callback(err); - } - return callback(null, savedblog); + blog.save(function feedback(err, savedblog) { + if (err) return callback(err); + emptyBlog.id = savedblog.id; + messageCenter.global.updateBlog(user, emptyBlog, change, function(err) { + if (err) { + return callback(err); + } + return callback(null, savedblog); + }); }); }); - }); }); } if (callback) { diff --git a/model/config.js b/model/config.js index 9a5b46ba..c166bb4c 100644 --- a/model/config.js +++ b/model/config.js @@ -124,6 +124,7 @@ Config.prototype.getJSON = function getJSON() { this.json = yaml.load(this.yaml); if (this.name === "votes") this.json = freshupVotes(this.json); if (this.name === "languageflags") this.json = freshupEmoji(this.json); + checkAndRepair[this.name](this); return this.json; } catch (err) { return { error: "YAML convert error for: " + this.name + " ", errorMessage: err }; @@ -282,69 +283,85 @@ const checkAndRepair = { let v = c.getJSON(); if (!v) v = []; c.json = v; + }, + newArticles: function(c) { + let v = c.getJSON(); + if (!v) v = {}; + for (const k in v) { + for (const k2 in v[k]) { + if (["collection", "title", "categoryEN"].indexOf(k2) >= 0) continue; + delete v[k][k2]; + } + } + c.json = v; } }; Config.prototype.setAndSave = function setAndSave(user, data, callback) { - debug("setAndSave"); - - util.requireTypes([user, data, callback], ["object", "object", "function"]); - const self = this; + function _configSetAndSave(user, data, callback) { + debug("setAndSave"); + util.requireTypes([user, data, callback], ["object", "object", "function"]); + // try to convert YAML if necessary + + delete self.json; + + + async.eachOf(data, function setAndSaveEachOf(value, key, cbEachOf) { + // There is no Value for the key, so do nothing + if (typeof (value) === "undefined") return cbEachOf(); + + // The Value to be set, is the same then in the object itself + // so do nothing + if (value === self[key]) return cbEachOf(); + if (JSON.stringify(value) === JSON.stringify(self[key])) return cbEachOf(); + if (typeof (self[key]) === "undefined" && value === "") return cbEachOf(); + + + debug("Set Key %s to value >>%s<<", key, value); + debug("Old Value Was >>%s<<", self[key]); + + + async.series([ + function calculateID(cb) { + if (self.id !== 0) return cb(); + self.save(cb); + }, + function(cb) { + // do not log validation key in logfile + const toValue = value; + + messageCenter.global.sendInfo({ oid: self.id, user: user.OSMUser, table: "config", property: key, from: self[key], to: toValue }, cb); + }, + function(cb) { + self[key] = value; + cb(); + } + ], function(err) { + cbEachOf(err); + }); + }, function setAndSaveFinalCB(err) { + debug("setAndSaveFinalCB"); + if (err) return callback(err); + checkAndRepair[self.name](self); + if (self.json && self.json.error) { + return callback(new Error(self.json.errorMessage)); + } + actualiseConfigMap(self); - // try to convert YAML if necessary - - delete self.json; - - - async.eachOf(data, function setAndSaveEachOf(value, key, cbEachOf) { - // There is no Value for the key, so do nothing - if (typeof (value) === "undefined") return cbEachOf(); - - // The Value to be set, is the same then in the object itself - // so do nothing - if (value === self[key]) return cbEachOf(); - if (JSON.stringify(value) === JSON.stringify(self[key])) return cbEachOf(); - if (typeof (self[key]) === "undefined" && value === "") return cbEachOf(); - - - debug("Set Key %s to value >>%s<<", key, value); - debug("Old Value Was >>%s<<", self[key]); - - - async.series([ - function calculateID(cb) { - if (self.id !== 0) return cb(); - self.save(cb); - }, - function(cb) { - // do not log validation key in logfile - const toValue = value; - - messageCenter.global.sendInfo({ oid: self.id, user: user.OSMUser, table: "config", property: key, from: self[key], to: toValue }, cb); - }, - function(cb) { - self[key] = value; - cb(); + if (self.name === "slacknotification") { + // Reinitialise Slack Receiver if something is changed on slack notification. + slackReceiver.initialise(); } - ], function(err) { - cbEachOf(err); + self.save(callback); }); - }, function setAndSaveFinalCB(err) { - debug("setAndSaveFinalCB"); - if (err) return callback(err); - checkAndRepair[self.name](self); - if (self.json && self.json.error) { - return callback(new Error(self.json.errorMessage)); - } - actualiseConfigMap(self); - - if (self.name === "slacknotification") { - // Reinitialise Slack Receiver if something is changed on slack notification. - slackReceiver.initialise(); - } - self.save(callback); + } + if (callback) { + return _configSetAndSave(user, data, callback); + } + return new Promise((resolve, reject) => { + _configSetAndSave(user, data, (err) => { (err) ? reject(err) : resolve(); }); }); }; @@ -408,7 +425,8 @@ function initialise(callback) { initConfigElement.bind(null, "automatictranslatetext"), initConfigElement.bind(null, "votes"), initConfigElement.bind(null, "eventsfilter"), - initConfigElement.bind(null, "ignoreforsearch") + initConfigElement.bind(null, "ignoreforsearch"), + initConfigElement.bind(null, "newArticles") ], function final(err) { debug("finalFunction initialise"); @@ -432,23 +450,31 @@ module.exports.getConfig = function(text) { }; module.exports.getConfigObject = function(text, callback) { - debug("exports.getConfigObject"); - let config = null; - assert(configMap); - if (configMap[text]) { - config = configMap[text]; - } else { - for (const key in configMap) { - const c = configMap[key]; - if (c.id === text) { - config = c; - break; + function _getConfigObject(text, callback) { + debug("exports.getConfigObject"); + let config = null; + assert(configMap); + if (configMap[text]) { + config = configMap[text]; + } else { + for (const key in configMap) { + const c = configMap[key]; + if (c.id === text) { + config = c; + break; + } } } + assert(config); + if (callback) return callback(null, config); + return config; } - assert(config); - if (callback) return callback(null, config); - return config; + if (callback) { + return _getConfigObject(text, callback); + } + return new Promise((resolve, reject) => { + _getConfigObject(text, (err, result) => (err) ? reject(err) : resolve(result)); + }); }; module.exports.initialise = initialise; diff --git a/model/logModule.js b/model/logModule.js index cc99272a..4cf69f4d 100644 --- a/model/logModule.js +++ b/model/logModule.js @@ -34,8 +34,17 @@ module.exports.log = function log(object, callback) { module.exports.find = function(obj1, obj2, callback) { - debug("find"); - pgMap.find(this, obj1, obj2, callback); + const self = this; + function _find(obj1, obj2, callback) { + debug("find"); + pgMap.find(self, obj1, obj2, callback); + } + if (callback) { + return _find(obj1, obj2, callback); + } + return new Promise((resolve, reject) => { + _find(obj1, obj2, (err, result) => (err) ? reject(err) : resolve(result)); + }); }; module.exports.findById = function(id, callback) { debug("findById"); diff --git a/notification/slackReceiver.js b/notification/slackReceiver.js index 948c24af..c45bd3f5 100644 --- a/notification/slackReceiver.js +++ b/notification/slackReceiver.js @@ -247,21 +247,24 @@ function initialise(callback) { debug("initialise"); slackhook = config.getValue("slacktool"); messageCenter.initialise(); - const channelList = configModule.getConfigObject("slacknotification").getJSON(); - - channelReceiverMap = {}; - for (let i = 0; i < channelList.length; i++) { - const channel = channelList[i]; - if (channel.channel.substring(0, 1) !== "#") continue; - channelReceiverMap["Slack Connection " + i] = new ConfigFilter(channel, new SlackReceiver(channel.slack + channel.channel, channel.slack, channel.channel)); - } - iteratorReceiver.receiverMap = channelReceiverMap; - assert(messageCenter.global); - if (!registered) { - messageCenter.global.registerReceiver(iteratorReceiver); - registered = true; - } - if (callback) return callback(); + configModule.getConfigObject("slacknotification", function(err, slackConfig) { + if (err) return callback(err); + const channelList = slackConfig.getJSON(); + + channelReceiverMap = {}; + for (let i = 0; i < channelList.length; i++) { + const channel = channelList[i]; + if (channel.channel.substring(0, 1) !== "#") continue; + channelReceiverMap["Slack Connection " + i] = new ConfigFilter(channel, new SlackReceiver(channel.slack + channel.channel, channel.slack, channel.channel)); + } + iteratorReceiver.receiverMap = channelReceiverMap; + assert(messageCenter.global); + if (!registered) { + messageCenter.global.registerReceiver(iteratorReceiver); + registered = true; + } + if (callback) return callback(); + }); } diff --git a/routes/config.js b/routes/config.js index be57bf33..23def839 100644 --- a/routes/config.js +++ b/routes/config.js @@ -60,6 +60,7 @@ function renderConfigName(req, res, next) { if (name === "languageflags") jadeFile = "calendarflags"; if (name === "calendartranslation") jadeFile = name; if (name === "editorstrings") jadeFile = name; + if (name === "newArticles") jadeFile = "config"; if (name === "categorytranslation") jadeFile = name; if (name === "automatictranslatetext") jadeFile = name; if (name === "slacknotification") jadeFile = name; diff --git a/test/index/admin_home.html b/test/index/admin_home.html index 46221043..e97aa36d 100644 --- a/test/index/admin_home.html +++ b/test/index/admin_home.html @@ -60,6 +60,7 @@