use ProtectionStatus to create chats

This commit is contained in:
B. Petersen
2020-10-01 18:36:13 +02:00
parent d05dd977d9
commit ab8bf3c2f3
7 changed files with 69 additions and 45 deletions

View File

@@ -1299,15 +1299,15 @@ dc_chat_t* dc_get_chat (dc_context_t* context, uint32_t ch
* *
* @memberof dc_context_t * @memberof dc_context_t
* @param context The context object. * @param context The context object.
* @param verified If set to 1 the function creates a secure verified group. * @param protect If set to 1 the function creates group with protection initially enabled.
* Only secure-verified members are allowed in these groups * Only verified members are allowed in these groups
* and end-to-end-encryption is always enabled. * and end-to-end-encryption is always enabled.
* @param name The name of the group chat to create. * @param name The name of the group chat to create.
* The name may be changed later using dc_set_chat_name(). * The name may be changed later using dc_set_chat_name().
* To find out the name of a group later, see dc_chat_get_name() * To find out the name of a group later, see dc_chat_get_name()
* @return The chat ID of the new group chat, 0 on errors. * @return The chat ID of the new group chat, 0 on errors.
*/ */
uint32_t dc_create_group_chat (dc_context_t* context, int verified, const char* name); uint32_t dc_create_group_chat (dc_context_t* context, int protect, const char* name);
/** /**

View File

@@ -25,7 +25,7 @@ use async_std::task::{block_on, spawn};
use num_traits::{FromPrimitive, ToPrimitive}; use num_traits::{FromPrimitive, ToPrimitive};
use deltachat::accounts::Accounts; use deltachat::accounts::Accounts;
use deltachat::chat::{ChatId, ChatVisibility, MuteDuration}; use deltachat::chat::{ChatId, ChatVisibility, MuteDuration, ProtectionStatus};
use deltachat::constants::DC_MSG_ID_LAST_SPECIAL; use deltachat::constants::DC_MSG_ID_LAST_SPECIAL;
use deltachat::contact::{Contact, Origin}; use deltachat::contact::{Contact, Origin};
use deltachat::context::Context; use deltachat::context::Context;
@@ -1170,7 +1170,7 @@ pub unsafe extern "C" fn dc_get_chat(context: *mut dc_context_t, chat_id: u32) -
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn dc_create_group_chat( pub unsafe extern "C" fn dc_create_group_chat(
context: *mut dc_context_t, context: *mut dc_context_t,
verified: libc::c_int, protect: libc::c_int,
name: *const libc::c_char, name: *const libc::c_char,
) -> u32 { ) -> u32 {
if context.is_null() || name.is_null() { if context.is_null() || name.is_null() {
@@ -1178,14 +1178,14 @@ pub unsafe extern "C" fn dc_create_group_chat(
return 0; return 0;
} }
let ctx = &*context; let ctx = &*context;
let verified = if let Some(s) = contact::VerifiedStatus::from_i32(verified) { let protect = if let Some(s) = contact::ProtectionStatus::from_i32(protect) {
s s
} else { } else {
return 0; return 0;
}; };
block_on(async move { block_on(async move {
chat::create_group_chat(&ctx, verified, to_string_lossy(name)) chat::create_group_chat(&ctx, protect, to_string_lossy(name))
.await .await
.log_err(ctx, "Failed to create group chat") .log_err(ctx, "Failed to create group chat")
.map(|id| id.to_u32()) .map(|id| id.to_u32())

View File

@@ -4,7 +4,7 @@ use std::str::FromStr;
use anyhow::{bail, ensure}; use anyhow::{bail, ensure};
use async_std::path::Path; use async_std::path::Path;
use deltachat::chat::{self, Chat, ChatId, ChatItem, ChatVisibility}; use deltachat::chat::{self, Chat, ChatId, ChatItem, ChatVisibility, ProtectionStatus};
use deltachat::chatlist::*; use deltachat::chatlist::*;
use deltachat::constants::*; use deltachat::constants::*;
use deltachat::contact::*; use deltachat::contact::*;
@@ -357,7 +357,7 @@ pub async fn cmdline(context: Context, line: &str, chat_id: &mut ChatId) -> Resu
createchat <contact-id>\n\ createchat <contact-id>\n\
createchatbymsg <msg-id>\n\ createchatbymsg <msg-id>\n\
creategroup <name>\n\ creategroup <name>\n\
createverified <name>\n\ createprotected <name>\n\
addmember <contact-id>\n\ addmember <contact-id>\n\
removemember <contact-id>\n\ removemember <contact-id>\n\
groupname <name>\n\ groupname <name>\n\
@@ -654,15 +654,16 @@ pub async fn cmdline(context: Context, line: &str, chat_id: &mut ChatId) -> Resu
"creategroup" => { "creategroup" => {
ensure!(!arg1.is_empty(), "Argument <name> missing."); ensure!(!arg1.is_empty(), "Argument <name> missing.");
let chat_id = let chat_id =
chat::create_group_chat(&context, VerifiedStatus::Unverified, arg1).await?; chat::create_group_chat(&context, ProtectionStatus::Unprotected, arg1).await?;
println!("Group#{} created successfully.", chat_id); println!("Group#{} created successfully.", chat_id);
} }
"createverified" => { "createprotected" => {
ensure!(!arg1.is_empty(), "Argument <name> missing."); ensure!(!arg1.is_empty(), "Argument <name> missing.");
let chat_id = chat::create_group_chat(&context, VerifiedStatus::Verified, arg1).await?; let chat_id =
chat::create_group_chat(&context, ProtectionStatus::Protected, arg1).await?;
println!("VerifiedGroup#{} created successfully.", chat_id); println!("Group#{} created and protected successfully.", chat_id);
} }
"addmember" => { "addmember" => {
ensure!(sel_chat.is_some(), "No chat selected"); ensure!(sel_chat.is_some(), "No chat selected");

View File

@@ -1,5 +1,6 @@
//! # Chat module //! # Chat module
use deltachat_derive::{FromSql, ToSql};
use std::convert::TryFrom; use std::convert::TryFrom;
use std::time::{Duration, SystemTime}; use std::time::{Duration, SystemTime};
@@ -45,6 +46,33 @@ pub enum ChatItem {
}, },
} }
#[derive(
Debug,
Display,
Clone,
Copy,
PartialEq,
Eq,
FromPrimitive,
ToPrimitive,
FromSql,
ToSql,
IntoStaticStr,
Serialize,
Deserialize,
)]
#[repr(u32)]
pub enum ProtectionStatus {
Unprotected = 0,
Protected = 1,
}
impl Default for ProtectionStatus {
fn default() -> Self {
ProtectionStatus::Unprotected
}
}
/// Chat ID, including reserved IDs. /// Chat ID, including reserved IDs.
/// ///
/// Some chat IDs are reserved to identify special chat types. This /// Some chat IDs are reserved to identify special chat types. This
@@ -1865,7 +1893,7 @@ pub async fn get_chat_contacts(context: &Context, chat_id: ChatId) -> Vec<u32> {
pub async fn create_group_chat( pub async fn create_group_chat(
context: &Context, context: &Context,
verified: VerifiedStatus, protect: ProtectionStatus,
chat_name: impl AsRef<str>, chat_name: impl AsRef<str>,
) -> Result<ChatId, Error> { ) -> Result<ChatId, Error> {
let chat_name = improve_single_line_input(chat_name); let chat_name = improve_single_line_input(chat_name);
@@ -1877,16 +1905,13 @@ pub async fn create_group_chat(
let grpid = dc_create_id(); let grpid = dc_create_id();
context.sql.execute( context.sql.execute(
"INSERT INTO chats (type, name, grpid, param, created_timestamp) VALUES(?, ?, ?, \'U=1\', ?);", "INSERT INTO chats (type, name, grpid, param, created_timestamp, protected) VALUES(?, ?, ?, \'U=1\', ?, ?);",
paramsv![ paramsv![
if verified != VerifiedStatus::Unverified { Chattype::Group,
Chattype::VerifiedGroup
} else {
Chattype::Group
},
chat_name, chat_name,
grpid, grpid,
time(), time(),
protect,
], ],
).await?; ).await?;
@@ -2898,7 +2923,7 @@ mod tests {
async fn test_add_contact_to_chat_ex_add_self() { async fn test_add_contact_to_chat_ex_add_self() {
// Adding self to a contact should succeed, even though it's pointless. // Adding self to a contact should succeed, even though it's pointless.
let t = TestContext::new().await; let t = TestContext::new().await;
let chat_id = create_group_chat(&t.ctx, VerifiedStatus::Unverified, "foo") let chat_id = create_group_chat(&t.ctx, ProtectionStatus::Unprotected, "foo")
.await .await
.unwrap(); .unwrap();
let added = add_contact_to_chat_ex(&t.ctx, chat_id, DC_CONTACT_ID_SELF, false) let added = add_contact_to_chat_ex(&t.ctx, chat_id, DC_CONTACT_ID_SELF, false)
@@ -3284,7 +3309,7 @@ mod tests {
.await .await
.unwrap(); .unwrap();
async_std::task::sleep(std::time::Duration::from_millis(1000)).await; async_std::task::sleep(std::time::Duration::from_millis(1000)).await;
let chat_id3 = create_group_chat(&t.ctx, VerifiedStatus::Unverified, "foo") let chat_id3 = create_group_chat(&t.ctx, ProtectionStatus::Unprotected, "foo")
.await .await
.unwrap(); .unwrap();
@@ -3329,7 +3354,7 @@ mod tests {
#[async_std::test] #[async_std::test]
async fn test_set_chat_name() { async fn test_set_chat_name() {
let t = TestContext::new().await; let t = TestContext::new().await;
let chat_id = create_group_chat(&t.ctx, VerifiedStatus::Unverified, "foo") let chat_id = create_group_chat(&t.ctx, ProtectionStatus::Unprotected, "foo")
.await .await
.unwrap(); .unwrap();
assert_eq!( assert_eq!(
@@ -3372,7 +3397,7 @@ mod tests {
#[async_std::test] #[async_std::test]
async fn test_shall_attach_selfavatar() { async fn test_shall_attach_selfavatar() {
let t = TestContext::new().await; let t = TestContext::new().await;
let chat_id = create_group_chat(&t.ctx, VerifiedStatus::Unverified, "foo") let chat_id = create_group_chat(&t.ctx, ProtectionStatus::Unprotected, "foo")
.await .await
.unwrap(); .unwrap();
assert!(!shall_attach_selfavatar(&t.ctx, chat_id).await.unwrap()); assert!(!shall_attach_selfavatar(&t.ctx, chat_id).await.unwrap());
@@ -3396,7 +3421,7 @@ mod tests {
#[async_std::test] #[async_std::test]
async fn test_set_mute_duration() { async fn test_set_mute_duration() {
let t = TestContext::new().await; let t = TestContext::new().await;
let chat_id = create_group_chat(&t.ctx, VerifiedStatus::Unverified, "foo") let chat_id = create_group_chat(&t.ctx, ProtectionStatus::Unprotected, "foo")
.await .await
.unwrap(); .unwrap();
// Initial // Initial

View File

@@ -440,13 +440,13 @@ mod tests {
#[async_std::test] #[async_std::test]
async fn test_try_load() { async fn test_try_load() {
let t = TestContext::new().await; let t = TestContext::new().await;
let chat_id1 = create_group_chat(&t.ctx, VerifiedStatus::Unverified, "a chat") let chat_id1 = create_group_chat(&t.ctx, ProtectionStatus::Unprotected, "a chat")
.await .await
.unwrap(); .unwrap();
let chat_id2 = create_group_chat(&t.ctx, VerifiedStatus::Unverified, "b chat") let chat_id2 = create_group_chat(&t.ctx, ProtectionStatus::Unprotected, "b chat")
.await .await
.unwrap(); .unwrap();
let chat_id3 = create_group_chat(&t.ctx, VerifiedStatus::Unverified, "c chat") let chat_id3 = create_group_chat(&t.ctx, ProtectionStatus::Unprotected, "c chat")
.await .await
.unwrap(); .unwrap();
@@ -489,7 +489,7 @@ mod tests {
async fn test_sort_self_talk_up_on_forward() { async fn test_sort_self_talk_up_on_forward() {
let t = TestContext::new().await; let t = TestContext::new().await;
t.ctx.update_device_chats().await.unwrap(); t.ctx.update_device_chats().await.unwrap();
create_group_chat(&t.ctx, VerifiedStatus::Unverified, "a chat") create_group_chat(&t.ctx, ProtectionStatus::Unprotected, "a chat")
.await .await
.unwrap(); .unwrap();
@@ -546,7 +546,7 @@ mod tests {
#[async_std::test] #[async_std::test]
async fn test_get_summary_unwrap() { async fn test_get_summary_unwrap() {
let t = TestContext::new().await; let t = TestContext::new().await;
let chat_id1 = create_group_chat(&t.ctx, VerifiedStatus::Unverified, "a chat") let chat_id1 = create_group_chat(&t.ctx, ProtectionStatus::Unprotected, "a chat")
.await .await
.unwrap(); .unwrap();

View File

@@ -4,7 +4,7 @@ use sha2::{Digest, Sha256};
use mailparse::SingleInfo; use mailparse::SingleInfo;
use crate::chat::{self, Chat, ChatId}; use crate::chat::{self, Chat, ChatId, ProtectionStatus};
use crate::config::Config; use crate::config::Config;
use crate::constants::*; use crate::constants::*;
use crate::contact::*; use crate::contact::*;
@@ -1199,7 +1199,7 @@ async fn create_or_lookup_group(
|| X_MrAddToGrp.is_some() && addr_cmp(&self_addr, X_MrAddToGrp.as_ref().unwrap())) || X_MrAddToGrp.is_some() && addr_cmp(&self_addr, X_MrAddToGrp.as_ref().unwrap()))
{ {
// group does not exist but should be created // group does not exist but should be created
let create_verified = if mime_parser.get(HeaderDef::ChatVerified).is_some() { let create_protected = if mime_parser.get(HeaderDef::ChatVerified).is_some() {
if let Err(err) = if let Err(err) =
check_verified_properties(context, mime_parser, from_id as u32, to_ids).await check_verified_properties(context, mime_parser, from_id as u32, to_ids).await
{ {
@@ -1207,9 +1207,9 @@ async fn create_or_lookup_group(
let s = format!("{}. See 'Info' for more details", err); let s = format!("{}. See 'Info' for more details", err);
mime_parser.repl_msg_by_error(&s); mime_parser.repl_msg_by_error(&s);
} }
VerifiedStatus::Verified ProtectionStatus::Protected
} else { } else {
VerifiedStatus::Unverified ProtectionStatus::Unprotected
}; };
if !allow_creation { if !allow_creation {
@@ -1222,7 +1222,7 @@ async fn create_or_lookup_group(
&grpid, &grpid,
grpname.as_ref().unwrap(), grpname.as_ref().unwrap(),
create_blocked, create_blocked,
create_verified, create_protected,
) )
.await; .await;
chat_id_blocked = create_blocked; chat_id_blocked = create_blocked;
@@ -1463,7 +1463,7 @@ async fn create_or_lookup_adhoc_group(
&grpid, &grpid,
grpname, grpname,
create_blocked, create_blocked,
VerifiedStatus::Unverified, ProtectionStatus::Unprotected,
) )
.await; .await;
for &member_id in &member_ids { for &member_id in &member_ids {
@@ -1480,20 +1480,17 @@ async fn create_group_record(
grpid: impl AsRef<str>, grpid: impl AsRef<str>,
grpname: impl AsRef<str>, grpname: impl AsRef<str>,
create_blocked: Blocked, create_blocked: Blocked,
create_verified: VerifiedStatus, create_protected: ProtectionStatus,
) -> ChatId { ) -> ChatId {
if context.sql.execute( if context.sql.execute(
"INSERT INTO chats (type, name, grpid, blocked, created_timestamp) VALUES(?, ?, ?, ?, ?);", "INSERT INTO chats (type, name, grpid, blocked, created_timestamp, protected) VALUES(?, ?, ?, ?, ?, ?);",
paramsv![ paramsv![
if VerifiedStatus::Unverified != create_verified { Chattype::Group,
Chattype::VerifiedGroup
} else {
Chattype::Group
},
grpname.as_ref(), grpname.as_ref(),
grpid.as_ref(), grpid.as_ref(),
create_blocked, create_blocked,
time(), time(),
create_protected,
], ],
).await ).await
.is_err() .is_err()
@@ -2182,7 +2179,7 @@ mod tests {
assert!(one2one.get_visibility() == ChatVisibility::Archived); assert!(one2one.get_visibility() == ChatVisibility::Archived);
// create a group with bob, archive group // create a group with bob, archive group
let group_id = chat::create_group_chat(&t.ctx, VerifiedStatus::Unverified, "foo") let group_id = chat::create_group_chat(&t.ctx, ProtectionStatus::Unprotected, "foo")
.await .await
.unwrap(); .unwrap();
chat::add_contact_to_chat(&t.ctx, group_id, bob_id).await; chat::add_contact_to_chat(&t.ctx, group_id, bob_id).await;

View File

@@ -1102,6 +1102,7 @@ mod tests {
use super::*; use super::*;
use crate::chat; use crate::chat;
use crate::chat::ProtectionStatus;
use crate::peerstate::Peerstate; use crate::peerstate::Peerstate;
use crate::test_utils::TestContext; use crate::test_utils::TestContext;
@@ -1328,7 +1329,7 @@ mod tests {
let alice = TestContext::new_alice().await; let alice = TestContext::new_alice().await;
let bob = TestContext::new_bob().await; let bob = TestContext::new_bob().await;
let chatid = chat::create_group_chat(&alice.ctx, VerifiedStatus::Verified, "the chat") let chatid = chat::create_group_chat(&alice.ctx, ProtectionStatus::Protected, "the chat")
.await .await
.unwrap(); .unwrap();