Skip to content

Commit c741566

Browse files
committed
Do not add register endpoint if there is no SMTP server
1 parent bff73c3 commit c741566

File tree

5 files changed

+42
-28
lines changed

5 files changed

+42
-28
lines changed

lib/src/db.rs

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,20 @@ use tracing::{info, instrument};
2020

2121
use crate::{
2222
agents::ForAgent,
23-
atomic_url::AtomicUrl,
23+
atomic_url::{AtomicUrl, Routes},
2424
atoms::IndexAtom,
2525
commit::CommitResponse,
2626
db::{query_index::requires_query_index, 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

3739
use 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,6 +222,28 @@ 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
fn build_index_for_atom(
224248
&self,
225249
atom: &IndexAtom,
@@ -321,8 +345,17 @@ impl Db {
321345
}
322346

323347
/// Adds an [Endpoint] to the store. This means adding a route with custom behavior.
324-
pub fn register_endpoint(&mut self, endpoint: Endpoint) {
348+
pub fn register_endpoint(&mut self, endpoint: Endpoint) -> AtomicResult<()> {
349+
let mut resource = endpoint.to_resource(self)?;
350+
let endpoints_collection = self.get_server_url().set_route(Routes::Endpoints);
351+
resource.set_propval(
352+
urls::PARENT.into(),
353+
Value::AtomicUrl(endpoints_collection.to_string()),
354+
self,
355+
)?;
356+
resource.save_locally(self)?;
325357
self.endpoints.push(endpoint);
358+
Ok(())
326359
}
327360

328361
/// Registers an SMTP client to the store, allowing the store to send emails.

lib/src/endpoints.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,13 @@ impl std::fmt::Debug for Endpoint {
8787
}
8888
}
8989

90-
pub fn default_endpoints() -> Vec<Endpoint> {
90+
pub fn build_default_endpoints() -> Vec<Endpoint> {
9191
vec![
9292
plugins::versioning::version_endpoint(),
9393
plugins::versioning::all_versions_endpoint(),
9494
plugins::path::path_endpoint(),
9595
plugins::search::search_endpoint(),
9696
plugins::files::upload_endpoint(),
97-
plugins::register::register_endpoint(),
98-
plugins::register::confirm_email_endpoint(),
9997
#[cfg(feature = "html")]
10098
plugins::bookmark::bookmark_endpoint(),
10199
plugins::importer::import_endpoint(),

lib/src/plugins/mod.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ They are used for performing custom queries, or calculating dynamic attributes.
3434
Add these by registering the handler at [crate::db::Db::get_resource_extended].
3535
*/
3636

37-
use crate::endpoints::Endpoint;
38-
3937
// Class Extenders
4038
pub mod chatroom;
4139
pub mod importer;
@@ -55,19 +53,3 @@ pub mod versioning;
5553

5654
// Utilities / helpers
5755
mod utils;
58-
59-
pub fn default_endpoints() -> Vec<Endpoint> {
60-
vec![
61-
versioning::version_endpoint(),
62-
versioning::all_versions_endpoint(),
63-
path::path_endpoint(),
64-
search::search_endpoint(),
65-
files::upload_endpoint(),
66-
register::register_endpoint(),
67-
register::confirm_email_endpoint(),
68-
add_pubkey::request_email_add_pubkey(),
69-
add_pubkey::confirm_add_pubkey(),
70-
#[cfg(feature = "html")]
71-
bookmark::bookmark_endpoint(),
72-
]
73-
}

lib/src/populate.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,6 @@ pub fn populate_all(store: &crate::Db) -> AtomicResult<()> {
383383
.map_err(|e| format!("Failed to create default ontology. {}", e))?;
384384
set_drive_rights(store, true)?;
385385
populate_collections(store).map_err(|e| format!("Failed to populate collections. {}", e))?;
386-
populate_endpoints(store).map_err(|e| format!("Failed to populate endpoints. {}", e))?;
387386
populate_sidebar_items(store)
388387
.map_err(|e| format!("Failed to populate sidebar items. {}", e))?;
389388
Ok(())

server/src/appstate.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ pub struct AppState {
2929

3030
/// Initializes the Store and sets the default agent.
3131
pub fn init_store(config: &Config) -> AtomicServerResult<Db> {
32-
let store = atomic_lib::Db::init(&config.store_path, &config.server_url)?;
32+
let mut store = atomic_lib::Db::init(&config.store_path, &config.server_url)?;
3333

3434
tracing::info!("Setting default agent");
3535
set_default_agent(config, &store)?;
36+
store.register_default_endpoints()?;
37+
3638
Ok(store)
3739
}
3840

0 commit comments

Comments
 (0)