Add get room info endpoint

This commit is contained in:
Niels Andriesse 2021-03-25 13:56:26 +11:00
parent 31c7c7fb15
commit 42e2e0df41
4 changed files with 41 additions and 6 deletions

View File

@ -7,6 +7,7 @@ pub enum Error {
InvalidOnionRequest,
/// Usually this means the endpoint or HTTP method specified in the RPC call was malformed.
InvalidRpcCall,
NoSuchRoom,
Unauthorized,
ValidationFailed,
}
@ -16,7 +17,8 @@ impl warp::reject::Reject for Error {}
pub fn into_response(e: Rejection) -> Result<Response, Rejection> {
if let Some(error) = e.find::<Error>() {
match error {
Error::DecryptionFailed | Error::InvalidOnionRequest | Error::InvalidRpcCall | Error::ValidationFailed => return Ok(StatusCode::BAD_REQUEST.into_response()),
Error::DecryptionFailed | Error::InvalidOnionRequest | Error::InvalidRpcCall
| Error::NoSuchRoom | Error::ValidationFailed => return Ok(StatusCode::BAD_REQUEST.into_response()),
Error::Unauthorized => return Ok(StatusCode::FORBIDDEN.into_response()),
Error::DatabaseFailedInternally => {
return Ok(StatusCode::INTERNAL_SERVER_ERROR.into_response())

View File

@ -629,6 +629,36 @@ pub async fn get_banned_public_keys(
// General
pub async fn get_info(room_id: &str) -> Result<Response, Rejection> {
#[derive(Debug, Deserialize, Serialize)]
struct Info {
name: String,
image_id: Option<String>,
}
// Get a connection
let pool = &storage::MAIN_POOL;
let conn = pool.get().map_err(|_| Error::DatabaseFailedInternally)?;
// Get the room info if possible
let raw_query = format!("SELECT name, image_id FROM {} where id = (?1)", storage::MAIN_TABLE);
let info: Info = match conn.query_row(&raw_query, params![room_id], |row|
Ok(Info {
name: row.get(0)?,
image_id: row.get(1).ok(),
})
) {
Ok(info) => info,
Err(_) => return Err(warp::reject::custom(Error::NoSuchRoom)),
};
// Return
#[derive(Debug, Deserialize, Serialize)]
struct Response {
status_code: u16,
info: Info,
}
let response = Response { status_code: StatusCode::OK.as_u16(), info };
return Ok(warp::reply::json(&response).into_response());
}
pub async fn get_member_count(
auth_token: &str, pool: &storage::DatabaseConnectionPool,
) -> Result<Response, Rejection> {

View File

@ -50,7 +50,7 @@ pub async fn handle_rpc_call(rpc_call: RpcCall) -> Result<Response, Rejection> {
let auth_token = get_auth_token(&rpc_call);
// Switch on the HTTP method
match rpc_call.method.as_ref() {
"GET" => return handle_get_request(rpc_call, &path, auth_token, query_params, &pool).await,
"GET" => return handle_get_request(rpc_call, &path, &room_id, auth_token, query_params, &pool).await,
"POST" => return handle_post_request(rpc_call, &path, auth_token, &pool).await,
"DELETE" => return handle_delete_request(rpc_call, &path, auth_token, &pool).await,
_ => {
@ -61,11 +61,11 @@ pub async fn handle_rpc_call(rpc_call: RpcCall) -> Result<Response, Rejection> {
}
async fn handle_get_request(
rpc_call: RpcCall, path: &str, auth_token: Option<String>,
rpc_call: RpcCall, path: &str, room_id: &str, auth_token: Option<String>,
query_params: HashMap<String, String>, pool: &storage::DatabaseConnectionPool,
) -> Result<Response, Rejection> {
// Getting an auth token challenge doesn't require authorization, so we
// handle it first
// Getting an auth token challenge or requesting room info don't require authorization, so
// we handle them first
if path == "auth_token_challenge" {
let challenge = handlers::get_auth_token_challenge(query_params, pool).await?;
#[derive(Debug, Deserialize, Serialize)]
@ -75,6 +75,8 @@ async fn handle_get_request(
}
let response = Response { status_code: StatusCode::OK.as_u16(), challenge };
return Ok(warp::reply::json(&response).into_response());
} else if path == "info" {
return handlers::get_info(&room_id).await;
}
// Check that the auth token is present
let auth_token = auth_token.ok_or(warp::reject::custom(Error::Unauthorized))?;

View File

@ -34,7 +34,8 @@ fn create_main_tables_if_needed(conn: &DatabaseConnection) {
let main_table_cmd = format!(
"CREATE TABLE IF NOT EXISTS {} (
id TEXT PRIMARY KEY,
name TEXT
name TEXT,
image_id TEXT
)",
MAIN_TABLE
);