Compare commits

...

2 Commits

Author SHA1 Message Date
Anna 739b8819c7
refactor: update dependencies 2023-09-17 03:54:09 -04:00
Anna 5ef3a8c6b4
fix: ignore housing parameters if not in a housing zone 2023-09-17 03:22:57 -04:00
7 changed files with 853 additions and 521 deletions

1253
server/Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -17,8 +17,8 @@ rayon = "1"
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
serde_yaml = "0.9" serde_yaml = "0.9"
sha3 = "0.10" sha3 = "0.10"
sqlx = { version = "0.6", features = ["runtime-tokio-rustls", "sqlite", "chrono"] } sqlx = { version = "0.7", features = ["runtime-tokio-rustls", "sqlite", "chrono"] }
toml = "0.7" toml = "0.8"
tokio = { version = "1", features = ["rt-multi-thread", "macros"] } tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
tokio-stream = { version = "0.1", default-features = false, features = ["net"] } tokio-stream = { version = "0.1", default-features = false, features = ["net"] }
uuid = { version = "1", features = ["serde", "v4"] } uuid = { version = "1", features = ["serde", "v4"] }

View File

@ -0,0 +1,3 @@
[toolchain]
channel = 'nightly'
targets = ['x86_64-unknown-linux-musl']

View File

@ -1,4 +1,4 @@
#![feature(drain_filter)] #![feature(extract_if)]
use std::collections::HashMap; use std::collections::HashMap;
use std::net::SocketAddr; use std::net::SocketAddr;

View File

@ -53,40 +53,75 @@ async fn logic(state: Arc<State>, id: i64, location: u32, query: GetLocationQuer
}; };
let location = location as i64; let location = location as i64;
let mut messages = sqlx::query_as!( let mut messages = if housing {
RetrievedMessage, sqlx::query_as!(
// language=sqlite RetrievedMessage,
r#" // language=sqlite
select m.id, r#"
m.x, select m.id,
m.y, m.x,
m.z, m.y,
m.yaw, m.z,
m.message, m.yaw,
coalesce(sum(v.vote between 0 and 1), 0) as positive_votes, m.message,
coalesce(sum(v.vote between -1 and 0), 0) as negative_votes, coalesce(sum(v.vote between 0 and 1), 0) as positive_votes,
v2.vote as user_vote, coalesce(sum(v.vote between -1 and 0), 0) as negative_votes,
m.glyph, coalesce(v2.vote, 0) as user_vote,
m.created, m.glyph,
m.user, m.created,
cast((julianday(current_timestamp) - julianday(u.last_seen)) * 1440 as int) as last_seen_minutes m.user,
from messages m coalesce(cast((julianday(current_timestamp) - julianday(u.last_seen)) * 1440 as int), 0) as last_seen_minutes
left join votes v on m.id = v.message from messages m
left join votes v2 on m.id = v2.message and v2.user = ? left join votes v on m.id = v.message
inner join users u on m.user = u.id left join votes v2 on m.id = v2.message and v2.user = ?
where m.territory = ? and m.world is ? and m.ward is ? and m.plot is ? inner join users u on m.user = u.id
group by m.id"#, where m.territory = ? and m.world is ? and m.ward is ? and m.plot is ?
id, group by m.id"#,
location, id,
world, location,
query.ward, world,
query.plot, query.ward,
) query.plot,
.fetch_all(&state.db) )
.await .fetch_all(&state.db)
.context("could not get messages from database") .await
.map_err(AnyhowRejection) .context("could not get messages from database")
.map_err(warp::reject::custom)?; .map_err(AnyhowRejection)
.map_err(warp::reject::custom)?
} else {
sqlx::query_as!(
RetrievedMessage,
// language=sqlite
r#"
select m.id,
m.x,
m.y,
m.z,
m.yaw,
m.message,
coalesce(sum(v.vote between 0 and 1), 0) as positive_votes,
coalesce(sum(v.vote between -1 and 0), 0) as negative_votes,
coalesce(v2.vote, 0) as user_vote,
m.glyph,
m.created,
m.user,
coalesce(cast((julianday(current_timestamp) - julianday(u.last_seen)) * 1440 as int), 0) as last_seen_minutes
from messages m
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 = ?
group by m.id"#,
id,
location,
)
.fetch_all(&state.db)
.await
.context("could not get messages from database")
.map_err(AnyhowRejection)
.map_err(warp::reject::custom)?
};
filter_messages(&mut messages, id); filter_messages(&mut messages, id);
Ok(warp::reply::json(&messages)) Ok(warp::reply::json(&messages))
} }
@ -94,7 +129,7 @@ async fn logic(state: Arc<State>, id: i64, location: u32, query: GetLocationQuer
fn filter_messages(messages: &mut Vec<RetrievedMessage>, id: i64) { fn filter_messages(messages: &mut Vec<RetrievedMessage>, id: i64) {
// remove messages where the user has been offline for over 35 minutes // remove messages where the user has been offline for over 35 minutes
// also remove messages with low score (that aren't the from the user) // also remove messages with low score (that aren't the from the user)
messages.drain_filter(|msg| msg.last_seen_minutes >= 35 || (msg.user != id && (msg.positive_votes - msg.negative_votes) < crate::consts::VOTE_THRESHOLD_HIDE)); messages.retain(|msg| msg.last_seen_minutes < 35 && (msg.user == id || (msg.positive_votes - msg.negative_votes) >= crate::consts::VOTE_THRESHOLD_HIDE));
// shuffle messages since we'll be excluding later based on messages // shuffle messages since we'll be excluding later based on messages
// that have already been included, so this will be more fair // that have already been included, so this will be more fair
@ -176,5 +211,5 @@ fn filter_messages(messages: &mut Vec<RetrievedMessage>, id: i64) {
}); });
let ids = ids.into_inner(); let ids = ids.into_inner();
messages.drain_filter(|msg| !ids.contains(&msg.id)); messages.retain(|msg| ids.contains(&msg.id));
} }

