Skip to content

Commit

Permalink
Do not add register endpoint if there is no SMTP server
Browse files Browse the repository at this point in the history
  • Loading branch information
joepio committed Jul 23, 2024
1 parent bff73c3 commit c741566
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 28 deletions.
43 changes: 38 additions & 5 deletions lib/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,20 @@ use tracing::{info, instrument};

use crate::{
agents::ForAgent,
atomic_url::AtomicUrl,
atomic_url::{AtomicUrl, Routes},
atoms::IndexAtom,
commit::CommitResponse,
db::{query_index::requires_query_index, val_prop_sub_index::find_in_val_prop_sub_index},
email::{self, MailMessage},
endpoints::{default_endpoints, Endpoint, HandleGetContext},
endpoints::{build_default_endpoints, Endpoint, HandleGetContext},
errors::{AtomicError, AtomicResult},
plugins,
query::QueryResult,
resources::PropVals,
storelike::Storelike,
urls,
values::SortableValue,
Atom, Query, Resource,
Atom, Query, Resource, Value,
};

use self::{
Expand Down Expand Up @@ -112,7 +114,7 @@ impl Db {
prop_val_sub_index,
server_url: AtomicUrl::try_from(server_url)?,
watched_queries,
endpoints: default_endpoints(),
endpoints: Vec::new(),
handle_commit: None,
smtp_client: None,
};
Expand Down Expand Up @@ -220,6 +222,28 @@ impl Db {
Some(Resource::from_propvals(propvals, subject))
}

pub fn register_default_endpoints(&mut self) -> AtomicResult<()> {
// First we delete all existing endpoint resources, as they might not be there in this new run
let found_endpoints = self.query(&Query::new_class(urls::ENDPOINT))?.resources;

for mut found in found_endpoints {
found.destroy(self)?;
}

let mut endpoints = build_default_endpoints();

if self.smtp_client.is_some() {
endpoints.push(plugins::register::register_endpoint());
endpoints.push(plugins::register::confirm_email_endpoint());
}

for endpoint in endpoints {
self.register_endpoint(endpoint)?;
}

Ok(())
}

fn build_index_for_atom(
&self,
atom: &IndexAtom,
Expand Down Expand Up @@ -321,8 +345,17 @@ impl Db {
}

/// Adds an [Endpoint] to the store. This means adding a route with custom behavior.
pub fn register_endpoint(&mut self, endpoint: Endpoint) {
pub fn register_endpoint(&mut self, endpoint: Endpoint) -> AtomicResult<()> {
let mut resource = endpoint.to_resource(self)?;
let endpoints_collection = self.get_server_url().set_route(Routes::Endpoints);
resource.set_propval(
urls::PARENT.into(),
Value::AtomicUrl(endpoints_collection.to_string()),
self,
)?;
resource.save_locally(self)?;
self.endpoints.push(endpoint);
Ok(())
}

/// Registers an SMTP client to the store, allowing the store to send emails.
Expand Down
4 changes: 1 addition & 3 deletions lib/src/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,13 @@ impl std::fmt::Debug for Endpoint {
}
}

pub fn default_endpoints() -> Vec<Endpoint> {
pub fn build_default_endpoints() -> Vec<Endpoint> {
vec![
plugins::versioning::version_endpoint(),
plugins::versioning::all_versions_endpoint(),
plugins::path::path_endpoint(),
plugins::search::search_endpoint(),
plugins::files::upload_endpoint(),
plugins::register::register_endpoint(),
plugins::register::confirm_email_endpoint(),
#[cfg(feature = "html")]
plugins::bookmark::bookmark_endpoint(),
plugins::importer::import_endpoint(),
Expand Down
18 changes: 0 additions & 18 deletions lib/src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ They are used for performing custom queries, or calculating dynamic attributes.
Add these by registering the handler at [crate::db::Db::get_resource_extended].
*/

use crate::endpoints::Endpoint;

// Class Extenders
pub mod chatroom;
pub mod importer;
Expand All @@ -55,19 +53,3 @@ pub mod versioning;

// Utilities / helpers
mod utils;

pub fn default_endpoints() -> Vec<Endpoint> {
vec![
versioning::version_endpoint(),
versioning::all_versions_endpoint(),
path::path_endpoint(),
search::search_endpoint(),
files::upload_endpoint(),
register::register_endpoint(),
register::confirm_email_endpoint(),
add_pubkey::request_email_add_pubkey(),
add_pubkey::confirm_add_pubkey(),
#[cfg(feature = "html")]
bookmark::bookmark_endpoint(),
]
}
1 change: 0 additions & 1 deletion lib/src/populate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,6 @@ pub fn populate_all(store: &crate::Db) -> AtomicResult<()> {
.map_err(|e| format!("Failed to create default ontology. {}", e))?;
set_drive_rights(store, true)?;
populate_collections(store).map_err(|e| format!("Failed to populate collections. {}", e))?;
populate_endpoints(store).map_err(|e| format!("Failed to populate endpoints. {}", e))?;
populate_sidebar_items(store)
.map_err(|e| format!("Failed to populate sidebar items. {}", e))?;
Ok(())
Expand Down
4 changes: 3 additions & 1 deletion server/src/appstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ pub struct AppState {

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

tracing::info!("Setting default agent");
set_default_agent(config, &store)?;
store.register_default_endpoints()?;

Ok(store)
}

Expand Down

0 comments on commit c741566

Please sign in to comment.