fix: do not expect that all http requests are valid reqwest requests

Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
Matthias Ahouansou 2024-04-01 23:22:19 -04:00 committed by June
parent 93a43a0eda
commit c31fb7134a
6 changed files with 30 additions and 41 deletions

View file

@ -157,20 +157,18 @@ pub(crate) async fn get_alias_helper(room_alias: OwnedRoomAliasId) -> Result<get
None => { None => {
for appservice in services().appservice.read().await.values() { for appservice in services().appservice.read().await.values() {
if appservice.aliases.is_match(room_alias.as_str()) if appservice.aliases.is_match(room_alias.as_str())
&& if let Some(opt_result) = services() && matches!(
.sending services()
.send_appservice_request( .sending
appservice.registration.clone(), .send_appservice_request(
appservice::query::query_room_alias::v1::Request { appservice.registration.clone(),
room_alias: room_alias.clone(), appservice::query::query_room_alias::v1::Request {
}, room_alias: room_alias.clone(),
) },
.await )
{ .await,
opt_result.is_ok() Ok(Some(_opt_result))
} else { ) {
false
} {
room_id = Some( room_id = Some(
services() services()
.rooms .rooms

View file

@ -53,10 +53,7 @@ pub async fn get_presence_route(body: Ruma<get_presence::v3::Request>) -> Result
{ {
let room_id = room_id?; let room_id = room_id?;
if let Some(presence) = services() if let Some(presence) = services().presence.get_presence(&room_id, sender_user)? {
.presence
.get_presence(&room_id, sender_user)?
{
presence_event = Some(presence); presence_event = Some(presence);
break; break;
} }

View file

@ -528,10 +528,7 @@ async fn process_room_presence_updates(
presence_updates: &mut HashMap<OwnedUserId, PresenceEvent>, room_id: &RoomId, since: u64, presence_updates: &mut HashMap<OwnedUserId, PresenceEvent>, room_id: &RoomId, since: u64,
) -> Result<()> { ) -> Result<()> {
// Take presence updates from this room // Take presence updates from this room
for (user_id, _, presence_event) in services() for (user_id, _, presence_event) in services().presence.presence_since(room_id, since) {
.presence
.presence_since(room_id, since)
{
match presence_updates.entry(user_id) { match presence_updates.entry(user_id) {
Entry::Vacant(slot) => { Entry::Vacant(slot) => {
slot.insert(presence_event); slot.insert(presence_event);

View file

@ -143,9 +143,7 @@ fn process_presence_timer(user_id: &OwnedUserId) -> Result<()> {
let mut status_msg = None; let mut status_msg = None;
for room_id in services().rooms.state_cache.rooms_joined(user_id) { for room_id in services().rooms.state_cache.rooms_joined(user_id) {
let presence_event = services() let presence_event = services().presence.get_presence(&room_id?, user_id)?;
.presence
.get_presence(&room_id?, user_id)?;
if let Some(presence_event) = presence_event { if let Some(presence_event) = presence_event {
presence_state = presence_event.content.presence; presence_state = presence_event.content.presence;

View file

@ -10,11 +10,16 @@ use crate::{services, utils, Error, Result};
/// ///
/// Only returns None if there is no url specified in the appservice /// Only returns None if there is no url specified in the appservice
/// registration file /// registration file
pub(crate) async fn send_request<T>(registration: Registration, request: T) -> Option<Result<T::IncomingResponse>> pub(crate) async fn send_request<T>(registration: Registration, request: T) -> Result<Option<T::IncomingResponse>>
where where
T: OutgoingRequest + Debug, T: OutgoingRequest + Debug,
{ {
let destination = registration.url?; let destination = match registration.url {
Some(url) => url,
None => {
return Ok(None);
},
};
let hs_token = registration.hs_token.as_str(); let hs_token = registration.hs_token.as_str();
@ -42,8 +47,7 @@ where
); );
*http_request.uri_mut() = parts.try_into().expect("our manipulation is always valid"); *http_request.uri_mut() = parts.try_into().expect("our manipulation is always valid");
let mut reqwest_request = let mut reqwest_request = reqwest::Request::try_from(http_request)?;
reqwest::Request::try_from(http_request).expect("all http requests are valid reqwest requests");
*reqwest_request.timeout_mut() = Some(Duration::from_secs(120)); *reqwest_request.timeout_mut() = Some(Duration::from_secs(120));
@ -62,7 +66,7 @@ where
"Could not send request to appservice {} at {}: {}", "Could not send request to appservice {} at {}: {}",
registration.id, destination, e registration.id, destination, e
); );
return Some(Err(e.into())); return Err(e.into());
}, },
}; };
@ -99,8 +103,8 @@ where
.expect("reqwest body is valid http body"), .expect("reqwest body is valid http body"),
); );
Some(response.map_err(|_| { response.map(Some).map_err(|_| {
warn!("Appservice returned invalid response bytes {}\n{}", destination, url); warn!("Appservice returned invalid response bytes {}\n{}", destination, url);
Error::BadServerResponse("Server returned bad response.") Error::BadServerResponse("Server returned bad response.")
})) })
} }

View file

@ -250,7 +250,7 @@ impl Service {
/// registration file /// registration file
pub async fn send_appservice_request<T>( pub async fn send_appservice_request<T>(
&self, registration: Registration, request: T, &self, registration: Registration, request: T,
) -> Option<Result<T::IncomingResponse>> ) -> Result<Option<T::IncomingResponse>>
where where
T: OutgoingRequest + Debug, T: OutgoingRequest + Debug,
{ {
@ -487,10 +487,7 @@ pub fn select_edus_presence(
// Look for presence updates in this room // Look for presence updates in this room
let mut presence_updates = Vec::new(); let mut presence_updates = Vec::new();
for (user_id, count, presence_event) in services() for (user_id, count, presence_event) in services().presence.presence_since(room_id, since) {
.presence
.presence_since(room_id, since)
{
if count > *max_edu_count { if count > *max_edu_count {
*max_edu_count = count; *max_edu_count = count;
} }
@ -655,10 +652,8 @@ async fn handle_events_kind_appservice(
) )
.await .await
{ {
None => Ok(kind.clone()), Ok(_) => Ok(kind.clone()),
Some(op_resp) => op_resp Err(e) => Err((kind.clone(), e)),
.map(|_response| kind.clone())
.map_err(|e| (kind.clone(), e)),
}; };
drop(permit); drop(permit);