diff options
Diffstat (limited to 'rust/src')
-rw-r--r-- | rust/src/bin/fatcatd.rs | 3 | ||||
-rw-r--r-- | rust/src/endpoint_handlers.rs | 3 | ||||
-rw-r--r-- | rust/src/endpoints.rs | 206 | ||||
-rw-r--r-- | rust/src/entity_crud.rs | 8 |
4 files changed, 134 insertions, 86 deletions
diff --git a/rust/src/bin/fatcatd.rs b/rust/src/bin/fatcatd.rs index 6f5610f0..b27ff911 100644 --- a/rust/src/bin/fatcatd.rs +++ b/rust/src/bin/fatcatd.rs @@ -35,8 +35,7 @@ impl AfterMiddleware for XClacksOverheadMiddleware { /// Create custom server, wire it to the autogenerated router, /// and pass it to the web server. fn main() -> Result<()> { - let _matches = App::new("server") - .get_matches(); + let _matches = App::new("server").get_matches(); dotenv::dotenv().ok(); diff --git a/rust/src/endpoint_handlers.rs b/rust/src/endpoint_handlers.rs index cc717344..1b7bd0b6 100644 --- a/rust/src/endpoint_handlers.rs +++ b/rust/src/endpoint_handlers.rs @@ -26,7 +26,6 @@ macro_rules! entity_auto_batch_handler { entity_list: &[models::$model], editor_id: FatcatId, ) -> Result<Editgroup> { - let editgroup_row = editgroup.db_create(conn, true)?; let editgroup_id = FatcatId::from_uuid(&editgroup_row.id); let edit_context = make_edit_context(editor_id, editgroup_id, true)?; @@ -39,7 +38,7 @@ macro_rules! entity_auto_batch_handler { .get_result(conn)?; self.get_editgroup_handler(conn, editgroup_id) } - } + }; } pub fn get_release_files( diff --git a/rust/src/endpoints.rs b/rust/src/endpoints.rs index fda4688c..7ac0a068 100644 --- a/rust/src/endpoints.rs +++ b/rust/src/endpoints.rs @@ -76,7 +76,6 @@ macro_rules! wrap_entity_handlers { $delete_edit_fn:ident, $delete_edit_resp:ident, $get_rev_fn:ident, $get_rev_resp:ident, $get_redirects_fn:ident, $get_redirects_resp:ident, $model:ident) => { - fn $get_fn( &self, ident: String, @@ -99,11 +98,12 @@ macro_rules! wrap_entity_handlers { let mut entity = $model::db_get(&conn, entity_id, hide_flags)?; entity.db_expand(&conn, expand_flags)?; Ok(entity) - }, + } } - })().map_err(|e| FatcatError::from(e)) { - Ok(entity) => - $get_resp::FoundEntity(entity), + })() + .map_err(|e| FatcatError::from(e)) + { + Ok(entity) => $get_resp::FoundEntity(entity), Err(fe) => generic_err_responses!(fe, $get_resp), }; Box::new(futures::done(Ok(ret))) @@ -116,19 +116,27 @@ macro_rules! wrap_entity_handlers { context: &Context, ) -> Box<dyn Future<Item = $post_resp, Error = ApiError> + Send> { let conn = self.db_pool.get().expect("db_pool error"); - let ret = match conn.transaction(|| { - let editgroup_id = FatcatId::from_str(&editgroup_id)?; - let auth_context = self.auth_confectionary.require_auth(&conn, &context.auth_data, Some(stringify!($post_fn)))?; - auth_context.require_role(FatcatRole::Editor)?; - auth_context.require_editgroup(&conn, editgroup_id)?; - let edit_context = make_edit_context(auth_context.editor_id, editgroup_id, false)?; - edit_context.check(&conn)?; - entity.db_create(&conn, &edit_context)?.into_model() - }).map_err(|e| FatcatError::from(e)) { + let ret = match conn + .transaction(|| { + let editgroup_id = FatcatId::from_str(&editgroup_id)?; + let auth_context = self.auth_confectionary.require_auth( + &conn, + &context.auth_data, + Some(stringify!($post_fn)), + )?; + auth_context.require_role(FatcatRole::Editor)?; + auth_context.require_editgroup(&conn, editgroup_id)?; + let edit_context = + make_edit_context(auth_context.editor_id, editgroup_id, false)?; + edit_context.check(&conn)?; + entity.db_create(&conn, &edit_context)?.into_model() + }) + .map_err(|e| FatcatError::from(e)) + { Ok(edit) => { self.metrics.incr("entities.created").ok(); $post_resp::CreatedEntity(edit) - }, + } Err(fe) => generic_auth_err_responses!(fe, $post_resp), }; Box::new(futures::done(Ok(ret))) @@ -141,7 +149,11 @@ macro_rules! wrap_entity_handlers { ) -> Box<dyn Future<Item = $post_auto_batch_resp, Error = ApiError> + Send> { let conn = self.db_pool.get().expect("db_pool error"); let ret = match conn.transaction(|| { - let auth_context = self.auth_confectionary.require_auth(&conn, &context.auth_data, Some(stringify!($post_auto_batch_fn)))?; + let auth_context = self.auth_confectionary.require_auth( + &conn, + &context.auth_data, + Some(stringify!($post_auto_batch_fn)), + )?; auth_context.require_role(FatcatRole::Admin)?; let mut editgroup = auto_batch.editgroup.clone(); // TODO: this is duplicated code with create_editgroup() @@ -151,16 +163,21 @@ macro_rules! wrap_entity_handlers { && !auth_context.has_role(FatcatRole::Admin) { return Err(FatcatError::InsufficientPrivileges( - "not authorized to create editgroups in others' names".to_string() - )) + "not authorized to create editgroups in others' names".to_string(), + )); } } None => { editgroup.editor_id = Some(auth_context.editor_id.to_string()); } }; - self.$post_auto_batch_handler(&conn, editgroup, &auto_batch.entity_list, auth_context.editor_id) - .map_err(|e| FatcatError::from(e)) + self.$post_auto_batch_handler( + &conn, + editgroup, + &auto_batch.entity_list, + auth_context.editor_id, + ) + .map_err(|e| FatcatError::from(e)) }) { Ok(editgroup) => { // TODO: need a count helper on editgroup @@ -168,7 +185,7 @@ macro_rules! wrap_entity_handlers { self.metrics.incr("editgroup.created").ok(); self.metrics.incr("editgroup.accepted").ok(); $post_auto_batch_resp::CreatedEditgroup(editgroup) - }, + } Err(fe) => generic_auth_err_responses!(fe, $post_auto_batch_resp), }; Box::new(futures::done(Ok(ret))) @@ -182,20 +199,30 @@ macro_rules! wrap_entity_handlers { context: &Context, ) -> Box<dyn Future<Item = $update_resp, Error = ApiError> + Send> { let conn = self.db_pool.get().expect("db_pool error"); - let ret = match conn.transaction(|| { - let editgroup_id = FatcatId::from_str(&editgroup_id)?; - let auth_context = self.auth_confectionary.require_auth(&conn, &context.auth_data, Some(stringify!($update_fn)))?; - auth_context.require_role(FatcatRole::Editor)?; - let entity_id = FatcatId::from_str(&ident)?; - auth_context.require_editgroup(&conn, editgroup_id)?; - let edit_context = make_edit_context(auth_context.editor_id, editgroup_id, false)?; - edit_context.check(&conn)?; - entity.db_update(&conn, &edit_context, entity_id)?.into_model() - }).map_err(|e| FatcatError::from(e)) { + let ret = match conn + .transaction(|| { + let editgroup_id = FatcatId::from_str(&editgroup_id)?; + let auth_context = self.auth_confectionary.require_auth( + &conn, + &context.auth_data, + Some(stringify!($update_fn)), + )?; + auth_context.require_role(FatcatRole::Editor)?; + let entity_id = FatcatId::from_str(&ident)?; + auth_context.require_editgroup(&conn, editgroup_id)?; + let edit_context = + make_edit_context(auth_context.editor_id, editgroup_id, false)?; + edit_context.check(&conn)?; + entity + .db_update(&conn, &edit_context, entity_id)? + .into_model() + }) + .map_err(|e| FatcatError::from(e)) + { Ok(edit) => { self.metrics.incr("entities.updated").ok(); $update_resp::UpdatedEntity(edit) - }, + } Err(fe) => generic_auth_err_responses!(fe, $update_resp), }; Box::new(futures::done(Ok(ret))) @@ -208,20 +235,28 @@ macro_rules! wrap_entity_handlers { context: &Context, ) -> Box<dyn Future<Item = $delete_resp, Error = ApiError> + Send> { let conn = self.db_pool.get().expect("db_pool error"); - let ret = match conn.transaction(|| { - let editgroup_id = FatcatId::from_str(&editgroup_id)?; - let auth_context = self.auth_confectionary.require_auth(&conn, &context.auth_data, Some(stringify!($delete_fn)))?; - auth_context.require_role(FatcatRole::Editor)?; - let entity_id = FatcatId::from_str(&ident)?; - auth_context.require_editgroup(&conn, editgroup_id)?; - let edit_context = make_edit_context(auth_context.editor_id, editgroup_id, false)?; - edit_context.check(&conn)?; - $model::db_delete(&conn, &edit_context, entity_id)?.into_model() - }).map_err(|e| FatcatError::from(e)) { + let ret = match conn + .transaction(|| { + let editgroup_id = FatcatId::from_str(&editgroup_id)?; + let auth_context = self.auth_confectionary.require_auth( + &conn, + &context.auth_data, + Some(stringify!($delete_fn)), + )?; + auth_context.require_role(FatcatRole::Editor)?; + let entity_id = FatcatId::from_str(&ident)?; + auth_context.require_editgroup(&conn, editgroup_id)?; + let edit_context = + make_edit_context(auth_context.editor_id, editgroup_id, false)?; + edit_context.check(&conn)?; + $model::db_delete(&conn, &edit_context, entity_id)?.into_model() + }) + .map_err(|e| FatcatError::from(e)) + { Ok(edit) => { self.metrics.incr("entities.deleted").ok(); $delete_resp::DeletedEntity(edit) - }, + } Err(fe) => generic_auth_err_responses!(fe, $delete_resp), }; Box::new(futures::done(Ok(ret))) @@ -238,9 +273,10 @@ macro_rules! wrap_entity_handlers { let ret = match (|| { let entity_id = FatcatId::from_str(&ident)?; $model::db_get_history(&conn, entity_id, limit) - })().map_err(|e| FatcatError::from(e)) { - Ok(history) => - $get_history_resp::FoundEntityHistory(history), + })() + .map_err(|e| FatcatError::from(e)) + { + Ok(history) => $get_history_resp::FoundEntityHistory(history), Err(fe) => generic_err_responses!(fe, $get_history_resp), }; Box::new(futures::done(Ok(ret))) @@ -268,11 +304,12 @@ macro_rules! wrap_entity_handlers { let mut entity = $model::db_get_rev(&conn, rev_id, hide_flags)?; entity.db_expand(&conn, expand_flags)?; Ok(entity) - }, + } } - })().map_err(|e| FatcatError::from(e)) { - Ok(entity) => - $get_rev_resp::FoundEntityRevision(entity), + })() + .map_err(|e| FatcatError::from(e)) + { + Ok(entity) => $get_rev_resp::FoundEntityRevision(entity), Err(fe) => generic_err_responses!(fe, $get_rev_resp), }; Box::new(futures::done(Ok(ret))) @@ -288,9 +325,10 @@ macro_rules! wrap_entity_handlers { let ret = match (|| { let edit_id = Uuid::from_str(&edit_id)?; $model::db_get_edit(&conn, edit_id)?.into_model() - })().map_err(|e| FatcatError::from(e)) { - Ok(edit) => - $get_edit_resp::FoundEdit(edit), + })() + .map_err(|e| FatcatError::from(e)) + { + Ok(edit) => $get_edit_resp::FoundEdit(edit), Err(fe) => generic_err_responses!(fe, $get_edit_resp), }; Box::new(futures::done(Ok(ret))) @@ -306,23 +344,29 @@ macro_rules! wrap_entity_handlers { let ret = match conn.transaction(|| { let editgroup_id = FatcatId::from_str(&editgroup_id)?; let edit_id = Uuid::from_str(&edit_id)?; - let auth_context = self.auth_confectionary.require_auth(&conn, &context.auth_data, Some(stringify!($delete_edit_fn)))?; + let auth_context = self.auth_confectionary.require_auth( + &conn, + &context.auth_data, + Some(stringify!($delete_edit_fn)), + )?; auth_context.require_role(FatcatRole::Editor)?; let edit = $model::db_get_edit(&conn, edit_id)?; if !(edit.editgroup_id == editgroup_id.to_uuid()) { return Err(FatcatError::BadRequest( - "editgroup_id parameter didn't match that of the edit".to_string() - )) + "editgroup_id parameter didn't match that of the edit".to_string(), + )); } auth_context.require_editgroup(&conn, editgroup_id)?; // check for editgroup being deleted happens in db_delete_edit() - $model::db_delete_edit(&conn, edit_id) - .map_err(|e| FatcatError::from(e)) + $model::db_delete_edit(&conn, edit_id).map_err(|e| FatcatError::from(e)) }) { - Ok(()) => - $delete_edit_resp::DeletedEdit(Success { - success: true, - message: format!("Successfully deleted work-in-progress {} edit: {}", stringify!($model), edit_id) + Ok(()) => $delete_edit_resp::DeletedEdit(Success { + success: true, + message: format!( + "Successfully deleted work-in-progress {} edit: {}", + stringify!($model), + edit_id + ), }), Err(fe) => generic_auth_err_responses!(fe, $delete_edit_resp), }; @@ -340,15 +384,15 @@ macro_rules! wrap_entity_handlers { let entity_id = FatcatId::from_str(&ident)?; let redirects: Vec<FatcatId> = $model::db_get_redirects(&conn, entity_id)?; Ok(redirects.into_iter().map(|fcid| fcid.to_string()).collect()) - })().map_err(|e: Error| FatcatError::from(e)) { - Ok(redirects) => - $get_redirects_resp::FoundEntityRedirects(redirects), + })() + .map_err(|e: Error| FatcatError::from(e)) + { + Ok(redirects) => $get_redirects_resp::FoundEntityRedirects(redirects), Err(fe) => generic_err_responses!(fe, $get_redirects_resp), }; Box::new(futures::done(Ok(ret))) } - - } + }; } macro_rules! wrap_lookup_handler { @@ -371,14 +415,16 @@ macro_rules! wrap_lookup_handler { Some(param) => HideFlags::from_str(¶m).unwrap(), }; // No transaction for GET - let ret = match self.$get_handler(&conn, &$idname, &wikidata_qid, expand_flags, hide_flags).map_err(|e| FatcatError::from(e)) { - Ok(entity) => - $get_resp::FoundEntity(entity), + let ret = match self + .$get_handler(&conn, &$idname, &wikidata_qid, expand_flags, hide_flags) + .map_err(|e| FatcatError::from(e)) + { + Ok(entity) => $get_resp::FoundEntity(entity), Err(fe) => generic_err_responses!(fe, $get_resp), }; Box::new(futures::done(Ok(ret))) } - } + }; } macro_rules! wrap_fcid_handler { @@ -393,14 +439,15 @@ macro_rules! wrap_fcid_handler { let ret = match (|| { let fcid = FatcatId::from_str(&id)?; self.$get_handler(&conn, fcid) - })().map_err(|e| FatcatError::from(e)) { - Ok(entity) => - $get_resp::Found(entity), + })() + .map_err(|e| FatcatError::from(e)) + { + Ok(entity) => $get_resp::Found(entity), Err(fe) => generic_err_responses!(fe, $get_resp), }; Box::new(futures::done(Ok(ret))) } - } + }; } macro_rules! wrap_fcid_hide_handler { @@ -420,14 +467,15 @@ macro_rules! wrap_fcid_hide_handler { Some(param) => HideFlags::from_str(¶m)?, }; self.$get_handler(&conn, fcid, hide_flags) - })().map_err(|e| FatcatError::from(e)) { - Ok(entity) => - $get_resp::Found(entity), + })() + .map_err(|e| FatcatError::from(e)) + { + Ok(entity) => $get_resp::Found(entity), Err(fe) => generic_err_responses!(fe, $get_resp), }; Box::new(futures::done(Ok(ret))) } - } + }; } impl Api for Server { diff --git a/rust/src/entity_crud.rs b/rust/src/entity_crud.rs index 89ee83bf..bac8c0fc 100644 --- a/rust/src/entity_crud.rs +++ b/rust/src/entity_crud.rs @@ -334,7 +334,9 @@ macro_rules! generic_db_create { fn db_create(&self, conn: &DbConn, edit_context: &EditContext) -> Result<Self::EditRow> { if self.redirect.is_some() { return Err(FatcatError::BadRequest( - "can't create an entity that redirects from the start".to_string()).into()); + "can't create an entity that redirects from the start".to_string(), + ) + .into()); } let rev_id = self.db_insert_rev(conn)?; let ident: Uuid = insert_into($ident_table::table) @@ -351,7 +353,7 @@ macro_rules! generic_db_create { .get_result(conn)?; Ok(edit) } - } + }; } macro_rules! generic_db_create_batch { @@ -764,7 +766,7 @@ macro_rules! generic_db_insert_rev { fn db_insert_rev(&self, conn: &DbConn) -> Result<Uuid> { Self::db_insert_revs(conn, &[self]).map(|id_list| id_list[0]) } - } + }; } impl EntityCrud for ContainerEntity { |