@@ -20,18 +20,20 @@ use tracing::instrument;
2020
2121use crate :: {
2222 agents:: ForAgent ,
23- atomic_url:: AtomicUrl ,
23+ atomic_url:: { AtomicUrl , Routes } ,
2424 atoms:: IndexAtom ,
2525 commit:: CommitResponse ,
2626 db:: { query_index:: NO_VALUE , val_prop_sub_index:: find_in_val_prop_sub_index} ,
2727 email:: { self , MailMessage } ,
28- endpoints:: { default_endpoints , Endpoint , HandleGetContext } ,
28+ endpoints:: { build_default_endpoints , Endpoint , HandleGetContext } ,
2929 errors:: { AtomicError , AtomicResult } ,
30+ plugins,
3031 query:: QueryResult ,
3132 resources:: PropVals ,
3233 storelike:: Storelike ,
34+ urls,
3335 values:: SortableValue ,
34- Atom , Query , Resource ,
36+ Atom , Query , Resource , Value ,
3537} ;
3638
3739use self :: {
@@ -112,7 +114,7 @@ impl Db {
112114 prop_val_sub_index,
113115 server_url : AtomicUrl :: try_from ( server_url) ?,
114116 watched_queries,
115- endpoints : default_endpoints ( ) ,
117+ endpoints : Vec :: new ( ) ,
116118 handle_commit : None ,
117119 smtp_client : None ,
118120 } ;
@@ -220,9 +222,40 @@ impl Db {
220222 Some ( Resource :: from_propvals ( propvals, subject) )
221223 }
222224
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+
223247 /// 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 ) ?;
225257 self . endpoints . push ( endpoint) ;
258+ Ok ( ( ) )
226259 }
227260
228261 /// Registers an SMTP client to the store, allowing the store to send emails.
0 commit comments