feat: set required/optional for message, drawing, and voice notes, independently

This commit is contained in:
Lewis Wynne 2026-04-28 15:50:36 +01:00
parent 1910eec649
commit b784f4dd9c
7 changed files with 526 additions and 244 deletions

View file

@ -72,155 +72,183 @@ in
};
};
features = {
submissions = {
enable = mkOption {
type = types.bool;
default = true;
description = "Allow new guestbook submissions. When false, the form is hidden and submissions are rejected.";
submissions = {
enable = mkOption {
type = types.bool;
default = true;
description = "Allow new guestbook submissions. When false, the form is hidden and submissions are rejected.";
};
};
websites = {
enable = mkOption {
type = types.bool;
default = true;
description = "Show website field in form and render website links in entries. When false, the input is hidden, submitted values are ignored, and existing links are not displayed.";
};
};
drawing = {
enable = mkOption {
type = types.bool;
default = false;
description = "Enable the drawing canvas in the submission form. Stores PNG files in dataDir/drawings/.";
};
required = mkOption {
type = types.bool;
default = false;
description = "Require a drawing on every submission. No-op when drawing.enable=false.";
};
};
voice = {
enable = mkOption {
type = types.bool;
default = false;
description = "Enable voice note recording in the submission form. Stores WebM files in dataDir/voice_notes/.";
};
required = mkOption {
type = types.bool;
default = false;
description = "Require a voice note on every submission. No-op when voice.enable=false.";
};
};
message = {
required = mkOption {
type = types.bool;
default = false;
description = "Require a non-empty message on every submission. Individual checks take priority over content.required.";
};
};
content = {
required = mkOption {
type = types.bool;
default = true;
description = "Require at least one of message, drawing, or voice note. Set to false to allow name-only submissions.";
};
};
telegram = {
enable = mkEnableOption "Telegram moderation notifications";
botTokenFile = mkOption {
type = types.path;
description = "Path to a file containing the Telegram bot token.";
};
chatId = mkOption {
type = types.int;
description = "Telegram chat ID for moderation messages.";
};
retry = {
interval = mkOption {
type = types.int;
default = 20;
description = "Seconds between retry attempts for failed Telegram notifications.";
};
limit = mkOption {
type = types.int;
default = 3;
description = "Maximum number of retry attempts for failed Telegram notifications.";
};
};
websites = {
enable = mkOption {
type = types.bool;
default = true;
description = "Show website field in form and render website links in entries. When false, the input is hidden, submitted values are ignored, and existing links are not displayed.";
};
reminderInterval = mkOption {
type = types.int;
default = 86400;
description = "Seconds between pending entry reminders. Set to 0 to disable.";
};
};
drawing = {
security = {
htmlInjection = {
enable = mkOption {
type = types.bool;
default = false;
description = "Enable the drawing canvas in the submission form. Stores PNG files in dataDir/drawings/.";
description = "Allow raw HTML/JS in entry names and message bodies. When false, HTML is escaped. Website URLs are always escaped.";
};
};
honeypot = {
enable = mkOption {
type = types.bool;
default = true;
description = "Enable honeypot field for spam prevention.";
};
};
captcha = {
enable = mkEnableOption "captcha on submission form";
question = mkOption {
type = types.str;
default = "";
description = "Captcha question displayed as a label.";
};
canvasWidth = mkOption {
answer = mkOption {
type = types.str;
default = "";
description = "Captcha answer to validate against.";
};
exact = mkOption {
type = types.bool;
default = false;
description = "Require exact match. When false, the answer just needs to be contained in the response.";
};
caseSensitive = mkOption {
type = types.bool;
default = false;
description = "Require case-sensitive match.";
};
};
};
limits = {
name.length = mkOption {
type = types.int;
default = 0;
description = "Maximum length for names. 0 for unlimited.";
};
message.length = mkOption {
type = types.int;
default = 0;
description = "Maximum length for messages. 0 for unlimited.";
};
website.length = mkOption {
type = types.int;
default = 0;
description = "Maximum length for website URLs. 0 for unlimited.";
};
drawing = {
width = mkOption {
type = types.int;
default = 320;
description = "Drawing canvas width in pixels.";
};
canvasHeight = mkOption {
height = mkOption {
type = types.int;
default = 200;
description = "Drawing canvas height in pixels.";
};
};
voiceNote = {
enable = mkOption {
type = types.bool;
default = false;
description = "Enable voice note recording in the submission form. Stores WebM files in dataDir/voice_notes/.";
};
maxDuration = mkOption {
type = types.int;
default = 20;
description = "Maximum voice note duration in seconds. Max file size is derived as duration * 10KB.";
};
};
telegram = {
enable = mkEnableOption "Telegram moderation notifications";
botTokenFile = mkOption {
type = types.path;
description = "Path to a file containing the Telegram bot token.";
};
chatId = mkOption {
type = types.int;
description = "Telegram chat ID for moderation messages.";
};
retry = {
interval = mkOption {
type = types.int;
default = 20;
description = "Seconds between retry attempts for failed Telegram notifications.";
};
limit = mkOption {
type = types.int;
default = 3;
description = "Maximum number of retry attempts for failed Telegram notifications.";
};
};
reminderInterval = mkOption {
type = types.int;
default = 86400;
description = "Seconds between pending entry reminders. Set to 0 to disable.";
};
};
security = {
htmlInjection = {
enable = mkOption {
type = types.bool;
default = false;
description = "Allow raw HTML/JS in entry names and message bodies. When false, HTML is escaped. Website URLs are always escaped.";
};
};
honeypot = {
enable = mkOption {
type = types.bool;
default = true;
description = "Enable honeypot field for spam prevention.";
};
};
captcha = {
enable = mkEnableOption "captcha on submission form";
question = mkOption {
type = types.str;
default = "";
description = "Captcha question displayed as a label.";
};
answer = mkOption {
type = types.str;
default = "";
description = "Captcha answer to validate against.";
};
exact = mkOption {
type = types.bool;
default = false;
description = "Require exact match. When false, the answer just needs to be contained in the response.";
};
caseSensitive = mkOption {
type = types.bool;
default = false;
description = "Require case-sensitive match.";
};
};
};
};
limits = {
name = mkOption {
voice.duration = mkOption {
type = types.int;
default = 0;
description = "Maximum length for names. 0 for unlimited.";
};
message = mkOption {
type = types.int;
default = 0;
description = "Maximum length for messages. 0 for unlimited.";
};
website = mkOption {
type = types.int;
default = 0;
description = "Maximum length for website URLs. 0 for unlimited.";
default = 20;
description = "Maximum voice note duration in seconds. Max file size is derived as duration * 10KB.";
};
};
@ -280,13 +308,13 @@ in
description = "Label for the drawing field (when drawing.enable=true).";
};
voiceNote = mkOption {
voice = mkOption {
type = types.str;
default = "Leave a voice note (optional)";
description = "Label for the voice note field (when voiceNote.enable=true).";
description = "Label for the voice note field (when voice.enable=true).";
};
voiceNoteRecord = mkOption {
voiceRecord = mkOption {
type = types.str;
default = "Start recording";
description = "Initial text on the voice note record button.";
@ -321,31 +349,35 @@ in
BOOK_DATA_DIR = cfg.dataDir;
BOOK_SITE_TITLE = cfg.siteTitle;
BOOK_ENABLE_SUBMISSIONS = if cfg.features.submissions.enable then "true" else "false";
BOOK_ENABLE_WEBSITE_LINKS = if cfg.features.websites.enable then "true" else "false";
BOOK_ENABLE_DRAWINGS = if cfg.features.drawing.enable then "true" else "false";
BOOK_ENABLE_HTML_INJECTION = if cfg.features.security.htmlInjection.enable then "true" else "false";
BOOK_ENABLE_HONEYPOT = if cfg.features.security.honeypot.enable then "true" else "false";
BOOK_ENABLE_CAPTCHA = if cfg.features.security.captcha.enable then "true" else "false";
BOOK_CAPTCHA_QUESTION = cfg.features.security.captcha.question;
BOOK_CAPTCHA_ANSWER = cfg.features.security.captcha.answer;
BOOK_CAPTCHA_EXACT = if cfg.features.security.captcha.exact then "true" else "false";
BOOK_CAPTCHA_CASESENSITIVE = if cfg.features.security.captcha.caseSensitive then "true" else "false";
BOOK_MAX_NAME_LENGTH = toString cfg.limits.name;
BOOK_MAX_MESSAGE_LENGTH = toString cfg.limits.message;
BOOK_MAX_WEBSITE_LENGTH = toString cfg.limits.website;
BOOK_ENABLE_SUBMISSIONS = if cfg.submissions.enable then "true" else "false";
BOOK_ENABLE_WEBSITE_LINKS = if cfg.websites.enable then "true" else "false";
BOOK_ENABLE_DRAWINGS = if cfg.drawing.enable then "true" else "false";
BOOK_ENABLE_HTML_INJECTION = if cfg.security.htmlInjection.enable then "true" else "false";
BOOK_ENABLE_HONEYPOT = if cfg.security.honeypot.enable then "true" else "false";
BOOK_ENABLE_CAPTCHA = if cfg.security.captcha.enable then "true" else "false";
BOOK_CAPTCHA_QUESTION = cfg.security.captcha.question;
BOOK_CAPTCHA_ANSWER = cfg.security.captcha.answer;
BOOK_CAPTCHA_EXACT = if cfg.security.captcha.exact then "true" else "false";
BOOK_CAPTCHA_CASESENSITIVE = if cfg.security.captcha.caseSensitive then "true" else "false";
BOOK_MAX_NAME_LENGTH = toString cfg.limits.name.length;
BOOK_MAX_MESSAGE_LENGTH = toString cfg.limits.message.length;
BOOK_MAX_WEBSITE_LENGTH = toString cfg.limits.website.length;
BOOK_STYLE = cfg.styles.css;
BOOK_BUTTON_TEXT = cfg.styles.labels.submit;
BOOK_LABEL_NAME = cfg.styles.labels.name;
BOOK_LABEL_WEBSITE = cfg.styles.labels.website;
BOOK_LABEL_MESSAGE = cfg.styles.labels.message;
BOOK_LABEL_DRAWING = cfg.styles.labels.drawing;
BOOK_LABEL_VOICE_NOTE = cfg.styles.labels.voiceNote;
BOOK_VOICE_NOTE_RECORD_TEXT = cfg.styles.labels.voiceNoteRecord;
BOOK_CANVAS_WIDTH = toString cfg.features.drawing.canvasWidth;
BOOK_CANVAS_HEIGHT = toString cfg.features.drawing.canvasHeight;
BOOK_ENABLE_VOICE_NOTES = if cfg.features.voiceNote.enable then "true" else "false";
BOOK_VOICE_NOTE_MAX_DURATION = toString cfg.features.voiceNote.maxDuration;
BOOK_LABEL_VOICE_NOTE = cfg.styles.labels.voice;
BOOK_VOICE_NOTE_RECORD_TEXT = cfg.styles.labels.voiceRecord;
BOOK_CANVAS_WIDTH = toString cfg.limits.drawing.width;
BOOK_CANVAS_HEIGHT = toString cfg.limits.drawing.height;
BOOK_ENABLE_VOICE_NOTES = if cfg.voice.enable then "true" else "false";
BOOK_VOICE_NOTE_MAX_DURATION = toString cfg.limits.voice.duration;
BOOK_MESSAGE_REQUIRED = if cfg.message.required then "true" else "false";
BOOK_DRAWING_REQUIRED = if cfg.drawing.required then "true" else "false";
BOOK_VOICE_NOTE_REQUIRED = if cfg.voice.required then "true" else "false";
BOOK_CONTENT_REQUIRED = if cfg.content.required then "true" else "false";
BOOK_TEXTAREA_WIDTH = toString cfg.styles.message.width;
BOOK_TEXTAREA_HEIGHT = toString cfg.styles.message.height;
} // lib.optionalAttrs (cfg.styles.cssFile != null) {
@ -354,11 +386,11 @@ in
BOOK_TEMPLATE = cfg.styles.templateFile;
} // lib.optionalAttrs (cfg.styles.successTemplateFile != null) {
BOOK_SUCCESS_TEMPLATE = cfg.styles.successTemplateFile;
} // lib.optionalAttrs cfg.features.telegram.enable {
BOOK_TELEGRAM_CHAT_ID = toString cfg.features.telegram.chatId;
BOOK_TELEGRAM_RETRY_INTERVAL = toString cfg.features.telegram.retry.interval;
BOOK_TELEGRAM_RETRY_LIMIT = toString cfg.features.telegram.retry.limit;
BOOK_TELEGRAM_REMINDER_INTERVAL = toString cfg.features.telegram.reminderInterval;
} // lib.optionalAttrs cfg.telegram.enable {
BOOK_TELEGRAM_CHAT_ID = toString cfg.telegram.chatId;
BOOK_TELEGRAM_RETRY_INTERVAL = toString cfg.telegram.retry.interval;
BOOK_TELEGRAM_RETRY_LIMIT = toString cfg.telegram.retry.limit;
BOOK_TELEGRAM_REMINDER_INTERVAL = toString cfg.telegram.reminderInterval;
};
serviceConfig = {
Type = "simple";
@ -372,8 +404,8 @@ in
ReadWritePaths = [ cfg.dataDir ];
};
script = ''
${lib.optionalString cfg.features.telegram.enable ''
export BOOK_TELEGRAM_BOT_TOKEN="$(< "${cfg.features.telegram.botTokenFile}")"
${lib.optionalString cfg.telegram.enable ''
export BOOK_TELEGRAM_BOT_TOKEN="$(< "${cfg.telegram.botTokenFile}")"
''}
exec ${cfg.package}/bin/guestbook
'';