diff options
author | Bryan Newbold <bnewbold@robocracy.org> | 2019-01-11 13:12:55 -0800 |
---|---|---|
committer | Bryan Newbold <bnewbold@robocracy.org> | 2019-01-11 13:12:55 -0800 |
commit | 5b6a9ee3fa9b781c67074b9815cdaef84548692d (patch) | |
tree | 4626d06f7e48c4482b7700da16e88578b37f7b9e /rust | |
parent | 9bb1b10d10cd2b1863c9e5f136621a845b0cb3c1 (diff) | |
download | fatcat-5b6a9ee3fa9b781c67074b9815cdaef84548692d.tar.gz fatcat-5b6a9ee3fa9b781c67074b9815cdaef84548692d.zip |
more progress on editing changes
Diffstat (limited to 'rust')
-rw-r--r-- | rust/src/editing_crud.rs | 38 | ||||
-rw-r--r-- | rust/src/endpoint_handlers.rs | 46 | ||||
-rw-r--r-- | rust/src/endpoints.rs | 68 |
3 files changed, 76 insertions, 76 deletions
diff --git a/rust/src/editing_crud.rs b/rust/src/editing_crud.rs index c409e368..86cc2fb3 100644 --- a/rust/src/editing_crud.rs +++ b/rust/src/editing_crud.rs @@ -5,7 +5,6 @@ use crate::errors::*; use crate::identifiers::{self, FatcatId}; use crate::server::DbConn; use diesel::prelude::*; -use diesel::{self, insert_into}; use fatcat_api_spec::models::*; use std::str::FromStr; use uuid::Uuid; @@ -70,14 +69,14 @@ pub trait EditgroupCrud { conn: &DbConn, editor_id: FatcatId, limit: u64, - since: Option<()>, - before: Option<()>, + since: Option<chrono::DateTime<chrono::Utc>>, + before: Option<chrono::DateTime<chrono::Utc>>, ) -> Result<Vec<EditgroupRow>>; fn db_get_range_reviewable( conn: &DbConn, limit: u64, - since: Option<()>, - before: Option<()>, + since: Option<chrono::DateTime<chrono::Utc>>, + before: Option<chrono::DateTime<chrono::Utc>>, ) -> Result<Vec<EditgroupRow>>; fn db_create(&self, conn: &DbConn, autoaccept: bool) -> Result<EditgroupRow>; fn db_update( @@ -121,8 +120,8 @@ impl EditgroupCrud for Editgroup { conn: &DbConn, editor_id: FatcatId, limit: u64, - since: Option<()>, - before: Option<()>, + since: Option<chrono::DateTime<chrono::Utc>>, + before: Option<chrono::DateTime<chrono::Utc>>, ) -> Result<Vec<EditgroupRow>> { // TODO: since/before let rows: Vec<EditgroupRow> = match (since, before) { @@ -141,8 +140,8 @@ impl EditgroupCrud for Editgroup { fn db_get_range_reviewable( conn: &DbConn, limit: u64, - since: Option<()>, - before: Option<()>, + since: Option<chrono::DateTime<chrono::Utc>>, + before: Option<chrono::DateTime<chrono::Utc>>, ) -> Result<Vec<EditgroupRow>> { // TODO: since/before let rows: Vec<EditgroupRow> = match (since, before) { @@ -218,19 +217,20 @@ impl EditgroupCrud for Editgroup { pub trait EditgroupAnnotationCrud { fn db_get(conn: &DbConn, annotation_id: Uuid) -> Result<EditgroupAnnotationRow>; + fn db_expand(&mut self, conn: &DbConn, expand: ExpandFlags) -> Result<()>; fn db_get_range_for_editor( conn: &DbConn, editor_id: FatcatId, limit: u64, - since: Option<()>, - before: Option<()>, + since: Option<chrono::DateTime<chrono::Utc>>, + before: Option<chrono::DateTime<chrono::Utc>>, ) -> Result<Vec<EditgroupAnnotationRow>>; fn db_get_range_for_editgroup( conn: &DbConn, editgroup_id: FatcatId, limit: u64, - since: Option<()>, - before: Option<()>, + since: Option<chrono::DateTime<chrono::Utc>>, + before: Option<chrono::DateTime<chrono::Utc>>, ) -> Result<Vec<EditgroupAnnotationRow>>; fn db_create(&self, conn: &DbConn) -> Result<EditgroupAnnotationRow>; } @@ -243,12 +243,16 @@ impl EditgroupAnnotationCrud for EditgroupAnnotation { Ok(row) } + fn db_expand(&mut self, conn: &DbConn, expand: ExpandFlags) -> Result<()> { + unimplemented!() + } + fn db_get_range_for_editor( conn: &DbConn, editor_id: FatcatId, limit: u64, - since: Option<()>, - before: Option<()>, + since: Option<chrono::DateTime<chrono::Utc>>, + before: Option<chrono::DateTime<chrono::Utc>>, ) -> Result<Vec<EditgroupAnnotationRow>> { // TODO: since/before let rows: Vec<EditgroupAnnotationRow> = match (since, before) { @@ -265,8 +269,8 @@ impl EditgroupAnnotationCrud for EditgroupAnnotation { conn: &DbConn, editgroup_id: FatcatId, limit: u64, - since: Option<()>, - before: Option<()>, + since: Option<chrono::DateTime<chrono::Utc>>, + before: Option<chrono::DateTime<chrono::Utc>>, ) -> Result<Vec<EditgroupAnnotationRow>> { // TODO: since/before let rows: Vec<EditgroupAnnotationRow> = match (since, before) { diff --git a/rust/src/endpoint_handlers.rs b/rust/src/endpoint_handlers.rs index 79da2dbe..4b3108af 100644 --- a/rust/src/endpoint_handlers.rs +++ b/rust/src/endpoint_handlers.rs @@ -9,15 +9,13 @@ use crate::database_models::*; use crate::database_schema::*; use crate::editing::*; use crate::entity_crud::{EntityCrud, ExpandFlags, HideFlags}; +use crate::editing_crud::{EditorCrud, EditgroupCrud}; use crate::errors::*; use crate::identifiers::*; use crate::server::*; -use chrono; use diesel::prelude::*; -use diesel::{self, insert_into}; use fatcat_api_spec::models; use fatcat_api_spec::models::*; -use std::str::FromStr; macro_rules! entity_batch_handler { ($post_batch_handler:ident, $model:ident) => { @@ -385,26 +383,10 @@ impl Server { pub fn create_editgroup_handler( &self, conn: &DbConn, - entity: models::Editgroup, + editgroup: models::Editgroup, ) -> Result<Editgroup> { - unimplemented!() - /* - let row: EditgroupRow = insert_into(editgroup::table) - .values(( - editgroup::editor_id.eq(FatcatId::from_str(&entity.editor_id.unwrap())?.to_uuid()), - editgroup::description.eq(entity.description), - editgroup::extra_json.eq(entity.extra), - )) - .get_result(conn)?; - - Ok(Editgroup { - editgroup_id: Some(uuid2fcid(&row.id)), - editor_id: Some(uuid2fcid(&row.editor_id)), - description: row.description, - edits: None, - extra: row.extra_json, - }) - */ + let row = editgroup.db_create(conn, false)?; + Ok(row.into_model_partial()) } pub fn get_editgroup_handler( @@ -412,7 +394,8 @@ impl Server { conn: &DbConn, editgroup_id: FatcatId, ) -> Result<Editgroup> { - let row: EditgroupRow = editgroup::table.find(editgroup_id.to_uuid()).first(conn)?; + let row: EditgroupRow = Editgroup::db_get(conn, editgroup_id)?; + let mut editgroup = row.into_model_partial(); let edits = EditgroupEdits { containers: Some( @@ -473,22 +456,12 @@ impl Server { ), }; - unimplemented!(); - // XXX: - /* - let eg = Editgroup { - editgroup_id: Some(uuid2fcid(&row.id)), - editor_id: Some(uuid2fcid(&row.editor_id)), - description: row.description, - edits: Some(edits), - extra: row.extra_json, - }; - Ok(eg) - */ + editgroup.edits = Some(edits); + Ok(editgroup) } pub fn get_editor_handler(&self, conn: &DbConn, editor_id: FatcatId) -> Result<Editor> { - let row: EditorRow = editor::table.find(editor_id.to_uuid()).first(conn)?; + let row: EditorRow = Editor::db_get(conn, editor_id)?; Ok(row.into_model()) } @@ -497,6 +470,7 @@ impl Server { conn: &DbConn, editor_id: FatcatId, ) -> Result<Vec<ChangelogEntry>> { + // XXX: delete me? // TODO: single query let editor: EditorRow = editor::table.find(editor_id.to_uuid()).first(conn)?; let changes: Vec<(ChangelogRow, EditgroupRow)> = changelog::table diff --git a/rust/src/endpoints.rs b/rust/src/endpoints.rs index f6d73bbb..5e7ff234 100644 --- a/rust/src/endpoints.rs +++ b/rust/src/endpoints.rs @@ -11,6 +11,7 @@ use crate::auth::FatcatRole; use crate::database_models::EntityEditRow; use crate::editing::*; use crate::entity_crud::{EntityCrud, ExpandFlags, HideFlags}; +use crate::editing_crud::{EditorCrud, EditgroupCrud, EditgroupAnnotationCrud}; use crate::errors::*; use crate::identifiers::FatcatId; use crate::server::*; @@ -23,6 +24,7 @@ use futures::{self, Future}; use sentry::integrations::failure::capture_fail; use std::str::FromStr; use uuid::{self, Uuid}; +use std::cmp; // This makes response matching below *much* more terse use crate::errors::FatcatError::*; @@ -744,7 +746,9 @@ impl Api for Server { let ret = match conn .transaction(|| { let editor_id = FatcatId::from_str(&editor_id)?; - unimplemented!() + let limit = cmp::min(100, limit.unwrap_or(20)) as u64; + let row = Editgroup::db_get_range_for_editor(&conn, editor_id, limit, since, before)?; + Ok(row.into_iter().map(|eg| eg.into_model_partial()).collect()) }) .map_err(|e: Error| FatcatError::from(e)) { @@ -766,11 +770,13 @@ impl Api for Server { let ret = match conn .transaction(|| { let editor_id = FatcatId::from_str(&editor_id)?; - unimplemented!() + let limit = cmp::min(100, limit.unwrap_or(20)) as u64; + let annotations = EditgroupAnnotation::db_get_range_for_editor(&conn, editor_id, limit, since, before)?; + Ok(annotations.into_iter().map(|a| a.into_model()).collect()) }) .map_err(|e: Error| FatcatError::from(e)) { - Ok(editgroups) => GetEditorAnnotationsResponse::Success(editgroups), + Ok(annotations) => GetEditorAnnotationsResponse::Success(annotations), Err(fe) => generic_err_responses!(fe, GetEditorAnnotationsResponse), }; Box::new(futures::done(Ok(ret))) @@ -838,8 +844,17 @@ impl Api for Server { let ret = match conn .transaction(|| { let editgroup_id = FatcatId::from_str(&editgroup_id)?; - unimplemented!() - // TODO: let expand_flags = ExpandFlags::from_str(¶m)?; + let limit: u64 = 1000; + // TODO: controllable expansion... for now always expands editors + let annotations = EditgroupAnnotation::db_get_range_for_editgroup(&conn, editgroup_id, limit, None, None)?; + let mut annotations: Vec<EditgroupAnnotation> = annotations.into_iter().map(|a| a.into_model()).collect(); + if let Some(expand) = expand { + let expand = ExpandFlags::from_str(&expand)?; + for a in annotations.iter_mut() { + a.db_expand(&conn, expand)?; + }; + }; + Ok(annotations) }) .map_err(|e: Error| FatcatError::from(e)) { @@ -859,8 +874,18 @@ impl Api for Server { ) -> Box<Future<Item = GetEditgroupsReviewableResponse, Error = ApiError> + Send> { let conn = self.db_pool.get().expect("db_pool error"); let ret = match conn - .transaction(|| unimplemented!()) - .map_err(|e: Error| FatcatError::from(e)) + .transaction(|| { + let limit = cmp::min(100, limit.unwrap_or(20)) as u64; + let row = Editgroup::db_get_range_reviewable(&conn, limit, since, before)?; + let mut editgroups: Vec<Editgroup> = row.into_iter().map(|eg| eg.into_model_partial()).collect(); + if let Some(expand) = expand { + let expand = ExpandFlags::from_str(&expand)?; + for eg in editgroups.iter_mut() { + eg.db_expand(&conn, expand)?; + }; + }; + Ok(editgroups) + }).map_err(|e: Error| FatcatError::from(e)) { Ok(editgroups) => GetEditgroupsReviewableResponse::Found(editgroups), Err(fe) => generic_err_responses!(fe, GetEditgroupsReviewableResponse), @@ -916,6 +941,8 @@ impl Api for Server { Box::new(futures::done(Ok(ret))) } + /// Note: this currently won't return the full (hydrated) editgroup including all edits, just + /// the partial model. fn update_editgroup( &self, editgroup_id: String, @@ -938,20 +965,15 @@ impl Api for Server { &context.auth_data, Some("update_editgroup"), )?; - unimplemented!(); // submit - // XXX let existing = Editgroup::db_get(&conn, editgroup_id)?; - /* - if existing.editor_id == auth_context.editor_id.to_uuid() { - // self edit of editgroup allowed - auth_context.require_role(FatcatRole::Editor)?; - } else { - // admin can update any editgroup - auth_context.require_role(FatcatRole::Admin)?; - }; - editgroup - .db_update(&conn, editgroup_id) - .map(|e| e.into_model()) - */ + let existing = Editgroup::db_get(&conn, editgroup_id)?; + if existing.editor_id == auth_context.editor_id.to_uuid() { + // self edit of editgroup allowed + auth_context.require_role(FatcatRole::Editor)?; + } else { + // admin can update any editgroup + auth_context.require_role(FatcatRole::Admin)?; + }; + editgroup.db_update(&conn, editgroup_id, submit).map(|eg| eg.into_model_partial()) }) .map_err(|e: Error| FatcatError::from(e)) { @@ -991,8 +1013,8 @@ impl Api for Server { annotation.editor_id = Some(auth_context.editor_id.to_string()); } }; - unimplemented!(); - // TODO: self.create_editgroup_annotation_handler(&conn, annotation) + // TODO: verify editgroup_id + annotation.db_create(&conn).map(|a| a.into_model()) }) .map_err(|e: Error| FatcatError::from(e)) { |