mirror of
https://github.com/chatmail/core.git
synced 2026-04-28 02:46:29 +03:00
Adapt to async, set first subject to 'Message from <sender name>'
This commit is contained in:
@@ -749,24 +749,30 @@ async fn add_parts(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|| -> Result<()> {
|
async fn update_last_subject(
|
||||||
let mut chat = Chat::load_from_db(context, *chat_id)?;
|
context: &Context,
|
||||||
|
chat_id: ChatId,
|
||||||
|
mime_parser: &MimeMessage,
|
||||||
|
) -> Result<()> {
|
||||||
|
let mut chat = Chat::load_from_db(context, chat_id).await?;
|
||||||
chat.param.set(
|
chat.param.set(
|
||||||
Param::LastSubject,
|
Param::LastSubject,
|
||||||
mime_parser
|
mime_parser
|
||||||
.get_subject()
|
.get_subject()
|
||||||
.ok_or_else(|| format_err!("No subject in email"))?,
|
.ok_or_else(|| format_err!("No subject in email"))?,
|
||||||
);
|
);
|
||||||
chat.update_param(context)?;
|
chat.update_param(context).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}()
|
}
|
||||||
.unwrap_or_else(|e| {
|
update_last_subject(context, chat_id, mime_parser)
|
||||||
warn!(
|
.await
|
||||||
context,
|
.unwrap_or_else(|e| {
|
||||||
"Could not update LastSubject of chat: {}",
|
warn!(
|
||||||
e.to_string()
|
context,
|
||||||
)
|
"Could not update LastSubject of chat: {}",
|
||||||
});
|
e.to_string()
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -374,16 +374,19 @@ impl<'a, 'b> MimeFactory<'a, 'b> {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
let raw = message::get_summarytext_by_raw(
|
let self_name =
|
||||||
self.msg.viewtype,
|
match self.context.get_config(Config::Displayname).await {
|
||||||
Some(""),
|
Some(name) => Some(name),
|
||||||
&self.msg.param,
|
None => self.context.get_config(Config::Displayname).await,
|
||||||
32,
|
}
|
||||||
self.context,
|
.unwrap_or_else(|| "Delta Chat".to_string());
|
||||||
)
|
|
||||||
.await;
|
self.context
|
||||||
let raw_subject = raw.lines().next().unwrap_or_default();
|
.stock_string_repl_str(
|
||||||
format!("Chat: {}", raw_subject)
|
StockMessage::SubjectForNewContact,
|
||||||
|
self_name,
|
||||||
|
)
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1262,20 +1265,25 @@ mod tests {
|
|||||||
|
|
||||||
use crate::test_utils::{dummy_context, TestContext};
|
use crate::test_utils::{dummy_context, TestContext};
|
||||||
|
|
||||||
fn configured_offline_context() -> TestContext {
|
async fn configured_offline_context() -> TestContext {
|
||||||
let t = dummy_context();
|
let t = dummy_context().await;
|
||||||
t.ctx
|
t.ctx
|
||||||
.set_config(Config::Addr, Some("alice@example.org"))
|
.set_config(Config::Addr, Some("alice@example.org"))
|
||||||
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
t.ctx
|
t.ctx
|
||||||
.set_config(Config::ConfiguredAddr, Some("alice@example.org"))
|
.set_config(Config::ConfiguredAddr, Some("alice@example.org"))
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
t.ctx
|
||||||
|
.set_config(Config::Configured, Some("1"))
|
||||||
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
t.ctx.set_config(Config::Configured, Some("1")).unwrap();
|
|
||||||
t
|
t
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[async_std::test]
|
||||||
fn test_subject() {
|
async fn test_subject() {
|
||||||
// 1.: Receive a mail from an MUA or Delta Chat
|
// 1.: Receive a mail from an MUA or Delta Chat
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
msg_to_subject_str(
|
msg_to_subject_str(
|
||||||
@@ -1286,7 +1294,8 @@ mod tests {
|
|||||||
Date: Sun, 22 Mar 2020 22:37:56 +0000\n\
|
Date: Sun, 22 Mar 2020 22:37:56 +0000\n\
|
||||||
\n\
|
\n\
|
||||||
hello\n"
|
hello\n"
|
||||||
),
|
)
|
||||||
|
.await,
|
||||||
"Re: Chat: hello"
|
"Re: Chat: hello"
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -1301,28 +1310,39 @@ mod tests {
|
|||||||
Date: Sun, 22 Mar 2020 22:37:56 +0000\n\
|
Date: Sun, 22 Mar 2020 22:37:56 +0000\n\
|
||||||
\n\
|
\n\
|
||||||
hello\n"
|
hello\n"
|
||||||
),
|
)
|
||||||
|
.await,
|
||||||
"Re: Chat: hello"
|
"Re: Chat: hello"
|
||||||
);
|
);
|
||||||
|
|
||||||
// 3. Send the first message to a new contact
|
// 3. Send the first message to a new contact
|
||||||
let t = configured_offline_context();
|
let t = configured_offline_context().await;
|
||||||
t.ctx.set_config(Config::ShowEmails, Some("2")).unwrap();
|
t.ctx
|
||||||
|
.set_config(Config::ShowEmails, Some("2"))
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let contact_id =
|
let contact_id =
|
||||||
Contact::add_or_lookup(&t.ctx, "Dave", "dave@example.org", Origin::ManuallyCreated)
|
Contact::add_or_lookup(&t.ctx, "Dave", "dave@example.org", Origin::ManuallyCreated)
|
||||||
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.0;
|
.0;
|
||||||
|
|
||||||
let chat_id = chat::create_by_contact_id(&t.ctx, contact_id).unwrap();
|
let chat_id = chat::create_by_contact_id(&t.ctx, contact_id)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let mut new_msg = Message::new(Viewtype::Text);
|
let mut new_msg = Message::new(Viewtype::Text);
|
||||||
new_msg.set_text(Some("Hi".to_string()));
|
new_msg.set_text(Some("Hi".to_string()));
|
||||||
new_msg.chat_id = chat_id;
|
new_msg.chat_id = chat_id;
|
||||||
chat::prepare_msg(&t.ctx, chat_id, &mut new_msg).unwrap();
|
chat::prepare_msg(&t.ctx, chat_id, &mut new_msg)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let mf = MimeFactory::from_msg(&t.ctx, &new_msg, false).unwrap();
|
let mf = MimeFactory::from_msg(&t.ctx, &new_msg, false)
|
||||||
assert_eq!(mf.subject_str(), "Chat: ");
|
.await
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(mf.subject_str().await, "Chat: ");
|
||||||
|
|
||||||
// 4. Receive messages with unicode characters and make sure that we do not panic (we do not care about the result)
|
// 4. Receive messages with unicode characters and make sure that we do not panic (we do not care about the result)
|
||||||
msg_to_subject_str(
|
msg_to_subject_str(
|
||||||
@@ -1335,7 +1355,8 @@ mod tests {
|
|||||||
\n\
|
\n\
|
||||||
hello\n"
|
hello\n"
|
||||||
.as_bytes(),
|
.as_bytes(),
|
||||||
);
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
msg_to_subject_str(
|
msg_to_subject_str(
|
||||||
"From: Charlie <charlie@example.org>\n\
|
"From: Charlie <charlie@example.org>\n\
|
||||||
@@ -1347,28 +1368,40 @@ mod tests {
|
|||||||
\n\
|
\n\
|
||||||
hello\n"
|
hello\n"
|
||||||
.as_bytes(),
|
.as_bytes(),
|
||||||
);
|
)
|
||||||
|
.await;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn msg_to_subject_str(imf_raw: &[u8]) -> String {
|
async fn msg_to_subject_str(imf_raw: &[u8]) -> String {
|
||||||
use crate::chatlist::Chatlist;
|
use crate::chatlist::Chatlist;
|
||||||
use crate::dc_receive_imf::dc_receive_imf;
|
use crate::dc_receive_imf::dc_receive_imf;
|
||||||
|
|
||||||
let t = configured_offline_context();
|
let t = configured_offline_context().await;
|
||||||
t.ctx.set_config(Config::ShowEmails, Some("2")).unwrap();
|
t.ctx
|
||||||
|
.set_config(Config::ShowEmails, Some("2"))
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
dc_receive_imf(&t.ctx, imf_raw, "INBOX", 1, false).unwrap();
|
dc_receive_imf(&t.ctx, imf_raw, "INBOX", 1, false)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let chats = Chatlist::try_load(&t.ctx, 0, None, None).unwrap();
|
let chats = Chatlist::try_load(&t.ctx, 0, None, None).await.unwrap();
|
||||||
|
|
||||||
let chat_id = chat::create_by_msg_id(&t.ctx, chats.get_msg_id(0).unwrap()).unwrap();
|
let chat_id = chat::create_by_msg_id(&t.ctx, chats.get_msg_id(0).unwrap())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let mut new_msg = Message::new(Viewtype::Text);
|
let mut new_msg = Message::new(Viewtype::Text);
|
||||||
new_msg.set_text(Some("Hi".to_string()));
|
new_msg.set_text(Some("Hi".to_string()));
|
||||||
new_msg.chat_id = chat_id;
|
new_msg.chat_id = chat_id;
|
||||||
chat::prepare_msg(&t.ctx, chat_id, &mut new_msg).unwrap();
|
chat::prepare_msg(&t.ctx, chat_id, &mut new_msg)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let mf = MimeFactory::from_msg(&t.ctx, &new_msg, false).unwrap();
|
let mf = MimeFactory::from_msg(&t.ctx, &new_msg, false)
|
||||||
mf.subject_str()
|
.await
|
||||||
|
.unwrap();
|
||||||
|
mf.subject_str().await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -179,6 +179,9 @@ pub enum StockMessage {
|
|||||||
|
|
||||||
#[strum(props(fallback = "Unknown Sender for this chat. See 'info' for more details."))]
|
#[strum(props(fallback = "Unknown Sender for this chat. See 'info' for more details."))]
|
||||||
UnknownSenderForChat = 72,
|
UnknownSenderForChat = 72,
|
||||||
|
|
||||||
|
#[strum(props(fallback = "Message from %1$s"))]
|
||||||
|
SubjectForNewContact = 73,
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
Reference in New Issue
Block a user