From 581b4806ed6f14e5f6a2ae2c91336e5ba24c6752 Mon Sep 17 00:00:00 2001 From: Anna Date: Sun, 19 Feb 2023 23:50:25 -0500 Subject: [PATCH] fix(server): account for world --- .../migrations/12_even_more_housing_changes.sql | 3 +++ server/src/message.rs | 4 ++++ server/src/web.rs | 4 ++-- server/src/web/get_location.rs | 15 ++++++++++++--- server/src/web/get_message.rs | 1 + server/src/web/get_mine.rs | 1 + server/src/web/write.rs | 7 ++++--- 7 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 server/migrations/12_even_more_housing_changes.sql diff --git a/server/migrations/12_even_more_housing_changes.sql b/server/migrations/12_even_more_housing_changes.sql new file mode 100644 index 0000000..e25a78b --- /dev/null +++ b/server/migrations/12_even_more_housing_changes.sql @@ -0,0 +1,3 @@ +alter table messages + add column world int; +create index messages_world_idx on messages (world); diff --git a/server/src/message.rs b/server/src/message.rs index d73e9a0..0ccb764 100644 --- a/server/src/message.rs +++ b/server/src/message.rs @@ -23,6 +23,8 @@ pub struct Message { #[serde(default = "glyph_default")] pub glyph: i8, + #[serde(default)] + pub world: Option, #[serde(default)] pub ward: Option, #[serde(default)] @@ -57,6 +59,7 @@ pub struct RetrievedMessage { pub struct RetrievedMessageTerritory { pub id: String, pub territory: i64, + pub world: Option, pub ward: Option, pub plot: Option, pub x: f64, @@ -76,6 +79,7 @@ pub struct RetrievedMessageTerritory { pub struct OwnMessage { pub id: String, pub territory: i64, + pub world: Option, pub ward: Option, pub plot: Option, pub x: f64, diff --git a/server/src/web.rs b/server/src/web.rs index c300ea8..39b47ce 100644 --- a/server/src/web.rs +++ b/server/src/web.rs @@ -73,7 +73,7 @@ pub enum WebError { TooManyMessages, NoSuchMessage, InvalidExtraCode, - MissingWard, + MissingHousingInfo, UnnecessaryHousingInfo, } @@ -94,7 +94,7 @@ async fn handle_rejection(err: Rejection) -> Result { WebError::TooManyMessages => (StatusCode::BAD_REQUEST, "too_many_messages", "you have run out of messages - delete one and try again".into()), WebError::NoSuchMessage => (StatusCode::NOT_FOUND, "no_such_message", "no message with that id was found".into()), WebError::InvalidExtraCode => (StatusCode::BAD_REQUEST, "invalid_extra_code", "that extra code was not found".into()), - WebError::MissingWard => (StatusCode::BAD_REQUEST, "missing_ward", "a ward was not provided - try updating the plugin".into()), + WebError::MissingHousingInfo => (StatusCode::BAD_REQUEST, "missing_housing_info", "housing info was not provided - try updating the plugin".into()), WebError::UnnecessaryHousingInfo => (StatusCode::BAD_REQUEST, "unnecessary_housing_info", "a ward/plot was provided but not necessary - try updating the plugin".into()), } } else if err.is_not_found() { diff --git a/server/src/web/get_location.rs b/server/src/web/get_location.rs index b0611db..97b0f7f 100644 --- a/server/src/web/get_location.rs +++ b/server/src/web/get_location.rs @@ -26,6 +26,8 @@ pub fn get_location(state: Arc) -> BoxedFilter<(impl Reply, )> { #[derive(Deserialize)] pub struct GetLocationQuery { + #[serde(default)] + world: Option, #[serde(default)] ward: Option, #[serde(default)] @@ -34,14 +36,20 @@ pub struct GetLocationQuery { async fn logic(state: Arc, id: i64, location: u32, query: GetLocationQuery) -> Result { let housing = HOUSING_ZONES.contains(&location); - if housing && query.ward.is_none() { - return Err(warp::reject::custom(WebError::MissingWard)); + if housing && (query.world.is_none() || query.ward.is_none()) { + return Err(warp::reject::custom(WebError::MissingHousingInfo)); } if !housing && (query.ward.is_some() || query.plot.is_some()) { return Err(warp::reject::custom(WebError::UnnecessaryHousingInfo)); } + let world = if housing { + query.world + } else { + None + }; + let location = location as i64; let mut messages = sqlx::query_as!( RetrievedMessage, @@ -64,10 +72,11 @@ async fn logic(state: Arc, id: i64, location: u32, query: GetLocationQuer left join votes v on m.id = v.message left join votes v2 on m.id = v2.message and v2.user = ? inner join users u on m.user = u.id - where m.territory = ? and m.ward is ? and m.plot is ? + where m.territory = ? and m.world is ? and m.ward is ? and m.plot is ? group by m.id"#, id, location, + world, query.ward, query.plot, ) diff --git a/server/src/web/get_message.rs b/server/src/web/get_message.rs index b934a64..89208fd 100644 --- a/server/src/web/get_message.rs +++ b/server/src/web/get_message.rs @@ -27,6 +27,7 @@ async fn logic(state: Arc, id: i64, message_id: Uuid) -> Result, id: i64, extra: i64, mut query: HashMap) -> BoxedFilter<(impl Reply, )> { async fn logic(state: Arc, id: i64, extra: i64, message: Message) -> Result { let housing = HOUSING_ZONES.contains(&message.territory); - if housing && message.ward.is_none() { - return Err(warp::reject::custom(WebError::MissingWard)); + if housing && (message.world.is_none() || message.ward.is_none()) { + return Err(warp::reject::custom(WebError::MissingHousingInfo)); } if !housing && (message.ward.is_some() || message.plot.is_some()) { @@ -67,10 +67,11 @@ async fn logic(state: Arc, id: i64, extra: i64, message: Message) -> Resu sqlx::query!( // language=sqlite - "insert into messages (id, user, territory, ward, plot, x, y, z, yaw, message, glyph) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", + "insert into messages (id, user, territory, world, ward, plot, x, y, z, yaw, message, glyph) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", message_id, id, territory, + message.world, message.ward, message.plot, message.x,