plexus_core/plexus/
view.rs1use std::collections::{BTreeMap, BTreeSet};
37use std::hash::{Hash, Hasher};
38
39use plexus_auth_core::grants::{Gate, Grant};
40
41use super::auth::AuthContext;
42use super::schema::{ChildHashes, ChildSummary, MethodRole, PluginHashes, PluginSchema, ViewKind};
43
44#[derive(Debug, Clone)]
46pub struct View {
47 pub kind: ViewKind,
49 pub schema: PluginSchema,
53 pub hash: String,
55 pub activations: BTreeMap<String, PluginSchema>,
58}
59
60impl View {
61 pub fn schema_of(&self, ns: &str) -> Option<&PluginSchema> {
65 if ns == self.schema.namespace {
66 Some(&self.schema)
67 } else {
68 self.activations.get(ns)
69 }
70 }
71
72 pub fn public_methods(&self) -> Vec<String> {
76 self.activations
77 .values()
78 .flat_map(|s| {
79 let ns = s.namespace.clone();
80 s.methods.iter().filter(|m| m.public).map(move |m| m.grant_or(&ns)).collect::<Vec<_>>()
81 })
82 .collect()
83 }
84
85 #[allow(deprecated)]
87 pub fn hashes(&self) -> PluginHashes {
88 PluginHashes {
89 namespace: self.schema.namespace.clone(),
90 self_hash: self.schema.self_hash.clone(),
91 children_hash: self.schema.children_hash.clone(),
92 hash: self.schema.hash.clone(),
93 children: self
94 .schema
95 .children
96 .as_ref()
97 .map(|kids| kids.iter().map(|c| ChildHashes { namespace: c.namespace.clone(), hash: c.hash.clone() }).collect()),
98 }
99 }
100}
101
102fn digest(value: &impl serde::Serialize) -> String {
106 let text = serde_json::to_string(value).unwrap_or_default();
107 let mut h = std::collections::hash_map::DefaultHasher::new();
108 text.hash(&mut h);
109 format!("{:016x}", h.finish())
110}
111
112struct Asker<'a> {
114 gate: &'a Gate,
115 caller: Option<&'a AuthContext>,
116 signed_in: bool,
117 signed_in_grants: &'a BTreeSet<String>,
120}
121
122impl Asker<'_> {
123 fn keep(&self, grant: &str, public: bool) -> Option<Option<Vec<String>>> {
125 if !self.signed_in {
126 return public.then_some(None);
127 }
128 let caller = self.caller.expect("signed in");
129 let held: Vec<String> =
130 self.gate.held_on(caller, &Grant::new(grant)).iter().map(|s| s.as_str().to_string()).collect();
131 if !held.is_empty() {
132 return Some(Some(held));
133 }
134 (public || self.signed_in_grants.contains(grant)).then_some(None)
135 }
136}
137
138fn activation_view(asker: &Asker<'_>, kind: ViewKind, mut schema: PluginSchema) -> Option<PluginSchema> {
141 let ns = schema.namespace.clone();
142 let mut kept = Vec::new();
143 let mut schema_entry = None;
144 for mut m in std::mem::take(&mut schema.methods) {
145 let is_schema_entry = m.name == "schema" && matches!(m.role, MethodRole::Rpc) && m.hash == "auto_schema";
146 if is_schema_entry {
147 schema_entry = Some(m);
148 continue;
149 }
150 let grant = m.grant_or(&ns);
151 if let Some(held) = asker.keep(&grant, m.public) {
152 m.held_on = held;
153 kept.push(m);
154 }
155 }
156 if kept.is_empty() {
157 return None;
158 }
159 kept.extend(schema_entry);
160 schema.methods = kept;
161 #[allow(deprecated)]
162 if let Some(children) = schema.children.as_mut() {
163 let names: BTreeSet<&str> = schema.methods.iter().map(|m| m.name.as_str()).collect();
166 children.retain(|c| names.contains(c.namespace.as_str()));
167 }
168 schema.roles = BTreeMap::new();
169 schema.view = Some(kind);
170 rehash(&mut schema, None);
171 Some(schema)
172}
173
174#[allow(deprecated)]
177fn rehash(schema: &mut PluginSchema, prefix: Option<&str>) {
178 schema.self_hash = String::new();
179 schema.children_hash = None;
180 schema.hash = String::new();
181 let self_hash = digest(&(&schema.namespace, &schema.methods, &schema.view));
182 let children_hash = schema.children.as_ref().map(|kids| digest(kids));
183 let composite = digest(&(&self_hash, &children_hash, &*schema));
184 schema.self_hash = self_hash;
185 schema.children_hash = children_hash;
186 schema.hash = match prefix {
187 Some(p) => format!("{p}{composite}"),
188 None => composite,
189 };
190}
191
192pub(crate) fn build(
196 gate: &Gate,
197 signed_in_grants: &BTreeSet<String>,
198 caller: Option<&AuthContext>,
199 mut hub: PluginSchema,
200 activations: Vec<PluginSchema>,
201) -> View {
202 let signed_in = caller.and_then(|c| c.principal()).is_some();
203 let kind = if signed_in { ViewKind::Caller } else { ViewKind::Anonymous };
204 let asker = Asker { gate, caller: if signed_in { caller } else { None }, signed_in, signed_in_grants };
205
206 let kept: BTreeMap<String, PluginSchema> = activations
207 .into_iter()
208 .filter_map(|s| activation_view(&asker, kind, s))
209 .map(|s| (s.namespace.clone(), s))
210 .collect();
211
212 #[allow(deprecated)]
213 {
214 let mut children: Vec<ChildSummary> = hub.children.take().unwrap_or_default();
215 children.retain(|c| kept.contains_key(&c.namespace));
216 for c in children.iter_mut() {
217 c.hash = kept[&c.namespace].hash.clone();
218 }
219 hub.children = Some(children);
220 }
221 for m in hub.methods.iter_mut() {
223 m.held_on = None;
224 }
225 hub.roles = BTreeMap::new();
226 hub.view = Some(kind);
227 rehash(&mut hub, Some(if signed_in { "v-" } else { "v-anon-" }));
228 let hash = hub.hash.clone();
229 View { kind, schema: hub, hash, activations: kept }
230}
231
232pub(crate) fn whole(hub: PluginSchema, activations: Vec<PluginSchema>) -> View {
234 let hash = hub.hash.clone();
235 View {
236 kind: ViewKind::Caller,
237 schema: hub,
238 hash,
239 activations: activations.into_iter().map(|s| (s.namespace.clone(), s)).collect(),
240 }
241}