2022-01-22 16:58:32 +01:00
|
|
|
use crate::{database::DatabaseGuard, utils::HtmlEscape, Error, Result, Ruma};
|
2021-10-18 06:51:11 +02:00
|
|
|
use ruma::{
|
2022-02-18 15:33:14 +01:00
|
|
|
api::client::{error::ErrorKind, room::report_content},
|
2021-10-18 06:51:11 +02:00
|
|
|
events::room::message,
|
2021-11-27 16:04:19 +01:00
|
|
|
int,
|
2021-10-18 06:51:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/// # `POST /_matrix/client/r0/rooms/{roomId}/report/{eventId}`
|
|
|
|
///
|
|
|
|
/// Reports an inappropriate event to homeserver admins
|
|
|
|
///
|
|
|
|
pub async fn report_event_route(
|
|
|
|
db: DatabaseGuard,
|
2022-04-06 21:31:29 +02:00
|
|
|
body: Ruma<report_content::v3::IncomingRequest>,
|
2022-02-18 15:33:14 +01:00
|
|
|
) -> Result<report_content::v3::Response> {
|
2021-10-18 06:51:11 +02:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
|
|
|
|
2021-10-20 13:12:06 +02:00
|
|
|
let pdu = match db.rooms.get_pdu(&body.event_id)? {
|
|
|
|
Some(pdu) => pdu,
|
2021-10-18 06:51:11 +02:00
|
|
|
_ => {
|
|
|
|
return Err(Error::BadRequest(
|
|
|
|
ErrorKind::InvalidParam,
|
|
|
|
"Invalid Event ID",
|
|
|
|
))
|
|
|
|
}
|
2021-10-20 13:12:06 +02:00
|
|
|
};
|
2021-10-18 06:51:11 +02:00
|
|
|
|
2022-03-05 03:16:21 +01:00
|
|
|
if let Some(true) = body.score.map(|s| s > int!(0) || s < int!(-100)) {
|
2021-10-18 06:51:11 +02:00
|
|
|
return Err(Error::BadRequest(
|
|
|
|
ErrorKind::InvalidParam,
|
|
|
|
"Invalid score, must be within 0 to -100",
|
|
|
|
));
|
|
|
|
};
|
|
|
|
|
2022-03-05 03:16:21 +01:00
|
|
|
if let Some(true) = body.reason.clone().map(|s| s.chars().count() > 250) {
|
2021-10-18 06:51:11 +02:00
|
|
|
return Err(Error::BadRequest(
|
|
|
|
ErrorKind::InvalidParam,
|
2021-10-20 13:12:06 +02:00
|
|
|
"Reason too long, should be 250 characters or fewer",
|
2021-10-18 06:51:11 +02:00
|
|
|
));
|
|
|
|
};
|
|
|
|
|
2022-01-30 22:15:53 +01:00
|
|
|
db.admin
|
|
|
|
.send_message(message::RoomMessageEventContent::text_html(
|
2021-10-18 07:38:41 +02:00
|
|
|
format!(
|
2021-10-20 13:12:06 +02:00
|
|
|
"Report received from: {}\n\n\
|
2022-03-05 03:16:21 +01:00
|
|
|
Event ID: {:?}\n\
|
|
|
|
Room ID: {:?}\n\
|
|
|
|
Sent By: {:?}\n\n\
|
|
|
|
Report Score: {:?}\n\
|
|
|
|
Report Reason: {:?}",
|
2021-10-18 07:38:41 +02:00
|
|
|
sender_user, pdu.event_id, pdu.room_id, pdu.sender, body.score, body.reason
|
2021-11-26 20:36:40 +01:00
|
|
|
),
|
2021-10-18 07:38:41 +02:00
|
|
|
format!(
|
2022-03-05 03:16:21 +01:00
|
|
|
"<details><summary>Report received from: <a href=\"https://matrix.to/#/{0:?}\">{0:?}\
|
|
|
|
</a></summary><ul><li>Event Info<ul><li>Event ID: <code>{1:?}</code>\
|
|
|
|
<a href=\"https://matrix.to/#/{2:?}/{1:?}\">🔗</a></li><li>Room ID: <code>{2:?}</code>\
|
|
|
|
</li><li>Sent By: <a href=\"https://matrix.to/#/{3:?}\">{3:?}</a></li></ul></li><li>\
|
|
|
|
Report Info<ul><li>Report Score: {4:?}</li><li>Report Reason: {5}</li></ul></li>\
|
2021-10-20 13:12:06 +02:00
|
|
|
</ul></details>",
|
2021-10-18 07:38:41 +02:00
|
|
|
sender_user,
|
|
|
|
pdu.event_id,
|
|
|
|
pdu.room_id,
|
|
|
|
pdu.sender,
|
|
|
|
body.score,
|
2022-04-06 18:49:46 +02:00
|
|
|
HtmlEscape(body.reason.as_deref().unwrap_or(""))
|
2021-11-26 20:36:40 +01:00
|
|
|
),
|
2022-01-30 22:15:53 +01:00
|
|
|
));
|
2021-10-18 06:51:11 +02:00
|
|
|
|
|
|
|
db.flush()?;
|
|
|
|
|
2022-02-18 15:33:14 +01:00
|
|
|
Ok(report_content::v3::Response {})
|
2021-10-18 06:51:11 +02:00
|
|
|
}
|