! First pass base implementation for /messages/recent
This api method is made for clients to easily say "give me the n[1,100] latest messages in some channel This commit contains code that is largely untested but contains some base code such that I can correct issues in the next _tested_ commit
This commit is contained in:
@@ -203,5 +203,29 @@ impl Message {
|
||||
|
||||
Ok(Response::Set(messages))
|
||||
}
|
||||
|
||||
/// On success Response::Set<UserMessage>
|
||||
/// On user failure Response::RestrictedInput<String>
|
||||
/// On error Err<SqlError>
|
||||
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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user