Start working on implementing chatlist.to_json() and chat.to_chatlist_item_json()

This commit is contained in:
jikstra
2020-01-14 10:15:50 +01:00
parent e9a9291ce8
commit 783ddca74d
2 changed files with 34 additions and 8 deletions

View File

@@ -395,12 +395,12 @@ impl Chat {
///
/// This is somewhat experimental, even more so than the rest of
/// deltachat, and the data returned is still subject to change.
pub fn get_info(&self, context: &Context) -> Result<ChatInfo, Error> {
pub fn to_chatlist_item_json(&self, context: &Context) -> Result<ChatlistItem, Error> {
let draft = match get_draft(context, self.id)? {
Some(message) => message.text.unwrap_or_else(String::new),
_ => String::new(),
};
Ok(ChatInfo {
Ok(ChatlistItem {
id: self.id,
type_: self.typ as u32,
name: self.name.clone(),
@@ -411,6 +411,7 @@ impl Chat {
color: self.get_color(context),
profile_image: self.get_profile_image(context).unwrap_or_else(PathBuf::new),
subtitle: self.get_subtitle(context),
lastmsg_id: self.get_lastmsg_id(context).unwrap_or_else(|| MsgId::new_unset()).to_u32(),
draft,
})
}
@@ -425,7 +426,7 @@ impl Chat {
SELECT MAX(timestamp)
FROM msgs
WHERE chat_id=c.id
AND (hidden=0 OR state=?))
AND (hidden=0))
WHERE c.id=?
",
params![self.id],
@@ -688,7 +689,7 @@ impl Chat {
/// The current state of a chat.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ChatInfo {
pub struct ChatlistItem {
/// The chat ID.
pub id: ChatId,
@@ -740,6 +741,7 @@ pub struct ChatInfo {
/// which contain non-text parts. Perhaps it should be a
/// simple `has_draft` bool instead.
pub draft: String,
pub lastmsg_id: u32
// ToDo:
// - [ ] deaddrop,
// - [ ] summary,
@@ -2415,12 +2417,12 @@ mod tests {
use crate::test_utils::*;
#[test]
fn test_chat_info() {
fn test_get_chatlist_item_json() {
let t = dummy_context();
let bob = Contact::create(&t.ctx, "bob", "bob@example.com").unwrap();
let chat_id = create_by_contact_id(&t.ctx, bob).unwrap();
let chat = Chat::load_from_db(&t.ctx, chat_id).unwrap();
let info = chat.get_info(&t.ctx).unwrap();
let info = chat.to_chatlist_item_json(&t.ctx).unwrap();
// Ensure we can serialise this.
println!("{}", serde_json::to_string_pretty(&info).unwrap());
@@ -2437,12 +2439,13 @@ mod tests {
"color": 15895624,
"profile_image": "",
"subtitle": "bob@example.com",
"draft": ""
"draft": "",
"lastmsg_id": 0
}
"#;
// Ensure we can deserialise this.
let loaded: ChatInfo = serde_json::from_str(expected).unwrap();
let loaded: ChatlistItem = serde_json::from_str(expected).unwrap();
assert_eq!(info, loaded);
}

View File

@@ -39,6 +39,25 @@ pub struct Chatlist {
/// Stores pairs of `chat_id, message_id`
ids: Vec<(ChatId, MsgId)>,
}
///
/// - dc_lot_t::text1: contains the username or the strings "Me", "Draft" and so on.
/// The string may be colored by having a look at text1_meaning.
/// If there is no such name or it should not be displayed, the element is NULL.
/// - dc_lot_t::text1_meaning: one of DC_TEXT1_USERNAME, DC_TEXT1_SELF or DC_TEXT1_DRAFT.
/// Typically used to show dc_lot_t::text1 with different colors. 0 if not applicable.
/// - dc_lot_t::text2: contains an excerpt of the message text or strings as
/// "No messages". May be NULL of there is no such text (eg. for the archive link)
/// - dc_lot_t::timestamp: the timestamp of the message. 0 if not applicable.
/// - dc_lot_t::state: The state of the message as one of the DC_STATE_* constants (see #dc_msg_get_state()).
#[derive(Debug)]
pub struct ChatlistSummary {
username: Option<String>,
chat_type: u32,
message_excerpt: Option<String>,
timestamp: i64,
message_state: MessageState
}
impl Chatlist {
/// Get a list of chats.
@@ -301,6 +320,10 @@ impl Chatlist {
};
let lastmsg_id = self.ids[index].1;
self._get_summary(context, ret, chat, lastmsg_id)
}
pub fn _get_summary(&self, context: &Context, mut ret: Lot, chat: &Chat, lastmsg_id: MsgId) -> Lot {
let mut lastcontact = None;
let lastmsg = if let Ok(lastmsg) = Message::load_from_db(context, lastmsg_id) {