chore: snapshot workshop v2 rewrite

This commit is contained in:
Thomas Camlong committed 2026-07-14 18:42:08 +02:00
1 parent aff5a84a1d
commit 6040d09630
34 files changed
+876 -910

No files matched your search

+36 -8
View File
@@ -20,12 +20,10 @@ function fail(ErrorType, _code, message) {
throw new ErrorType(message);
}
function requireWritableAccount(app, auth, posting) {
function requireWritableAccount(app, auth) {
if (!auth) fail(UnauthorizedError, "authentication_required", "Authentication is required");
const account = app.findRecordById("users", auth.id);
if (account.getString("state") === "disabled") fail(ForbiddenError, "account_disabled", "Account is disabled");
if (posting && account.getString("state") === "posting_banned")
fail(ForbiddenError, "posting_banned", "Account cannot publish submissions");
return account;
}
@@ -78,10 +76,31 @@ function snapshot(record) {
}
function requireStaff(app, auth) {
const account = requireWritableAccount(app, auth, false);
const role = account.getString("role");
if (role !== "moderator" && role !== "admin") fail(ForbiddenError, "forbidden", "Staff access is required");
return role;
requireWritableAccount(app, auth);
let staff;
try {
staff = app.findFirstRecordByFilter("workshop_staff", "user = {:user}", { user: auth.id });
} catch {
fail(ForbiddenError, "forbidden", "Staff access is required");
}
return staff.getString("role");
}
function setStaffRole(app, user, role) {
let staff = null;
try {
staff = app.findFirstRecordByFilter("workshop_staff", "user = {:user}", { user: user.id });
} catch {
// Members do not have a staff record.
}
if (role === "member") {
if (staff) app.delete(staff);
return;
}
if (!staff) staff = new Record(app.findCollectionByNameOrId("workshop_staff"));
staff.set("user", user.id);
staff.set("role", role);
app.save(staff);
}
function body(e) {
@@ -104,4 +123,13 @@ function audit(app, actor, action, targetType, targetId, reason, targetSnapshot)
app.save(record);
}
module.exports = { audit, body, fail, requireStaff, requireWritableAccount, snapshot, validateSubmission };
module.exports = {
audit,
body,
fail,
requireStaff,
requireWritableAccount,
setStaffRole,
snapshot,
validateSubmission,
};
+2 -39
View File
@@ -1,42 +1,5 @@
/// <reference path="../pb_data/types.d.ts" />
onBootstrap((e) => {
e.next();
const users = e.app.findCollectionByNameOrId("users");
const clientId = $os.getenv("GITHUB_CLIENT_ID");
const clientSecret = $os.getenv("GITHUB_CLIENT_SECRET");
users.oauth2.enabled = Boolean(clientId && clientSecret);
users.oauth2.providers = clientId && clientSecret ? [{ name: "github", clientId, clientSecret }] : [];
e.app.save(users);
});
onRecordAuthWithOAuth2Request((e) => {
const utils = require(`${__hooks}/workshop-utils.js`);
if (e.record && e.record.getString("state") === "disabled")
utils.fail(ForbiddenError, "account_disabled", "Account is disabled");
if (e.isNewRecord) {
e.createData.displayName = e.oAuth2User.name || e.oAuth2User.username || "Community member";
e.createData.avatarUrl = e.oAuth2User.avatarUrl || "";
e.createData.role = "member";
e.createData.state = "active";
}
e.next();
}, "users");
onRecordAuthRequest((e) => {
const utils = require(`${__hooks}/workshop-utils.js`);
if (e.record.getString("state") === "disabled") utils.fail(ForbiddenError, "account_disabled", "Account is disabled");
e.next();
}, "users");
onRecordUpdateRequest((e) => {
const original = e.app.findRecordById("users", e.record.id);
e.record.set("role", original.get("role"));
e.record.set("state", original.get("state"));
e.record.set("moderationReason", original.get("moderationReason"));
e.next();
}, "users");
onRecordCreateRequest((e) => {
const utils = require(`${__hooks}/workshop-utils.js`);
const account = utils.requireWritableAccount(e.app, e.auth, true);
@@ -116,8 +79,7 @@ routerAdd(
const utils = require(`${__hooks}/workshop-utils.js`);
const actorRole = utils.requireStaff(e.app, e.auth);
const data = utils.body(e);
if (!["active", "posting_banned", "disabled"].includes(data.state))
throw new BadRequestError("Invalid account state");
if (!["active", "disabled"].includes(data.state)) throw new BadRequestError("Invalid account state");
const id = e.request.pathValue("id");
if (id === e.auth.id) throw new BadRequestError("You cannot moderate your own account");
e.app.runInTransaction((tx) => {
@@ -149,6 +111,7 @@ routerAdd(
const previous = utils.snapshot(target);
target.set("role", data.role);
tx.save(target);
utils.setStaffRole(tx, target, data.role);
utils.audit(tx, e.auth, "set_role", "user", id, data.reason, previous);
});
return e.json(200, { success: true });