Skip to content

Commit

Permalink
#430 create agent does not depend on store
Browse files Browse the repository at this point in the history
  • Loading branch information
joepio committed Jun 9, 2022
1 parent f36e2b0 commit 12141b5
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 34 deletions.
1 change: 0 additions & 1 deletion lib/defaults/default_store.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
"@id": "https://atomicdata.dev/properties/isDynamic",
"https://atomicdata.dev/properties/datatype": "https://atomicdata.dev/datatypes/boolean",
"https://atomicdata.dev/properties/description": "If this is true, a Property is calculated server side and should therefore not appear in forms.",
"https://atomicdata.dev/properties/parent": "https://atomicdata.dev/properties",
"https://atomicdata.dev/properties/isA": [
"https://atomicdata.dev/classes/Property"
],
Expand Down
27 changes: 15 additions & 12 deletions lib/src/agents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! Agents are actors (such as users) that can edit content.
//! https://docs.atomicdata.dev/commits/concepts.html

use crate::{errors::AtomicResult, urls, Resource, Storelike};
use crate::{errors::AtomicResult, urls, Resource, Storelike, Value};

#[derive(Clone, Debug)]
pub struct Agent {
Expand All @@ -19,21 +19,24 @@ pub struct Agent {
impl Agent {
/// Converts Agent to Resource.
/// Does not include private key, only public.
pub fn to_resource(&self, store: &impl Storelike) -> AtomicResult<Resource> {
let mut agent = Resource::new_instance(urls::AGENT, store)?;
agent.set_subject(self.subject.clone());
pub fn to_resource(&self) -> AtomicResult<Resource> {
let mut resource = Resource::new(self.subject.clone());
resource.set_class(urls::AGENT)?;
resource.set_subject(self.subject.clone());
if let Some(name) = &self.name {
agent.set_propval_string(crate::urls::NAME.into(), name, store)?;
resource.set_propval_unsafe(crate::urls::NAME.into(), Value::String(name.into()));
}
agent.set_propval_string(crate::urls::PUBLIC_KEY.into(), &self.public_key, store)?;
resource.set_propval_unsafe(
crate::urls::PUBLIC_KEY.into(),
Value::String(self.public_key.clone()),
);
// Agents must be read by anyone when validating their keys
agent.push_propval(crate::urls::READ, urls::PUBLIC_AGENT.into(), true, store)?;
agent.set_propval_string(
resource.push_propval(crate::urls::READ, urls::PUBLIC_AGENT.into(), true)?;
resource.set_propval_unsafe(
crate::urls::CREATED_AT.into(),
&self.created_at.to_string(),
store,
)?;
Ok(agent)
Value::Timestamp(self.created_at),
);
Ok(resource)
}

/// Creates a new Agent, generates a new Keypair.
Expand Down
1 change: 1 addition & 0 deletions lib/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub fn get_authentication_headers(url: &str, agent: &Agent) -> AtomicResult<Vec<
/// Fetches a URL, returns its body.
/// Uses the store's Agent agent (if set) to sign the request.
pub fn fetch_body(url: &str, content_type: &str, for_agent: Option<Agent>) -> AtomicResult<String> {
println!("fetching body of {}", url);
if !url.starts_with("http") {
return Err(format!("Could not fetch url '{}', must start with http.", url).into());
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl CollectionBuilder {
/// If that is what you need, use `.into_resource`
pub fn to_resource(&self, store: &impl Storelike) -> AtomicResult<crate::Resource> {
let mut resource = store.get_resource_new(&self.subject);
resource.set_class(urls::COLLECTION, store)?;
resource.set_class(urls::COLLECTION)?;
if let Some(val) = &self.property {
resource.set_propval_string(crate::urls::COLLECTION_PROPERTY.into(), val, store)?;
}
Expand Down
5 changes: 2 additions & 3 deletions lib/src/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -752,9 +752,7 @@ mod test {
&agent.subject,
"http://localhost/agents/7LsjMW5gOfDdJzK/atgjQ1t20J/rw8MjVg6xwqm+h8U="
);
store
.add_resource(&agent.to_resource(&store).unwrap())
.unwrap();
store.add_resource(&agent.to_resource().unwrap()).unwrap();
let subject = "https://localhost/new_thing";
let mut commitbuilder = crate::commit::CommitBuilder::new(subject.into());
let property1 = crate::urls::DESCRIPTION;
Expand Down Expand Up @@ -785,6 +783,7 @@ mod test {

fn invalid_subjects() {
let store = crate::Store::init().unwrap();
store.populate().unwrap();
let agent = store.create_agent(Some("test_actor")).unwrap();
let resource = Resource::new("https://localhost/test_resource".into());

Expand Down
2 changes: 1 addition & 1 deletion lib/src/db/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ fn destroy_resource_and_check_collection_and_commits() {
// Create a new agent, check if it is added to the new Agents collection as a Member.
let mut resource = crate::agents::Agent::new(None, &store)
.unwrap()
.to_resource(&store)
.to_resource()
.unwrap();
let _res = resource.save_locally(&store).unwrap();
let agents_collection_2 = store
Expand Down
2 changes: 1 addition & 1 deletion lib/src/plugins/invite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn construct_invite_redirect(
match store.get_resource(&public_key) {
Ok(_found) => {}
Err(_) => {
new_agent.to_resource(store)?.save_locally(store)?;
new_agent.to_resource()?.save_locally(store)?;
}
};

Expand Down
8 changes: 4 additions & 4 deletions lib/src/populate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ pub fn create_drive(store: &impl Storelike) -> AtomicResult<()> {
.get_self_url()
.ok_or("No self_url set, cannot populate store with Drive")?;
let mut drive = store.get_resource_new(&self_url);
drive.set_class(urls::DRIVE, store)?;
drive.set_class(urls::DRIVE)?;
let server_url = url::Url::parse(store.get_server_url())?;
drive.set_propval_string(
urls::NAME.into(),
Expand All @@ -173,10 +173,10 @@ pub fn set_drive_rights(store: &impl Storelike, public_read: bool) -> AtomicResu
let write_agent = store.get_default_agent()?.subject;
let read_agent = write_agent.clone();

drive.push_propval(urls::WRITE, write_agent.into(), true, store)?;
drive.push_propval(urls::READ, read_agent.into(), true, store)?;
drive.push_propval(urls::WRITE, write_agent.into(), true)?;
drive.push_propval(urls::READ, read_agent.into(), true)?;
if public_read {
drive.push_propval(urls::READ, urls::PUBLIC_AGENT.into(), true, store)?;
drive.push_propval(urls::READ, urls::PUBLIC_AGENT.into(), true)?;
}

if let Err(_no_description) = drive.get(urls::DESCRIPTION) {
Expand Down
11 changes: 4 additions & 7 deletions lib/src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,6 @@ impl Resource {
property: &str,
value: SubResource,
skip_existing: bool,
// TODO: Use Store to validate datatype
_store: &impl Storelike,
) -> AtomicResult<()> {
let mut vec = match self.propvals.get(property) {
Some(some) => match some {
Expand Down Expand Up @@ -348,12 +346,11 @@ impl Resource {
}

/// Overwrites the is_a (Class) of the Resource.
pub fn set_class(&mut self, is_a: &str, store: &impl Storelike) -> AtomicResult<()> {
self.set_propval(
pub fn set_class(&mut self, is_a: &str) -> AtomicResult<()> {
self.set_propval_unsafe(
crate::urls::IS_A.into(),
Value::ResourceArray([is_a.into()].into()),
store,
)?;
);
Ok(())
}

Expand Down Expand Up @@ -711,7 +708,7 @@ mod test {
let append_value = "http://localhost/someURL";
let mut resource = Resource::new_generate_subject(&store);
resource
.push_propval(&property, append_value.into(), false, &store)
.push_propval(&property, append_value.into(), false)
.unwrap();
let vec = resource.get(&property).unwrap().to_subjects(None).unwrap();
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion lib/src/storelike.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub trait Storelike: Sized {
/// Does not create a Commit - the recommended way is to use `agent.to_resource().save_locally()`.
fn create_agent(&self, name: Option<&str>) -> AtomicResult<crate::agents::Agent> {
let agent = Agent::new(name, self)?;
self.add_resource(&agent.to_resource(self)?)?;
self.add_resource(&agent.to_resource()?)?;
Ok(agent)
}

Expand Down
4 changes: 2 additions & 2 deletions server/src/appstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ fn set_default_agent(config: &Config, store: &impl Storelike) -> AtomicServerRes
store,
&agent_config.private_key,
);
store.add_resource(&recreated_agent.to_resource(store)?)?;
store.add_resource(&recreated_agent.to_resource()?)?;
agent_config
} else {
return Err(format!(
Expand Down Expand Up @@ -158,7 +158,7 @@ fn set_up_initial_invite(store: &impl Storelike) -> AtomicServerResult<()> {
let subject = format!("{}/setup", store.get_server_url());
tracing::info!("Creating initial Invite at {}", subject);
let mut invite = store.get_resource_new(&subject);
invite.set_class(atomic_lib::urls::INVITE, store)?;
invite.set_class(atomic_lib::urls::INVITE)?;
invite.set_subject(subject);
// This invite can be used only once
invite.set_propval(
Expand Down
2 changes: 1 addition & 1 deletion server/src/handlers/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub async fn upload_handler(
let mut parent = store.get_resource(&query.parent)?;
// parent.append_subjects(urls::ATTACHMENTS, created_file_subjects, false, store)?;
for created in created_file_subjects {
parent.push_propval(urls::ATTACHMENTS, created.into(), false, store)?;
parent.push_propval(urls::ATTACHMENTS, created.into(), false)?;
}
commit_responses.push(parent.save(store)?);

Expand Down

0 comments on commit 12141b5

Please sign in to comment.