Skip to main content

qualia_client_core/api/
profile.rs

1//! Profile + social connect, personal directory
2
3#![allow(non_snake_case)]
4
5use super::*;
6
7/// Gets user-profile.
8pub fn get_user_profile() -> Result<serde_json::Value, String> {
9    let profile = crate::user_profile::load_profile();
10    serde_json::to_value(profile).map_err(|e| e.to_string())
11}
12
13/// Saves user-profile.
14pub fn save_user_profile(profile_json: String) -> Result<serde_json::Value, String> {
15    // People tab (and other callers) may send a *partial* profile JSON — e.g. only
16    // `display_name` + `sharing.allow_group_chat_invites`. Full-struct deserialize used
17    // to fail with `missing field share_display_name`. Merge onto the loaded profile.
18    let patch: serde_json::Value =
19        serde_json::from_str(&profile_json).map_err(|e| format!("invalid profile json: {e}"))?;
20    let base = crate::user_profile::load_profile();
21    let mut profile = crate::user_profile::apply_profile_patch(&base, &patch)?;
22    profile.public_did = crate::user_profile::resolve_public_did(&profile);
23    crate::user_profile::save_profile(&profile)?;
24    serde_json::to_value(profile).map_err(|e| e.to_string())
25}
26
27/// Generates a connect invite.
28pub fn generate_connect_invite(front_door_id: Option<String>) -> Result<serde_json::Value, String> {
29    let invite = crate::social_connect::generate_connect_invite(front_door_id)?;
30    serde_json::to_value(invite).map_err(|e| e.to_string())
31}
32
33/// Accepts a connect invite.
34pub fn accept_connect_invite(input: String) -> Result<serde_json::Value, String> {
35    let contact = crate::social_connect::accept_connect_invite(&input)?;
36    serde_json::to_value(contact).map_err(|e| e.to_string())
37}
38
39/// Lists chat contacts.
40pub fn list_chat_contacts() -> Result<serde_json::Value, String> {
41    let contacts = crate::social_connect::list_chat_contacts();
42    serde_json::to_value(contacts).map_err(|e| e.to_string())
43}
44
45// ── Personal directory (AD-like): categorised addressbook + agreement slots ─────
46
47/// The unified, categorised personal directory — the addressbook (Parties joined across the directory-actor
48/// + chat-contact stores by DID) grouped into categories, with a per-entry slot for the agreements
49/// governing that relationship. See `docs/plans/rights-aware-peer-agreement-addressbook.md`.
50pub fn list_directory() -> Result<serde_json::Value, String> {
51    let actors = get_directory_actors()?;
52    let contacts = crate::social_connect::list_chat_contacts();
53    let view = crate::directory::build_view(&actors, &contacts);
54    serde_json::to_value(view).map_err(|e| e.to_string())
55}
56
57/// The directory categories (built-in + user-created).
58pub fn list_directory_categories() -> Result<serde_json::Value, String> {
59    serde_json::to_value(crate::directory::list_categories()).map_err(|e| e.to_string())
60}
61
62/// Create a custom directory category. Returns the new category.
63pub fn create_directory_category(label: String) -> Result<serde_json::Value, String> {
64    let cat = crate::directory::create_category(&label)?;
65    serde_json::to_value(cat).map_err(|e| e.to_string())
66}
67
68/// Set the categories a directory entry (by DID) belongs to; returns the refreshed directory.
69pub fn set_directory_entry_categories(
70    did: String,
71    categories: Vec<String>,
72) -> Result<serde_json::Value, String> {
73    crate::directory::set_entry_categories(&did, categories)?;
74    list_directory()
75}
76
77/// Faceted + concept-aware search over the directory. `query` is meaning-aware (a token expands across a
78/// concept cluster, so "doctor" finds a "clinician"); `facets_json` is a JSON object of
79/// `{facet_id: [selected values]}` (AND across facets, OR within). Returns ranked entries + drill-down
80/// facet counts. Both empty → the whole directory with all facet counts.
81pub fn search_directory(query: String, facets_json: String) -> Result<serde_json::Value, String> {
82    let selected: std::collections::BTreeMap<String, Vec<String>> = if facets_json.trim().is_empty()
83    {
84        std::collections::BTreeMap::new()
85    } else {
86        serde_json::from_str(&facets_json).map_err(|e| format!("bad facets json: {e}"))?
87    };
88    let actors = get_directory_actors()?;
89    let contacts = crate::social_connect::list_chat_contacts();
90    let result = crate::directory::search(&actors, &contacts, &query, &selected);
91    serde_json::to_value(result).map_err(|e| e.to_string())
92}