mirror of
https://github.com/chatmail/core.git
synced 2026-05-02 21:06:31 +03:00
api(deltachat-jsonrpc): return vcard contact directly in MessageObject
This commit is contained in:
@@ -89,7 +89,7 @@ impl ContactObject {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, TypeDef, schemars::JsonSchema)]
|
#[derive(Clone, Serialize, TypeDef, schemars::JsonSchema)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct VcardContact {
|
pub struct VcardContact {
|
||||||
/// Email address.
|
/// Email address.
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
use crate::api::VcardContact;
|
||||||
use anyhow::{Context as _, Result};
|
use anyhow::{Context as _, Result};
|
||||||
use deltachat::chat::Chat;
|
use deltachat::chat::Chat;
|
||||||
use deltachat::chat::ChatItem;
|
use deltachat::chat::ChatItem;
|
||||||
@@ -87,6 +88,8 @@ pub struct MessageObject {
|
|||||||
download_state: DownloadState,
|
download_state: DownloadState,
|
||||||
|
|
||||||
reactions: Option<JSONRPCReactions>,
|
reactions: Option<JSONRPCReactions>,
|
||||||
|
|
||||||
|
vcard_contact: Option<VcardContact>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, TypeDef, schemars::JsonSchema)]
|
#[derive(Serialize, TypeDef, schemars::JsonSchema)]
|
||||||
@@ -173,6 +176,13 @@ impl MessageObject {
|
|||||||
Some(reactions.into())
|
Some(reactions.into())
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let vcard_contacts: Vec<VcardContact> = message
|
||||||
|
.vcard_contacts(context)
|
||||||
|
.await?
|
||||||
|
.into_iter()
|
||||||
|
.map(Into::into)
|
||||||
|
.collect();
|
||||||
|
|
||||||
Ok(MessageObject {
|
Ok(MessageObject {
|
||||||
id: msg_id.to_u32(),
|
id: msg_id.to_u32(),
|
||||||
chat_id: message.get_chat_id().to_u32(),
|
chat_id: message.get_chat_id().to_u32(),
|
||||||
@@ -232,6 +242,8 @@ impl MessageObject {
|
|||||||
download_state,
|
download_state,
|
||||||
|
|
||||||
reactions,
|
reactions,
|
||||||
|
|
||||||
|
vcard_contact: vcard_contacts.first().cloned(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ use std::collections::BTreeSet;
|
|||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
use anyhow::{ensure, format_err, Context as _, Result};
|
use anyhow::{ensure, format_err, Context as _, Result};
|
||||||
|
use deltachat_contact_tools::{parse_vcard, VcardContact};
|
||||||
use deltachat_derive::{FromSql, ToSql};
|
use deltachat_derive::{FromSql, ToSql};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use tokio::{fs, io};
|
use tokio::{fs, io};
|
||||||
@@ -607,6 +608,20 @@ impl Message {
|
|||||||
self.param.get_path(Param::File, context).unwrap_or(None)
|
self.param.get_path(Param::File, context).unwrap_or(None)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns vector of vcards if the file has a vCard attachment.
|
||||||
|
pub async fn vcard_contacts(&self, context: &Context) -> Result<Vec<VcardContact>> {
|
||||||
|
if self.viewtype != Viewtype::Vcard {
|
||||||
|
return Ok(Vec::new());
|
||||||
|
}
|
||||||
|
|
||||||
|
let path = self
|
||||||
|
.get_file(context)
|
||||||
|
.context("vCard message does not have an attachment")?;
|
||||||
|
let bytes = tokio::fs::read(path).await?;
|
||||||
|
let vcard_contents = std::str::from_utf8(&bytes).context("vCard is not a valid UTF-8")?;
|
||||||
|
Ok(parse_vcard(vcard_contents))
|
||||||
|
}
|
||||||
|
|
||||||
/// Save file copy at the user-provided path.
|
/// Save file copy at the user-provided path.
|
||||||
pub async fn save_file(&self, context: &Context, path: &Path) -> Result<()> {
|
pub async fn save_file(&self, context: &Context, path: &Path) -> Result<()> {
|
||||||
let path_src = self.get_file(context).context("No file")?;
|
let path_src = self.get_file(context).context("No file")?;
|
||||||
|
|||||||
Reference in New Issue
Block a user