From 8c2efa707ac9d09c6db835d137dc58e4f5d67d53 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Sun, 3 Nov 2019 23:21:33 +0100 Subject: [PATCH] name special contact-ids where easily possible the point of this pr is to get an overview how and where DC_CONTACT_ID_DEVICE is used, to prepare introducing a device-"chat". i did not change the sql statements for now as this would require some more refactoring and has the potential to introduce bugs. --- src/chat.rs | 22 +++++++++++----------- src/chatlist.rs | 2 +- src/contact.rs | 4 ++-- src/dc_receive_imf.rs | 15 ++++++++------- src/location.rs | 2 +- src/message.rs | 2 +- 6 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/chat.rs b/src/chat.rs index 3b6f29b27..977b7249c 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -258,7 +258,7 @@ impl Chat { } if (self.typ == Chattype::Group || self.typ == Chattype::VerifiedGroup) - && !is_contact_in_chat(context, self.id, 1 as u32) + && !is_contact_in_chat(context, self.id, DC_CONTACT_ID_SELF) { emit_event!( context, @@ -414,7 +414,7 @@ impl Chat { &context.sql, "INSERT INTO locations \ (timestamp,from_id,chat_id, latitude,longitude,independent)\ - VALUES (?,?,?, ?,?,1);", + VALUES (?,?,?, ?,?,1);", // 1=DC_CONTACT_ID_SELF params![ timestamp, DC_CONTACT_ID_SELF, @@ -446,7 +446,7 @@ impl Chat { params![ new_rfc724_mid, self.id as i32, - 1i32, + DC_CONTACT_ID_SELF, to_id as i32, timestamp, msg.type_0, @@ -914,7 +914,7 @@ fn do_set_draft(context: &Context, chat_id: u32, msg: &mut Message) -> Result<() VALUES (?,?,?, ?,?,?,?,?);", params![ chat_id as i32, - 1, + DC_CONTACT_ID_SELF, time(), msg.type_0, MessageState::OutDraft, @@ -993,8 +993,8 @@ pub fn get_chat_msgs( " ON m.chat_id=chats.id", " LEFT JOIN contacts", " ON m.from_id=contacts.id", - " WHERE m.from_id!=1", - " AND m.from_id!=2", + " WHERE m.from_id!=1", // 1=DC_CONTACT_ID_SELF + " AND m.from_id!=2", // 2=DC_CONTACT_ID_DEVICE " AND m.hidden=0", " AND chats.blocked=2", " AND contacts.blocked=0", @@ -1350,7 +1350,7 @@ pub fn create_group_chat( let chat_id = sql::get_rowid(context, &context.sql, "chats", "grpid", grpid); if chat_id != 0 { - if add_to_chat_contacts_table(context, chat_id, 1) { + if add_to_chat_contacts_table(context, chat_id, DC_CONTACT_ID_SELF) { let mut draft_msg = Message::new(Viewtype::Text); draft_msg.set_text(Some(draft_txt)); set_draft_raw(context, chat_id, &mut draft_msg); @@ -1557,7 +1557,7 @@ pub fn remove_contact_from_chat( /* this allows to delete pending references to deleted contacts. Of course, this should _not_ happen. */ if let Ok(chat) = Chat::load_from_db(context, chat_id) { if real_group_exists(context, chat_id) { - if !is_contact_in_chat(context, chat_id, 1 as u32) { + if !is_contact_in_chat(context, chat_id, DC_CONTACT_ID_SELF) { emit_event!( context, Event::ErrorSelfNotInGroup( @@ -1653,7 +1653,7 @@ pub fn set_chat_name( if real_group_exists(context, chat_id) { if chat.name == new_name.as_ref() { success = true; - } else if !is_contact_in_chat(context, chat_id, 1) { + } else if !is_contact_in_chat(context, chat_id, DC_CONTACT_ID_SELF) { emit_event!( context, Event::ErrorSelfNotInGroup("Cannot set chat name; self not in group".into()) @@ -1905,8 +1905,8 @@ pub fn add_device_msg(context: &Context, chat_id: u32, text: impl AsRef) { "INSERT INTO msgs (chat_id,from_id,to_id, timestamp,type,state, txt,rfc724_mid) VALUES (?,?,?, ?,?,?, ?,?);", params![ chat_id as i32, - 2, - 2, + DC_CONTACT_ID_DEVICE, + DC_CONTACT_ID_DEVICE, dc_create_smeared_timestamp(context), Viewtype::Text, MessageState::InNoticed, diff --git a/src/chatlist.rs b/src/chatlist.rs index 9e1e82ed4..ffd6d6f9a 100644 --- a/src/chatlist.rs +++ b/src/chatlist.rs @@ -295,7 +295,7 @@ impl Chatlist { let mut lastcontact = None; let lastmsg = if let Ok(lastmsg) = Message::load_from_db(context, lastmsg_id) { - if lastmsg.from_id != 1 + if lastmsg.from_id != DC_CONTACT_ID_SELF && (chat.typ == Chattype::Group || chat.typ == Chattype::VerifiedGroup) { lastcontact = Contact::load_from_db(context, lastmsg.from_id).ok(); diff --git a/src/contact.rs b/src/contact.rs index 22c99d3ad..f0c765c4c 100644 --- a/src/contact.rs +++ b/src/contact.rs @@ -264,7 +264,7 @@ impl Contact { .unwrap_or_default(); if addr_normalized == addr_self { - return 1; + return DC_CONTACT_ID_SELF; } context.sql.query_get_value( @@ -301,7 +301,7 @@ impl Contact { .unwrap_or_default(); if addr == addr_self { - return Ok((1, sth_modified)); + return Ok((DC_CONTACT_ID_SELF, sth_modified)); } if !may_be_valid_addr(&addr) { diff --git a/src/dc_receive_imf.rs b/src/dc_receive_imf.rs index 0690be868..352390bdd 100644 --- a/src/dc_receive_imf.rs +++ b/src/dc_receive_imf.rs @@ -391,7 +391,7 @@ unsafe fn add_parts( } else { MessageState::InFresh }; - *to_id = 1; + *to_id = DC_CONTACT_ID_SELF; // handshake messages must be processed _before_ chats are created // (eg. contacs may be marked as verified) if mime_parser.lookup_field("Secure-Join").is_some() { @@ -551,8 +551,9 @@ unsafe fn add_parts( if to_ids.is_empty() && 0 != to_self { // from_id==to_id==DC_CONTACT_ID_SELF - this is a self-sent messages, // maybe an Autocrypt Setup Messag - let (id, bl) = chat::create_or_lookup_by_contact_id(context, 1, Blocked::Not) - .unwrap_or_default(); + let (id, bl) = + chat::create_or_lookup_by_contact_id(context, DC_CONTACT_ID_SELF, Blocked::Not) + .unwrap_or_default(); *chat_id = id; chat_id_blocked = bl; @@ -1350,8 +1351,8 @@ unsafe fn create_or_lookup_adhoc_group( if !member_ids.contains(&from_id) { member_ids.push(from_id); } - if !member_ids.contains(&1) { - member_ids.push(1); + if !member_ids.contains(&DC_CONTACT_ID_SELF) { + member_ids.push(DC_CONTACT_ID_SELF); } if member_ids.len() < 3 { // too few contacts given @@ -1471,7 +1472,7 @@ fn create_adhoc_grp_id(context: &Context, member_ids: &[u32]) -> String { .sql .query_map( format!( - "SELECT addr FROM contacts WHERE id IN({}) AND id!=1", + "SELECT addr FROM contacts WHERE id IN({}) AND id!=1", // 1=DC_CONTACT_ID_SELF member_ids_str ), params![], @@ -1525,7 +1526,7 @@ fn search_chat_ids_by_contact_ids( WHERE cc.chat_id IN(SELECT chat_id FROM chats_contacts WHERE contact_id IN({})) \ AND c.type=120 \ AND cc.contact_id!=1 \ - ORDER BY cc.chat_id, cc.contact_id;", + ORDER BY cc.chat_id, cc.contact_id;", // 1=DC_CONTACT_ID_GROUP contact_ids_str ), params![], diff --git a/src/location.rs b/src/location.rs index 6efef7a50..14ef83dda 100644 --- a/src/location.rs +++ b/src/location.rs @@ -275,7 +275,7 @@ pub fn set(context: &Context, latitude: f64, longitude: f64, accuracy: f64) -> b accuracy, time(), chat_id, - 1, + DC_CONTACT_ID_SELF, ] ) { warn!(context, "failed to store location {:?}", err); diff --git a/src/message.rs b/src/message.rs index d7d6d8756..b2988d910 100644 --- a/src/message.rs +++ b/src/message.rs @@ -714,7 +714,7 @@ pub fn get_msg_info(context: &Context, msg_id: MsgId) -> String { ret += "\n"; } - if msg.from_id == 2 || msg.to_id == 2 { + if msg.from_id == DC_CONTACT_ID_DEVICE || msg.to_id == DC_CONTACT_ID_DEVICE { // device-internal message, no further details needed return ret; }