This commit is contained in:
parent
cd4096b2ff
commit
a0afb6dfd3
24 changed files with 536 additions and 274 deletions
|
|
@ -7,7 +7,7 @@ use sqlx::{FromRow, Row};
|
|||
use std::collections::HashSet;
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
use super::contact::ContactTrie;
|
||||
use super::contact::MentionTrie;
|
||||
use crate::AppError;
|
||||
use crate::db::DbId;
|
||||
|
||||
|
|
@ -19,24 +19,24 @@ pub struct JournalEntry {
|
|||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash, FromRow)]
|
||||
pub struct ContactMention {
|
||||
pub struct Mention {
|
||||
pub entry_id: DbId,
|
||||
pub contact_id: DbId,
|
||||
pub url: String,
|
||||
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> {
|
||||
pub fn extract_mentions(&self, trie: &MentionTrie) -> HashSet<Mention> {
|
||||
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 {
|
||||
trie.get(&caps[1]).map(|url| Mention {
|
||||
entry_id: self.id,
|
||||
contact_id: cid.to_owned(),
|
||||
url: url.to_string(),
|
||||
input_text: caps[1].to_string(),
|
||||
byte_range_start: u32::try_from(range.start).unwrap(),
|
||||
byte_range_end: u32::try_from(range.end).unwrap(),
|
||||
|
|
@ -49,9 +49,9 @@ impl JournalEntry {
|
|||
|
||||
pub async fn insert_mentions(
|
||||
&self,
|
||||
trie: Arc<RwLock<ContactTrie>>,
|
||||
trie: Arc<RwLock<MentionTrie>>,
|
||||
pool: &SqlitePool,
|
||||
) -> Result<HashSet<ContactMention>, AppError> {
|
||||
) -> Result<HashSet<Mention>, AppError> {
|
||||
let mentions = {
|
||||
let trie = trie.read().unwrap();
|
||||
self.extract_mentions(&trie)
|
||||
|
|
@ -59,12 +59,12 @@ impl JournalEntry {
|
|||
|
||||
for mention in &mentions {
|
||||
sqlx::query!(
|
||||
"insert into contact_mentions(
|
||||
entry_id, contact_id, input_text,
|
||||
"insert into journal_mentions(
|
||||
entry_id, url, input_text,
|
||||
byte_range_start, byte_range_end
|
||||
) values ($1, $2, $3, $4, $5)",
|
||||
mention.entry_id,
|
||||
mention.contact_id,
|
||||
mention.url,
|
||||
mention.input_text,
|
||||
mention.byte_range_start,
|
||||
mention.byte_range_end
|
||||
|
|
@ -79,8 +79,8 @@ impl JournalEntry {
|
|||
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
|
||||
let mentions: Vec<Mention> = sqlx::query_as(
|
||||
"select * from journal_mentions
|
||||
where entry_id = $1 order by byte_range_start desc",
|
||||
)
|
||||
.bind(self.id)
|
||||
|
|
@ -89,9 +89,10 @@ impl JournalEntry {
|
|||
|
||||
let mut value = self.value.clone();
|
||||
for mention in mentions {
|
||||
tracing::debug!("url ({})", mention.url);
|
||||
value.replace_range(
|
||||
(mention.byte_range_start as usize)..(mention.byte_range_end as usize),
|
||||
&format!("[{}](/contact/{})", mention.input_text, mention.contact_id),
|
||||
&format!("[{}]({})", mention.input_text, mention.url),
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -119,7 +120,7 @@ impl JournalEntry {
|
|||
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-target="closest .entry"
|
||||
hx-swap="outerHTML"
|
||||
title="Save" { "✓" }
|
||||
button x-bind:disabled="(date === initial_date) && (value === initial_value)"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue