Compare commits

..

No commits in common. "5cec8add5ef45d9607ff05778b868ebcf3e1085e" and "7ee86f0d2f2c027c4d06c4aae6ce161c9e7a0b38" have entirely different histories.

11 changed files with 171 additions and 247 deletions

13
Cargo.lock generated
View File

@ -2995,7 +2995,7 @@ dependencies = [
[[package]] [[package]]
name = "letterbox-notmuch" name = "letterbox-notmuch"
version = "0.15.11" version = "0.15.9"
dependencies = [ dependencies = [
"itertools", "itertools",
"log", "log",
@ -3010,18 +3010,15 @@ dependencies = [
[[package]] [[package]]
name = "letterbox-procmail2notmuch" name = "letterbox-procmail2notmuch"
version = "0.15.11" version = "0.15.9"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"clap", "clap",
"serde",
"sqlx",
"tokio 1.44.2",
] ]
[[package]] [[package]]
name = "letterbox-server" name = "letterbox-server"
version = "0.15.11" version = "0.15.9"
dependencies = [ dependencies = [
"ammonia", "ammonia",
"anyhow", "anyhow",
@ -3065,7 +3062,7 @@ dependencies = [
[[package]] [[package]]
name = "letterbox-shared" name = "letterbox-shared"
version = "0.15.11" version = "0.15.9"
dependencies = [ dependencies = [
"build-info", "build-info",
"letterbox-notmuch", "letterbox-notmuch",
@ -3075,7 +3072,7 @@ dependencies = [
[[package]] [[package]]
name = "letterbox-web" name = "letterbox-web"
version = "0.15.11" version = "0.15.9"
dependencies = [ dependencies = [
"build-info", "build-info",
"build-info-build", "build-info-build",

View File

@ -8,7 +8,7 @@ authors = ["Bill Thiede <git@xinu.tv>"]
edition = "2021" edition = "2021"
license = "UNLICENSED" license = "UNLICENSED"
publish = ["xinu"] publish = ["xinu"]
version = "0.15.11" version = "0.15.9"
repository = "https://git.z.xinu.tv/wathiede/letterbox" repository = "https://git.z.xinu.tv/wathiede/letterbox"
[profile.dev] [profile.dev]

View File

@ -12,7 +12,4 @@ version.workspace = true
[dependencies] [dependencies]
anyhow = "1.0.69" anyhow = "1.0.69"
clap = { version = "4.5.37", features = ["derive", "env"] } clap = { version = "4.5.37", features = ["derive"] }
serde = { version = "1.0.219", features = ["derive"] }
sqlx = { version = "0.8.5", features = ["postgres", "runtime-tokio"] }
tokio = { version = "1.44.2", features = ["rt", "macros", "rt-multi-thread"] }

View File

@ -1,12 +1,8 @@
use std::{collections::HashMap, convert::Infallible, io::Write, str::FromStr}; use std::{collections::HashMap, convert::Infallible, io::Write, str::FromStr};
use clap::{Parser, Subcommand}; use clap::{Parser, ValueEnum};
use serde::{Deserialize, Serialize};
use sqlx::{types::Json, PgPool};
#[derive( #[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash, Ord, PartialOrd)]
Copy, Clone, Debug, Default, PartialEq, Eq, Hash, Ord, PartialOrd, Serialize, Deserialize,
)]
enum MatchType { enum MatchType {
From, From,
Sender, Sender,
@ -23,17 +19,16 @@ enum MatchType {
#[default] #[default]
Unknown, Unknown,
} }
#[derive(Debug, Default, Serialize, Deserialize)] #[derive(Debug, Default)]
struct Match { struct Match {
match_type: MatchType, match_type: MatchType,
needle: String, needle: String,
} }
#[derive(Debug, Default, Serialize, Deserialize)] #[derive(Debug, Default)]
struct Rule { struct Rule {
stop_on_match: bool,
matches: Vec<Match>, matches: Vec<Match>,
tag: Option<String>, tags: Option<String>,
} }
fn unescape(s: &str) -> String { fn unescape(s: &str) -> String {
@ -160,109 +155,13 @@ impl FromStr for Match {
} }
} }
#[derive(Debug, Subcommand)]
enum Mode {
Debug,
Notmuchrc,
LoadSql {
#[arg(short, long, default_value = env!("DATABASE_URL"))]
dsn: String,
},
}
/// Simple program to greet a person
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
#[arg(short, long, default_value = "/home/wathiede/dotfiles/procmailrc")]
input: String,
#[command(subcommand)]
mode: Mode,
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let args = Args::parse();
let mut rules = Vec::new();
let mut cur_rule = Rule::default();
for l in std::fs::read_to_string(args.input)?.lines() {
let l = if let Some(idx) = l.find('#') {
&l[..idx]
} else {
l
}
.trim();
if l.is_empty() {
continue;
}
if l.find('=').is_some() {
// Probably a variable assignment, skip line
continue;
}
let first = l.chars().nth(0).unwrap_or(' ');
match first {
':' => {
// start of rule
}
'*' => {
// add to current rule
let m: Match = l.parse()?;
cur_rule.matches.push(m);
}
'.' => {
// delivery to folder
cur_rule.tag = Some(cleanup_match(
"",
&l.replace('.', "/")
.replace(' ', "")
.trim_matches('/')
.to_string(),
));
rules.push(cur_rule);
cur_rule = Rule::default();
}
'/' => cur_rule = Rule::default(), // Ex. /dev/null
'|' => cur_rule = Rule::default(), // external command
'$' => {
// TODO(wathiede): tag messages with no other tag as 'inbox'
cur_rule.tag = Some(cleanup_match("", "inbox"));
rules.push(cur_rule);
cur_rule = Rule::default();
} // variable, should only be $DEFAULT in my config
_ => panic!("Unhandled first character '{}'\nLine: {}", first, l),
}
}
match args.mode {
Mode::Debug => print_rules(&rules),
Mode::Notmuchrc => notmuch_from_rules(std::io::stdout(), &rules)?,
Mode::LoadSql { dsn } => load_sql(&dsn, &rules).await?,
}
Ok(())
}
fn print_rules(rules: &[Rule]) {
let mut tally = HashMap::new();
for r in rules {
for m in &r.matches {
*tally.entry(m.match_type).or_insert(0) += 1;
}
}
let mut sorted: Vec<_> = tally.iter().map(|(k, v)| (v, k)).collect();
sorted.sort();
sorted.reverse();
for (v, k) in sorted {
println!("{k:?}: {v}");
}
}
fn notmuch_from_rules<W: Write>(mut w: W, rules: &[Rule]) -> anyhow::Result<()> { fn notmuch_from_rules<W: Write>(mut w: W, rules: &[Rule]) -> anyhow::Result<()> {
// TODO(wathiede): if reindexing this many tags is too slow, see if combining rules per tag is // TODO(wathiede): if reindexing this many tags is too slow, see if combining rules per tag is
// faster. // faster.
let mut lines = Vec::new(); let mut lines = Vec::new();
for r in rules { for r in rules {
for m in &r.matches { for m in &r.matches {
if let Some(t) = &r.tag { if let Some(t) = &r.tags {
if let MatchType::Unknown = m.match_type { if let MatchType::Unknown = m.match_type {
eprintln!("rule has unknown match {:?}", r); eprintln!("rule has unknown match {:?}", r);
continue; continue;
@ -307,25 +206,92 @@ fn notmuch_from_rules<W: Write>(mut w: W, rules: &[Rule]) -> anyhow::Result<()>
Ok(()) Ok(())
} }
async fn load_sql(dsn: &str, rules: &[Rule]) -> anyhow::Result<()> { #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
let pool = PgPool::connect(dsn).await?; enum Mode {
println!("clearing email_rule table"); Debug,
sqlx::query!("DELETE FROM email_rule") Notmuchrc,
.execute(&pool) }
.await?;
for (order, rule) in rules.iter().enumerate() { /// Simple program to greet a person
println!("inserting {order}: {rule:?}"); #[derive(Parser, Debug)]
sqlx::query!( #[command(version, about, long_about = None)]
r#" struct Args {
INSERT INTO email_rule (sort_order, rule) #[arg(short, long, default_value = "/home/wathiede/dotfiles/procmailrc")]
VALUES ($1, $2) input: String,
"#,
order as i32, #[arg(value_enum)]
Json(rule) as _ mode: Mode,
) }
.execute(&pool)
.await?; fn main() -> anyhow::Result<()> {
let args = Args::parse();
let mut rules = Vec::new();
let mut cur_rule = Rule::default();
for l in std::fs::read_to_string(args.input)?.lines() {
let l = if let Some(idx) = l.find('#') {
&l[..idx]
} else {
l
}
.trim();
if l.is_empty() {
continue;
}
if l.find('=').is_some() {
// Probably a variable assignment, skip line
continue;
}
let first = l.chars().nth(0).unwrap_or(' ');
match first {
':' => {
// start of rule
}
'*' => {
// add to current rule
let m: Match = l.parse()?;
cur_rule.matches.push(m);
}
'.' => {
// delivery to folder
cur_rule.tags = Some(cleanup_match(
"",
&l.replace('.', "/")
.replace(' ', "")
.trim_matches('/')
.to_string(),
));
rules.push(cur_rule);
cur_rule = Rule::default();
}
'/' => cur_rule = Rule::default(), // Ex. /dev/null
'|' => cur_rule = Rule::default(), // external command
'$' => {
// TODO(wathiede): tag messages with no other tag as 'inbox'
cur_rule.tags = Some(cleanup_match("", "inbox"));
rules.push(cur_rule);
cur_rule = Rule::default();
} // variable, should only be $DEFAULT in my config
_ => panic!("Unhandled first character '{}'\nLine: {}", first, l),
}
}
match args.mode {
Mode::Debug => print_rules(&rules),
Mode::Notmuchrc => notmuch_from_rules(std::io::stdout(), &rules)?,
} }
Ok(()) Ok(())
} }
fn print_rules(rules: &[Rule]) {
let mut tally = HashMap::new();
for r in rules {
for m in &r.matches {
*tally.entry(m.match_type).or_insert(0) += 1;
}
}
let mut sorted: Vec<_> = tally.iter().map(|(k, v)| (v, k)).collect();
sorted.sort();
sorted.reverse();
for (v, k) in sorted {
println!("{k:?}: {v}");
}
}

View File

@ -27,8 +27,8 @@ css-inline = "0.14.0"
futures = "0.3.31" futures = "0.3.31"
headers = "0.4.0" headers = "0.4.0"
html-escape = "0.2.13" html-escape = "0.2.13"
letterbox-notmuch = { version = "0.15.11", path = "../notmuch", registry = "xinu" } letterbox-notmuch = { version = "0.15.9", path = "../notmuch", registry = "xinu" }
letterbox-shared = { version = "0.15.11", path = "../shared", registry = "xinu" } letterbox-shared = { version = "0.15.9", path = "../shared", registry = "xinu" }
linkify = "0.10.0" linkify = "0.10.0"
log = "0.4.17" log = "0.4.17"
lol_html = "2.0.0" lol_html = "2.0.0"

View File

@ -1,3 +0,0 @@
DROP TABLE IF NOT EXISTS email_rule;
-- Add down migration script here

View File

@ -1,5 +0,0 @@
CREATE TABLE IF NOT EXISTS email_rule (
id integer NOT NULL GENERATED ALWAYS AS IDENTITY,
sort_order integer NOT NULL,
rule jsonb NOT NULL
);

View File

@ -142,7 +142,6 @@ async fn view_cid(
Ok(inline_attachment_response(attachment)) Ok(inline_attachment_response(attachment))
} }
// TODO make this work with gitea message ids like `wathiede/letterbox/pulls/91@git.z.xinu.tv`
async fn view_original( async fn view_original(
State(AppState { nm, .. }): State<AppState>, State(AppState { nm, .. }): State<AppState>,
extract::Path(id): extract::Path<String>, extract::Path(id): extract::Path<String>,

View File

@ -12,6 +12,6 @@ version.workspace = true
[dependencies] [dependencies]
build-info = "0.0.40" build-info = "0.0.40"
letterbox-notmuch = { version = "0.15.11", path = "../notmuch", registry = "xinu" } letterbox-notmuch = { version = "0.15.9", path = "../notmuch", registry = "xinu" }
serde = { version = "1.0.147", features = ["derive"] } serde = { version = "1.0.147", features = ["derive"] }
strum_macros = "0.27.1" strum_macros = "0.27.1"

View File

@ -33,8 +33,8 @@ wasm-bindgen = "=0.2.100"
uuid = { version = "1.13.1", features = [ uuid = { version = "1.13.1", features = [
"js", "js",
] } # direct dep to set js feature, prevents Rng issues ] } # direct dep to set js feature, prevents Rng issues
letterbox-shared = { version = "0.15.11", path = "../shared", registry = "xinu" } letterbox-shared = { version = "0.15.9", path = "../shared", registry = "xinu" }
letterbox-notmuch = { version = "0.15.11", path = "../notmuch", registry = "xinu" } letterbox-notmuch = { version = "0.15.9", path = "../notmuch", registry = "xinu" }
seed_hooks = { version = "0.4.0", registry = "xinu" } seed_hooks = { version = "0.4.0", registry = "xinu" }
strum_macros = "0.27.1" strum_macros = "0.27.1"
gloo-console = "0.3.0" gloo-console = "0.3.0"
@ -50,10 +50,9 @@ features = [
"Clipboard", "Clipboard",
"DomRect", "DomRect",
"Element", "Element",
"History",
"MediaQueryList", "MediaQueryList",
"Navigator", "Navigator",
"Performance",
"ScrollRestoration",
"Window", "Window",
"History",
"ScrollRestoration",
] ]

View File

@ -263,107 +263,81 @@ fn search_results(
} else { } else {
set_title(query); set_title(query);
} }
let rows: Vec<_> = results let rows = results.iter().map(|r| {
.iter() let tid = r.thread.clone();
.map(|r| { let check_tid = r.thread.clone();
let tid = r.thread.clone(); let datetime = human_age(r.timestamp as i64);
let check_tid = r.thread.clone(); let unread_idx = r.tags.iter().position(|e| e == &"unread");
let datetime = human_age(r.timestamp as i64); let mut tags = r.tags.clone();
let unread_idx = r.tags.iter().position(|e| e == &"unread"); if let Some(idx) = unread_idx {
let mut tags = r.tags.clone(); tags.remove(idx);
if let Some(idx) = unread_idx { };
tags.remove(idx); let is_unread = unread_idx.is_some();
}; let mut title_break = None;
let is_unread = unread_idx.is_some(); const TITLE_LENGTH_WRAP_LIMIT: usize = 40;
let mut title_break = None; for w in r.subject.split_whitespace() {
const TITLE_LENGTH_WRAP_LIMIT: usize = 40; if w.len() > TITLE_LENGTH_WRAP_LIMIT {
for w in r.subject.split_whitespace() { title_break = Some(C!["break-all", "text-pretty"]);
if w.len() > TITLE_LENGTH_WRAP_LIMIT {
title_break = Some(C!["break-all", "text-pretty"]);
}
} }
}
div![
C![
"flex",
"flex-nowrap",
"w-auto",
"flex-auto",
"py-4",
"border-b",
"border-neutral-800"
],
div![ div![
C![ C!["flex", "items-center", "mr-4"],
"flex", input![
"flex-nowrap", C![&tw_classes::CHECKBOX],
"w-auto",
"flex-auto",
"py-4",
"border-b",
"border-neutral-800"
],
div![
C!["flex", "items-center", "mr-4"],
input![
C![&tw_classes::CHECKBOX],
attrs! {
At::Type=>"checkbox",
At::Checked=>selected_threads.contains(&tid).as_at_value(),
}
],
ev(Ev::Input, move |e| {
if let Some(input) = e
.target()
.as_ref()
.expect("failed to get reference to target")
.dyn_ref::<web_sys::HtmlInputElement>()
{
if input.checked() {
Msg::SelectionAddThread(check_tid)
} else {
Msg::SelectionRemoveThread(check_tid)
}
} else {
Msg::Noop
}
}),
],
a![
C!["flex-grow"],
IF!(is_unread => C!["font-bold"]),
attrs! { attrs! {
At::Href => urls::thread(&tid) At::Type=>"checkbox",
}, At::Checked=>selected_threads.contains(&tid).as_at_value(),
div![title_break, &r.subject], }
span![C!["text-xs"], pretty_authors(&r.authors)], ],
div![ ev(Ev::Input, move |e| {
C!["flex", "flex-wrap", "justify-between"], if let Some(input) = e
span![tags_chiclet(&tags)], .target()
span![C!["text-sm"], datetime] .as_ref()
] .expect("failed to get reference to target")
.dyn_ref::<web_sys::HtmlInputElement>()
{
if input.checked() {
Msg::SelectionAddThread(check_tid)
} else {
Msg::SelectionRemoveThread(check_tid)
}
} else {
Msg::Noop
}
}),
],
a![
C!["flex-grow"],
IF!(is_unread => C!["font-bold"]),
attrs! {
At::Href => urls::thread(&tid)
},
div![title_break, &r.subject],
span![C!["text-xs"], pretty_authors(&r.authors)],
div![
C!["flex", "flex-wrap", "justify-between"],
span![tags_chiclet(&tags)],
span![C!["text-sm"], datetime]
] ]
] ]
})
.collect();
let show_bulk_edit = !selected_threads.is_empty();
let all_selected = (selected_threads.len() == results.len()) && !rows.is_empty();
let content = if rows.is_empty() {
let caught_up = query.contains("is:unread");
let read_emoji = ["👻", "👽", "👾", "🤖", "💀"];
let no_results_emoji = ["🙈", "👀", "🤦", "🤷", "🙅", "🛟", "🍩", "🌑", "💿", "🔍"];
let now = seed::window()
.performance()
.map(|p| p.now() as usize)
.unwrap_or(0);
let (emoji, text) = if caught_up {
let idx = now % read_emoji.len();
(read_emoji[idx], "All caught up!")
} else {
let idx = now % no_results_emoji.len();
(no_results_emoji[idx], "No results")
};
div![
C!["text-center"],
h1![C!["text-9xl"], emoji],
p![C!["mt-8", "text-3xl", "font-semibold"], text]
] ]
} else { });
div![rows] let show_bulk_edit = !selected_threads.is_empty();
}; let all_selected = selected_threads.len() == results.len();
div![ div![
C!["flex", "flex-col", "flex-auto", "p-4"], C!["flex", "flex-col", "flex-auto", "p-4"],
search_toolbar(count, pager, show_bulk_edit, all_selected), search_toolbar(count, pager, show_bulk_edit, all_selected),
content, div![rows],
search_toolbar(count, pager, show_bulk_edit, all_selected), search_toolbar(count, pager, show_bulk_edit, all_selected),
] ]
} }