Dwelling/shared/util.js

66 lines
2.5 KiB
JavaScript

const moment = require("moment-timezone");
const date_formats = Object.freeze({
post_date: "Do MMMM YYYY HH:mm:ss z",
file_date: "YYYY-MM-DD HH:mm:ss z",
rss_date: "ddd, DD MMM YYYY HH:mm:ss ZZ",
id_date: "YYYYMMDD-HHmm" });
function datetime(date, format=date_formats.post_date, timezone="UTC") {
return moment.utc(date).tz(timezone).format(format);
}
function getClientTimezone(ctx) {
if (ctx.header['x-client-timezone'] !== undefined)
return ctx.header['x-client-timezone'];
else
return "UTC";
}
function getServiceByHost(host, service="") {
if (host.endsWith("onion"))
switch (service) {
case "radio": return "http://wsmkgnmhmzqm7kyzv7jnzzafvgm7xlmlfvzhgorpapd5or2arnhuktqd.onion";
case "files": return "http://qf5e43nlhvnrutmikuvbdfj3cmtthokpbaxtkm6mjlslttzvtgm4fxid.onion";
case "git": return "http://qqitm7qlsbbubwmjos4cqzmvkqidg34rfnbyhuydhalep33fbvh22xyd.onion";
default: return "http://moq7aejnf4xk5k2bkaltli3ftkhusy2mbrd3pj23nrca343ku2mgk4yd.onion";
}
else if (host.endsWith("i2p"))
switch (service) {
case "radio": return "http://radio.arav.i2p";
case "files": return "http://files.arav.i2p";
case "git": return "http://git.arav.i2p";
default: return "http://arav.i2p";
}
else if (host.startsWith("[300")) // Yggdrasil
switch (service) {
case "radio": return "http://[300:a98d:d6d0:8a08::e]";
case "files": return "http://[300:a98d:d6d0:8a08::d]";
case "git": return "http://[300:a98d:d6d0:8a08::b]";
default: return "http://[300:a98d:d6d0:8a08::f]";
}
else
switch (service) {
case "radio": return "https://radio.arav.top";
case "files": return "https://files.arav.top";
case "git": return "https://git.arav.top";
default: return "https://arav.top";
}
}
function rssLink(schemahost, date, category, timezone="UTC") {
return `${schemahost}/mindflow#${category}-${datetime(date, date_formats.id_date, timezone)}`;
}
// This code was taken from https://www.npmjs.com/package/mysql#custom-format
// Only fourth line was changed.
function mysqlQueryFormat(query, values) {
if (!values) return query;
return query.replace(/\:(\w+)/g, function (txt, key) {
if (values[key] !== undefined)
return this.escape(values[key]);
return txt;
}.bind(this));
};
module.exports = { date_formats, datetime, getClientTimezone, getServiceByHost, rssLink, mysqlQueryFormat }