Newer
Older
import { RateLimiter } from "../lib/RateLimiter";
import { prisma } from "../lib/prisma";
import { SocketServer } from "../lib/SocketServer";
import {
Instance,
InstanceNotBanned,
InstanceNotFound,
} from "../models/Instance";
app.use(RateLimiter.ADMIN);
app.use(async (req, res, next) => {
if (!req.session.user) {
res.status(401).json({
success: false,
error: "You are not logged in",
});
return;
}
const user = await User.fromAuthSession(req.session.user);
if (!user) {
res.status(400).json({
success: false,
error: "User data does not exist?",
});
return;
}
if (!user.isAdmin) {
res.status(403).json({
success: false,
error: "user is not admin",
});
return;
}
next();
});
app.get("/check", (req, res) => {
res.send({ success: true });
});
// TODO: Delete before merge
app.get("/log", (req, res) => {
const user = "grant@grants.cafe";
for (let i = 0; i < 100; i++) {
LogMan.log("pixel_place", user, { x: 0, y: 0, hex: "ABC123" });
LogMan.log("pixel_undo", user, { x: 0, y: 0, hex: "FFFFFF" });
LogMan.log("mod_fill", user, { from: [0, 0], to: [1, 1], hex: "000000" });
LogMan.log("mod_override", user, { x: 0, y: 0, hex: "111111" });
LogMan.log("mod_rollback", user, { x: 0, y: 0, hex: "222222" });
LogMan.log("mod_rollback_undo", user, { x: 0, y: 0, hex: "333333" });
LogMan.log("canvas_size", { width: 100, height: 100 });
LogMan.log("canvas_freeze", {});
LogMan.log("canvas_unfreeze", {});
}
res.send("ok");
});
app.get("/canvas/size", async (req, res) => {
const config = Canvas.getCanvasConfig();
res.json({
success: true,
size: {
width: config.size[0],
height: config.size[1],
},
});
});
/**
* Update canvas size
*
* @header X-Audit
* @body width number
* @body height number
*/
app.post("/canvas/size", async (req, res) => {
const width = parseInt(req.body.width || "-1");
const height = parseInt(req.body.height || "-1");
if (
isNaN(width) ||
isNaN(height) ||
width < 1 ||
height < 1 ||
width > 10000 ||
height > 10000
) {
res.status(400).json({ success: false, error: "what are you doing" });
return;
}
await Canvas.setSize(width, height);
// we log this here because Canvas#setSize is ran at launch
// this is currently the only way the size is changed is via the API
LogMan.log("canvas_size", { width, height });
const user = (await User.fromAuthSession(req.session.user!))!;
const auditLog = AuditLog.Factory(user.sub)
.doing("CANVAS_SIZE")
.reason(req.header("X-Audit") || null)
.withComment(`Changed canvas size to ${width}x${height}`)
.create();
/**
* Get canvas frozen status
*/
app.get("/canvas/freeze", async (req, res) => {
res.send({ success: true, frozen: Canvas.frozen });
});
/**
* Freeze the canvas
*
* @header X-Audit
*/
app.post("/canvas/freeze", async (req, res) => {
await Canvas.setFrozen(true);
// same reason as canvas size changes, we log this here because #setFrozen is ran at startup
LogMan.log("canvas_freeze", {});
const user = (await User.fromAuthSession(req.session.user!))!;
const auditLog = AuditLog.Factory(user.sub)
.doing("CANVAS_FREEZE")
.reason(req.header("X-Audit") || null)
.withComment(`Freezed the canvas`)
.create();
res.send({ success: true, auditLog });
});
/**
* Unfreeze the canvas
*
* @header X-Audit
*/
app.delete("/canvas/freeze", async (req, res) => {
await Canvas.setFrozen(false);
// same reason as canvas size changes, we log this here because #setFrozen is ran at startup
LogMan.log("canvas_unfreeze", {});
const user = (await User.fromAuthSession(req.session.user!))!;
const auditLog = AuditLog.Factory(user.sub)
.doing("CANVAS_UNFREEZE")
.reason(req.header("X-Audit") || null)
.withComment(`Un-Freezed the canvas`)
.create();
res.send({ success: true, auditLog });
});

Grant
committed
app.put("/canvas/heatmap", async (req, res) => {
try {
await Canvas.generateHeatmap();
res.send({ success: true });
} catch (e) {
Logger.error(e);
res.send({ success: false, error: "Failed to generate" });
}
});
app.post("/canvas/forceUpdateTop", async (req, res) => {
Logger.info("Starting force updating isTop");
await Canvas.forceUpdatePixelIsTop();
Logger.info("Finished force updating isTop");
res.send({ success: true });
});
app.get("/canvas/:x/:y", async (req, res) => {
const x = parseInt(req.params.x);
const y = parseInt(req.params.y);
res.json(await Canvas.getPixel(x, y));
});
app.post("/canvas/stress", async (req, res) => {
Loading
Loading full blame...