|
2 | 2 | //! Agents are actors (such as users) that can edit content. |
3 | 3 | //! https://docs.atomicdata.dev/commits/concepts.html |
4 | 4 |
|
5 | | -use crate::{errors::AtomicResult, urls, Resource, Storelike, Value}; |
| 5 | +use crate::{errors::AtomicResult, urls, Query, Resource, Storelike, Value}; |
6 | 6 |
|
7 | 7 | #[derive(Clone, Debug)] |
8 | 8 | pub struct Agent { |
@@ -39,6 +39,41 @@ impl Agent { |
39 | 39 | Ok(resource) |
40 | 40 | } |
41 | 41 |
|
| 42 | + pub fn from_email(email: &str, store: &impl Storelike) -> AtomicResult<Self> { |
| 43 | + let mut query = Query::new(); |
| 44 | + query.property = Some(urls::EMAIL.into()); |
| 45 | + query.value = Some(Value::String(email.to_string())); |
| 46 | + let response = store.query(&query)?; |
| 47 | + if response.resources.is_empty() { |
| 48 | + return Err(format!("Agent with Email {} not found", email).into()); |
| 49 | + } |
| 50 | + if response.resources.len() > 1 { |
| 51 | + return Err(format!( |
| 52 | + "Email {} is not unique, {} agents have this email", |
| 53 | + email, response.count |
| 54 | + ) |
| 55 | + .into()); |
| 56 | + } |
| 57 | + let resource = response.resources.first().unwrap(); |
| 58 | + Agent::from_resource(resource.clone()) |
| 59 | + } |
| 60 | + |
| 61 | + pub fn from_resource(resource: Resource) -> AtomicResult<Self> { |
| 62 | + let name = if let Ok(name) = resource.get(urls::NAME) { |
| 63 | + Some(name.to_string()) |
| 64 | + } else { |
| 65 | + None |
| 66 | + }; |
| 67 | + |
| 68 | + return Ok(Self { |
| 69 | + created_at: resource.get(urls::CREATED_AT)?.to_int()?, |
| 70 | + name, |
| 71 | + public_key: resource.get(urls::PUBLIC_KEY)?.to_string(), |
| 72 | + private_key: None, |
| 73 | + subject: resource.get_subject().into(), |
| 74 | + }); |
| 75 | + } |
| 76 | + |
42 | 77 | /// Creates a new Agent, generates a new Keypair. |
43 | 78 | pub fn new(name: Option<&str>, store: &impl Storelike) -> AtomicResult<Agent> { |
44 | 79 | let keypair = generate_keypair()?; |
|
0 commit comments