Compare commits
14 Commits
server-sid
...
pretty
| Author | SHA1 | Date | |
|---|---|---|---|
| 98df04527d | |||
| da15ef0f15 | |||
| 035508f3ad | |||
| 69558f15b4 | |||
| a8f538eddf | |||
| 01e5ea14ab | |||
| 042d475c75 | |||
| dd0af52feb | |||
| 130f9bbeba | |||
| 0a05b32a7a | |||
| c3f897c61a | |||
| c62bac037f | |||
| 79a57f3082 | |||
| c33de9d754 |
675
Cargo.lock
generated
675
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
2
dev.sh
2
dev.sh
@@ -1,7 +1,7 @@
|
|||||||
cd -- "$( dirname -- "${BASH_SOURCE[0]}" )"
|
cd -- "$( dirname -- "${BASH_SOURCE[0]}" )"
|
||||||
tmux new-session -d -s letterbox-dev
|
tmux new-session -d -s letterbox-dev
|
||||||
tmux rename-window web
|
tmux rename-window web
|
||||||
tmux send-keys "cd web; trunk serve --release --address 0.0.0.0 --port 6758 --proxy-backend http://localhost:9345/ --proxy-rewrite=/api/ -w ../shared -w ../notmuch -w ./" C-m
|
tmux send-keys "cd web; trunk serve -w ../shared -w ../notmuch -w ./" C-m
|
||||||
tmux new-window -n server
|
tmux new-window -n server
|
||||||
tmux send-keys "cd server; cargo watch -x run -w ../shared -w ../notmuch -w ./" C-m
|
tmux send-keys "cd server; cargo watch -x run -w ../shared -w ../notmuch -w ./" C-m
|
||||||
tmux attach -d -t letterbox-dev
|
tmux attach -d -t letterbox-dev
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ edition = "2021"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
anyhow = "1.0.75"
|
||||||
|
clap = { version = "4.4.7", features = ["derive"] }
|
||||||
log = "0.4.14"
|
log = "0.4.14"
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
serde_json = { version = "1.0", features = ["unbounded_depth"] }
|
serde_json = { version = "1.0", features = ["unbounded_depth"] }
|
||||||
|
|||||||
@@ -454,6 +454,8 @@ pub enum NotmuchError {
|
|||||||
SerdeJson(#[from] serde_json::Error),
|
SerdeJson(#[from] serde_json::Error),
|
||||||
#[error("failed to parse bytes as str")]
|
#[error("failed to parse bytes as str")]
|
||||||
Utf8Error(#[from] std::str::Utf8Error),
|
Utf8Error(#[from] std::str::Utf8Error),
|
||||||
|
#[error("failed to parse bytes as String")]
|
||||||
|
StringUtf8Error(#[from] std::string::FromUtf8Error),
|
||||||
#[error("failed to parse str as int")]
|
#[error("failed to parse str as int")]
|
||||||
ParseIntError(#[from] std::num::ParseIntError),
|
ParseIntError(#[from] std::num::ParseIntError),
|
||||||
}
|
}
|
||||||
|
|||||||
46
notmuch/src/main.rs
Normal file
46
notmuch/src/main.rs
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
use clap::{Parser, Subcommand};
|
||||||
|
use notmuch::Notmuch;
|
||||||
|
|
||||||
|
#[derive(Parser)]
|
||||||
|
#[command(author, version, about, long_about = None)]
|
||||||
|
#[command(propagate_version = true)]
|
||||||
|
struct Cli {
|
||||||
|
/// Optional notmuch config file
|
||||||
|
#[arg(short, long)]
|
||||||
|
config: Option<PathBuf>,
|
||||||
|
|
||||||
|
#[command(subcommand)]
|
||||||
|
command: Commands,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Subcommand)]
|
||||||
|
enum Commands {
|
||||||
|
/// Show given search term
|
||||||
|
Show { search_term: String },
|
||||||
|
/// Search for given search term
|
||||||
|
Search { search_term: String },
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> anyhow::Result<()> {
|
||||||
|
let cli = Cli::parse();
|
||||||
|
|
||||||
|
let nm = if let Some(config_path) = cli.config {
|
||||||
|
Notmuch::with_config(config_path)
|
||||||
|
} else {
|
||||||
|
Notmuch::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
// You can check for the existence of subcommands, and if found use their
|
||||||
|
// matches just as you would the top level cmd
|
||||||
|
match &cli.command {
|
||||||
|
Commands::Search { search_term } => {
|
||||||
|
println!("{:#?}", nm.search(&search_term, 0, 10)?);
|
||||||
|
}
|
||||||
|
Commands::Show { search_term } => {
|
||||||
|
println!("{:#?}", nm.show(&search_term)?);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
name = "server"
|
name = "server"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
default-bin = "server"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
@@ -16,6 +17,7 @@ serde = { version = "1.0.147", features = ["derive"] }
|
|||||||
log = "0.4.17"
|
log = "0.4.17"
|
||||||
tokio = "1.26.0"
|
tokio = "1.26.0"
|
||||||
glog = "0.1.0"
|
glog = "0.1.0"
|
||||||
|
urlencoding = "2.1.3"
|
||||||
|
|
||||||
[dependencies.rocket_contrib]
|
[dependencies.rocket_contrib]
|
||||||
version = "0.4.11"
|
version = "0.4.11"
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate rocket;
|
extern crate rocket;
|
||||||
mod error;
|
|
||||||
mod nm;
|
|
||||||
|
|
||||||
use std::{error::Error, io::Cursor, str::FromStr};
|
use std::{error::Error, io::Cursor, str::FromStr};
|
||||||
|
|
||||||
use glog::Flags;
|
use glog::Flags;
|
||||||
use notmuch::{Notmuch, NotmuchError};
|
use notmuch::{Notmuch, NotmuchError, ThreadSet};
|
||||||
use rocket::{
|
use rocket::{
|
||||||
http::{ContentType, Header},
|
http::{ContentType, Header},
|
||||||
request::Request,
|
request::Request,
|
||||||
@@ -15,8 +13,8 @@ use rocket::{
|
|||||||
Response, State,
|
Response, State,
|
||||||
};
|
};
|
||||||
use rocket_cors::{AllowedHeaders, AllowedOrigins};
|
use rocket_cors::{AllowedHeaders, AllowedOrigins};
|
||||||
|
use server::{error::ServerError, nm::threadset_to_messages};
|
||||||
use crate::error::ServerError;
|
use shared::Message;
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
fn hello() -> &'static str {
|
fn hello() -> &'static str {
|
||||||
@@ -44,23 +42,32 @@ async fn search(
|
|||||||
) -> Result<Json<shared::SearchResult>, Debug<NotmuchError>> {
|
) -> Result<Json<shared::SearchResult>, Debug<NotmuchError>> {
|
||||||
let page = page.unwrap_or(0);
|
let page = page.unwrap_or(0);
|
||||||
let results_per_page = results_per_page.unwrap_or(10);
|
let results_per_page = results_per_page.unwrap_or(10);
|
||||||
|
let query = urlencoding::decode(query).map_err(NotmuchError::from)?;
|
||||||
info!(" search '{query}'");
|
info!(" search '{query}'");
|
||||||
let res = shared::SearchResult {
|
let res = shared::SearchResult {
|
||||||
summary: nm.search(query, page * results_per_page, results_per_page)?,
|
summary: nm.search(&query, page * results_per_page, results_per_page)?,
|
||||||
query: query.to_string(),
|
query: query.to_string(),
|
||||||
page,
|
page,
|
||||||
results_per_page,
|
results_per_page,
|
||||||
total: nm.count(query)?,
|
total: nm.count(&query)?,
|
||||||
};
|
};
|
||||||
Ok(Json(res))
|
Ok(Json(res))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/show/<query>")]
|
#[get("/show/<query>/pretty")]
|
||||||
async fn show(
|
async fn show_pretty(
|
||||||
nm: &State<Notmuch>,
|
nm: &State<Notmuch>,
|
||||||
query: &str,
|
query: &str,
|
||||||
) -> Result<Json<Vec<shared::Message>>, Debug<ServerError>> {
|
) -> Result<Json<Vec<Message>>, Debug<ServerError>> {
|
||||||
let res = nm::threadset_to_messages(nm.show(query).map_err(|e| -> ServerError { e.into() })?)?;
|
let query = urlencoding::decode(query).map_err(|e| ServerError::from(NotmuchError::from(e)))?;
|
||||||
|
let res = threadset_to_messages(nm.show(&query).map_err(ServerError::from)?)?;
|
||||||
|
Ok(Json(res))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[get("/show/<query>")]
|
||||||
|
async fn show(nm: &State<Notmuch>, query: &str) -> Result<Json<ThreadSet>, Debug<NotmuchError>> {
|
||||||
|
let query = urlencoding::decode(query).map_err(NotmuchError::from)?;
|
||||||
|
let res = nm.show(&query)?;
|
||||||
Ok(Json(res))
|
Ok(Json(res))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -151,6 +158,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
refresh,
|
refresh,
|
||||||
search_all,
|
search_all,
|
||||||
search,
|
search,
|
||||||
|
show_pretty,
|
||||||
show
|
show
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
9
server/src/error.rs
Normal file
9
server/src/error.rs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
|
#[derive(Error, Debug)]
|
||||||
|
pub enum ServerError {
|
||||||
|
#[error("notmuch")]
|
||||||
|
NotmuchError(#[from] notmuch::NotmuchError),
|
||||||
|
#[error("flatten")]
|
||||||
|
FlattenError,
|
||||||
|
}
|
||||||
2
server/src/lib.rs
Normal file
2
server/src/lib.rs
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
pub mod error;
|
||||||
|
pub mod nm;
|
||||||
15
server/src/nm.rs
Normal file
15
server/src/nm.rs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
use shared::Message;
|
||||||
|
|
||||||
|
use crate::error;
|
||||||
|
|
||||||
|
// TODO(wathiede): decide good error type
|
||||||
|
pub fn threadset_to_messages(
|
||||||
|
thread_set: notmuch::ThreadSet,
|
||||||
|
) -> Result<Vec<Message>, error::ServerError> {
|
||||||
|
for t in thread_set.0 {
|
||||||
|
for tn in t.0 {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(Vec::new())
|
||||||
|
}
|
||||||
@@ -9,27 +9,5 @@ pub struct SearchResult {
|
|||||||
pub results_per_page: usize,
|
pub results_per_page: usize,
|
||||||
pub total: usize,
|
pub total: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
pub struct ShowResult {
|
pub struct Message {}
|
||||||
messages: Vec<Message>,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub type AttachementId = String;
|
|
||||||
|
|
||||||
/// # Number of seconds since the Epoch
|
|
||||||
pub type UnixTime = isize;
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Default)]
|
|
||||||
pub struct Message {
|
|
||||||
pub from: String,
|
|
||||||
pub to: Option<String>,
|
|
||||||
pub cc: Option<String>,
|
|
||||||
pub timestamp: UnixTime, // date header as unix time
|
|
||||||
pub date_relative: String, // user-friendly timestamp
|
|
||||||
pub tags: Vec<String>,
|
|
||||||
|
|
||||||
// HTML formatted body
|
|
||||||
pub body: String,
|
|
||||||
pub attachment: Vec<AttachementId>,
|
|
||||||
}
|
|
||||||
|
|||||||
11
web/Trunk.toml
Normal file
11
web/Trunk.toml
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
[build]
|
||||||
|
release = true
|
||||||
|
|
||||||
|
[serve]
|
||||||
|
# The address to serve on.
|
||||||
|
address = "0.0.0.0"
|
||||||
|
port = 6758
|
||||||
|
|
||||||
|
[[proxy]]
|
||||||
|
backend = "http://localhost:9345/"
|
||||||
|
rewrite= "/api/"
|
||||||
@@ -4,18 +4,19 @@
|
|||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||||
<link rel="modulepreload" href="/pkg/package.js" as="script" type="text/javascript">
|
|
||||||
<link rel="preload" href="/pkg/package_bg.wasm" as="fetch" type="application/wasm" crossorigin="anonymous">
|
|
||||||
<link rel="stylesheet", href="https://jenil.github.io/bulmaswatch/cyborg/bulmaswatch.min.css">
|
<link rel="stylesheet", href="https://jenil.github.io/bulmaswatch/cyborg/bulmaswatch.min.css">
|
||||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css" integrity="sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ==" crossorigin="anonymous" referrerpolicy="no-referrer" />
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css" integrity="sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ==" crossorigin="anonymous" referrerpolicy="no-referrer" />
|
||||||
<style>
|
<style>
|
||||||
.message {
|
.message {
|
||||||
padding-left: 0.5em;
|
padding: 0.5em;*/
|
||||||
}
|
}
|
||||||
.body {
|
.body {
|
||||||
background: white;
|
background: white;
|
||||||
color: black;
|
color: black;
|
||||||
padding-bottom: 1em;
|
padding: 0.5em;
|
||||||
|
margin-left: -0.5em;
|
||||||
|
margin-right: -0.5em;
|
||||||
|
margin-top: 0.5em;
|
||||||
}
|
}
|
||||||
.error {
|
.error {
|
||||||
background-color: red;
|
background-color: red;
|
||||||
@@ -27,12 +28,24 @@ iframe {
|
|||||||
height: 100%;
|
height: 100%;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.index {
|
||||||
|
table-layout: fixed;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
.index .from {
|
.index .from {
|
||||||
width: 200px;
|
overflow: hidden;
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
white-space: nowrap;
|
||||||
|
width: 15em;
|
||||||
}
|
}
|
||||||
.index .subject {
|
.index .subject {
|
||||||
|
overflow: hidden;
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
white-space: nowrap;
|
||||||
}
|
}
|
||||||
.index .date {
|
.index .date {
|
||||||
|
width: 8em;
|
||||||
white-space: nowrap;
|
white-space: nowrap;
|
||||||
}
|
}
|
||||||
.footer {
|
.footer {
|
||||||
@@ -83,10 +96,6 @@ input::placeholder, .input::placeholder{
|
|||||||
|
|
||||||
<body>
|
<body>
|
||||||
<section id="app"></section>
|
<section id="app"></section>
|
||||||
<script type="module">
|
|
||||||
import init from '/pkg/package.js';
|
|
||||||
init('/pkg/package_bg.wasm');
|
|
||||||
</script>
|
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -1,16 +0,0 @@
|
|||||||
use seed::Url;
|
|
||||||
|
|
||||||
const BASE_URL: &str = "/api";
|
|
||||||
pub fn refresh() -> String {
|
|
||||||
format!("{BASE_URL}/refresh")
|
|
||||||
}
|
|
||||||
pub fn search(query: &str, page: usize, results_per_page: usize) -> String {
|
|
||||||
let query = Url::encode_uri_component(query);
|
|
||||||
format!("{BASE_URL}/search/{query}?page={page}&results_per_page={results_per_page}")
|
|
||||||
}
|
|
||||||
pub fn show(tid: &str) -> String {
|
|
||||||
format!("{BASE_URL}/show/{tid}")
|
|
||||||
}
|
|
||||||
pub fn original(message_id: &str) -> String {
|
|
||||||
format!("{BASE_URL}/original/{message_id}")
|
|
||||||
}
|
|
||||||
329
web/src/lib.rs
329
web/src/lib.rs
@@ -1,6 +1,7 @@
|
|||||||
mod api;
|
// (Lines like the one below ignore selected Clippy rules
|
||||||
mod nm;
|
// - it's useful when you want to check your code with `cargo make verify`
|
||||||
|
// but some rules are too "annoying" or are not applicable for your case.)
|
||||||
|
#![allow(clippy::wildcard_imports)]
|
||||||
use std::{
|
use std::{
|
||||||
collections::hash_map::DefaultHasher,
|
collections::hash_map::DefaultHasher,
|
||||||
hash::{Hash, Hasher},
|
hash::{Hash, Hasher},
|
||||||
@@ -8,9 +9,9 @@ use std::{
|
|||||||
|
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use log::{debug, error, info, Level};
|
use log::{debug, error, info, Level};
|
||||||
use notmuch::ThreadSet;
|
use notmuch::{Content, Part, Thread, ThreadNode, ThreadSet};
|
||||||
use seed::{prelude::*, *};
|
use seed::{prelude::*, *};
|
||||||
use serde::Deserialize;
|
use serde::de::Deserialize;
|
||||||
use wasm_timer::Instant;
|
use wasm_timer::Instant;
|
||||||
|
|
||||||
const SEARCH_RESULTS_PER_PAGE: usize = 20;
|
const SEARCH_RESULTS_PER_PAGE: usize = 20;
|
||||||
@@ -21,9 +22,12 @@ const SEARCH_RESULTS_PER_PAGE: usize = 20;
|
|||||||
|
|
||||||
// `init` describes what should happen when your app started.
|
// `init` describes what should happen when your app started.
|
||||||
fn init(url: Url, orders: &mut impl Orders<Msg>) -> Model {
|
fn init(url: Url, orders: &mut impl Orders<Msg>) -> Model {
|
||||||
orders
|
if url.hash().is_none() {
|
||||||
.subscribe(on_url_changed)
|
orders.request_url(urls::search("is:unread", 0));
|
||||||
.notify(subs::UrlChanged(url.clone()));
|
} else {
|
||||||
|
orders.notify(subs::UrlRequested::new(url));
|
||||||
|
};
|
||||||
|
orders.subscribe(on_url_changed);
|
||||||
|
|
||||||
Model {
|
Model {
|
||||||
context: Context::None,
|
context: Context::None,
|
||||||
@@ -41,7 +45,7 @@ fn on_url_changed(uc: subs::UrlChanged) -> Msg {
|
|||||||
);
|
);
|
||||||
let hpp = url.remaining_hash_path_parts();
|
let hpp = url.remaining_hash_path_parts();
|
||||||
match hpp.as_slice() {
|
match hpp.as_slice() {
|
||||||
["t", tid] => Msg::ShowRequest(tid.to_string()),
|
["t", tid] => Msg::ShowPrettyRequest(tid.to_string()),
|
||||||
["s", query] => {
|
["s", query] => {
|
||||||
let query = Url::decode_uri_component(query).unwrap_or("".to_string());
|
let query = Url::decode_uri_component(query).unwrap_or("".to_string());
|
||||||
Msg::SearchRequest {
|
Msg::SearchRequest {
|
||||||
@@ -116,8 +120,11 @@ enum RefreshingState {
|
|||||||
|
|
||||||
// (Remove the line below once any of your `Msg` variants doesn't implement `Copy`.)
|
// (Remove the line below once any of your `Msg` variants doesn't implement `Copy`.)
|
||||||
// `Msg` describes the different events you can modify state with.
|
// `Msg` describes the different events you can modify state with.
|
||||||
pub enum Msg {
|
enum Msg {
|
||||||
Noop,
|
Noop,
|
||||||
|
// Tell the client to refresh its state
|
||||||
|
Reload,
|
||||||
|
// Tell the server to update state
|
||||||
RefreshStart,
|
RefreshStart,
|
||||||
RefreshDone(Option<FetchError>),
|
RefreshDone(Option<FetchError>),
|
||||||
SearchRequest {
|
SearchRequest {
|
||||||
@@ -127,7 +134,9 @@ pub enum Msg {
|
|||||||
},
|
},
|
||||||
SearchResult(fetch::Result<shared::SearchResult>),
|
SearchResult(fetch::Result<shared::SearchResult>),
|
||||||
ShowRequest(String),
|
ShowRequest(String),
|
||||||
ShowResult(fetch::Result<Vec<shared::Message>>),
|
ShowResult(fetch::Result<ThreadSet>),
|
||||||
|
ShowPrettyRequest(String),
|
||||||
|
ShowPrettyResult(fetch::Result<Vec<shared::Message>>),
|
||||||
NextPage,
|
NextPage,
|
||||||
PreviousPage,
|
PreviousPage,
|
||||||
}
|
}
|
||||||
@@ -144,18 +153,12 @@ fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
|
|||||||
model.refreshing_state = if let Some(err) = err {
|
model.refreshing_state = if let Some(err) = err {
|
||||||
RefreshingState::Error(format!("{:?}", err))
|
RefreshingState::Error(format!("{:?}", err))
|
||||||
} else {
|
} else {
|
||||||
// If looking at search page, refresh the search to view update on the server side.
|
|
||||||
if let Context::Search(sr) = &model.context {
|
|
||||||
let query = sr.query.clone();
|
|
||||||
let page = sr.page;
|
|
||||||
let results_per_page = sr.results_per_page;
|
|
||||||
orders.perform_cmd(async move {
|
|
||||||
Msg::SearchResult(search_request(&query, page, results_per_page).await)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
RefreshingState::None
|
RefreshingState::None
|
||||||
};
|
};
|
||||||
|
orders.perform_cmd(async move { Msg::Reload });
|
||||||
|
}
|
||||||
|
Msg::Reload => {
|
||||||
|
orders.perform_cmd(async move { on_url_changed(subs::UrlChanged(Url::current())) });
|
||||||
}
|
}
|
||||||
|
|
||||||
Msg::SearchRequest {
|
Msg::SearchRequest {
|
||||||
@@ -182,13 +185,27 @@ fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
|
|||||||
.skip()
|
.skip()
|
||||||
.perform_cmd(async move { Msg::ShowResult(show_request(&tid).await) });
|
.perform_cmd(async move { Msg::ShowResult(show_request(&tid).await) });
|
||||||
}
|
}
|
||||||
|
// TODO(wathiede): remove
|
||||||
Msg::ShowResult(Ok(response_data)) => {
|
Msg::ShowResult(Ok(response_data)) => {
|
||||||
debug!("fetch ok {:#?}", response_data);
|
debug!("fetch ok {:#?}", response_data);
|
||||||
model.context = Context::Thread(response_data);
|
//model.context = Context::Thread(response_data);
|
||||||
}
|
}
|
||||||
Msg::ShowResult(Err(fetch_error)) => {
|
Msg::ShowResult(Err(fetch_error)) => {
|
||||||
error!("fetch failed {:?}", fetch_error);
|
error!("fetch failed {:?}", fetch_error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Msg::ShowPrettyRequest(tid) => {
|
||||||
|
orders
|
||||||
|
.skip()
|
||||||
|
.perform_cmd(async move { Msg::ShowPrettyResult(show_pretty_request(&tid).await) });
|
||||||
|
}
|
||||||
|
Msg::ShowPrettyResult(Ok(response_data)) => {
|
||||||
|
info!("fetch ok {:#?}", response_data);
|
||||||
|
model.context = Context::Thread(response_data);
|
||||||
|
}
|
||||||
|
Msg::ShowPrettyResult(Err(fetch_error)) => {
|
||||||
|
error!("fetch failed {:?}", fetch_error);
|
||||||
|
}
|
||||||
Msg::NextPage => {
|
Msg::NextPage => {
|
||||||
match &model.context {
|
match &model.context {
|
||||||
Context::Search(sr) => {
|
Context::Search(sr) => {
|
||||||
@@ -210,19 +227,6 @@ fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn show_request(tid: &str) -> fetch::Result<Vec<shared::Message>> {
|
|
||||||
let b = Request::new(api::show(tid))
|
|
||||||
.method(Method::Get)
|
|
||||||
.fetch()
|
|
||||||
.await?
|
|
||||||
.check_status()?
|
|
||||||
.bytes()
|
|
||||||
.await?;
|
|
||||||
let mut deserializer = serde_json::Deserializer::from_slice(&b);
|
|
||||||
deserializer.disable_recursion_limit();
|
|
||||||
Ok(Vec::<shared::Message>::deserialize(&mut deserializer)
|
|
||||||
.map_err(|_| FetchError::JsonError(fetch::JsonError::Serde(JsValue::NULL)))?)
|
|
||||||
}
|
|
||||||
async fn search_request(
|
async fn search_request(
|
||||||
query: &str,
|
query: &str,
|
||||||
page: usize,
|
page: usize,
|
||||||
@@ -237,6 +241,28 @@ async fn search_request(
|
|||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mod api {
|
||||||
|
use seed::Url;
|
||||||
|
|
||||||
|
const BASE_URL: &str = "/api";
|
||||||
|
pub fn refresh() -> String {
|
||||||
|
format!("{BASE_URL}/refresh")
|
||||||
|
}
|
||||||
|
pub fn search(query: &str, page: usize, results_per_page: usize) -> String {
|
||||||
|
let query = Url::encode_uri_component(query);
|
||||||
|
format!("{BASE_URL}/search/{query}?page={page}&results_per_page={results_per_page}")
|
||||||
|
}
|
||||||
|
pub fn show(tid: &str) -> String {
|
||||||
|
format!("{BASE_URL}/show/{tid}")
|
||||||
|
}
|
||||||
|
pub fn show_pretty(tid: &str) -> String {
|
||||||
|
format!("{BASE_URL}/show/{tid}/pretty")
|
||||||
|
}
|
||||||
|
pub fn original(message_id: &str) -> String {
|
||||||
|
format!("{BASE_URL}/original/{message_id}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async fn refresh_request() -> fetch::Result<()> {
|
async fn refresh_request() -> fetch::Result<()> {
|
||||||
let t = Request::new(api::refresh())
|
let t = Request::new(api::refresh())
|
||||||
.method(Method::Get)
|
.method(Method::Get)
|
||||||
@@ -249,10 +275,158 @@ async fn refresh_request() -> fetch::Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn show_request(tid: &str) -> fetch::Result<ThreadSet> {
|
||||||
|
let b = Request::new(api::show(tid))
|
||||||
|
.method(Method::Get)
|
||||||
|
.fetch()
|
||||||
|
.await?
|
||||||
|
.check_status()?
|
||||||
|
.bytes()
|
||||||
|
.await?;
|
||||||
|
let mut deserializer = serde_json::Deserializer::from_slice(&b);
|
||||||
|
deserializer.disable_recursion_limit();
|
||||||
|
Ok(ThreadSet::deserialize(&mut deserializer)
|
||||||
|
.map_err(|_| FetchError::JsonError(fetch::JsonError::Serde(JsValue::NULL)))?)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn show_pretty_request(tid: &str) -> fetch::Result<Vec<shared::Message>> {
|
||||||
|
Request::new(api::show_pretty(tid))
|
||||||
|
.method(Method::Get)
|
||||||
|
.fetch()
|
||||||
|
.await?
|
||||||
|
.check_status()?
|
||||||
|
.json()
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
// ------ ------
|
// ------ ------
|
||||||
// View
|
// View
|
||||||
// ------ ------
|
// ------ ------
|
||||||
|
|
||||||
|
// <subject>
|
||||||
|
// <tags>
|
||||||
|
//
|
||||||
|
// <from1> <date>
|
||||||
|
// <to1>
|
||||||
|
// <content1>
|
||||||
|
// <zippy>
|
||||||
|
// <children1>
|
||||||
|
// </zippy>
|
||||||
|
//
|
||||||
|
// <from2> <date>
|
||||||
|
// <to2>
|
||||||
|
// <body2>
|
||||||
|
fn view_message(thread: &ThreadNode) -> Node<Msg> {
|
||||||
|
let message = thread.0.as_ref().expect("ThreadNode missing Message");
|
||||||
|
let children = &thread.1;
|
||||||
|
div![
|
||||||
|
C!["message"],
|
||||||
|
/* TODO(wathiede): collect all the tags and show them here. */
|
||||||
|
/* TODO(wathiede): collect all the attachments from all the subparts */
|
||||||
|
div![C!["header"], "From: ", &message.headers.from],
|
||||||
|
div![C!["header"], "Date: ", &message.headers.date],
|
||||||
|
div![C!["header"], "To: ", &message.headers.to],
|
||||||
|
div![
|
||||||
|
C!["body"],
|
||||||
|
match &message.body {
|
||||||
|
Some(body) => view_body(body.as_slice()),
|
||||||
|
None => div!["<no body>"],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
children.iter().map(view_message)
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn view_body(body: &[Part]) -> Node<Msg> {
|
||||||
|
div![body.iter().map(view_part)]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn view_text_plain(content: &Option<Content>) -> Node<Msg> {
|
||||||
|
match &content {
|
||||||
|
Some(Content::String(content)) => p![C!["view-part-text-plain"], content],
|
||||||
|
_ => div![
|
||||||
|
C!["error"],
|
||||||
|
format!("Unhandled content enum for text/plain"),
|
||||||
|
],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn view_part(part: &Part) -> Node<Msg> {
|
||||||
|
match part.content_type.as_str() {
|
||||||
|
"text/plain" => view_text_plain(&part.content),
|
||||||
|
"text/html" => {
|
||||||
|
if let Some(Content::String(html)) = &part.content {
|
||||||
|
let inliner = css_inline::CSSInliner::options()
|
||||||
|
.load_remote_stylesheets(false)
|
||||||
|
.remove_style_tags(true)
|
||||||
|
.build();
|
||||||
|
let inlined = inliner.inline(html).expect("failed to inline CSS");
|
||||||
|
|
||||||
|
return div![C!["view-part-text-html"], raw![&inlined]];
|
||||||
|
} else {
|
||||||
|
div![
|
||||||
|
C!["error"],
|
||||||
|
format!("Unhandled content enum for multipart/mixed"),
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://en.wikipedia.org/wiki/MIME#alternative
|
||||||
|
// RFC1341 states: In general, user agents that compose multipart/alternative entities
|
||||||
|
// should place the body parts in increasing order of preference, that is, with the
|
||||||
|
// preferred format last.
|
||||||
|
"multipart/alternative" => {
|
||||||
|
if let Some(Content::Multipart(parts)) = &part.content {
|
||||||
|
for part in parts.iter().rev() {
|
||||||
|
if part.content_type == "text/html" {
|
||||||
|
if let Some(Content::String(html)) = &part.content {
|
||||||
|
let inliner = css_inline::CSSInliner::options()
|
||||||
|
.load_remote_stylesheets(false)
|
||||||
|
.remove_style_tags(true)
|
||||||
|
.build();
|
||||||
|
let inlined = inliner.inline(html).expect("failed to inline CSS");
|
||||||
|
return div![Node::from_html(None, &inlined)];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if part.content_type == "text/plain" {
|
||||||
|
return view_text_plain(&part.content);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
div!["No known multipart/alternative parts"]
|
||||||
|
} else {
|
||||||
|
div![
|
||||||
|
C!["error"],
|
||||||
|
format!("multipart/alternative with non-multipart content"),
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"multipart/mixed" => match &part.content {
|
||||||
|
Some(Content::Multipart(parts)) => div![parts.iter().map(view_part)],
|
||||||
|
_ => div![
|
||||||
|
C!["error"],
|
||||||
|
format!("Unhandled content enum for multipart/mixed"),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
_ => div![
|
||||||
|
C!["error"],
|
||||||
|
format!("Unhandled content type: {}", part.content_type)
|
||||||
|
],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn first_subject(thread: &ThreadNode) -> Option<String> {
|
||||||
|
if let Some(msg) = &thread.0 {
|
||||||
|
return Some(msg.headers.subject.clone());
|
||||||
|
} else {
|
||||||
|
for tn in &thread.1 {
|
||||||
|
if let Some(s) = first_subject(&tn) {
|
||||||
|
return Some(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
fn set_title(title: &str) {
|
fn set_title(title: &str) {
|
||||||
seed::document().set_title(&format!("lb: {}", title));
|
seed::document().set_title(&format!("lb: {}", title));
|
||||||
}
|
}
|
||||||
@@ -326,7 +500,7 @@ fn view_mobile_search_results(query: &str, search_results: &shared::SearchResult
|
|||||||
],
|
],
|
||||||
td![C!["subject"], tags_chiclet(&r.tags), " ", &r.subject],
|
td![C!["subject"], tags_chiclet(&r.tags), " ", &r.subject],
|
||||||
td![C!["date"], &r.date_relative],
|
td![C!["date"], &r.date_relative],
|
||||||
ev(Ev::Click, move |_| Msg::ShowRequest(tid)),
|
ev(Ev::Click, move |_| Msg::ShowPrettyRequest(tid)),
|
||||||
]
|
]
|
||||||
*/
|
*/
|
||||||
let tid = r.thread.clone();
|
let tid = r.thread.clone();
|
||||||
@@ -334,14 +508,13 @@ fn view_mobile_search_results(query: &str, search_results: &shared::SearchResult
|
|||||||
div![
|
div![
|
||||||
C!["subject"],
|
C!["subject"],
|
||||||
&r.subject,
|
&r.subject,
|
||||||
ev(Ev::Click, move |_| Msg::ShowRequest(tid)),
|
ev(Ev::Click, move |_| Msg::ShowPrettyRequest(tid)),
|
||||||
],
|
],
|
||||||
div![
|
div![
|
||||||
span![C!["from"], pretty_authors(&r.authors)],
|
span![C!["from"], pretty_authors(&r.authors)],
|
||||||
span![C!["tags"], tags_chiclet(&r.tags, true)],
|
span![C!["tags"], tags_chiclet(&r.tags, true)],
|
||||||
],
|
],
|
||||||
span![C!["date"], &r.date_relative],
|
span![C!["date"], &r.date_relative],
|
||||||
hr![],
|
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
let first = search_results.page * search_results.results_per_page;
|
let first = search_results.page * search_results.results_per_page;
|
||||||
@@ -412,12 +585,8 @@ fn view_search_pager(start: usize, count: usize, total: usize) -> Node<Msg> {
|
|||||||
nav![
|
nav![
|
||||||
C!["pagination"],
|
C!["pagination"],
|
||||||
a![
|
a![
|
||||||
C![
|
C!["pagination-previous", "button",],
|
||||||
"pagination-previous",
|
IF!(is_first => attrs!{ At::Disabled=>true }),
|
||||||
"button",
|
|
||||||
IF!(is_first => "is-static"),
|
|
||||||
IF!(is_first => "is-info"),
|
|
||||||
],
|
|
||||||
"<",
|
"<",
|
||||||
ev(Ev::Click, |_| Msg::PreviousPage)
|
ev(Ev::Click, |_| Msg::PreviousPage)
|
||||||
],
|
],
|
||||||
@@ -434,6 +603,61 @@ fn view_search_pager(start: usize, count: usize, total: usize) -> Node<Msg> {
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn view_thread(messages: &[shared::Message]) -> Node<Msg> {
|
||||||
|
div!["TODO(wathiede): view_thread(messages)"]
|
||||||
|
/*
|
||||||
|
assert_eq!(thread_set.0.len(), 1);
|
||||||
|
let thread = &thread_set.0[0];
|
||||||
|
assert_eq!(thread.0.len(), 1);
|
||||||
|
let thread_node = &thread.0[0];
|
||||||
|
let subject = first_subject(&thread_node).unwrap_or("<No subject>".to_string());
|
||||||
|
set_title(&subject);
|
||||||
|
div![
|
||||||
|
C!["container"],
|
||||||
|
h1![C!["title"], subject],
|
||||||
|
view_message(&thread_node),
|
||||||
|
a![
|
||||||
|
attrs! {At::Href=>api::original(&thread_node.0.as_ref().expect("message missing").id)},
|
||||||
|
"Original"
|
||||||
|
],
|
||||||
|
/*
|
||||||
|
div![
|
||||||
|
C!["debug"],
|
||||||
|
"Add zippy for debug dump",
|
||||||
|
view_debug_thread_set(thread_set)
|
||||||
|
] /* pre![format!("Thread: {:#?}", thread_set).replace(" ", " ")] */
|
||||||
|
*/
|
||||||
|
]
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
fn view_debug_thread_set(thread_set: &ThreadSet) -> Node<Msg> {
|
||||||
|
ul![thread_set
|
||||||
|
.0
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(i, t)| { li!["t", i, ": ", view_debug_thread(t),] })]
|
||||||
|
}
|
||||||
|
fn view_debug_thread(thread: &Thread) -> Node<Msg> {
|
||||||
|
ul![thread
|
||||||
|
.0
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(i, tn)| { li!["tn", i, ": ", view_debug_thread_node(tn),] })]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn view_debug_thread_node(thread_node: &ThreadNode) -> Node<Msg> {
|
||||||
|
ul![
|
||||||
|
IF!(thread_node.0.is_some()=>li!["tn id:", &thread_node.0.as_ref().unwrap().id]),
|
||||||
|
thread_node.1.iter().enumerate().map(|(i, tn)| li![
|
||||||
|
"tn",
|
||||||
|
i,
|
||||||
|
": ",
|
||||||
|
view_debug_thread_node(tn)
|
||||||
|
])
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
fn view_header(query: &str, refresh_request: &RefreshingState) -> Node<Msg> {
|
fn view_header(query: &str, refresh_request: &RefreshingState) -> Node<Msg> {
|
||||||
let is_loading = refresh_request == &RefreshingState::Loading;
|
let is_loading = refresh_request == &RefreshingState::Loading;
|
||||||
let is_error = if let RefreshingState::Error(err) = refresh_request {
|
let is_error = if let RefreshingState::Error(err) = refresh_request {
|
||||||
@@ -509,34 +733,29 @@ fn view_footer(render_time_ms: u128) -> Node<Msg> {
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn view_thread(messages: &[shared::Message]) -> Node<Msg> {
|
|
||||||
div![
|
|
||||||
"MESSAGES GO HERE",
|
|
||||||
ol![messages.iter().map(|msg| li![format!("{:?}", msg)])]
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
||||||
fn view_desktop(model: &Model) -> Node<Msg> {
|
fn view_desktop(model: &Model) -> Node<Msg> {
|
||||||
let content = match &model.context {
|
let content = match &model.context {
|
||||||
Context::None => div![h1!["Loading"]],
|
Context::None => div![h1!["Loading"]],
|
||||||
Context::Thread(thread_set) => view_thread(thread_set),
|
Context::Thread(messages) => view_thread(messages),
|
||||||
Context::Search(search_results) => view_search_results(&model.query, search_results),
|
Context::Search(search_results) => view_search_results(&model.query, search_results),
|
||||||
};
|
};
|
||||||
div![
|
div![
|
||||||
view_header(&model.query, &model.refreshing_state),
|
view_header(&model.query, &model.refreshing_state),
|
||||||
section![C!["section"], div![C!["container"], content],]
|
section![C!["section"], content],
|
||||||
|
view_header(&model.query, &model.refreshing_state),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn view_mobile(model: &Model) -> Node<Msg> {
|
fn view_mobile(model: &Model) -> Node<Msg> {
|
||||||
let content = match &model.context {
|
let content = match &model.context {
|
||||||
Context::None => div![h1!["Loading"]],
|
Context::None => div![h1!["Loading"]],
|
||||||
Context::Thread(thread_set) => view_thread(thread_set),
|
Context::Thread(messages) => view_thread(messages),
|
||||||
Context::Search(search_results) => view_mobile_search_results(&model.query, search_results),
|
Context::Search(search_results) => view_mobile_search_results(&model.query, search_results),
|
||||||
};
|
};
|
||||||
div![
|
div![
|
||||||
view_header(&model.query, &model.refreshing_state),
|
view_header(&model.query, &model.refreshing_state),
|
||||||
section![C!["section"], div![C!["content"], content],]
|
section![C!["section"], div![C!["content"], content]],
|
||||||
|
view_header(&model.query, &model.refreshing_state),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
193
web/src/nm.rs
193
web/src/nm.rs
@@ -1,193 +0,0 @@
|
|||||||
use notmuch::{Content, Part, Thread, ThreadNode, ThreadSet};
|
|
||||||
use seed::{prelude::*, *};
|
|
||||||
use serde::de::Deserialize;
|
|
||||||
|
|
||||||
use crate::{api, set_title, Msg};
|
|
||||||
|
|
||||||
pub async fn show_request(tid: &str) -> fetch::Result<ThreadSet> {
|
|
||||||
let b = Request::new(api::show(tid))
|
|
||||||
.method(Method::Get)
|
|
||||||
.fetch()
|
|
||||||
.await?
|
|
||||||
.check_status()?
|
|
||||||
.bytes()
|
|
||||||
.await?;
|
|
||||||
let mut deserializer = serde_json::Deserializer::from_slice(&b);
|
|
||||||
deserializer.disable_recursion_limit();
|
|
||||||
Ok(ThreadSet::deserialize(&mut deserializer)
|
|
||||||
.map_err(|_| FetchError::JsonError(fetch::JsonError::Serde(JsValue::NULL)))?)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn view_thread(thread_set: &ThreadSet) -> Node<Msg> {
|
|
||||||
assert_eq!(thread_set.0.len(), 1);
|
|
||||||
let thread = &thread_set.0[0];
|
|
||||||
assert_eq!(thread.0.len(), 1);
|
|
||||||
let thread_node = &thread.0[0];
|
|
||||||
let subject = first_subject(&thread_node).unwrap_or("<No subject>".to_string());
|
|
||||||
set_title(&subject);
|
|
||||||
div![
|
|
||||||
h1![subject],
|
|
||||||
a![
|
|
||||||
attrs! {At::Href=>api::original(&thread_node.0.as_ref().expect("message missing").id)},
|
|
||||||
"Original"
|
|
||||||
],
|
|
||||||
view_message(&thread_node),
|
|
||||||
div![
|
|
||||||
C!["debug"],
|
|
||||||
"Add zippy for debug dump",
|
|
||||||
view_debug_thread_set(thread_set)
|
|
||||||
] /* pre![format!("Thread: {:#?}", thread_set).replace(" ", " ")] */
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
||||||
// <subject>
|
|
||||||
// <tags>
|
|
||||||
//
|
|
||||||
// <from1> <date>
|
|
||||||
// <to1>
|
|
||||||
// <content1>
|
|
||||||
// <zippy>
|
|
||||||
// <children1>
|
|
||||||
// </zippy>
|
|
||||||
//
|
|
||||||
// <from2> <date>
|
|
||||||
// <to2>
|
|
||||||
// <body2>
|
|
||||||
fn view_message(thread: &ThreadNode) -> Node<Msg> {
|
|
||||||
let message = thread.0.as_ref().expect("ThreadNode missing Message");
|
|
||||||
let children = &thread.1;
|
|
||||||
div![
|
|
||||||
C!["message"],
|
|
||||||
/* TODO(wathiede): collect all the tags and show them here. */
|
|
||||||
/* TODO(wathiede): collect all the attachments from all the subparts */
|
|
||||||
div![C!["header"], "From: ", &message.headers.from],
|
|
||||||
div![C!["header"], "Date: ", &message.headers.date],
|
|
||||||
div![C!["header"], "To: ", &message.headers.to],
|
|
||||||
hr![],
|
|
||||||
div![
|
|
||||||
C!["body"],
|
|
||||||
match &message.body {
|
|
||||||
Some(body) => view_body(body.as_slice()),
|
|
||||||
None => div!["<no body>"],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
children.iter().map(view_message)
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
||||||
fn view_body(body: &[Part]) -> Node<Msg> {
|
|
||||||
div![body.iter().map(view_part)]
|
|
||||||
}
|
|
||||||
|
|
||||||
fn view_text_plain(content: &Option<Content>) -> Node<Msg> {
|
|
||||||
match &content {
|
|
||||||
Some(Content::String(content)) => p![C!["view-part-text-plain"], content],
|
|
||||||
_ => div![
|
|
||||||
C!["error"],
|
|
||||||
format!("Unhandled content enum for text/plain"),
|
|
||||||
],
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn view_part(part: &Part) -> Node<Msg> {
|
|
||||||
match part.content_type.as_str() {
|
|
||||||
"text/plain" => view_text_plain(&part.content),
|
|
||||||
"text/html" => {
|
|
||||||
if let Some(Content::String(html)) = &part.content {
|
|
||||||
let inliner = css_inline::CSSInliner::options()
|
|
||||||
.load_remote_stylesheets(false)
|
|
||||||
.remove_style_tags(true)
|
|
||||||
.build();
|
|
||||||
let inlined = inliner.inline(html).expect("failed to inline CSS");
|
|
||||||
|
|
||||||
return div![C!["view-part-text-html"], div!["TEST"], raw![&inlined]];
|
|
||||||
} else {
|
|
||||||
div![
|
|
||||||
C!["error"],
|
|
||||||
format!("Unhandled content enum for multipart/mixed"),
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://en.wikipedia.org/wiki/MIME#alternative
|
|
||||||
// RFC1341 states: In general, user agents that compose multipart/alternative entities
|
|
||||||
// should place the body parts in increasing order of preference, that is, with the
|
|
||||||
// preferred format last.
|
|
||||||
"multipart/alternative" => {
|
|
||||||
if let Some(Content::Multipart(parts)) = &part.content {
|
|
||||||
for part in parts.iter().rev() {
|
|
||||||
if part.content_type == "text/html" {
|
|
||||||
if let Some(Content::String(html)) = &part.content {
|
|
||||||
let inliner = css_inline::CSSInliner::options()
|
|
||||||
.load_remote_stylesheets(false)
|
|
||||||
.remove_style_tags(true)
|
|
||||||
.build();
|
|
||||||
let inlined = inliner.inline(html).expect("failed to inline CSS");
|
|
||||||
return div![Node::from_html(None, &inlined)];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if part.content_type == "text/plain" {
|
|
||||||
return view_text_plain(&part.content);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
div!["No known multipart/alternative parts"]
|
|
||||||
} else {
|
|
||||||
div![
|
|
||||||
C!["error"],
|
|
||||||
format!("multipart/alternative with non-multipart content"),
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
"multipart/mixed" => match &part.content {
|
|
||||||
Some(Content::Multipart(parts)) => div![parts.iter().map(view_part)],
|
|
||||||
_ => div![
|
|
||||||
C!["error"],
|
|
||||||
format!("Unhandled content enum for multipart/mixed"),
|
|
||||||
],
|
|
||||||
},
|
|
||||||
_ => div![
|
|
||||||
C!["error"],
|
|
||||||
format!("Unhandled content type: {}", part.content_type)
|
|
||||||
],
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn first_subject(thread: &ThreadNode) -> Option<String> {
|
|
||||||
if let Some(msg) = &thread.0 {
|
|
||||||
return Some(msg.headers.subject.clone());
|
|
||||||
} else {
|
|
||||||
for tn in &thread.1 {
|
|
||||||
if let Some(s) = first_subject(&tn) {
|
|
||||||
return Some(s);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
fn view_debug_thread_set(thread_set: &ThreadSet) -> Node<Msg> {
|
|
||||||
ul![thread_set
|
|
||||||
.0
|
|
||||||
.iter()
|
|
||||||
.enumerate()
|
|
||||||
.map(|(i, t)| { li!["t", i, ": ", view_debug_thread(t),] })]
|
|
||||||
}
|
|
||||||
fn view_debug_thread(thread: &Thread) -> Node<Msg> {
|
|
||||||
ul![thread
|
|
||||||
.0
|
|
||||||
.iter()
|
|
||||||
.enumerate()
|
|
||||||
.map(|(i, tn)| { li!["tn", i, ": ", view_debug_thread_node(tn),] })]
|
|
||||||
}
|
|
||||||
|
|
||||||
fn view_debug_thread_node(thread_node: &ThreadNode) -> Node<Msg> {
|
|
||||||
ul![
|
|
||||||
IF!(thread_node.0.is_some()=>li!["tn id:", &thread_node.0.as_ref().unwrap().id]),
|
|
||||||
thread_node.1.iter().enumerate().map(|(i, tn)| li![
|
|
||||||
"tn",
|
|
||||||
i,
|
|
||||||
": ",
|
|
||||||
view_debug_thread_node(tn)
|
|
||||||
])
|
|
||||||
]
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user