major features update

This commit is contained in:
Robert Perce 2025-11-27 13:45:21 -06:00
parent 519fb49901
commit 4e2fab67c5
48 changed files with 3925 additions and 208 deletions

144
src/models/journal.rs Normal file
View file

@ -0,0 +1,144 @@
use chrono::NaiveDate;
use maud::{Markup, PreEscaped, html};
use regex::Regex;
use serde_json::json;
use sqlx::sqlite::{SqlitePool, SqliteRow};
use sqlx::{FromRow, Row};
use std::collections::HashSet;
use std::sync::{Arc, RwLock};
use super::contact::ContactTrie;
use crate::AppError;
use crate::db::DbId;
#[derive(Debug)]
pub struct JournalEntry {
pub id: DbId,
pub value: String,
pub date: NaiveDate,
}
#[derive(Debug, PartialEq, Eq, Hash, FromRow)]
pub struct ContactMention {
pub entry_id: DbId,
pub contact_id: DbId,
pub input_text: String,
pub byte_range_start: u32,
pub byte_range_end: u32,
}
impl JournalEntry {
pub fn extract_mentions(&self, trie: &ContactTrie) -> HashSet<ContactMention> {
let name_re = Regex::new(r"\[\[(.+?)\]\]").unwrap();
name_re
.captures_iter(&self.value)
.map(|caps| {
let range = caps.get_match().range();
trie.get(&caps[1]).map(|cid| ContactMention {
entry_id: self.id,
contact_id: cid.to_owned(),
input_text: caps[1].to_string(),
byte_range_start: u32::try_from(range.start).unwrap(),
byte_range_end: u32::try_from(range.end).unwrap(),
})
})
.filter(|o| o.is_some())
.map(|o| o.unwrap())
.collect()
}
pub async fn insert_mentions(
&self,
trie: Arc<RwLock<ContactTrie>>,
pool: &SqlitePool,
) -> Result<HashSet<ContactMention>, AppError> {
let mentions = {
let trie = trie.read().unwrap();
self.extract_mentions(&trie)
};
for mention in &mentions {
sqlx::query!(
"insert into contact_mentions(
entry_id, contact_id, input_text,
byte_range_start, byte_range_end
) values ($1, $2, $3, $4, $5)",
mention.entry_id,
mention.contact_id,
mention.input_text,
mention.byte_range_start,
mention.byte_range_end
)
.execute(pool)
.await?;
}
Ok(mentions)
}
pub async fn to_html(&self, pool: &SqlitePool) -> Result<Markup, AppError> {
// important to sort desc so that changing contents early in the string
// doesn't break inserting mentions at byte offsets further in
let mentions: Vec<ContactMention> = sqlx::query_as(
"select * from contact_mentions
where entry_id = $1 order by byte_range_start desc",
)
.bind(self.id)
.fetch_all(pool)
.await?;
let mut value = self.value.clone();
for mention in mentions {
value.replace_range(
(mention.byte_range_start as usize)..(mention.byte_range_end as usize),
&format!("[{}](/contact/{})", mention.input_text, mention.contact_id),
);
}
let entry_url = format!("/journal_entry/{}", self.id);
let date = self.date.to_string();
Ok(html! {
.entry {
.view ":class"="{ hide: edit }" {
.date { (date) }
.content { (PreEscaped(markdown::to_html(&value))) }
}
form .edit ":class"="{ hide: !edit }" x-data=(json!({ "date": date, "initial_date": date, "value": self.value, "initial_value": self.value })) {
input name="date" x-model="date";
.controls {
textarea name="value" x-model="value" {}
button title="Delete"
hx-delete=(entry_url)
hx-target="closest .entry"
hx-swap="delete" {
svg .icon xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 640" {
path d="M232.7 69.9C237.1 56.8 249.3 48 263.1 48L377 48C390.8 48 403 56.8 407.4 69.9L416 96L512 96C529.7 96 544 110.3 544 128C544 145.7 529.7 160 512 160L128 160C110.3 160 96 145.7 96 128C96 110.3 110.3 96 128 96L224 96L232.7 69.9zM128 208L512 208L512 512C512 547.3 483.3 576 448 576L192 576C156.7 576 128 547.3 128 512L128 208zM216 272C202.7 272 192 282.7 192 296L192 488C192 501.3 202.7 512 216 512C229.3 512 240 501.3 240 488L240 296C240 282.7 229.3 272 216 272zM320 272C306.7 272 296 282.7 296 296L296 488C296 501.3 306.7 512 320 512C333.3 512 344 501.3 344 488L344 296C344 282.7 333.3 272 320 272zM424 272C410.7 272 400 282.7 400 296L400 488C400 501.3 410.7 512 424 512C437.3 512 448 501.3 448 488L448 296C448 282.7 437.3 272 424 272z";
}
}
button x-bind:disabled="(date === initial_date) && (value === initial_value)"
x-on:click="initial_date = date; initial_value = value"
hx-patch=(entry_url)
hx-target="previous .entry"
hx-swap="outerHTML"
title="Save" { "" }
button x-bind:disabled="(date === initial_date) && (value === initial_value)"
title="Discard changes"
x-on:click="date = initial_date; value = initial_value"
{ "" }
}
}
}
})
}
}
impl FromRow<'_, SqliteRow> for JournalEntry {
fn from_row(row: &SqliteRow) -> sqlx::Result<Self> {
let id: DbId = row.try_get("id")?;
let value: String = row.try_get("value")?;
let date_str: &str = row.try_get("date")?;
let date = NaiveDate::parse_from_str(date_str, "%Y-%m-%d").unwrap();
Ok(Self { id, value, date })
}
}