This commit is contained in:
parent
8e37fceec6
commit
4f23083f44
|
@ -39,3 +39,15 @@ pub struct RetrievedMessage {
|
|||
pub positive_votes: i32,
|
||||
pub negative_votes: i32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct RetrievedMessageTerritory {
|
||||
pub id: String,
|
||||
puv territory: i64,
|
||||
pub x: f64,
|
||||
pub y: f64,
|
||||
pub z: f64,
|
||||
pub message: String,
|
||||
pub positive_votes: i32,
|
||||
pub negative_votes: i32,
|
||||
}
|
||||
|
|
|
@ -34,13 +34,16 @@ impl Pack {
|
|||
if let Some(conj_idx) = conjunction {
|
||||
if let Some(template_2_idx) = template_2_idx {
|
||||
let conj = self.conjunctions.get(conj_idx)?;
|
||||
if conj.len() > 1 || conj.chars().next().map(|x| !x.is_ascii_punctuation()).unwrap_or(false) {
|
||||
let is_punc = conj.len() == 1 && conj.chars().next().map(|x| x.is_ascii_punctuation()).unwrap_or(false);
|
||||
if is_punch {
|
||||
formatted.push_str(conj);
|
||||
formatted.push('\n');
|
||||
} else {
|
||||
formatted.push('\n');
|
||||
formatted.push_str(conj);
|
||||
formatted.push(' ');
|
||||
}
|
||||
|
||||
formatted.push_str(conj);
|
||||
formatted.push(' ');
|
||||
|
||||
let template_2 = self.templates.get(template_2_idx)?;
|
||||
if template_2.contains("{0}") && word_2_idx.is_none() {
|
||||
return None;
|
||||
|
|
|
@ -14,14 +14,16 @@ mod write;
|
|||
mod erase;
|
||||
mod get_location;
|
||||
mod vote;
|
||||
mod get_mine;
|
||||
|
||||
pub fn routes(state: Arc<State>) -> BoxedFilter<(impl Reply, )> {
|
||||
register::register(Arc::clone(&state))
|
||||
.or(unregister::unregister(Arc::clone(&state)))
|
||||
.or(write::write(Arc::clone(&state)))
|
||||
.or(erase::erase(Arc::clone(&state)))
|
||||
.or(get_location::get_location(Arc::clone(&state)))
|
||||
.or(vote::vote(Arc::clone(&state)))
|
||||
.or(get_location::get_location(Arc::clone(&state)))
|
||||
.or(get_mine::get_mine(Arc::clone(&state)))
|
||||
.recover(handle_rejection)
|
||||
.boxed()
|
||||
}
|
||||
|
@ -56,6 +58,7 @@ pub enum WebError {
|
|||
InvalidAuthToken,
|
||||
InvalidPackId,
|
||||
InvalidIndex,
|
||||
TooManyMessages,
|
||||
}
|
||||
|
||||
impl Reject for WebError {}
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use anyhow::Context;
|
||||
use warp::{Filter, Rejection, Reply};
|
||||
use warp::filters::BoxedFilter;
|
||||
|
||||
use crate::message::{RetrievedMessage, RetrievedMessageTerritory};
|
||||
use crate::State;
|
||||
use crate::web::AnyhowRejection;
|
||||
|
||||
pub fn get_mine(state: Arc<State>) -> BoxedFilter<(impl Reply, )> {
|
||||
warp::get()
|
||||
.and(warp::path("messages"))
|
||||
.and(warp::path::end())
|
||||
.and(super::get_id(Arc::clone(&state)))
|
||||
.and_then(move |id: i64| logic(Arc::clone(&state), id))
|
||||
.boxed()
|
||||
}
|
||||
|
||||
async fn logic(state: Arc<State>, id: i64) -> Result<impl Reply, Rejection> {
|
||||
let messages = sqlx::query_as!(
|
||||
RetrievedMessageTerritory,
|
||||
// language=sqlite
|
||||
r#"
|
||||
select m.id,
|
||||
m.territory,
|
||||
m.x,
|
||||
m.y,
|
||||
m.z,
|
||||
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
|
||||
from messages m
|
||||
left join votes v on m.id = v.message
|
||||
where m.user = ?
|
||||
group by m.id"#,
|
||||
id,
|
||||
)
|
||||
.fetch_all(&state.db)
|
||||
.await
|
||||
.context("could not get messages from database")
|
||||
.map_err(AnyhowRejection)
|
||||
.map_err(warp::reject::custom)?;
|
||||
Ok(warp::reply::json(&messages))
|
||||
}
|
|
@ -36,6 +36,21 @@ async fn logic(state: Arc<State>, id: i64, message: Message) -> Result<impl Repl
|
|||
.map_err(warp::reject::custom)?
|
||||
};
|
||||
|
||||
let existing = sqlx::query!(
|
||||
// language=sqlite
|
||||
"select count(*) as count from messages where user = ?",
|
||||
id
|
||||
)
|
||||
.fetch_one(&state.db)
|
||||
.await
|
||||
.context("could not get count of messages")
|
||||
.map_err(AnyhowRejection)
|
||||
.map_err(warp::reject::custom)?;
|
||||
|
||||
if existing.count >= 10 {
|
||||
return Err(warp::reject::custom(WebError::TooManyMessages));
|
||||
}
|
||||
|
||||
let message_id = Uuid::new_v4().simple().to_string();
|
||||
let territory = message.territory as i64;
|
||||
|
||||
|
|
Loading…
Reference in New Issue