mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-05-26 20:49:55 +00:00
feat: Add Meowlnir invite interception support
Co-authored-by: Jade Ellis <jade@ellis.link>
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
use std::{fmt::Debug, mem};
|
||||
|
||||
use bytes::BytesMut;
|
||||
use conduwuit::{Err, Result, config::MeowlnirConfig, debug_error, err, utils, warn};
|
||||
use reqwest::Client;
|
||||
use ruma::api::{IncomingResponse, MatrixVersion, OutgoingRequest, SendAccessToken};
|
||||
|
||||
/// Sends a request to an antispam service
|
||||
pub(crate) async fn send_meowlnir_request<T>(
|
||||
client: &Client,
|
||||
config: &MeowlnirConfig,
|
||||
request: T,
|
||||
) -> Result<Option<T::IncomingResponse>>
|
||||
where
|
||||
T: OutgoingRequest + Debug + Send,
|
||||
{
|
||||
const VERSIONS: [MatrixVersion; 1] = [MatrixVersion::V1_15];
|
||||
if config.secret.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
let secret = config.secret.as_str();
|
||||
let http_request = request
|
||||
.try_into_http_request::<BytesMut>(
|
||||
config.base_url.as_str(),
|
||||
SendAccessToken::Always(secret),
|
||||
&VERSIONS,
|
||||
)?
|
||||
.map(BytesMut::freeze);
|
||||
let reqwest_request = reqwest::Request::try_from(http_request)?;
|
||||
|
||||
let mut response = client.execute(reqwest_request).await.map_err(|e| {
|
||||
warn!("Could not send request to antispam: {e:?}");
|
||||
e
|
||||
})?;
|
||||
|
||||
// reqwest::Response -> http::Response conversion
|
||||
let status = response.status();
|
||||
let mut http_response_builder = http::Response::builder()
|
||||
.status(status)
|
||||
.version(response.version());
|
||||
mem::swap(
|
||||
response.headers_mut(),
|
||||
http_response_builder
|
||||
.headers_mut()
|
||||
.expect("http::response::Builder is usable"),
|
||||
);
|
||||
|
||||
let body = response.bytes().await?; // TODO: handle timeout
|
||||
|
||||
if !status.is_success() {
|
||||
debug_error!("Antispam response bytes: {:?}", utils::string_from_bytes(&body));
|
||||
return match status {
|
||||
| http::StatusCode::FORBIDDEN =>
|
||||
Err!(Request(Forbidden("Request was rejected by antispam service.",))),
|
||||
| _ => Err!(BadServerResponse(warn!(
|
||||
"Antispam returned unsuccessful HTTP response {status}",
|
||||
))),
|
||||
};
|
||||
}
|
||||
|
||||
let response = T::IncomingResponse::try_from_http_response(
|
||||
http_response_builder
|
||||
.body(body)
|
||||
.expect("reqwest body is valid http body"),
|
||||
);
|
||||
|
||||
response.map(Some).map_err(|e| {
|
||||
err!(BadServerResponse(warn!(
|
||||
"Antispam returned invalid/malformed response bytes: {e}",
|
||||
)))
|
||||
})
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
mod antispam;
|
||||
mod appservice;
|
||||
mod data;
|
||||
mod dest;
|
||||
@@ -12,7 +13,9 @@ use std::{
|
||||
|
||||
use async_trait::async_trait;
|
||||
use conduwuit::{
|
||||
Result, Server, debug, debug_warn, err, error,
|
||||
Result, Server,
|
||||
config::MeowlnirConfig,
|
||||
debug, debug_warn, err, error,
|
||||
smallvec::SmallVec,
|
||||
utils::{ReadyExt, TryReadyExt, available_parallelism, math::usize_from_u64_truncated},
|
||||
warn,
|
||||
@@ -334,6 +337,18 @@ impl Service {
|
||||
appservice::send_request(client, registration, request).await
|
||||
}
|
||||
|
||||
/// Sends a request to the chosen antispam configuration
|
||||
pub async fn send_meowlnir_antispam_request<T>(
|
||||
&self,
|
||||
config: &MeowlnirConfig,
|
||||
request: T,
|
||||
) -> Result<Option<T::IncomingResponse>>
|
||||
where
|
||||
T: OutgoingRequest + Debug + Send,
|
||||
{
|
||||
antispam::send_meowlnir_request(&self.services.client.appservice, config, request).await
|
||||
}
|
||||
|
||||
/// Clean up queued sending event data
|
||||
///
|
||||
/// Used after we remove an appservice registration or a user deletes a push
|
||||
|
||||
Reference in New Issue
Block a user