entries: add short_id helper, use in telegram
This commit is contained in:
parent
3c6bb59980
commit
a5533b684a
2 changed files with 20 additions and 1 deletions
|
|
@ -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.
|
/// Serialize entry back to file format.
|
||||||
pub fn to_file_contents(&self) -> String {
|
pub fn to_file_contents(&self) -> String {
|
||||||
let frontmatter = toml::to_string_pretty(&self.meta).unwrap();
|
let frontmatter = toml::to_string_pretty(&self.meta).unwrap();
|
||||||
|
|
@ -360,4 +365,18 @@ Hello!"#;
|
||||||
std::fs::create_dir_all(dir.path().join("entries")).unwrap();
|
std::fs::create_dir_all(dir.path().join("entries")).unwrap();
|
||||||
assert!(delete_entry(dir.path(), "nonexistent").is_err());
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ use crate::entries::{self, Entry, Status};
|
||||||
|
|
||||||
/// Send a notification to Telegram about a new entry.
|
/// Send a notification to Telegram about a new entry.
|
||||||
async fn notify(bot: &Bot, chat_id: ChatId, entry: &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!(
|
let text = format!(
|
||||||
"New guestbook entry:\n\nName: {}\nWebsite: {}\n\n{}\n\n/allow_{}\n/deny_{}",
|
"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
|
entry.meta.name, entry.meta.website, entry.body, short_id, short_id
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue