Skip to content

Commit 3c1eb62

Browse files
committed
Improve register endpoint, WIP
1 parent 7a61cb6 commit 3c1eb62

File tree

4 files changed

+38
-21
lines changed

4 files changed

+38
-21
lines changed

lib/src/db/query_index.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ pub fn should_update(
254254
|| &index_atom.property == sort_by
255255
}
256256
// We should not create indexes for Collections that iterate over _all_ resources.
257-
(a, b, c) => todo!("This query filter is not supported yet! Please create an issue on Github for filter {:?} {:?} {:?}", a, b, c),
257+
(a, b, c) => return Err(format!("This query filter is not supported yet! Please create an issue on Github for filter {:?} {:?} {:?}", a, b, c).into()),
258258
};
259259
Ok(should)
260260
}

lib/src/plugins/register.rs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
//! Creates a new Drive and optionally also an Agent.
22
3-
use crate::{agents::Agent, endpoints::Endpoint, errors::AtomicResult, urls, Resource, Storelike};
3+
use crate::{
4+
agents::Agent,
5+
endpoints::Endpoint,
6+
errors::AtomicResult,
7+
urls::{self, PUBLIC_AGENT},
8+
values::SubResource,
9+
Resource, Storelike,
10+
};
411

512
pub fn register_endpoint() -> Endpoint {
613
Endpoint {
@@ -41,13 +48,16 @@ pub fn construct_register_redirect(
4148
return Err("No name provided".into());
4249
};
4350

44-
let mut new_agent = None;
45-
4651
let drive_creator_agent: String = if let Some(key) = pub_key {
47-
let new = Agent::new_from_public_key(store, &key)?.subject;
48-
new_agent = Some(new.clone());
49-
new
52+
let mut new = Agent::new_from_public_key(store, &key)?;
53+
new.name = Some(name.clone());
54+
let net_agent_subject = new.subject.to_string();
55+
new.to_resource()?.save(store)?;
56+
net_agent_subject
5057
} else if let Some(agent) = for_agent {
58+
if agent == PUBLIC_AGENT {
59+
return Err("No `public-key` provided.".into());
60+
}
5161
agent.to_string()
5262
} else {
5363
return Err("No `public-key` provided".into());
@@ -56,16 +66,23 @@ pub fn construct_register_redirect(
5666
// Create the new Drive
5767
let drive = crate::populate::create_drive(store, Some(&name), &drive_creator_agent, false)?;
5868

69+
// Add the drive to the Agent's list of drives
70+
let mut agent = store.get_resource(&drive_creator_agent)?;
71+
agent.push_propval(
72+
urls::DRIVES,
73+
SubResource::Subject(drive.get_subject().into()),
74+
true,
75+
)?;
76+
agent.save_locally(store)?;
77+
5978
// Construct the Redirect Resource, which might provide the Client with a Subject for his Agent.
6079
let mut redirect = Resource::new_instance(urls::REDIRECT, store)?;
6180
redirect.set_propval_string(urls::DESTINATION.into(), drive.get_subject(), store)?;
62-
if let Some(agent) = new_agent {
63-
redirect.set_propval(
64-
urls::REDIRECT_AGENT.into(),
65-
crate::Value::AtomicUrl(agent),
66-
store,
67-
)?;
68-
}
81+
redirect.set_propval(
82+
urls::REDIRECT_AGENT.into(),
83+
crate::Value::AtomicUrl(drive_creator_agent),
84+
store,
85+
)?;
6986
// The front-end requires the @id to be the same as requested
7087
redirect.set_subject(requested_subject);
7188
Ok(redirect)

lib/src/populate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,9 @@ pub fn create_drive(
175175
self_url.to_string()
176176
};
177177

178-
let mut drive = if drive_name.is_some() {
178+
let mut drive = if let Some(drive_name_some) = drive_name {
179179
if store.get_resource(&drive_subject).is_ok() {
180-
return Err("Drive URL is already taken".into());
180+
return Err(format!("Name '{}' is already taken", drive_name_some).into());
181181
}
182182
Resource::new(drive_subject)
183183
} else {

0 commit comments

Comments
 (0)