~ knotless Search/
↑↓ move↵ open⇧↵ all results

zat.dev/zds

atproto pds · mirror of https://tangled.org/zat.dev/zds

0
0
81 lines · 3.8 KB Raw History
const std = @import("std");
const build_options = @import("build_options");
const http_api = @import("../http/api.zig");
const icons = @import("web/icons.zig");
const skin = @import("web/skin.zig");

const http = std.http;

/// Serves the resident account page. The whole document is built at compile
/// time, once per skin (with and without the served font face); a request
/// only picks the selected copy.
pub fn page(request: *http_api.Request) !void {
    switch (skin.current()) {
        inline else => |selected| {
            const body = if (skin.fontServed()) comptime render(selected, true) else comptime render(selected, false);
            try http_api.respond(request, .ok, body, &html_headers);
        },
    }
}

pub fn redirectToSecurity(request: *http_api.Request) !void {
    try redirect(request, "/account/manage");
}

pub fn redirectToSpaces(request: *http_api.Request) !void {
    try redirect(request, "/account/spaces");
}

fn redirect(request: *http_api.Request, location: []const u8) !void {
    const headers = [_]http.Header{
        .{ .name = "location", .value = location },
        .{ .name = "access-control-allow-origin", .value = "*" },
        .{ .name = "connection", .value = "close" },
    };
    try http_api.respondNowClose(request, .see_other, "", &headers);
}

// Presentation stays in replaceable static assets under web/, outside account
// and protocol logic: the page calls existing XRPCs and nothing else.
const template = @embedFile("web/account.html");
const shared_head = "<script>" ++ @embedFile("web/appearance.js") ++ "</script><style>" ++ @embedFile("web/appearance.css") ++ "</style>";
const license = @embedFile("web/lucide-license.html");

fn render(comptime selected: skin.Skin, comptime with_font: bool) []const u8 {
    comptime {
        @setEvalBranchQuota(1_000_000);
        const head_end = std.mem.indexOf(u8, template, "</head>").?;
        const body_end = std.mem.lastIndexOf(u8, template, "</body>").?;
        const version_at = std.mem.indexOf(u8, template, "{{version}}").?;
        const page_html = template[0..head_end] ++ shared_head ++ skin.accountStyleFor(selected, with_font) ++
            template[head_end..version_at] ++ build_options.version ++ template[version_at + "{{version}}".len .. body_end] ++
            license ++ drawings ++ skin.scriptFor(selected) ++ template[body_end..];
        return page_html;
    }
}

/// Operator drawings replace default icons by slot; empty in normal builds.
const drawings = if (icons.has_drawings) "<style>" ++ icons.style ++ "</style><script>" ++ icons.script ++ "</script>" else "";

const html_headers = [_]http.Header{
    .{ .name = "content-type", .value = "text/html; charset=utf-8" },
    .{ .name = "cache-control", .value = "no-store" },
    .{ .name = "access-control-allow-origin", .value = "*" },
    .{ .name = "access-control-allow-private-network", .value = "true" },
    .{ .name = "connection", .value = "close" },
} ++ http_api.htmlSecurityHeaders(.{
    .scripts = http_api.inlineCode(template ++ shared_head).scripts ++ .{icons.script} ++ skin.policy_scripts,
    .styles = http_api.inlineCode(template ++ shared_head).styles ++ .{icons.style} ++ skin.account_policy_styles,
});

test "the account page carries the build version and every skin's copy fits its policy" {
    inline for (comptime std.enums.values(skin.Skin)) |selected| {
        inline for (.{ false, true }) |with_font| {
            const body = comptime render(selected, with_font);
            try std.testing.expect(std.mem.indexOf(u8, body, "zds " ++ build_options.version ++ ",") != null);
            try std.testing.expect(std.mem.indexOf(u8, body, "{{") == null);
            try std.testing.expect(std.mem.indexOf(u8, body, "https://tangled.org/zat.dev/zds") != null);
            try std.testing.expect(std.mem.indexOf(u8, body, "ISC License") != null);
        }
    }
}