simplify workshop submission storage
This commit is contained in:
1 parent
6f957ee984
commit
98d56264da
22 files changed
+52
-29625
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -1,11 +0,0 @@
|
||||
const { validatePocketBaseWidget } = require(`${__hooks}/widget-validator.bundle.cjs`);
|
||||
|
||||
const validateWidgetManifest = (content) => {
|
||||
const result = validatePocketBaseWidget(content);
|
||||
if (!result.success) {
|
||||
throw new BadRequestError(result.error);
|
||||
}
|
||||
return result.content;
|
||||
};
|
||||
|
||||
module.exports = { validateWidgetManifest };
|
||||
@@ -1,35 +1,19 @@
|
||||
const CUSTOM_WIDGET_SCHEMA = "homarr-custom-widget-v2";
|
||||
const CUSTOM_CSS_SCHEMA = "homarr-custom-css-v1";
|
||||
const MAX_CSS_LENGTH = 16_384;
|
||||
const MAX_CONTENT_LENGTH = 1_000_000;
|
||||
const GITHUB_USERNAME_PATTERN = /^[A-Za-z0-9](?:[A-Za-z0-9-]{0,37}[A-Za-z0-9])?$/;
|
||||
|
||||
const rejectRequest = (message) => {
|
||||
throw new BadRequestError(message);
|
||||
};
|
||||
|
||||
const validateAndNormalizeSubmission = (record) => {
|
||||
const type = record.getString("type");
|
||||
const content = record.getString("content");
|
||||
if (!content || content.length > MAX_CONTENT_LENGTH) rejectRequest("Submission content is invalid");
|
||||
record.set("title", record.getString("title").trim());
|
||||
record.set("description", record.getString("description").trim());
|
||||
record.set("changelog", record.getString("changelog").trim());
|
||||
if (type === "customCss") {
|
||||
if (!content.trim() || content.length > MAX_CSS_LENGTH) rejectRequest("Custom CSS is empty or too large");
|
||||
record.set("widgetSchema", CUSTOM_CSS_SCHEMA);
|
||||
return;
|
||||
}
|
||||
if (type !== "customWidget") rejectRequest("Submission type is invalid");
|
||||
const { validateWidgetManifest } = require(`${__hooks}/widget-validator.js`);
|
||||
record.set("content", validateWidgetManifest(content));
|
||||
record.set("widgetSchema", CUSTOM_WIDGET_SCHEMA);
|
||||
};
|
||||
|
||||
const normalizedIdentityText = (...values) => {
|
||||
for (const value of values) {
|
||||
if (typeof value !== "string") continue;
|
||||
const normalized = value.replace(/[\u0000-\u001f\u007f]/g, "").trim();
|
||||
const normalized = [...value]
|
||||
.filter((character) => {
|
||||
const code = character.codePointAt(0) || 0;
|
||||
return code > 31 && code !== 127;
|
||||
})
|
||||
.join("")
|
||||
.trim();
|
||||
if (normalized) return normalized;
|
||||
}
|
||||
return "";
|
||||
@@ -91,5 +75,4 @@ module.exports = {
|
||||
escapeHtml,
|
||||
rejectRequest,
|
||||
sendEmail,
|
||||
validateAndNormalizeSubmission,
|
||||
};
|
||||
@@ -68,20 +68,6 @@ onRecordUpdateRequest((event) => {
|
||||
}, "users");
|
||||
|
||||
onRecordCreateRequest((event) => {
|
||||
try {
|
||||
const { validateAndNormalizeSubmission } = require(`${__hooks}/workshop-utils.js`);
|
||||
validateAndNormalizeSubmission(event.record);
|
||||
} catch (error) {
|
||||
console.log(
|
||||
JSON.stringify({
|
||||
event: "workshop_submission_rejected",
|
||||
operation: "create",
|
||||
type: event.record.getString("type"),
|
||||
errorName: error && error.name ? error.name : "ValidationError",
|
||||
}),
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
event.record.set("revision", 1);
|
||||
event.record.set("expectedRevision", 0);
|
||||
event.record.set("changelog", "");
|
||||
@@ -90,27 +76,13 @@ onRecordCreateRequest((event) => {
|
||||
}, "submissions");
|
||||
|
||||
onRecordUpdateRequest((event) => {
|
||||
const { rejectRequest, validateAndNormalizeSubmission } = require(`${__hooks}/workshop-utils.js`);
|
||||
const { rejectRequest } = require(`${__hooks}/workshop-utils.js`);
|
||||
const original = event.record.original();
|
||||
const currentRevision = original.getInt("revision");
|
||||
const expectedRevision = event.record.getInt("expectedRevision");
|
||||
if (!Number.isSafeInteger(expectedRevision) || expectedRevision < 1) {
|
||||
rejectRequest("Submission changed since it was read");
|
||||
}
|
||||
try {
|
||||
validateAndNormalizeSubmission(event.record);
|
||||
} catch (error) {
|
||||
console.log(
|
||||
JSON.stringify({
|
||||
event: "workshop_submission_rejected",
|
||||
operation: "update",
|
||||
submissionId: event.record.id,
|
||||
type: event.record.getString("type"),
|
||||
errorName: error && error.name ? error.name : "ValidationError",
|
||||
}),
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
event.record.set("expectedRevision", expectedRevision);
|
||||
event.record.set("revision", currentRevision + 1);
|
||||
event.next();
|
||||
|
||||
Reference in new issue
Block a user