jsonrpc(feat): Add API to get json encryption info

Add a new api to jsonrpc that returns json structured data about chat encryption.
This commit is contained in:
Sebastian Klähn
2024-11-10 11:39:26 +01:00
parent 3b2f18f926
commit 5fde02f9fd
3 changed files with 78 additions and 1 deletions

View File

@@ -45,7 +45,7 @@ pub mod types;
use num_traits::FromPrimitive;
use types::account::Account;
use types::chat::FullChat;
use types::chat::{EncryptionInfo, FullChat};
use types::contact::{ContactObject, VcardContact};
use types::events::Event;
use types::http::HttpResponse;
@@ -708,6 +708,19 @@ impl CommandApi {
ChatId::new(chat_id).get_encryption_info(&ctx).await
}
/// Get encryption info for a chat.
async fn get_chat_encryption_info_json(
&self,
account_id: u32,
chat_id: u32,
) -> Result<EncryptionInfo> {
let ctx = self.get_context(account_id).await?;
Ok(ChatId::new(chat_id)
.get_encryption_info_json(&ctx)
.await?
.into())
}
/// Get QR code text that will offer a [SecureJoin](https://securejoin.delta.chat/) invitation.
///
/// If `chat_id` is a group chat ID, SecureJoin QR code for the group is returned.

View File

@@ -9,6 +9,7 @@ use deltachat::context::Context;
use num_traits::cast::ToPrimitive;
use serde::{Deserialize, Serialize};
use typescript_type_def::TypeDef;
use yerpc::JsonSchema;
use super::color_int_to_hex_string;
use super::contact::ContactObject;
@@ -239,3 +240,23 @@ impl JSONRPCChatVisibility {
}
}
}
#[derive(Debug, JsonSchema, TypeDef, Serialize, Deserialize)]
pub struct EncryptionInfo {
/// Addresses with End-to-end encryption preferred.
pub mutual: Vec<String>,
/// Addresses with End-to-end encryption available.
pub no_preference: Vec<String>,
/// Addresses with no encryption.
pub reset: Vec<String>,
}
impl From<chat::EncryptionInfo> for EncryptionInfo {
fn from(encryption_info: chat::EncryptionInfo) -> Self {
EncryptionInfo {
mutual: encryption_info.mutual,
no_preference: encryption_info.no_preference,
reset: encryption_info.reset,
}
}
}

View File

@@ -105,6 +105,17 @@ pub enum ProtectionStatus {
ProtectionBroken = 3, // `2` was never used as a value.
}
/// Encryption info for a single chat.
#[derive(Debug)]
pub struct EncryptionInfo {
/// Addresses with End-to-end encryption preferred.
pub mutual: Vec<String>,
/// Addresses with End-to-end encryption available.
pub no_preference: Vec<String>,
/// Addresses with no encryption.
pub reset: Vec<String>,
}
/// The reason why messages cannot be sent to the chat.
///
/// The reason is mainly for logging and displaying in debug REPL, thus not translated.
@@ -1335,6 +1346,38 @@ impl ChatId {
Ok(ret.trim().to_string())
}
/// Returns encryption preferences of all chat contacts.
pub async fn get_encryption_info_json(self, context: &Context) -> Result<EncryptionInfo> {
let mut mutual = vec![];
let mut no_preference = vec![];
let mut reset = vec![];
for contact_id in get_chat_contacts(context, self)
.await?
.iter()
.filter(|&contact_id| !contact_id.is_special())
{
let contact = Contact::get_by_id(context, *contact_id).await?;
let addr = contact.get_addr().to_string();
let peerstate = Peerstate::from_addr(context, &addr).await?;
match peerstate
.filter(|peerstate| peerstate.peek_key(false).is_some())
.map(|peerstate| peerstate.prefer_encrypt)
{
Some(EncryptPreference::Mutual) => mutual.push(addr),
Some(EncryptPreference::NoPreference) => no_preference.push(addr),
Some(EncryptPreference::Reset) | None => reset.push(addr),
};
}
Ok(EncryptionInfo {
mutual,
no_preference,
reset,
})
}
/// Bad evil escape hatch.
///
/// Avoid using this, eventually types should be cleaned up enough