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
512pub 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)
0 commit comments