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

This commit is contained in:
Ginger
2026-04-11 14:30:27 -04:00
parent 01f0c8ac1a
commit a1da8bbaa9
+21 -29
View File
@@ -22,7 +22,7 @@ struct Report {
event_id: Option<OwnedEventId>,
user_id: Option<OwnedUserId>,
report_type: String,
reason: Option<String>,
reason: String,
}
/// # `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.")));
}
if body.reason.as_ref().is_some_and(|s| s.len() > 750) {
if body.reason.len() > 750 {
return Err!(Request(
InvalidParam("Reason too long, should be 750 characters or fewer",)
));
@@ -52,8 +52,7 @@ pub(crate) async fn report_room_route(
// their discretion.
info!(
"Received room report by user {sender_user} for room {} with reason: \"{}\"",
body.room_id,
body.reason.as_deref().unwrap_or("")
body.room_id, body.reason
);
if !services
@@ -78,7 +77,7 @@ pub(crate) async fn report_room_route(
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}`
@@ -98,26 +97,22 @@ pub(crate) async fn report_event_route(
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
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")));
};
is_event_report_valid(
&services,
&pdu.event_id,
&body.room_id,
sender_user,
body.reason.as_ref(),
&pdu,
)
.await?;
is_event_report_valid(&services, &pdu.event_id, &body.room_id, sender_user, &reason, &pdu)
.await?;
info!(
"Received event report by user {sender_user} for room {} and event ID {}, with reason: \
\"{}\"",
body.room_id,
body.event_id,
body.reason.as_deref().unwrap_or("")
body.room_id, body.event_id, reason
);
let report = Report {
sender: sender_user.to_owned(),
@@ -125,11 +120,11 @@ pub(crate) async fn report_event_route(
event_id: Some(body.event_id.clone()),
user_id: None,
report_type: "event".to_owned(),
reason: body.reason.clone(),
reason,
};
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")]
@@ -144,7 +139,7 @@ pub(crate) async fn report_user_route(
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(
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 {
// 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 {
@@ -168,13 +163,12 @@ pub(crate) async fn report_user_route(
info!(
"Received room report from {sender_user} for user {} with reason: \"{}\"",
body.user_id,
body.reason.as_deref().unwrap_or("")
body.user_id, body.reason
);
services.admin.send_message(build_report(report)).await.ok();
Ok(report_user::v3::Response {})
Ok(report_user::v3::Response::new())
}
/// in the following order:
@@ -188,7 +182,7 @@ async fn is_event_report_valid(
event_id: &EventId,
room_id: &RoomId,
sender_user: &UserId,
reason: Option<&String>,
reason: &str,
pdu: &PduEvent,
) -> Result<()> {
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",)));
}
if reason.as_ref().is_some_and(|s| s.len() > 750) {
if reason.len() > 750 {
return Err!(Request(
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() {
let _ = writeln!(text, "- Reported Event ID: `{}`", report.event_id.unwrap());
}
if let Some(reason) = report.reason {
let _ = writeln!(text, "- Report Reason: {reason}");
}
let _ = writeln!(text, "- Report Reason: {}", report.reason);
RoomMessageEventContent::text_markdown(text).add_mentions(Mentions::with_room_mention())
}