Fix compact polling auth token handling

This commit is contained in:
Niels Andriesse 2021-04-22 15:41:29 +10:00
parent 2e99af78e1
commit dbe6412b14
3 changed files with 63 additions and 13 deletions

View file

@ -17,18 +17,20 @@ pub enum Error {
impl warp::reject::Reject for Error {}
#[rustfmt::skip]
pub fn into_response(e: Rejection) -> Result<Response, Rejection> {
pub fn status_code(e: Rejection) -> StatusCode {
if let Some(error) = e.find::<Error>() {
match error {
Error::DecryptionFailed | Error::InvalidOnionRequest | Error::InvalidRpcCall
| Error::NoSuchRoom | Error::ValidationFailed => return Ok(StatusCode::BAD_REQUEST.into_response()),
Error::NoAuthToken => return Ok(StatusCode::UNAUTHORIZED.into_response()),
Error::Unauthorized => return Ok(StatusCode::FORBIDDEN.into_response()),
Error::DatabaseFailedInternally => {
return Ok(StatusCode::INTERNAL_SERVER_ERROR.into_response())
}
| Error::NoSuchRoom | Error::ValidationFailed => return StatusCode::BAD_REQUEST,
Error::NoAuthToken => return StatusCode::UNAUTHORIZED,
Error::Unauthorized => return StatusCode::FORBIDDEN,
Error::DatabaseFailedInternally => return StatusCode::INTERNAL_SERVER_ERROR
};
} else {
return Ok(StatusCode::INTERNAL_SERVER_ERROR.into_response());
return StatusCode::INTERNAL_SERVER_ERROR;
}
}
pub fn into_response(e: Rejection) -> Result<Response, Rejection> {
return Ok(status_code(e).into_response());
}

View file

@ -804,19 +804,66 @@ pub fn compact_poll(
get_messages_query_params
.insert("from_server_id".to_string(), from_message_server_id.to_string());
}
let messages = get_messages(get_messages_query_params, &auth_token, &pool)?;
let messages = match get_messages(get_messages_query_params, &auth_token, &pool) {
Ok(messages) => messages,
Err(e) => {
let status_code = super::errors::status_code(e);
let response_body = models::CompactPollResponseBody {
room_id,
status_code: status_code.as_u16(),
messages: vec![],
deletions: vec![],
moderators: vec![],
};
response_bodies.push(response_body);
continue;
}
};
// Get the new deletions
let mut get_deletions_query_params: HashMap<String, String> = HashMap::new();
if let Some(from_deletion_server_id) = from_deletion_server_id {
get_deletions_query_params
.insert("from_server_id".to_string(), from_deletion_server_id.to_string());
}
let deletions = get_deleted_messages(get_deletions_query_params, &auth_token, &pool)?;
let deletions = match get_deleted_messages(get_deletions_query_params, &auth_token, &pool) {
Ok(deletions) => deletions,
Err(e) => {
let status_code = super::errors::status_code(e);
let response_body = models::CompactPollResponseBody {
room_id,
status_code: status_code.as_u16(),
messages: vec![],
deletions: vec![],
moderators: vec![],
};
response_bodies.push(response_body);
continue;
}
};
// Get the moderators
let moderators = get_moderators(&auth_token, &pool)?;
let moderators = match get_moderators(&auth_token, &pool) {
Ok(moderators) => moderators,
Err(e) => {
let status_code = super::errors::status_code(e);
let response_body = models::CompactPollResponseBody {
room_id,
status_code: status_code.as_u16(),
messages: vec![],
deletions: vec![],
moderators: vec![],
};
response_bodies.push(response_body);
continue;
}
};
// Add to the response
let response_body =
models::CompactPollResponseBody { room_id, deletions, messages, moderators };
let response_body = models::CompactPollResponseBody {
room_id,
status_code: StatusCode::OK.as_u16(),
deletions,
messages,
moderators,
};
response_bodies.push(response_body);
}
// Return

View file

@ -38,6 +38,7 @@ pub struct CompactPollRequestBody {
#[derive(Debug, Deserialize, Serialize)]
pub struct CompactPollResponseBody {
pub room_id: String,
pub status_code: u16,
pub deletions: Vec<i64>,
pub messages: Vec<Message>,
pub moderators: Vec<String>,