From fc3de90e1d6a4643fa017937445fa2b457b2c832 Mon Sep 17 00:00:00 2001 From: jikstra Date: Tue, 14 Jan 2020 19:57:41 +0100 Subject: [PATCH] Implement chatlist.get_summary_json --- src/chatlist.rs | 33 ++++++++++++++++++++------------- src/lot.rs | 3 ++- src/message.rs | 8 ++++++-- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/chatlist.rs b/src/chatlist.rs index 0bb43dcab..c5f64416d 100644 --- a/src/chatlist.rs +++ b/src/chatlist.rs @@ -5,7 +5,7 @@ use crate::constants::*; use crate::contact::*; use crate::context::*; use crate::error::Result; -use crate::lot::Lot; +use crate::lot::{Lot, Meaning}; use crate::message::{Message, MessageState, MsgId, MessageSummary}; use crate::stock::StockMessage; use serde::{Deserialize, Serialize}; @@ -53,7 +53,7 @@ pub struct Chatlist { #[derive(Debug, Default)] pub struct ChatlistSummary { username: Option, - chat_type: u32, + chat_type: Meaning, message_excerpt: Option, timestamp: i64, message_state: MessageState @@ -305,7 +305,6 @@ impl Chatlist { if index >= self.ids.len() { return Lot { text2: Some("ErrBadChatlistIndex".to_string()), ..Default::default() }; - } let chat_loaded: Chat; @@ -319,14 +318,23 @@ impl Chatlist { }; let lastmsg_id = self.ids[index].1; - self._get_summary(context, chat, lastmsg_id) + self._get_summary(context, chat, lastmsg_id).into_lot() } - pub fn get_summary_json(&self, context: &Context, chat_id: u32) -> ChatlistSummary { - ChatlistSummary { ..Default::default() } + pub fn get_summary_json(&self, context: &Context, chat_id: u32) -> String { + + if let Ok(chat) = Chat::load_from_db(context, chat_id) { + if let Some(lastmsg_id) = chat.get_lastmsg_id(context) { + self._get_summary(context, &chat, lastmsg_id) + } else { + MessageSummary::default() + } + } else { + MessageSummary::default() + }.to_json() } - pub fn _get_summary(&self, context: &Context, chat: &Chat, lastmsg_id: MsgId) -> Lot { + pub fn _get_summary(&self, context: &Context, chat: &Chat, lastmsg_id: MsgId) -> MessageSummary { let mut lastcontact = None; let lastmsg = if let Ok(lastmsg) = Message::load_from_db(context, lastmsg_id) { if lastmsg.from_id != DC_CONTACT_ID_SELF @@ -341,15 +349,14 @@ impl Chatlist { }; if chat.id.is_archived_link() { - Lot { text2: None, .. Default::default() } - } else if lastmsg.is_none() || lastmsg.as_ref().unwrap().from_id == DC_CONTACT_ID_UNDEFINED - { - Lot { - text2: Some(context.stock_str(StockMessage::NoMessages).to_string()), + MessageSummary { summarytext: None, .. Default::default() } + } else if lastmsg.is_none() || lastmsg.as_ref().unwrap().from_id == DC_CONTACT_ID_UNDEFINED { + MessageSummary { + summarytext: Some(context.stock_str(StockMessage::NoMessages).to_string()), .. Default::default() } } else { - MessageSummary::new(&mut lastmsg.unwrap(), chat, lastcontact.as_ref(), context).into_lot() + MessageSummary::new(&mut lastmsg.unwrap(), chat, lastcontact.as_ref(), context) } } } diff --git a/src/lot.rs b/src/lot.rs index e1c14e6e3..0e4c91dd2 100644 --- a/src/lot.rs +++ b/src/lot.rs @@ -1,4 +1,5 @@ use deltachat_derive::{FromSql, ToSql}; +use serde::{Deserialize, Serialize}; /// An object containing a set of values. /// The meaning of the values is defined by the function returning the object. @@ -20,7 +21,7 @@ pub struct Lot { } #[repr(u8)] -#[derive(Debug, Display, Clone, Copy, PartialEq, Eq, FromPrimitive, ToPrimitive, ToSql, FromSql)] +#[derive(Debug, Display, Clone, Copy, PartialEq, Eq, FromPrimitive, ToPrimitive, ToSql, FromSql, Serialize, Deserialize)] pub enum Meaning { None = 0, Text1Draft = 1, diff --git a/src/message.rs b/src/message.rs index d0851f430..7df0a7627 100644 --- a/src/message.rs +++ b/src/message.rs @@ -607,7 +607,7 @@ impl Message { } } -#[derive(Debug, Clone, Copy, PartialEq, Eq, FromPrimitive, ToPrimitive, ToSql, FromSql)] +#[derive(Debug, Display, Clone, Copy, PartialEq, Eq, FromPrimitive, ToPrimitive, ToSql, FromSql, Serialize, Deserialize)] #[repr(i32)] pub enum MessageState { Undefined = 0, @@ -708,7 +708,7 @@ impl MessageState { } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Serialize, Deserialize)] pub struct MessageSummary { pub text1: Option, pub text1_meaning: Meaning, @@ -783,6 +783,10 @@ impl MessageSummary { .. Default::default() } } + + pub fn to_json(&self) -> String { + serde_json::to_string(&self).unwrap_or_else(|_| "".to_string()) + } } pub fn get_msg_info(context: &Context, msg_id: MsgId) -> String {