+ db-lib::Channel::get added and Returns a Result<Option<Channel>> 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
This commit is contained in:
parent
432e94054f
commit
9ab9cdb176
@ -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<Option<Channel>, 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<String>, 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<Response<Self>, SqlError> {
|
||||
//! @returns -> on success : Ok(Response::Set(Vec<Channel>))
|
||||
//! @throw -> on sql fail : Err(SqlError)
|
||||
|
@ -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<Response<UserMessage>, 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))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -145,6 +145,9 @@ pub async fn recent_messages(pool: &Pool, response: &mut Response<Body>, 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;
|
||||
|
Loading…
Reference in New Issue
Block a user