feat: Consolidate antispam checks into a service

Also adds support for the spam checker join rule, and Draupnir callbacks
This commit is contained in:
timedout
2026-01-05 20:10:28 +00:00
committed by Jade Ellis
parent c249dd992e
commit 5ac82f36f3
13 changed files with 355 additions and 136 deletions
+7 -14
View File
@@ -1,30 +1,23 @@
use std::{fmt::Debug, mem};
use bytes::BytesMut;
use conduwuit::{Err, Result, config::MeowlnirConfig, debug_error, err, utils, warn};
use conduwuit::{Err, Result, 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>(
pub(crate) async fn send_antispam_request<T>(
client: &Client,
config: &MeowlnirConfig,
base_url: &str,
secret: &str,
request: T,
) -> Result<Option<T::IncomingResponse>>
) -> Result<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,
)?
.try_into_http_request::<BytesMut>(base_url, SendAccessToken::Always(secret), &VERSIONS)?
.map(BytesMut::freeze);
let reqwest_request = reqwest::Request::try_from(http_request)?;
@@ -64,7 +57,7 @@ where
.expect("reqwest body is valid http body"),
);
response.map(Some).map_err(|e| {
response.map_err(|e| {
err!(BadServerResponse(warn!(
"Antispam returned invalid/malformed response bytes: {e}",
)))
+2 -16
View File
@@ -1,4 +1,4 @@
mod antispam;
pub mod antispam;
mod appservice;
mod data;
mod dest;
@@ -13,9 +13,7 @@ use std::{
use async_trait::async_trait;
use conduwuit::{
Result, Server,
config::MeowlnirConfig,
debug, debug_warn, err, error,
Result, Server, debug, debug_warn, err, error,
smallvec::SmallVec,
utils::{ReadyExt, TryReadyExt, available_parallelism, math::usize_from_u64_truncated},
warn,
@@ -337,18 +335,6 @@ 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