Skip to content

Commit 61c686f

Browse files
committed
Do not add register endpoint if there is no SMTP server
1 parent bdc1214 commit 61c686f

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::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::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

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,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.

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;
@@ -54,19 +52,3 @@ pub mod versioning;
5452

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

lib/src/populate.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,6 @@ pub fn populate_all(store: &crate::Db) -> AtomicResult<()> {
313313
create_drive(store, None, &store.get_default_agent()?.subject, true)
314314
.map_err(|e| format!("Failed to create drive. {}", e))?;
315315
populate_collections(store).map_err(|e| format!("Failed to populate collections. {}", e))?;
316-
populate_endpoints(store).map_err(|e| format!("Failed to populate endpoints. {}", e))?;
317316
populate_sidebar_items(store)
318317
.map_err(|e| format!("Failed to populate sidebar items. {}", e))?;
319318
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)