72 lines
No EOL
2.6 KiB
TypeScript
72 lines
No EOL
2.6 KiB
TypeScript
import { eq, ge, select } from "dblang";
|
|
import express, { Request, Response } from "express";
|
|
import { suspectRequest } from "../sys/bruteforce.js";
|
|
import { oConf } from "../sys/config.js";
|
|
import { accounts, db, rooms } from "../sys/db.js";
|
|
import { error } from "../sys/log.js";
|
|
import { getSettings, SETTINGS } from "../sys/settings.js";
|
|
|
|
var Methods: ([string, (req: Request, res: Response) => Promise<void>])[] = [
|
|
["users", async (req: Request, res: Response) => {
|
|
let resp = await select([accounts.name], accounts)
|
|
.where(eq(accounts.viewable, true))
|
|
.query(db);
|
|
res.json(resp.map(d => d[accounts.name]));
|
|
}],
|
|
["server/publicKey", async (req: Request, res: Response) => {
|
|
let publicKey = await getSettings(SETTINGS.publicKey);
|
|
let expires = await getSettings(SETTINGS.certExpires);
|
|
if (publicKey && expires) {
|
|
res.json({
|
|
publicKey,
|
|
expires
|
|
});
|
|
}
|
|
}],
|
|
["isUser/:user", async (req: suspectRequest, res: Response) => {
|
|
var user = req.params.user;
|
|
let data = await select([accounts.accID], accounts)
|
|
.where(eq(accounts.name, user))
|
|
.query(db);
|
|
if (data.length == 0) {
|
|
if (req.suspect) req.suspect();
|
|
res.send("error");
|
|
} else {
|
|
res.send("ok");
|
|
}
|
|
}],
|
|
["publicRooms", async (req: Request, res: Response) => {
|
|
let data = await select([rooms.name, rooms.title, rooms.description, rooms.icon], rooms)
|
|
.where(ge(rooms.public, 1))
|
|
.query(db);
|
|
var out = data.map(d => {
|
|
let name = d[rooms.name];
|
|
let title = d[rooms.title];
|
|
let description = d[rooms.description];
|
|
let icon = d[rooms.icon];
|
|
if (name != null && title != null && description != null && icon != null) {
|
|
return { name, title, description, icon };
|
|
}
|
|
return null;
|
|
});
|
|
res.json(out.filter(d => d != null));
|
|
}]
|
|
];
|
|
|
|
export const addGetMethods = (server: express.Express) => {
|
|
server.get("/.well-known/outbag/server", (req, res) => {
|
|
res.json({
|
|
port: oConf.get("System", "PORTexposed"),
|
|
path: oConf.get("System", "PATHexposed"),
|
|
})
|
|
});
|
|
Methods.forEach((d) => void server.get("/api/" + d[0], async (req, res) => {
|
|
try {
|
|
await d[1](req, res);
|
|
if (!res.headersSent) res.status(500).send();
|
|
} catch (e) {
|
|
error("API", "Get Request Error:", e);
|
|
res.status(500).send();
|
|
}
|
|
}));
|
|
} |