qualia_client_core/wellfair/api/
coop.rs1use super::super::blob_store::BlobStore;
4use super::super::journal::JournalEntry;
5use wellfare_core::credentials::{
6 build_credential_envelope, build_presentation, credential_summary, CredentialRecord,
7 FieldSelectedPresentation,
8};
9use wellfare_core::projects::{
10 build_contribution_envelope, build_membership_envelope, build_project_envelope,
11 contribution_summary, derive_obligations, membership_summary, project_summary, Contribution,
12 Obligation, Project, ProjectMembership,
13};
14
15use super::*;
16
17impl WebizenHostApi {
18 pub fn add_project(&mut self, project: &Project) -> Result<JournalEntry, String> {
22 let asserted = Self::now_unix() as u32;
23 let hash =
24 Self::payload_hash_hex(&serde_json::to_string(project).map_err(|e| e.to_string())?);
25 let envelope = build_project_envelope(
26 project,
27 &self.owner_did,
28 &self.author_did,
29 asserted,
30 Some(hash),
31 );
32 let summary = project_summary(project);
33 self.submit_record_with_summary(QAPP_PROJECTS, envelope, SOURCE_PROJECTS, Some(summary))?;
34 self.finalize_batch().ok();
35 self.latest_journal_entry()
36 }
37
38 pub fn add_project_membership(
40 &mut self,
41 membership: &ProjectMembership,
42 ) -> Result<JournalEntry, String> {
43 let asserted = Self::now_unix() as u32;
44 let hash =
45 Self::payload_hash_hex(&serde_json::to_string(membership).map_err(|e| e.to_string())?);
46 let envelope = build_membership_envelope(
47 membership,
48 &self.owner_did,
49 &self.author_did,
50 asserted,
51 Some(hash),
52 );
53 let summary = membership_summary(membership);
54 self.submit_record_with_summary(QAPP_PROJECTS, envelope, SOURCE_PROJECTS, Some(summary))?;
55 self.finalize_batch().ok();
56 self.latest_journal_entry()
57 }
58
59 pub fn add_contribution(
61 &mut self,
62 contribution: &Contribution,
63 ) -> Result<JournalEntry, String> {
64 let asserted = Self::now_unix() as u32;
65 let hash = Self::payload_hash_hex(
66 &serde_json::to_string(contribution).map_err(|e| e.to_string())?,
67 );
68 let envelope = build_contribution_envelope(
69 contribution,
70 &self.owner_did,
71 &self.author_did,
72 asserted,
73 Some(hash),
74 );
75 let summary = contribution_summary(contribution);
76 self.submit_record_with_summary(QAPP_PROJECTS, envelope, SOURCE_PROJECTS, Some(summary))?;
77 self.finalize_batch().ok();
78 self.latest_journal_entry()
79 }
80
81 pub fn list_contributions(&self, limit: usize) -> Result<Vec<JournalEntry>, String> {
83 self.list_journal_by_kind("contribution", limit)
84 }
85
86 fn local_contributions(&self, limit: usize) -> Result<Vec<Contribution>, String> {
88 let mut out = Vec::new();
89 for row in self.list_contributions(limit)? {
90 if let Some(ref summary) = row.summary {
91 if let Some(c) = contribution_from_summary(
92 row.id.clone(),
93 summary,
94 row.asserted_instant.to_unix_secs() as u32,
95 ) {
96 out.push(c);
97 }
98 }
99 }
100 Ok(out)
101 }
102
103 pub fn project_obligations(&self, limit: usize) -> Result<Vec<Obligation>, String> {
107 Ok(derive_obligations(&self.local_contributions(limit)?))
108 }
109
110 pub fn synced_project_obligations(&self, limit: usize) -> Result<Vec<Obligation>, String> {
116 let mut contributions = self.local_contributions(limit)?;
117 for op in self.validated_sync_operations()? {
118 if op.kind == "contribution" {
119 if let Some(c) = contribution_from_summary(
120 op.record_id.clone(),
121 &op.payload_summary,
122 op.committed_unix,
123 ) {
124 contributions.push(c);
125 }
126 }
127 }
128 Ok(derive_obligations(&contributions))
129 }
130
131 pub fn add_credential(
135 &mut self,
136 credential: &CredentialRecord,
137 ) -> Result<JournalEntry, String> {
138 let asserted = Self::now_unix() as u32;
139 let json = serde_json::to_string(credential).map_err(|e| e.to_string())?;
140 let hash = BlobStore::open(&self.storage_root)
143 .and_then(|store| store.put(json.as_bytes()))
144 .map_err(|e| e.to_string())?;
145 let envelope = build_credential_envelope(
146 credential,
147 &self.owner_did,
148 &self.author_did,
149 asserted,
150 Some(hash),
151 );
152 let summary = credential_summary(credential);
153 self.submit_record_with_summary(
154 QAPP_CREDENTIALS,
155 envelope,
156 SOURCE_CREDENTIALS,
157 Some(summary),
158 )?;
159 self.finalize_batch().ok();
160 self.latest_journal_entry()
161 }
162
163 pub fn list_credentials(&self, limit: usize) -> Result<Vec<JournalEntry>, String> {
165 self.list_journal_by_kind("credential", limit)
166 }
167
168 pub fn get_credential(&self, record_id: &str) -> Result<Option<CredentialRecord>, String> {
171 let Some(entry) = self
172 .list_credentials(256)?
173 .into_iter()
174 .find(|e| e.id == record_id)
175 else {
176 return Ok(None);
177 };
178 let Some(hash) = entry.blob_hash else {
179 return Ok(None);
180 };
181 let store = BlobStore::open(&self.storage_root).map_err(|e| e.to_string())?;
182 let Some(bytes) = store.get(&hash).map_err(|e| e.to_string())? else {
183 return Ok(None);
184 };
185 let cred: CredentialRecord = serde_json::from_slice(&bytes).map_err(|e| e.to_string())?;
186 Ok(Some(cred))
187 }
188
189 pub fn present_credential(
192 &self,
193 record_id: &str,
194 selected_claim_keys: &[String],
195 ) -> Result<FieldSelectedPresentation, String> {
196 let cred = self
197 .get_credential(record_id)?
198 .ok_or_else(|| format!("credential '{record_id}' not found or blob missing"))?;
199 Ok(build_presentation(&cred, selected_claim_keys))
200 }
201}