diff options
-rw-r--r-- | rust/src/api_helpers.rs | 1 | ||||
-rw-r--r-- | rust/src/api_server.rs | 79 | ||||
-rw-r--r-- | rust/src/api_wrappers.rs | 2 | ||||
-rw-r--r-- | rust/src/database_entity_crud.rs | 8 |
4 files changed, 41 insertions, 49 deletions
diff --git a/rust/src/api_helpers.rs b/rust/src/api_helpers.rs index fd0ad9e7..925a6073 100644 --- a/rust/src/api_helpers.rs +++ b/rust/src/api_helpers.rs @@ -91,6 +91,7 @@ pub fn accept_editgroup(editgroup_id: Uuid, conn: &PgConnection) -> Result<Chang Ok(entry) } +#[derive(Clone)] pub struct FatCatId(Uuid); impl ToString for FatCatId { diff --git a/rust/src/api_server.rs b/rust/src/api_server.rs index 0e68777b..56fbca19 100644 --- a/rust/src/api_server.rs +++ b/rust/src/api_server.rs @@ -15,7 +15,6 @@ use diesel::{self, insert_into}; use errors::*; use fatcat_api::models; use fatcat_api::models::*; -use sha1::Sha1; use std::str::FromStr; use uuid::Uuid; use ConnectionPool; @@ -29,34 +28,19 @@ macro_rules! entity_batch_handler { editgroup: Option<String>, conn: &DbConn, ) -> Result<Vec<EntityEdit>> { - // editgroup override logic based on parameters - let eg_id: Option<Uuid> = match (editgroup, autoaccept) { - (Some(eg_string), _) => Some(fcid2uuid(&eg_string)?), - (None, true) => { - let eg_row: EditgroupRow = diesel::insert_into(editgroup::table) - .values((editgroup::editor_id.eq(editor_id),)) - .get_result(conn)?; - Some(eg_row.id) - }, - (None, false) => None + + let editgroup_id: Option<FatCatId> = match editgroup { + Some(s) => Some(FatCatId::from_str(&s)?), + None => None, }; - for entity in entity_list { - let mut e = entity.clone(); - // override individual editgroup IDs (if set earlier) - if let Some(inner_id) = eg_id { - e.editgroup_id = Some(uuid2fcid(&inner_id)); - } - // actual wrapped function call here - ret.push(self.$post_handler(e, autoaccept, conn)?); - } - let mut edit_context = make_edit_context(conn, eg_id)?; - edit_context.autoaccept = autoaccept; + let edit_context = make_edit_context(conn, editgroup_id.clone(), autoaccept)?; let model_list: Vec<&models::$model> = entity_list.iter().map(|e| e).collect(); let edits = $model::db_create_batch(conn, &edit_context, model_list.as_slice())?; + if autoaccept { - // if autoaccept, eg_id is always Some let _clr: ChangelogRow = diesel::insert_into(changelog::table) - .values((changelog::editgroup_id.eq(eg_id.unwrap()),)) + // if autoaccept, eg_id is always Some + .values((changelog::editgroup_id.eq(edit_context.editgroup_id.to_uuid()),)) .get_result(conn)?; } edits.into_iter().map(|e| e.into_model()).collect() @@ -75,17 +59,24 @@ macro_rules! count_entity { }}; } -fn make_edit_context(conn: &DbConn, editgroup_id: Option<FatCatId>) -> Result<EditContext> { +fn make_edit_context(conn: &DbConn, editgroup_id: Option<FatCatId>, autoaccept: bool) -> Result<EditContext> { let editor_id = Uuid::parse_str("00000000-0000-0000-AAAA-000000000001")?; // TODO: auth - let editgroup_id = match editgroup_id { - None => FatCatId::from_uuid(&get_or_create_editgroup(editor_id, conn)?), - Some(param) => param, + let editgroup_id: FatCatId = match (editgroup_id, autoaccept) { + (Some(eg), _) => eg, + // If autoaccept and no editgroup_id passed, always create a new one for this transaction + (None, true) => { + let eg_row: EditgroupRow = diesel::insert_into(editgroup::table) + .values((editgroup::editor_id.eq(editor_id),)) + .get_result(conn)?; + FatCatId::from_uuid(&eg_row.id) + }, + (None, false) => FatCatId::from_uuid(&get_or_create_editgroup(editor_id, conn)?), }; Ok(EditContext { editor_id: FatCatId::from_uuid(&editor_id), editgroup_id: editgroup_id, extra_json: None, - autoapprove: false, + autoaccept: autoaccept, }) } @@ -268,7 +259,7 @@ impl Server { entity: models::ContainerEntity, conn: &DbConn, ) -> Result<EntityEdit> { - let edit_context = make_edit_context(conn, entity.parse_editgroup_id()?)?; + let edit_context = make_edit_context(conn, entity.parse_editgroup_id()?, false)?; let edit = entity.db_create(conn, &edit_context)?; edit.into_model() } @@ -279,7 +270,7 @@ impl Server { entity: models::ContainerEntity, conn: &DbConn, ) -> Result<EntityEdit> { - let edit_context = make_edit_context(conn, entity.parse_editgroup_id()?)?; + let edit_context = make_edit_context(conn, entity.parse_editgroup_id()?, false)?; let edit = entity.db_update(conn, &edit_context, FatCatId::from_uuid(id))?; edit.into_model() } @@ -289,7 +280,7 @@ impl Server { editgroup_id: Option<Uuid>, conn: &DbConn, ) -> Result<EntityEdit> { - let edit_context = make_edit_context(conn, editgroup_id.map(|u| FatCatId::from_uuid(&u)))?; + let edit_context = make_edit_context(conn, editgroup_id.map(|u| FatCatId::from_uuid(&u)), false)?; let edit = ContainerEntity::db_delete(conn, &edit_context, FatCatId::from_uuid(id))?; edit.into_model() } @@ -299,7 +290,7 @@ impl Server { entity: models::CreatorEntity, conn: &DbConn, ) -> Result<EntityEdit> { - let edit_context = make_edit_context(conn, entity.parse_editgroup_id()?)?; + let edit_context = make_edit_context(conn, entity.parse_editgroup_id()?, false)?; let edit = entity.db_create(conn, &edit_context)?; edit.into_model() } @@ -310,7 +301,7 @@ impl Server { entity: models::CreatorEntity, conn: &DbConn, ) -> Result<EntityEdit> { - let edit_context = make_edit_context(conn, entity.parse_editgroup_id()?)?; + let edit_context = make_edit_context(conn, entity.parse_editgroup_id()?, false)?; let edit = entity.db_update(conn, &edit_context, FatCatId::from_uuid(id))?; edit.into_model() } @@ -320,7 +311,7 @@ impl Server { editgroup_id: Option<Uuid>, conn: &DbConn, ) -> Result<EntityEdit> { - let edit_context = make_edit_context(conn, editgroup_id.map(|u| FatCatId::from_uuid(&u)))?; + let edit_context = make_edit_context(conn, editgroup_id.map(|u| FatCatId::from_uuid(&u)), false)?; let edit = CreatorEntity::db_delete(conn, &edit_context, FatCatId::from_uuid(id))?; edit.into_model() } @@ -330,7 +321,7 @@ impl Server { entity: models::FileEntity, conn: &DbConn, ) -> Result<EntityEdit> { - let edit_context = make_edit_context(conn, entity.parse_editgroup_id()?)?; + let edit_context = make_edit_context(conn, entity.parse_editgroup_id()?, false)?; let edit = entity.db_create(conn, &edit_context)?; edit.into_model() } @@ -341,7 +332,7 @@ impl Server { entity: models::FileEntity, conn: &DbConn, ) -> Result<EntityEdit> { - let edit_context = make_edit_context(conn, entity.parse_editgroup_id()?)?; + let edit_context = make_edit_context(conn, entity.parse_editgroup_id()?, false)?; let edit = entity.db_update(conn, &edit_context, FatCatId::from_uuid(id))?; edit.into_model() } @@ -351,7 +342,7 @@ impl Server { editgroup_id: Option<Uuid>, conn: &DbConn, ) -> Result<EntityEdit> { - let edit_context = make_edit_context(conn, editgroup_id.map(|u| FatCatId::from_uuid(&u)))?; + let edit_context = make_edit_context(conn, editgroup_id.map(|u| FatCatId::from_uuid(&u)), false)?; let edit = FileEntity::db_delete(conn, &edit_context, FatCatId::from_uuid(id))?; edit.into_model() } @@ -361,7 +352,7 @@ impl Server { entity: models::ReleaseEntity, conn: &DbConn, ) -> Result<EntityEdit> { - let edit_context = make_edit_context(conn, entity.parse_editgroup_id()?)?; + let edit_context = make_edit_context(conn, entity.parse_editgroup_id()?, false)?; let edit = entity.db_create(conn, &edit_context)?; edit.into_model() } @@ -372,7 +363,7 @@ impl Server { entity: models::ReleaseEntity, conn: &DbConn, ) -> Result<EntityEdit> { - let edit_context = make_edit_context(conn, entity.parse_editgroup_id()?)?; + let edit_context = make_edit_context(conn, entity.parse_editgroup_id()?, false)?; let edit = entity.db_update(conn, &edit_context, FatCatId::from_uuid(id))?; edit.into_model() } @@ -382,7 +373,7 @@ impl Server { editgroup_id: Option<Uuid>, conn: &DbConn, ) -> Result<EntityEdit> { - let edit_context = make_edit_context(conn, editgroup_id.map(|u| FatCatId::from_uuid(&u)))?; + let edit_context = make_edit_context(conn, editgroup_id.map(|u| FatCatId::from_uuid(&u)), false)?; let edit = ReleaseEntity::db_delete(conn, &edit_context, FatCatId::from_uuid(id))?; edit.into_model() } @@ -392,7 +383,7 @@ impl Server { entity: models::WorkEntity, conn: &DbConn, ) -> Result<EntityEdit> { - let edit_context = make_edit_context(conn, entity.parse_editgroup_id()?)?; + let edit_context = make_edit_context(conn, entity.parse_editgroup_id()?, false)?; let edit = entity.db_create(conn, &edit_context)?; edit.into_model() } @@ -403,7 +394,7 @@ impl Server { entity: models::WorkEntity, conn: &DbConn, ) -> Result<EntityEdit> { - let edit_context = make_edit_context(conn, entity.parse_editgroup_id()?)?; + let edit_context = make_edit_context(conn, entity.parse_editgroup_id()?, false)?; let edit = entity.db_update(conn, &edit_context, FatCatId::from_uuid(id))?; edit.into_model() } @@ -414,7 +405,7 @@ impl Server { editgroup_id: Option<Uuid>, conn: &DbConn, ) -> Result<EntityEdit> { - let edit_context = make_edit_context(conn, editgroup_id.map(|u| FatCatId::from_uuid(&u)))?; + let edit_context = make_edit_context(conn, editgroup_id.map(|u| FatCatId::from_uuid(&u)), false)?; let edit = WorkEntity::db_delete(conn, &edit_context, FatCatId::from_uuid(id))?; edit.into_model() diff --git a/rust/src/api_wrappers.rs b/rust/src/api_wrappers.rs index 6272814e..faafe984 100644 --- a/rust/src/api_wrappers.rs +++ b/rust/src/api_wrappers.rs @@ -61,7 +61,7 @@ macro_rules! wrap_entity_handlers { _context: &Context, ) -> Box<Future<Item = $post_resp, Error = ApiError> + Send> { let conn = self.db_pool.get().expect("db_pool error"); - let ret = match conn.transaction(|| self.$post_handler(entity, false, &conn)) { + let ret = match conn.transaction(|| self.$post_handler(entity, &conn)) { Ok(edit) => $post_resp::CreatedEntity(edit), Err(Error(ErrorKind::Diesel(e), _)) => diff --git a/rust/src/database_entity_crud.rs b/rust/src/database_entity_crud.rs index 3f13a27d..9a47c2de 100644 --- a/rust/src/database_entity_crud.rs +++ b/rust/src/database_entity_crud.rs @@ -3,7 +3,7 @@ use chrono; use database_models::*; use database_schema::*; use diesel::prelude::*; -use diesel::{self, insert_into}; +use diesel::insert_into; use errors::*; use fatcat_api::models::*; use serde_json; @@ -16,7 +16,7 @@ pub struct EditContext { pub editor_id: FatCatId, pub editgroup_id: FatCatId, pub extra_json: Option<serde_json::Value>, - pub autoapprove: bool, + pub autoaccept: bool, } /* One goal here is to abstract the non-entity-specific bits into generic traits or functions, @@ -155,7 +155,7 @@ macro_rules! generic_db_create_batch { .iter() .map(|rev_id| Self::IdentNewRow { rev_id: Some(rev_id.clone()), - is_live: edit_context.autoapprove, + is_live: edit_context.autoaccept, redirect_id: None, }) .collect::<Vec<Self::IdentNewRow>>(), @@ -650,7 +650,7 @@ impl EntityCrud for ReleaseEntity { .iter() .map(|rev_id| Self::IdentNewRow { rev_id: Some(rev_id.clone()), - is_live: edit_context.autoapprove, + is_live: edit_context.autoaccept, redirect_id: None, }) .collect::<Vec<Self::IdentNewRow>>(), |