Implement chatlist.get_summary_json

This commit is contained in:
jikstra
2020-01-14 19:57:41 +01:00
parent 9dedfb51ad
commit fc3de90e1d
3 changed files with 28 additions and 16 deletions

View File

@@ -5,7 +5,7 @@ use crate::constants::*;
use crate::contact::*; use crate::contact::*;
use crate::context::*; use crate::context::*;
use crate::error::Result; use crate::error::Result;
use crate::lot::Lot; use crate::lot::{Lot, Meaning};
use crate::message::{Message, MessageState, MsgId, MessageSummary}; use crate::message::{Message, MessageState, MsgId, MessageSummary};
use crate::stock::StockMessage; use crate::stock::StockMessage;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@@ -53,7 +53,7 @@ pub struct Chatlist {
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct ChatlistSummary { pub struct ChatlistSummary {
username: Option<String>, username: Option<String>,
chat_type: u32, chat_type: Meaning,
message_excerpt: Option<String>, message_excerpt: Option<String>,
timestamp: i64, timestamp: i64,
message_state: MessageState message_state: MessageState
@@ -305,7 +305,6 @@ impl Chatlist {
if index >= self.ids.len() { if index >= self.ids.len() {
return Lot { text2: Some("ErrBadChatlistIndex".to_string()), ..Default::default() }; return Lot { text2: Some("ErrBadChatlistIndex".to_string()), ..Default::default() };
} }
let chat_loaded: Chat; let chat_loaded: Chat;
@@ -319,14 +318,23 @@ impl Chatlist {
}; };
let lastmsg_id = self.ids[index].1; 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 { pub fn get_summary_json(&self, context: &Context, chat_id: u32) -> String {
ChatlistSummary { ..Default::default() }
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 mut lastcontact = None;
let lastmsg = if let Ok(lastmsg) = Message::load_from_db(context, lastmsg_id) { let lastmsg = if let Ok(lastmsg) = Message::load_from_db(context, lastmsg_id) {
if lastmsg.from_id != DC_CONTACT_ID_SELF if lastmsg.from_id != DC_CONTACT_ID_SELF
@@ -341,15 +349,14 @@ impl Chatlist {
}; };
if chat.id.is_archived_link() { if chat.id.is_archived_link() {
Lot { text2: None, .. Default::default() } MessageSummary { summarytext: None, .. Default::default() }
} else if lastmsg.is_none() || lastmsg.as_ref().unwrap().from_id == DC_CONTACT_ID_UNDEFINED } else if lastmsg.is_none() || lastmsg.as_ref().unwrap().from_id == DC_CONTACT_ID_UNDEFINED {
{ MessageSummary {
Lot { summarytext: Some(context.stock_str(StockMessage::NoMessages).to_string()),
text2: Some(context.stock_str(StockMessage::NoMessages).to_string()),
.. Default::default() .. Default::default()
} }
} else { } else {
MessageSummary::new(&mut lastmsg.unwrap(), chat, lastcontact.as_ref(), context).into_lot() MessageSummary::new(&mut lastmsg.unwrap(), chat, lastcontact.as_ref(), context)
} }
} }
} }

View File

@@ -1,4 +1,5 @@
use deltachat_derive::{FromSql, ToSql}; use deltachat_derive::{FromSql, ToSql};
use serde::{Deserialize, Serialize};
/// An object containing a set of values. /// An object containing a set of values.
/// The meaning of the values is defined by the function returning the object. /// The meaning of the values is defined by the function returning the object.
@@ -20,7 +21,7 @@ pub struct Lot {
} }
#[repr(u8)] #[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 { pub enum Meaning {
None = 0, None = 0,
Text1Draft = 1, Text1Draft = 1,

View File

@@ -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)] #[repr(i32)]
pub enum MessageState { pub enum MessageState {
Undefined = 0, Undefined = 0,
@@ -708,7 +708,7 @@ impl MessageState {
} }
} }
#[derive(Debug, Default)] #[derive(Debug, Default, Serialize, Deserialize)]
pub struct MessageSummary { pub struct MessageSummary {
pub text1: Option<String>, pub text1: Option<String>,
pub text1_meaning: Meaning, pub text1_meaning: Meaning,
@@ -783,6 +783,10 @@ impl MessageSummary {
.. Default::default() .. 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 { pub fn get_msg_info(context: &Context, msg_id: MsgId) -> String {