@@ -20,18 +20,20 @@ use tracing::instrument;
20
20
21
21
use crate :: {
22
22
agents:: ForAgent ,
23
- atomic_url:: AtomicUrl ,
23
+ atomic_url:: { AtomicUrl , Routes } ,
24
24
atoms:: IndexAtom ,
25
25
commit:: CommitResponse ,
26
26
db:: { query_index:: NO_VALUE , val_prop_sub_index:: find_in_val_prop_sub_index} ,
27
27
email:: { self , MailMessage } ,
28
- endpoints:: { default_endpoints , Endpoint , HandleGetContext } ,
28
+ endpoints:: { build_default_endpoints , Endpoint , HandleGetContext } ,
29
29
errors:: { AtomicError , AtomicResult } ,
30
+ plugins,
30
31
query:: QueryResult ,
31
32
resources:: PropVals ,
32
33
storelike:: Storelike ,
34
+ urls,
33
35
values:: SortableValue ,
34
- Atom , Query , Resource ,
36
+ Atom , Query , Resource , Value ,
35
37
} ;
36
38
37
39
use self :: {
@@ -112,7 +114,7 @@ impl Db {
112
114
prop_val_sub_index,
113
115
server_url : AtomicUrl :: try_from ( server_url) ?,
114
116
watched_queries,
115
- endpoints : default_endpoints ( ) ,
117
+ endpoints : Vec :: new ( ) ,
116
118
handle_commit : None ,
117
119
smtp_client : None ,
118
120
} ;
@@ -220,9 +222,40 @@ impl Db {
220
222
Some ( Resource :: from_propvals ( propvals, subject) )
221
223
}
222
224
225
+ pub fn register_default_endpoints ( & mut self ) -> AtomicResult < ( ) > {
226
+ // First we delete all existing endpoint resources, as they might not be there in this new run
227
+ let found_endpoints = self . query ( & Query :: new_class ( urls:: ENDPOINT ) ) ?. resources ;
228
+
229
+ for mut found in found_endpoints {
230
+ found. destroy ( self ) ?;
231
+ }
232
+
233
+ let mut endpoints = build_default_endpoints ( ) ;
234
+
235
+ if self . smtp_client . is_some ( ) {
236
+ endpoints. push ( plugins:: register:: register_endpoint ( ) ) ;
237
+ endpoints. push ( plugins:: register:: confirm_email_endpoint ( ) ) ;
238
+ }
239
+
240
+ for endpoint in endpoints {
241
+ self . register_endpoint ( endpoint) ?;
242
+ }
243
+
244
+ Ok ( ( ) )
245
+ }
246
+
223
247
/// Adds an [Endpoint] to the store. This means adding a route with custom behavior.
224
- pub fn register_endpoint ( & mut self , endpoint : Endpoint ) {
248
+ pub fn register_endpoint ( & mut self , endpoint : Endpoint ) -> AtomicResult < ( ) > {
249
+ let mut resource = endpoint. to_resource ( self ) ?;
250
+ let endpoints_collection = self . get_server_url ( ) . set_route ( Routes :: Endpoints ) ;
251
+ resource. set_propval (
252
+ urls:: PARENT . into ( ) ,
253
+ Value :: AtomicUrl ( endpoints_collection. to_string ( ) ) ,
254
+ self ,
255
+ ) ?;
256
+ resource. save_locally ( self ) ?;
225
257
self . endpoints . push ( endpoint) ;
258
+ Ok ( ( ) )
226
259
}
227
260
228
261
/// Registers an SMTP client to the store, allowing the store to send emails.
0 commit comments