refactor: Fix errors in api/client/report.rs

This commit is contained in:
Ginger
2026-04-28 09:16:52 -04:00
parent 64f7791ddb
commit b7c9ef89f0
+20 -28
View File
@@ -22,7 +22,7 @@ struct Report {
event_id: Option<OwnedEventId>, event_id: Option<OwnedEventId>,
user_id: Option<OwnedUserId>, user_id: Option<OwnedUserId>,
report_type: String, report_type: String,
reason: Option<String>, reason: String,
} }
/// # `POST /_matrix/client/v3/rooms/{roomId}/report` /// # `POST /_matrix/client/v3/rooms/{roomId}/report`
@@ -39,7 +39,7 @@ pub(crate) async fn report_room_route(
return Err!(Request(UserSuspended("You cannot perform this action while suspended."))); return Err!(Request(UserSuspended("You cannot perform this action while suspended.")));
} }
if body.reason.as_ref().is_some_and(|s| s.len() > 750) { if body.reason.len() > 750 {
return Err!(Request( return Err!(Request(
InvalidParam("Reason too long, should be 750 characters or fewer",) InvalidParam("Reason too long, should be 750 characters or fewer",)
)); ));
@@ -52,8 +52,7 @@ pub(crate) async fn report_room_route(
// their discretion. // their discretion.
info!( info!(
"Received room report by user {sender_user} for room {} with reason: \"{}\"", "Received room report by user {sender_user} for room {} with reason: \"{}\"",
body.room_id, body.room_id, body.reason
body.reason.as_deref().unwrap_or("")
); );
if !services if !services
@@ -78,7 +77,7 @@ pub(crate) async fn report_room_route(
services.admin.send_message(build_report(report)).await.ok(); services.admin.send_message(build_report(report)).await.ok();
Ok(report_room::v3::Response {}) Ok(report_room::v3::Response::new())
} }
/// # `POST /_matrix/client/v3/rooms/{roomId}/report/{eventId}` /// # `POST /_matrix/client/v3/rooms/{roomId}/report/{eventId}`
@@ -98,26 +97,22 @@ pub(crate) async fn report_event_route(
delay_response().await; delay_response().await;
let reason = body
.reason
.clone()
.unwrap_or_else(|| "<no reason provided>".to_owned());
// check if we know about the reported event ID or if it's invalid // check if we know about the reported event ID or if it's invalid
let Ok(pdu) = services.rooms.timeline.get_pdu(&body.event_id).await else { let Ok(pdu) = services.rooms.timeline.get_pdu(&body.event_id).await else {
return Err!(Request(NotFound("Event ID is not known to us or Event ID is invalid"))); return Err!(Request(NotFound("Event ID is not known to us or Event ID is invalid")));
}; };
is_event_report_valid( is_event_report_valid(&services, &pdu.event_id, &body.room_id, sender_user, &reason, &pdu)
&services,
&pdu.event_id,
&body.room_id,
sender_user,
body.reason.as_ref(),
&pdu,
)
.await?; .await?;
info!( info!(
"Received event report by user {sender_user} for room {} and event ID {}, with reason: \ "Received event report by user {sender_user} for room {} and event ID {}, with reason: \
\"{}\"", \"{}\"",
body.room_id, body.room_id, body.event_id, reason
body.event_id,
body.reason.as_deref().unwrap_or("")
); );
let report = Report { let report = Report {
sender: sender_user.to_owned(), sender: sender_user.to_owned(),
@@ -125,11 +120,11 @@ pub(crate) async fn report_event_route(
event_id: Some(body.event_id.clone()), event_id: Some(body.event_id.clone()),
user_id: None, user_id: None,
report_type: "event".to_owned(), report_type: "event".to_owned(),
reason: body.reason.clone(), reason,
}; };
services.admin.send_message(build_report(report)).await.ok(); services.admin.send_message(build_report(report)).await.ok();
Ok(report_content::v3::Response {}) Ok(report_content::v3::Response::new())
} }
#[tracing::instrument(skip_all, fields(%client), name = "report_user", level = "info")] #[tracing::instrument(skip_all, fields(%client), name = "report_user", level = "info")]
@@ -144,7 +139,7 @@ pub(crate) async fn report_user_route(
return Err!(Request(UserSuspended("You cannot perform this action while suspended."))); return Err!(Request(UserSuspended("You cannot perform this action while suspended.")));
} }
if body.reason.as_ref().is_some_and(|s| s.len() > 750) { if body.reason.len() > 750 {
return Err!(Request( return Err!(Request(
InvalidParam("Reason too long, should be 750 characters or fewer",) InvalidParam("Reason too long, should be 750 characters or fewer",)
)); ));
@@ -154,7 +149,7 @@ pub(crate) async fn report_user_route(
if !services.users.is_active_local(&body.user_id).await { if !services.users.is_active_local(&body.user_id).await {
// return 200 as to not reveal if the user exists. Recommended by spec. // return 200 as to not reveal if the user exists. Recommended by spec.
return Ok(report_user::v3::Response {}); return Ok(report_user::v3::Response::new());
} }
let report = Report { let report = Report {
@@ -168,13 +163,12 @@ pub(crate) async fn report_user_route(
info!( info!(
"Received room report from {sender_user} for user {} with reason: \"{}\"", "Received room report from {sender_user} for user {} with reason: \"{}\"",
body.user_id, body.user_id, body.reason
body.reason.as_deref().unwrap_or("")
); );
services.admin.send_message(build_report(report)).await.ok(); services.admin.send_message(build_report(report)).await.ok();
Ok(report_user::v3::Response {}) Ok(report_user::v3::Response::new())
} }
/// in the following order: /// in the following order:
@@ -188,7 +182,7 @@ async fn is_event_report_valid(
event_id: &EventId, event_id: &EventId,
room_id: &RoomId, room_id: &RoomId,
sender_user: &UserId, sender_user: &UserId,
reason: Option<&String>, reason: &str,
pdu: &PduEvent, pdu: &PduEvent,
) -> Result<()> { ) -> Result<()> {
debug_info!( debug_info!(
@@ -200,7 +194,7 @@ async fn is_event_report_valid(
return Err!(Request(NotFound("Event ID does not belong to the reported room",))); return Err!(Request(NotFound("Event ID does not belong to the reported room",)));
} }
if reason.as_ref().is_some_and(|s| s.len() > 750) { if reason.len() > 750 {
return Err!(Request( return Err!(Request(
InvalidParam("Reason too long, should be 750 characters or fewer",) InvalidParam("Reason too long, should be 750 characters or fewer",)
)); ));
@@ -232,9 +226,7 @@ fn build_report(report: Report) -> RoomMessageEventContent {
if report.event_id.is_some() { if report.event_id.is_some() {
let _ = writeln!(text, "- Reported Event ID: `{}`", report.event_id.unwrap()); let _ = writeln!(text, "- Reported Event ID: `{}`", report.event_id.unwrap());
} }
if let Some(reason) = report.reason { let _ = writeln!(text, "- Report Reason: {}", report.reason);
let _ = writeln!(text, "- Report Reason: {reason}");
}
RoomMessageEventContent::text_markdown(text).add_mentions(Mentions::with_room_mention()) RoomMessageEventContent::text_markdown(text).add_mentions(Mentions::with_room_mention())
} }