From 9ab9cdb176b8b4a191c65d58d60d89c99a8cde8f Mon Sep 17 00:00:00 2001 From: shockrah Date: Wed, 21 Apr 2021 21:33:30 -0700 Subject: [PATCH] + db-lib::Channel::get added and Returns a Result> for simplicity This is really only for check if a particular channel exists for /messages/recent's backend * Flattening db-lib::Message::last_n SHould make code more readable as it now has a more linear flow * api::Messages::recent_messages now 404's when a channel that doesn't exist is request --- json-api/db/src/channels.rs | 15 +++++++++++++++ json-api/db/src/messages.rs | 37 +++++++++++++++++++++---------------- json-api/src/messages.rs | 3 +++ 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/json-api/db/src/channels.rs b/json-api/db/src/channels.rs index d6b0116..46adf69 100644 --- a/json-api/db/src/channels.rs +++ b/json-api/db/src/channels.rs @@ -14,6 +14,21 @@ pub const MAX_NAME_LEN: usize = 256; pub const MAX_DESCRIPTION_LEN: usize = 2048; impl Channel { + pub async fn get(p: &Pool, id: u64) -> Result, SqlError> { + let mut conn = p.get_conn().await?; + let q = "SELECT name, description, kind FROM channels WHERE id = :id"; + let params = params!{"id" => id}; + let row: Option<(String, Option, i32)> = + conn.exec_first(q, params).await?; + + if let Some(row) = row { + let chan = Channel { id, name: row.0, description: row.1, kind: row.2 }; + Ok(Some(chan)) + } else { + Ok(None) + } + } + pub async fn filter(p: &Pool, kind: Integer) -> Result, SqlError> { //! @returns -> on success : Ok(Response::Set(Vec)) //! @throw -> on sql fail : Err(SqlError) diff --git a/json-api/db/src/messages.rs b/json-api/db/src/messages.rs index 1e9c7d7..6c3f327 100644 --- a/json-api/db/src/messages.rs +++ b/json-api/db/src/messages.rs @@ -10,7 +10,7 @@ use mysql_async::Error as SqlError; use crate::Response; use crate::{UBigInt, BigInt}; -use crate::{Message, UserMessage}; +use crate::{Channel, Message, UserMessage}; use crate::common; use rand::RngCore; @@ -218,22 +218,27 @@ impl Message { pub async fn last_n(p: &Pool, limit: i32, channel_id: u64) -> Result, SqlError> { if limit <= 0 || limit > 100 { return Ok(Response::RestrictedInput("Invalid \"limit\" value".into())) - } else { - // Reminder: if content_type is not text/plain then content will be empty - let q = " SELECT mem.name, msg.id, msg.time, msg.content, msg.content_type, msg.author_id - FROM messages as msg - JOIN members as mem ON mem.id = msg.author_id - WHERE channel_id = :channel - ORDER BY id DESC LIMIT :limit"; - let params = params!{"limit" => limit, "channel" => channel_id}; - let mut conn = p.get_conn().await?; - let messages = conn.exec_map(q, params, |(name, id, time, content, content_type, author_id)| { - UserMessage { - name, id, time, content, content_type, author_id, channel_id - } - }).await?; - return Ok(Response::Set(messages)) + } + + // Explicitly return empty if the channel requested does not exist + if let None = Channel::get(p, channel_id).await? { + return Ok(Response::Empty) } + + // Reminder: if content_type is not text/plain then content will be empty + let q = " SELECT mem.name, msg.id, msg.time, msg.content, msg.content_type, msg.author_id + FROM messages as msg + JOIN members as mem ON mem.id = msg.author_id + WHERE channel_id = :channel + ORDER BY id DESC LIMIT :limit"; + let params = params!{"limit" => limit, "channel" => channel_id}; + let mut conn = p.get_conn().await?; + let messages = conn.exec_map(q, params, |(name, id, time, content, content_type, author_id)| { + UserMessage { + name, id, time, content, content_type, author_id, channel_id + } + }).await?; + return Ok(Response::Set(messages)) } } diff --git a/json-api/src/messages.rs b/json-api/src/messages.rs index 1e3aa15..2eef93c 100644 --- a/json-api/src/messages.rs +++ b/json-api/src/messages.rs @@ -145,6 +145,9 @@ pub async fn recent_messages(pool: &Pool, response: &mut Response, params: // 200 db::Response::Set(messages) => set_json_body(response, json!({"messages": messages})), + // 404 - assuming the request channel does not exist + db::Response::Empty => + *response.status_mut() = StatusCode::NOT_FOUND, // 400 db::Response::RestrictedInput(msg) => { *response.status_mut() = StatusCode::BAD_REQUEST;