Add shorthand for meta()

Signed-off-by: eternal-flame-AD <yume@yumechi.jp>
This commit is contained in:
ゆめ 2024-10-16 19:07:18 -05:00
parent 1af251420e
commit f1c0741089
No known key found for this signature in database
3 changed files with 47 additions and 3 deletions

View file

@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize};
use crate::{
delegate,
evaluate::{Disposition, Evaluator},
model::ap::{AnyObject, NoteObject},
APRequestInfo, HasAppState,
};
@ -14,6 +15,15 @@ pub struct Meta<E: IntoResponse + 'static, I: HasAppState<E>> {
_marker: std::marker::PhantomData<E>,
}
impl<E: IntoResponse + 'static, I: HasAppState<E>> Meta<E, I> {
pub fn new(inner: I) -> Self {
Self {
inner,
_marker: std::marker::PhantomData,
}
}
}
pub fn extract_host(act: &APRequestInfo) -> Option<String> {
act.activity
.as_ref()
@ -50,3 +60,26 @@ pub struct MetaItem {
instance_host: Option<String>,
attributed_to: Option<String>,
}
#[async_trait::async_trait]
impl<
E: IntoResponse + Serialize + Send + Sync + 'static,
I: HasAppState<E> + Evaluator<E> + Sync,
> Evaluator<E> for Meta<E, I>
{
fn name() -> &'static str {
"ExtractMeta"
}
async fn evaluate<'r>(
&self,
ctx: Option<serde_json::Value>,
info: &APRequestInfo<'r>,
) -> (Disposition<E>, Option<serde_json::Value>) {
let ctx = ctx.map(|mut c| {
c["meta"] = serde_json::to_value(extract_meta(info)).unwrap();
c
});
self.inner.evaluate(ctx, info).await
}
}

View file

@ -17,6 +17,7 @@ use axum::{
};
use client::ClientCache;
use evaluate::chain::audit::{Audit, AuditOptions};
use evaluate::chain::meta::Meta;
use evaluate::{
Disposition, Evaluator, ERR_BAD_REQUEST, ERR_INTERNAL_SERVER_ERROR, ERR_PAYLOAD_TOO_LARGE,
ERR_SERVICE_TEMPORARILY_UNAVAILABLE,
@ -103,6 +104,15 @@ pub trait HasAppState<E: IntoResponse + 'static>: Clone {
{
Audit::new(self, opts)
}
/// Wrap the evaluator in a meta chain.
fn extract_meta(self) -> Meta<E, Self>
where
Self: Sized + Send + Sync + Evaluator<E>,
E: Send + Sync,
{
Meta::new(self.clone())
}
}
/// Application state.

View file

@ -29,6 +29,7 @@ async fn build_state<E: IntoResponse + Clone + Serialize + Send + Sync + 'static
_args: &Args,
) -> impl HasAppState<E> + Evaluator<E> {
base.audited(AuditOptions::new(PathBuf::from("inbox_audit")))
.extract_meta()
}
#[tokio::main]
@ -41,9 +42,9 @@ async fn main() {
let args = Args::parse();
let state = build_state::<MisskeyError>(
Arc::new(BaseAppState::new(
args.backend.parse().expect("Invalid backend URL"),
)),
Arc::new(
BaseAppState::new(args.backend.parse().expect("Invalid backend URL")).with_empty_ctx(),
),
&args,
)
.await;