Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add decision- and blocking-functions to repl, cleanup #2258

Merged
merged 4 commits into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions deltachat-ffi/deltachat.h
Original file line number Diff line number Diff line change
Expand Up @@ -1757,6 +1757,7 @@ dc_array_t* dc_get_contacts (dc_context_t* context, uint32_t fl
/**
* Get the number of blocked contacts.
*
* @deprecated Deprecated on 2021-02-22: Use dc_array_get_cnt() on dc_get_blocked_contacts() instead.
* @memberof dc_context_t
* @param context The context object.
* @return The number of blocked contacts.
Expand Down
2 changes: 1 addition & 1 deletion deltachat-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1640,7 +1640,7 @@ pub unsafe extern "C" fn dc_get_blocked_cnt(context: *mut dc_context_t) -> libc:
}
let ctx = &*context;

block_on(Contact::get_blocked_cnt(&ctx)) as libc::c_int
block_on(async move { Contact::get_all_blocked(&ctx).await.len() as libc::c_int })
}

#[no_mangle]
Expand Down
47 changes: 38 additions & 9 deletions examples/repl/cmdline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,14 +255,10 @@ async fn log_msglist(context: &Context, msglist: &[MsgId]) -> Result<(), Error>
}

async fn log_contactlist(context: &Context, contacts: &[u32]) {
let mut contacts = contacts.to_vec();
if !contacts.contains(&1) {
contacts.push(1);
}
for contact_id in contacts {
let line;
let mut line2 = "".to_string();
if let Ok(contact) = Contact::get_by_id(context, contact_id).await {
if let Ok(contact) = Contact::get_by_id(context, *contact_id).await {
let name = contact.get_display_name();
let addr = contact.get_addr();
let verified_state = contact.is_verified(context).await;
Expand Down Expand Up @@ -292,14 +288,14 @@ async fn log_contactlist(context: &Context, contacts: &[u32]) {
let peerstate = Peerstate::from_addr(context, &addr)
.await
.expect("peerstate error");
if peerstate.is_some() && contact_id != 1 as libc::c_uint {
if peerstate.is_some() && *contact_id != 1 as libc::c_uint {
line2 = format!(
", prefer-encrypt={}",
peerstate.as_ref().unwrap().prefer_encrypt
);
}

println!("Contact#{}: {}{}", contact_id, line, line2);
println!("Contact#{}: {}{}", *contact_id, line, line2);
}
}
}
Expand Down Expand Up @@ -359,7 +355,6 @@ pub async fn cmdline(context: Context, line: &str, chat_id: &mut ChatId) -> Resu
listarchived\n\
chat [<chat-id>|0]\n\
createchat <contact-id>\n\
createchatbymsg <msg-id>\n\
creategroup <name>\n\
createprotected <name>\n\
addmember <contact-id>\n\
Expand All @@ -386,6 +381,10 @@ pub async fn cmdline(context: Context, line: &str, chat_id: &mut ChatId) -> Resu
protect <chat-id>\n\
unprotect <chat-id>\n\
delchat <chat-id>\n\
===========================Contact requests==\n\
decidestartchat <msg-id>\n\
decideblock <msg-id>\n\
decidenotnow <msg-id>\n\
===========================Message commands==\n\
listmsgs <query>\n\
msginfo <msg-id>\n\
Expand All @@ -401,6 +400,9 @@ pub async fn cmdline(context: Context, line: &str, chat_id: &mut ChatId) -> Resu
contactinfo <contact-id>\n\
delcontact <contact-id>\n\
cleanupcontacts\n\
block <contact-id>\n\
unblock <contact-id>\n\
listblocked\n\
======================================Misc.==\n\
getqr [<chat-id>]\n\
getbadqr\n\
Expand Down Expand Up @@ -659,7 +661,7 @@ pub async fn cmdline(context: Context, line: &str, chat_id: &mut ChatId) -> Resu

println!("Single#{} created successfully.", chat_id,);
}
"createchatbymsg" => {
"decidestartchat" | "createchatbymsg" => {
ensure!(!arg1.is_empty(), "Argument <msg-id> missing");
let msg_id = MsgId::new(arg1.parse()?);
match message::decide_on_contact_request(
Expand All @@ -676,6 +678,18 @@ pub async fn cmdline(context: Context, line: &str, chat_id: &mut ChatId) -> Resu
None => println!("Cannot crate chat."),
}
}
"decidenotnow" => {
ensure!(!arg1.is_empty(), "Argument <msg-id> missing");
let msg_id = MsgId::new(arg1.parse()?);
message::decide_on_contact_request(&context, msg_id, ContactRequestDecision::NotNow)
.await;
}
"decideblock" => {
ensure!(!arg1.is_empty(), "Argument <msg-id> missing");
let msg_id = MsgId::new(arg1.parse()?);
message::decide_on_contact_request(&context, msg_id, ContactRequestDecision::Block)
.await;
}
"creategroup" => {
ensure!(!arg1.is_empty(), "Argument <name> missing.");
let chat_id =
Expand Down Expand Up @@ -1078,6 +1092,21 @@ pub async fn cmdline(context: Context, line: &str, chat_id: &mut ChatId) -> Resu
ensure!(!arg1.is_empty(), "Argument <contact-id> missing.");
Contact::delete(&context, arg1.parse()?).await?;
}
"block" => {
ensure!(!arg1.is_empty(), "Argument <contact-id> missing.");
let contact_id = arg1.parse()?;
Contact::block(&context, contact_id).await;
}
"unblock" => {
ensure!(!arg1.is_empty(), "Argument <contact-id> missing.");
let contact_id = arg1.parse()?;
Contact::unblock(&context, contact_id).await;
}
"listblocked" => {
let contacts = Contact::get_all_blocked(&context).await;
log_contactlist(&context, &contacts).await;
println!("{} blocked contacts.", contacts.len());
}
"checkqr" => {
ensure!(!arg1.is_empty(), "Argument <qr-content> missing.");
let res = check_qr(&context, arg1).await;
Expand Down
11 changes: 8 additions & 3 deletions examples/repl/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,14 @@ const DB_COMMANDS: [&str; 9] = [
"housekeeping",
];

const CHAT_COMMANDS: [&str; 28] = [
const CHAT_COMMANDS: [&str; 30] = [
"listchats",
"listarchived",
"chat",
"createchat",
"createchatbymsg",
"decidestartchat",
"decideblock",
"decidenotnow",
"creategroup",
"createverified",
"addmember",
Expand Down Expand Up @@ -206,13 +208,16 @@ const MESSAGE_COMMANDS: [&str; 6] = [
"markseen",
"delmsg",
];
const CONTACT_COMMANDS: [&str; 6] = [
const CONTACT_COMMANDS: [&str; 9] = [
"listcontacts",
"listverified",
"addcontact",
"contactinfo",
"delcontact",
"cleanupcontacts",
"block",
"unblock",
"listblocked",
];
const MISC_COMMANDS: [&str; 10] = [
"getqr",
Expand Down
12 changes: 0 additions & 12 deletions src/contact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -674,18 +674,6 @@ impl Contact {
Ok(ret)
}

pub async fn get_blocked_cnt(context: &Context) -> usize {
context
.sql
.query_get_value::<isize>(
context,
"SELECT COUNT(*) FROM contacts WHERE id>? AND blocked!=0",
paramsv![DC_CONTACT_ID_LAST_SPECIAL as i32],
)
.await
.unwrap_or_default() as usize
}

/// Get blocked contacts.
pub async fn get_all_blocked(context: &Context) -> Vec<u32> {
context
Expand Down