@@ -9,7 +9,7 @@ use crate::{
9
9
parse:: ParseOpts ,
10
10
schema:: { Class , Property } ,
11
11
storelike:: Query ,
12
- urls, Storelike , Value ,
12
+ urls, Resource , Storelike , Value ,
13
13
} ;
14
14
15
15
const DEFAULT_ONTOLOGY_PATH : & str = "defaultOntology" ;
@@ -153,26 +153,52 @@ pub fn populate_base_models(store: &impl Storelike) -> AtomicResult<()> {
153
153
Ok ( ( ) )
154
154
}
155
155
156
- /// Creates a Drive resource at the base URL. Does not set rights. Use set_drive_rights for that.
157
- pub fn create_drive ( store : & impl Storelike ) -> AtomicResult < ( ) > {
158
- let self_url = store
159
- . get_self_url ( )
160
- . ok_or ( "No self_url set, cannot populate store with Drive" ) ?;
161
- let mut drive = store. get_resource_new ( & self_url) ;
156
+ /// Creates a Drive resource at the base URL if no name is passed.
157
+ #[ tracing:: instrument( skip( store) , level = "info" ) ]
158
+ pub fn create_drive (
159
+ store : & impl Storelike ,
160
+ drive_name : Option < & str > ,
161
+ for_agent : & str ,
162
+ public_read : bool ,
163
+ ) -> AtomicResult < Resource > {
164
+ let mut self_url = if let Some ( url) = store. get_self_url ( ) {
165
+ url. to_owned ( )
166
+ } else {
167
+ return Err ( "No self URL set. Cannot create drive." . into ( ) ) ;
168
+ } ;
169
+ let drive_subject: String = if let Some ( name) = drive_name {
170
+ // Let's make a subdomain
171
+ let host = self_url. host ( ) . expect ( "No host in server_url" ) ;
172
+ let subdomain_host = format ! ( "{}.{}" , name, host) ;
173
+ self_url. set_host ( Some ( & subdomain_host) ) ?;
174
+ self_url. to_string ( )
175
+ } else {
176
+ self_url. to_string ( )
177
+ } ;
178
+
179
+ let mut drive = if drive_name. is_some ( ) {
180
+ if store. get_resource ( & drive_subject) . is_ok ( ) {
181
+ return Err ( "Drive URL is already taken" . into ( ) ) ;
182
+ }
183
+ Resource :: new ( drive_subject)
184
+ } else {
185
+ // Only for the base URL (of no drive name is passed), we should not check if the drive exists.
186
+ // This is because we use `create_drive` in the `--initialize` command.
187
+ store. get_resource_new ( & drive_subject)
188
+ } ;
162
189
drive. set_class ( urls:: DRIVE ) ;
163
- let server_url = url:: Url :: parse ( store. get_server_url ( ) ) ?;
164
190
drive. set_string (
165
191
urls:: NAME . into ( ) ,
166
- server_url . host_str ( ) . ok_or ( "Can't use current base URL" ) ? ,
192
+ drive_name . unwrap_or_else ( || self_url . host_str ( ) . unwrap ( ) ) ,
167
193
store,
168
194
) ?;
169
195
drive. save_locally ( store) ?;
170
196
171
- Ok ( ( ) )
197
+ Ok ( drive )
172
198
}
173
199
174
200
pub fn create_default_ontology ( store : & impl Storelike ) -> AtomicResult < ( ) > {
175
- let mut drive = store. get_resource ( store. get_server_url ( ) ) ?;
201
+ let mut drive = store. get_resource ( store. get_server_url ( ) . as_str ( ) ) ?;
176
202
177
203
let ontology_subject = format ! ( "{}/{}" , drive. get_subject( ) , DEFAULT_ONTOLOGY_PATH ) ;
178
204
@@ -209,7 +235,7 @@ pub fn create_default_ontology(store: &impl Storelike) -> AtomicResult<()> {
209
235
/// Adds rights to the default agent to the Drive resource (at the base URL). Optionally give Public Read rights.
210
236
pub fn set_drive_rights ( store : & impl Storelike , public_read : bool ) -> AtomicResult < ( ) > {
211
237
// Now let's add the agent as the Root user and provide write access
212
- let mut drive = store. get_resource ( store. get_server_url ( ) ) ?;
238
+ let mut drive = store. get_resource ( store. get_server_url ( ) . as_str ( ) ) ?;
213
239
let write_agent = store. get_default_agent ( ) ?. subject ;
214
240
let read_agent = write_agent. clone ( ) ;
215
241
@@ -236,7 +262,9 @@ To use the data in your web apps checkout our client libraries: [@tomic/lib](htt
236
262
Use [@tomic/cli](https://docs.atomicdata.dev/js-cli) to generate typed ontologies inside your code.
237
263
"# , store. get_server_url( ) , & format!( "{}/{}" , drive. get_subject( ) , DEFAULT_ONTOLOGY_PATH ) ) , store) ?;
238
264
}
265
+
239
266
drive. save_locally ( store) ?;
267
+
240
268
Ok ( ( ) )
241
269
}
242
270
@@ -312,7 +340,11 @@ pub fn populate_importer(store: &crate::Db) -> AtomicResult<()> {
312
340
. ok_or ( "No self URL in this Store - required for populating importer" ) ?;
313
341
let mut importer = crate :: Resource :: new ( urls:: construct_path_import ( & base) ) ;
314
342
importer. set_class ( urls:: IMPORTER ) ;
315
- importer. set ( urls:: PARENT . into ( ) , Value :: AtomicUrl ( base) , store) ?;
343
+ importer. set (
344
+ urls:: PARENT . into ( ) ,
345
+ Value :: AtomicUrl ( base. to_string ( ) ) ,
346
+ store,
347
+ ) ?;
316
348
importer. set ( urls:: NAME . into ( ) , Value :: String ( "Import" . into ( ) ) , store) ?;
317
349
importer. save_locally ( store) ?;
318
350
Ok ( ( ) )
@@ -323,7 +355,7 @@ pub fn populate_importer(store: &crate::Db) -> AtomicResult<()> {
323
355
/// Useful for helping a new user get started.
324
356
pub fn populate_sidebar_items ( store : & crate :: Db ) -> AtomicResult < ( ) > {
325
357
let base = store. get_self_url ( ) . ok_or ( "No self_url" ) ?;
326
- let mut drive = store. get_resource ( & base) ?;
358
+ let mut drive = store. get_resource ( base. as_str ( ) ) ?;
327
359
let arr = vec ! [
328
360
format!( "{}/setup" , base) ,
329
361
format!( "{}/import" , base) ,
@@ -342,7 +374,13 @@ pub fn populate_all(store: &crate::Db) -> AtomicResult<()> {
342
374
// populate_base_models should be run in init, instead of here, since it will result in infinite loops without
343
375
populate_default_store ( store)
344
376
. map_err ( |e| format ! ( "Failed to populate default store. {}" , e) ) ?;
345
- create_drive ( store) . map_err ( |e| format ! ( "Failed to create drive. {}" , e) ) ?;
377
+ create_drive (
378
+ store,
379
+ None ,
380
+ & store. get_default_agent ( ) . unwrap ( ) . subject ,
381
+ true ,
382
+ )
383
+ . map_err ( |e| format ! ( "Failed to create drive. {}" , e) ) ?;
346
384
create_default_ontology ( store)
347
385
. map_err ( |e| format ! ( "Failed to create default ontology. {}" , e) ) ?;
348
386
set_drive_rights ( store, true ) ?;
0 commit comments