Files
Docwell/Database/pb_hooks/docwell.pb.js
T
tajniak81andClaude Opus 5.5 e33a99241e Add instance settings and mail delivery through PocketBase
Superadmins can now change signup rules (on/off and the domain allow-list)
and branding at runtime; the environment variables only seed the defaults.
Mail and S3 storage settings pass through to PocketBase's own settings, and
docwell-server sends its own emails through a PocketBase hook so there is a
single SMTP configuration. The boot hook no longer wipes SMTP settings that
were not set from the environment.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 16:22:53 +01:00

81 lines
2.7 KiB
JavaScript

// SPDX-License-Identifier: AGPL-3.0-or-later
/// <reference path="../pb_data/types.d.ts" />
// Token check used by docwell-server: returns the "users" record that owns the
// bearer token (with its organization expanded), or 401. Unlike auth-refresh,
// it does not issue a new token.
routerAdd(
"GET",
"/api/docwell/auth/verify",
(e) => {
e.auth.ignoreEmailVisibility(true);
e.app.expandRecord(e.auth, ["organization"], null);
return e.json(200, e.auth);
},
$apis.requireAuth("users"),
);
// Sends one email through PocketBase's SMTP settings, so docwell-server's own
// emails (invites, test mails) use the same mail configuration as the auth
// emails PocketBase sends itself.
routerAdd(
"POST",
"/api/docwell/mail",
(e) => {
const body = e.requestInfo().body;
const to = String(body.to || "").trim();
const subject = String(body.subject || "").trim();
if (!to || !subject) {
throw new BadRequestError("to and subject are required");
}
const meta = e.app.settings().meta;
const message = new MailerMessage({
from: { address: meta.senderAddress, name: meta.senderName },
to: [{ address: to }],
subject: subject,
html: String(body.html || ""),
text: String(body.text || ""),
});
e.app.newMailClient().send(message);
return e.noContent(204);
},
$apis.requireSuperuserAuth(),
);
// Settings that come from the environment are applied on every start, so
// changing an env var and restarting is enough. Settings without an env var
// are left alone: the admin panel manages them.
onBootstrap((e) => {
e.next();
const env = (key) => $os.getenv(key) || "";
const settings = e.app.settings();
if (env("DOCWELL_APP_NAME")) {
settings.meta.appName = env("DOCWELL_APP_NAME");
} else if (settings.meta.appName === "Acme") {
// PocketBase's placeholder name on a fresh database.
settings.meta.appName = "Docwell";
}
settings.meta.appURL = env("DOCWELL_WEB_URL") || "http://localhost:3000";
settings.meta.senderName = settings.meta.appName;
if (env("DOCWELL_MAIL_FROM")) {
settings.meta.senderAddress = env("DOCWELL_MAIL_FROM");
} else if (settings.meta.senderAddress === "support@example.com") {
settings.meta.senderAddress = "no-reply@docwell.localhost";
}
const smtpHost = env("DOCWELL_SMTP_HOST");
if (smtpHost) {
settings.smtp.enabled = true;
settings.smtp.host = smtpHost;
settings.smtp.port = parseInt(env("DOCWELL_SMTP_PORT") || "587", 10);
settings.smtp.username = env("DOCWELL_SMTP_USERNAME");
settings.smtp.password = env("DOCWELL_SMTP_PASSWORD");
settings.smtp.tls = env("DOCWELL_SMTP_TLS") === "true";
}
e.app.save(settings);
});