1
1
//! Creates a new Drive and optionally also an Agent.
2
2
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
+ } ;
4
11
5
12
pub fn register_endpoint ( ) -> Endpoint {
6
13
Endpoint {
@@ -41,13 +48,16 @@ pub fn construct_register_redirect(
41
48
return Err ( "No name provided" . into ( ) ) ;
42
49
} ;
43
50
44
- let mut new_agent = None ;
45
-
46
51
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
50
57
} else if let Some ( agent) = for_agent {
58
+ if agent == PUBLIC_AGENT {
59
+ return Err ( "No `public-key` provided." . into ( ) ) ;
60
+ }
51
61
agent. to_string ( )
52
62
} else {
53
63
return Err ( "No `public-key` provided" . into ( ) ) ;
@@ -56,16 +66,23 @@ pub fn construct_register_redirect(
56
66
// Create the new Drive
57
67
let drive = crate :: populate:: create_drive ( store, Some ( & name) , & drive_creator_agent, false ) ?;
58
68
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
+
59
78
// Construct the Redirect Resource, which might provide the Client with a Subject for his Agent.
60
79
let mut redirect = Resource :: new_instance ( urls:: REDIRECT , store) ?;
61
80
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
+ ) ?;
69
86
// The front-end requires the @id to be the same as requested
70
87
redirect. set_subject ( requested_subject) ;
71
88
Ok ( redirect)
0 commit comments