From a5533b684adbfba5dcbe4efb2cf56daf603de5cf Mon Sep 17 00:00:00 2001 From: lew Date: Fri, 10 Apr 2026 18:20:19 +0100 Subject: [PATCH] entries: add short_id helper, use in telegram --- src/entries.rs | 19 +++++++++++++++++++ src/telegram.rs | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/entries.rs b/src/entries.rs index 5e78d2a..3e0ca1e 100644 --- a/src/entries.rs +++ b/src/entries.rs @@ -49,6 +49,11 @@ impl Entry { }) } + /// Return the short ID (UUID portion after the underscore). + pub fn short_id(&self) -> &str { + self.id.split('_').last().unwrap_or(&self.id) + } + /// Serialize entry back to file format. pub fn to_file_contents(&self) -> String { let frontmatter = toml::to_string_pretty(&self.meta).unwrap(); @@ -360,4 +365,18 @@ Hello!"#; std::fs::create_dir_all(dir.path().join("entries")).unwrap(); assert!(delete_entry(dir.path(), "nonexistent").is_err()); } + + #[test] + fn test_short_id() { + let contents = "+++\nname = \"a\"\ndate = \"2026-04-10\"\nstatus = \"pending\"\n+++\nhi"; + let entry = Entry::parse("1744300800_abcd1234", contents).unwrap(); + assert_eq!(entry.short_id(), "abcd1234"); + } + + #[test] + fn test_short_id_no_underscore() { + let contents = "+++\nname = \"a\"\ndate = \"2026-04-10\"\nstatus = \"pending\"\n+++\nhi"; + let entry = Entry::parse("plainid", contents).unwrap(); + assert_eq!(entry.short_id(), "plainid"); + } } diff --git a/src/telegram.rs b/src/telegram.rs index 0b50ded..0e48359 100644 --- a/src/telegram.rs +++ b/src/telegram.rs @@ -7,7 +7,7 @@ use crate::entries::{self, Entry, Status}; /// Send a notification to Telegram about a new entry. async fn notify(bot: &Bot, chat_id: ChatId, entry: &Entry) { - let short_id = entry.id.split('_').last().unwrap_or(&entry.id); + let short_id = entry.short_id(); let text = format!( "New guestbook entry:\n\nName: {}\nWebsite: {}\n\n{}\n\n/allow_{}\n/deny_{}", entry.meta.name, entry.meta.website, entry.body, short_id, short_id