mirror of
https://github.com/chatmail/core.git
synced 2026-05-08 09:26:29 +03:00
Resultify Contact::block
This commit is contained in:
@@ -1737,9 +1737,13 @@ pub unsafe extern "C" fn dc_block_contact(
|
|||||||
let ctx = &*context;
|
let ctx = &*context;
|
||||||
block_on(async move {
|
block_on(async move {
|
||||||
if block == 0 {
|
if block == 0 {
|
||||||
Contact::unblock(&ctx, contact_id).await;
|
Contact::unblock(&ctx, contact_id)
|
||||||
|
.await
|
||||||
|
.ok_or_log_msg(&ctx, "Can't unblock contact");
|
||||||
} else {
|
} else {
|
||||||
Contact::block(&ctx, contact_id).await;
|
Contact::block(&ctx, contact_id)
|
||||||
|
.await
|
||||||
|
.ok_or_log_msg(&ctx, "Can't block contact");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1137,12 +1137,12 @@ pub async fn cmdline(context: Context, line: &str, chat_id: &mut ChatId) -> Resu
|
|||||||
"block" => {
|
"block" => {
|
||||||
ensure!(!arg1.is_empty(), "Argument <contact-id> missing.");
|
ensure!(!arg1.is_empty(), "Argument <contact-id> missing.");
|
||||||
let contact_id = arg1.parse()?;
|
let contact_id = arg1.parse()?;
|
||||||
Contact::block(&context, contact_id).await;
|
Contact::block(&context, contact_id).await?;
|
||||||
}
|
}
|
||||||
"unblock" => {
|
"unblock" => {
|
||||||
ensure!(!arg1.is_empty(), "Argument <contact-id> missing.");
|
ensure!(!arg1.is_empty(), "Argument <contact-id> missing.");
|
||||||
let contact_id = arg1.parse()?;
|
let contact_id = arg1.parse()?;
|
||||||
Contact::unblock(&context, contact_id).await;
|
Contact::unblock(&context, contact_id).await?;
|
||||||
}
|
}
|
||||||
"listblocked" => {
|
"listblocked" => {
|
||||||
let contacts = Contact::get_all_blocked(&context).await?;
|
let contacts = Contact::get_all_blocked(&context).await?;
|
||||||
|
|||||||
@@ -236,13 +236,13 @@ impl Contact {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Block the given contact.
|
/// Block the given contact.
|
||||||
pub async fn block(context: &Context, id: u32) {
|
pub async fn block(context: &Context, id: u32) -> Result<()> {
|
||||||
set_block_contact(context, id, true).await;
|
set_block_contact(context, id, true).await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Unblock the given contact.
|
/// Unblock the given contact.
|
||||||
pub async fn unblock(context: &Context, id: u32) {
|
pub async fn unblock(context: &Context, id: u32) -> Result<()> {
|
||||||
set_block_contact(context, id, false).await;
|
set_block_contact(context, id, false).await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add a single contact as a result of an _explicit_ user action.
|
/// Add a single contact as a result of an _explicit_ user action.
|
||||||
@@ -270,7 +270,7 @@ impl Contact {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if blocked {
|
if blocked {
|
||||||
Contact::unblock(context, contact_id).await;
|
Contact::unblock(context, contact_id).await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(contact_id)
|
Ok(contact_id)
|
||||||
@@ -1162,22 +1162,24 @@ fn sanitize_name_and_addr(name: &str, addr: &str) -> (String, String) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn set_block_contact(context: &Context, contact_id: u32, new_blocking: bool) {
|
async fn set_block_contact(context: &Context, contact_id: u32, new_blocking: bool) -> Result<()> {
|
||||||
if contact_id <= DC_CONTACT_ID_LAST_SPECIAL {
|
ensure!(
|
||||||
return;
|
contact_id > DC_CONTACT_ID_LAST_SPECIAL,
|
||||||
}
|
"Can't block special contact {}",
|
||||||
|
contact_id
|
||||||
|
);
|
||||||
|
|
||||||
if let Ok(contact) = Contact::load_from_db(context, contact_id).await {
|
let contact = Contact::load_from_db(context, contact_id).await?;
|
||||||
if contact.blocked != new_blocking
|
|
||||||
&& context
|
if contact.blocked != new_blocking {
|
||||||
|
context
|
||||||
.sql
|
.sql
|
||||||
.execute(
|
.execute(
|
||||||
"UPDATE contacts SET blocked=? WHERE id=?;",
|
"UPDATE contacts SET blocked=? WHERE id=?;",
|
||||||
paramsv![new_blocking as i32, contact_id as i32],
|
paramsv![new_blocking as i32, contact_id as i32],
|
||||||
)
|
)
|
||||||
.await
|
.await?;
|
||||||
.is_ok()
|
|
||||||
{
|
|
||||||
// also (un)block all chats with _only_ this contact - we do not delete them to allow a
|
// also (un)block all chats with _only_ this contact - we do not delete them to allow a
|
||||||
// non-destructive blocking->unblocking.
|
// non-destructive blocking->unblocking.
|
||||||
// (Maybe, beside normal chats (type=100) we should also block group chats with only this user.
|
// (Maybe, beside normal chats (type=100) we should also block group chats with only this user.
|
||||||
@@ -1205,13 +1207,13 @@ WHERE type=? AND id IN (
|
|||||||
// also unblock mailinglist
|
// also unblock mailinglist
|
||||||
// if the contact is a mailinglist address explicitly created to allow unblocking
|
// if the contact is a mailinglist address explicitly created to allow unblocking
|
||||||
if !new_blocking && contact.origin == Origin::MailinglistAddress {
|
if !new_blocking && contact.origin == Origin::MailinglistAddress {
|
||||||
if let Ok((chat_id, _, _)) = chat::get_chat_id_by_grpid(context, contact.addr).await
|
if let Ok((chat_id, _, _)) = chat::get_chat_id_by_grpid(context, contact.addr).await {
|
||||||
{
|
|
||||||
chat_id.set_blocked(context, Blocked::Not).await;
|
chat_id.set_blocked(context, Blocked::Not).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set profile image for a contact.
|
/// Set profile image for a contact.
|
||||||
|
|||||||
@@ -3018,7 +3018,9 @@ mod tests {
|
|||||||
assert_eq!(msgs.len(), 0);
|
assert_eq!(msgs.len(), 0);
|
||||||
|
|
||||||
// Unblock contact and check if the next message arrives in real chat
|
// Unblock contact and check if the next message arrives in real chat
|
||||||
Contact::unblock(&t, *blocked.first().unwrap()).await;
|
Contact::unblock(&t, *blocked.first().unwrap())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
let blocked = Contact::get_all_blocked(&t).await.unwrap();
|
let blocked = Contact::get_all_blocked(&t).await.unwrap();
|
||||||
assert_eq!(blocked.len(), 0);
|
assert_eq!(blocked.len(), 0);
|
||||||
|
|
||||||
|
|||||||
@@ -1196,7 +1196,11 @@ pub async fn decide_on_contact_request(
|
|||||||
Err(e) => warn!(context, "decide_on_contact_request error: {}", e),
|
Err(e) => warn!(context, "decide_on_contact_request error: {}", e),
|
||||||
},
|
},
|
||||||
|
|
||||||
(Block, false) => Contact::block(context, msg.from_id).await,
|
(Block, false) => {
|
||||||
|
if let Err(e) = Contact::block(context, msg.from_id).await {
|
||||||
|
warn!(context, "Can't block contact: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
(Block, true) => {
|
(Block, true) => {
|
||||||
if !msg.chat_id.set_blocked(context, Blocked::Manually).await {
|
if !msg.chat_id.set_blocked(context, Blocked::Manually).await {
|
||||||
warn!(context, "Block mailing list failed.")
|
warn!(context, "Block mailing list failed.")
|
||||||
|
|||||||
Reference in New Issue
Block a user