Compare commits

...

3 Commits

Author SHA1 Message Date
Simon Laux
0731027ef4 also include self contact in memberlist, otherwise fetching self avatar
does not work.
2026-06-03 11:56:47 +02:00
Simon Laux
22bc6aa4b0 Virtual directory for accessing avatars / contact profile images 2026-06-03 11:56:16 +02:00
Simon Laux
6ae6942c0f add get_webxdc_memberlist 2026-06-03 11:56:16 +02:00
3 changed files with 104 additions and 4 deletions

View File

@@ -2256,6 +2256,20 @@ impl CommandApi {
WebxdcMessageInfo::get_for_message(&ctx, MsgId::new(instance_msg_id)).await
}
/// Returns webxdc memberlist, each member is a tuple (private user id, display_name)
/// Only includes members that have a known public key in the database
async fn get_webxdc_memberlist(
&self,
account_id: u32,
instance_msg_id: u32,
) -> Result<Vec<(String, String)>> {
let ctx = self.get_context(account_id).await?;
Message::load_from_db(&ctx, MsgId::new(instance_msg_id))
.await?
.get_webxdc_memberlist(&ctx)
.await
}
/// Get href from a WebxdcInfoMessage which might include a hash holding
/// information about a specific position or state in a webxdc app (optional)
async fn get_webxdc_href(&self, account_id: u32, info_msg_id: u32) -> Result<Option<String>> {

View File

@@ -1600,6 +1600,18 @@ WHERE addr=?
&self.addr
}
/// Get display name. This is the name as defined by the contact himself,
/// modified by the user or, if both are unset, an empty string.
pub fn get_display_name_without_email(&self) -> String {
if !self.name.is_empty() {
return self.name.clone();
}
if !self.authname.is_empty() {
return self.authname.clone();
}
String::new()
}
/// Get a summary of name and address.
///
/// The returned string is either "Name (email@domain.com)" or just

View File

@@ -36,7 +36,7 @@ use tokio::{fs::File, io::BufReader};
use crate::chat::{self, Chat};
use crate::constants::Chattype;
use crate::contact::ContactId;
use crate::contact::{self, Contact, ContactId};
use crate::context::Context;
use crate::events::EventType;
use crate::key::self_fingerprint;
@@ -57,6 +57,7 @@ const WEBXDC_API_VERSION: u32 = 1;
/// Suffix used to recognize webxdc files.
pub const WEBXDC_SUFFIX: &str = "xdc";
const WEBXDC_DEFAULT_ICON: &str = "__webxdc__/default-icon.png";
const WEBXDC_AVATAR_VIRTUAL_DIR: &str = "__webxdc__/avatar/";
/// Text shown to classic e-mail users in the visible e-mail body.
const BODY_DESCR: &str = "Webxdc Status Update";
@@ -890,6 +891,28 @@ impl Message {
name
};
// Virtual directory for accessing avatars
if name.starts_with(WEBXDC_AVATAR_VIRTUAL_DIR) {
let memberlist = self.get_internal_webxdc_memberlist(context).await?;
let user_id = name
.strip_prefix(WEBXDC_AVATAR_VIRTUAL_DIR)
.context("invalid avatar user id")?
.strip_suffix(".jpg")
.context("invalid avatar user id")?;
if let Some((contact, _)) = memberlist
.iter()
.find(|(_, member_user_id)| member_user_id == user_id)
{
if let Some(profile_image_path) = contact.get_profile_image(context).await? {
return Ok(tokio::fs::read(profile_image_path).await?);
} else {
bail!("contact has no profile image")
}
} else {
bail!("user_id not found in group member list")
}
}
let mut archive = self.get_webxdc_archive(context).await?;
if name == "index.html"
@@ -979,11 +1002,62 @@ impl Message {
})
}
fn get_webxdc_user_id(&self, pub_key_fingerprint_hex: &str) -> String {
let data = format!("{}-{}", pub_key_fingerprint_hex, self.rfc724_mid);
let hash = Sha256::digest(data.as_bytes());
format!("{:x}", hash)
}
async fn get_webxdc_self_addr(&self, context: &Context) -> Result<String> {
let fingerprint = self_fingerprint(context).await?;
let data = format!("{}-{}", fingerprint, self.rfc724_mid);
let hash = Sha256::digest(data.as_bytes());
Ok(format!("{hash:x}"))
Ok(self.get_webxdc_user_id(fingerprint))
}
/// This is the internal memberlist, as it contains the contact_id it should never be shared with the webxdc app
/// used by the function serving the virtual avatar directory use `get_webxdc_memberlist` instead.
async fn get_internal_webxdc_memberlist(
&self,
context: &Context,
) -> Result<Vec<(Contact, String)>> {
// We could do the following to increase privacy:
// - remove displayname (not that big of a deal in reality)
// - only show people in the list that send an status update before in the group (would decrease usefulness, but would still bring enough benefit, if only as internal function to match avatars)
let mut contacts = chat::get_chat_contacts(context, self.get_chat_id()).await?;
let mut memberlist = Vec::with_capacity(contacts.len());
// DM chats don't include self contact
// also webxdc may still need it even when you were removed from a group,
// so we re-add the self contact here.
if !contacts.contains(&ContactId::SELF) {
contacts.push(ContactId::SELF);
}
for contact_id in contacts {
let contact = contact::Contact::get_by_id(context, contact_id).await?;
if let Some(fingerprint) = contact.fingerprint() {
memberlist.push((contact, self.get_webxdc_user_id(&fingerprint.hex())));
} else if contact_id == ContactId::SELF {
memberlist.push((contact, self.get_webxdc_self_addr(context).await?));
}
}
Ok(memberlist)
}
/// Returns webxdc memberlist, each member is a tuple (hashed user id/addr, display_name)
/// Only includes members that have a known public key in the database and it also includes self contact.
pub async fn get_webxdc_memberlist(&self, context: &Context) -> Result<Vec<(String, String)>> {
// We could do the following to increase privacy:
// - remove displayname (not that big of a deal in reality)
// - only show people in the list that send an status update before in the group (would decrease usefulness, but would still bring enough benefit, if only as internal function to match avatars)
let members = self.get_internal_webxdc_memberlist(context).await?;
let mut memberlist = Vec::with_capacity(members.len());
for (contact, member_id) in members {
// TODO: think about whether we want to expose the nickname the user set for the contact here or just the name the contact set themselves?
// The former could be interpreted as privacy risk
// A. a webxdc could leak nicknames you set for users in the group,
// B. while the second could be seen as less useful/convenient for users "why are the contacts called differently in the webxdc"
let display_name = contact.get_display_name_without_email();
memberlist.push((member_id, display_name));
}
Ok(memberlist)
}
/// Get link attached to an info message.