Skip to content

Commit

Permalink
Configuration of empty articles for blog creation. Fix #960
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFive committed Dec 4, 2022
1 parent 5a8d09b commit 93b4273
Show file tree
Hide file tree
Showing 11 changed files with 220 additions and 139 deletions.
6 changes: 6 additions & 0 deletions data/newArticles.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Calendar:
categoryEN: "Upcoming Events"
title: "#BlogName# Upcoming Events"
Picture:
categoryEN: "Picture"
title: "#BlogName# Picture"
36 changes: 18 additions & 18 deletions model/blog.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
164 changes: 95 additions & 69 deletions model/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down Expand Up @@ -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(); });
});
};

Expand Down Expand Up @@ -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");
Expand All @@ -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;
Expand Down
13 changes: 11 additions & 2 deletions model/logModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
33 changes: 18 additions & 15 deletions notification/slackReceiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}


Expand Down
1 change: 1 addition & 0 deletions routes/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions test/index/admin_home.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ <h3>Pictures</h3>
<h3>Blog</h3>
<p><a href="/config/editorstrings">Author Documentation</a></p>
<p><a href="/config/categorytranslation">Category Translation</a></p>
<p><a href="/config/newArticles">Empty Articles To Create</a></p>
<h3>Article</h3>
<p><a href="/config/automatictranslatetext">Text for Translate Links</a></p>
<p><a href="/config/ignoreforsearch">Not a Doublette Link</a></p>
Expand Down
Loading

0 comments on commit 93b4273

Please sign in to comment.