View File

@ -5,7 +5,6 @@ use uuid::Uuid;
use warp::{Filter, Rejection, Reply}; use warp::{Filter, Rejection, Reply};
use warp::filters::BoxedFilter; use warp::filters::BoxedFilter;
use crate::message::RetrievedMessageTerritory;
use crate::State; use crate::State;
use crate::web::{AnyhowRejection, WebError}; use crate::web::{AnyhowRejection, WebError};
@ -37,7 +36,7 @@ async fn logic(state: Arc<State>, id: i64, message_id: Uuid) -> Result<impl Repl
m.message, m.message,
coalesce(sum(v.vote between 0 and 1), 0) as positive_votes, coalesce(sum(v.vote between 0 and 1), 0) as positive_votes,
coalesce(sum(v.vote between -1 and 0), 0) as negative_votes, coalesce(sum(v.vote between -1 and 0), 0) as negative_votes,
v2.vote as user_vote, coalesce(v2.vote, 0) as user_vote,
m.glyph, m.glyph,
m.created m.created
from messages m from messages m

View File

@ -41,7 +41,7 @@ async fn logic(state: Arc<State>, id: i64, extra: i64, mut query: HashMap<String
m.message, m.message,
coalesce(sum(v.vote between 0 and 1), 0) as positive_votes, coalesce(sum(v.vote between 0 and 1), 0) as positive_votes,
coalesce(sum(v.vote between -1 and 0), 0) as negative_votes, coalesce(sum(v.vote between -1 and 0), 0) as negative_votes,
v2.vote as user_vote, coalesce(v2.vote, 0) as user_vote,
m.glyph, m.glyph,
m.created, m.created,
0 as "is_hidden: bool" 0 as "is_hidden: bool"