derv-net/lib.js

72 lines
No EOL
2.3 KiB
JavaScript

async function importRouters(app, routers) {
for (let router in routers) {
await app.use(router, (await import(routers[router])).default)
}
}
function apiStat(res, next, message, success = false, redirect = '/') {
res.api = { success, message, redirect };
next();
}
function splitUp(content) {
let out = content.split(/(\s+)/g);
for (let i in out) {
let res = out[i];
if (res.startsWith('file::')) {
out[i] = ['file', new URL(res.slice('file::'.length), 'https://tbg.dervland.net/').pathname , res.split('.').pop()];
continue;
}
if (res.startsWith('https://')) {
out[i] = ['link',res];
continue;
}
let urlThing = "";
if (res[0] == '@' || res[0] == '#') {
let type = res[0] == '@' ? '/users/' : '/walls/get/hub/';
let type2 = res[0] == '@' ? '' : '/0';
urlThing = new URL(type + res.slice(1) + type2, 'https://tbg.dervland.net/').pathname
}
if (res[0] == '@') {
out[i] = ['curl',res, urlThing];
continue;
}
if (res[0] == '#') {
out[i] = ['curl',res, urlThing];
continue;
}
out[i] = [res];
}
return out;
}
async function replyIterator(replies, db) {
for (let reply of replies) {
if (reply.childType == 'hub') {
reply.comment = { type: reply.childType, id: reply.childId, content: `The ${reply.childId} hub.`, username: false, date: undefined };
continue;
} else if (reply.childType == 'users') {
reply.comment = { type: reply.childType, id: reply.childId, content: `User ${reply.childId}'s wall.`, username: false, date: undefined };
continue;
}
let dat = await db.all('SELECT * FROM comment WHERE id = ?', [
reply.childId
]);
if (reply.childType != 'comment' || dat.length < 1) {
reply.comment = 'fail';
continue;
}
reply.comment = dat[0];
reply.comment.type = reply.childType;
//reply.comment.id = reply.childId;
}
for (let reply of replies) {
reply.comment.content = splitUp(reply.comment.content)
}
return replies;
}
export { importRouters, apiStat, replyIterator, splitUp };