core: impl TryFrom subject for ListRequest
parent
1c67750c5c
commit
c7df26c4ac
|
@ -23,6 +23,7 @@ use melib::Envelope;
|
|||
use models::changesets::*;
|
||||
use rusqlite::Connection as DbConnection;
|
||||
use rusqlite::OptionalExtension;
|
||||
use std::convert::TryFrom;
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
use std::process::{Command, Stdio};
|
||||
|
@ -590,29 +591,9 @@ impl Database {
|
|||
if !addr.contains_address(&list.list_address()) {
|
||||
return true;
|
||||
}
|
||||
if let Err(err) = self.request(
|
||||
list,
|
||||
match subaddr.as_str() {
|
||||
"subscribe" | "request" if env.subject().trim() == "subscribe" => {
|
||||
ListRequest::Subscribe
|
||||
}
|
||||
"unsubscribe" | "request" if env.subject().trim() == "unsubscribe" => {
|
||||
ListRequest::Unsubscribe
|
||||
}
|
||||
"request" => ListRequest::Other(env.subject().trim().to_string()),
|
||||
_ => {
|
||||
trace!(
|
||||
"unknown action = {} for addresses {:?} in list {}",
|
||||
subaddr,
|
||||
env.from(),
|
||||
list
|
||||
);
|
||||
ListRequest::Other(subaddr.trim().to_string())
|
||||
}
|
||||
},
|
||||
env,
|
||||
raw,
|
||||
) {
|
||||
if let Err(err) = ListRequest::try_from((subaddr.as_str(), env))
|
||||
.and_then(|req| self.request(list, req, env, raw))
|
||||
{
|
||||
info!("Processing request returned error: {}", err);
|
||||
}
|
||||
false
|
||||
|
|
|
@ -29,6 +29,11 @@ error_chain! {
|
|||
description("Not found")
|
||||
display("This {} is not present in the database.", model)
|
||||
}
|
||||
|
||||
InvalidRequest(reason: String) {
|
||||
description("List request is invalid")
|
||||
display("Your list request has been found invalid: {}.", reason)
|
||||
}
|
||||
}
|
||||
foreign_links {
|
||||
Sql(rusqlite::Error);
|
||||
|
|
|
@ -109,9 +109,9 @@ impl PostFilter for AddListHeaders {
|
|||
trace!("Running AddListHeaders filter");
|
||||
let (mut headers, body) = melib::email::parser::mail(&post.bytes).unwrap();
|
||||
let list_id = ctx.list.list_id();
|
||||
let reply_to = format!("<{}>", ctx.list.address);
|
||||
let sender = format!("<{}>", ctx.list.address);
|
||||
headers.push((&b"List-ID"[..], list_id.as_bytes()));
|
||||
headers.push((&b"Reply-To"[..], reply_to.as_bytes()));
|
||||
headers.push((&b"Sender"[..], sender.as_bytes()));
|
||||
let list_post = ctx.list.list_post();
|
||||
let list_unsubscribe = ctx.list.list_unsubscribe();
|
||||
let list_archive = ctx.list.list_archive();
|
||||
|
|
|
@ -236,6 +236,27 @@ impl std::fmt::Display for ListRequest {
|
|||
}
|
||||
}
|
||||
|
||||
impl<S: AsRef<str>> std::convert::TryFrom<(S, &melib::Envelope)> for ListRequest {
|
||||
type Error = crate::Error;
|
||||
|
||||
fn try_from((val, env): (S, &melib::Envelope)) -> std::result::Result<Self, Self::Error> {
|
||||
let val = val.as_ref();
|
||||
Ok(match val {
|
||||
"subscribe" | "request" if env.subject().trim() == "subscribe" => {
|
||||
ListRequest::Subscribe
|
||||
}
|
||||
"unsubscribe" | "request" if env.subject().trim() == "unsubscribe" => {
|
||||
ListRequest::Unsubscribe
|
||||
}
|
||||
"request" => ListRequest::Other(env.subject().trim().to_string()),
|
||||
_ => {
|
||||
trace!("unknown action = {} for addresses {:?}", val, env.from(),);
|
||||
ListRequest::Other(val.trim().to_string())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct NewListPost<'s> {
|
||||
pub list: i64,
|
||||
|
|
Loading…
Reference in New Issue