Add shorthand for meta()
Signed-off-by: eternal-flame-AD <yume@yumechi.jp>
This commit is contained in:
parent
1af251420e
commit
f1c0741089
3 changed files with 47 additions and 3 deletions
|
@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
delegate,
|
delegate,
|
||||||
|
evaluate::{Disposition, Evaluator},
|
||||||
model::ap::{AnyObject, NoteObject},
|
model::ap::{AnyObject, NoteObject},
|
||||||
APRequestInfo, HasAppState,
|
APRequestInfo, HasAppState,
|
||||||
};
|
};
|
||||||
|
@ -14,6 +15,15 @@ pub struct Meta<E: IntoResponse + 'static, I: HasAppState<E>> {
|
||||||
_marker: std::marker::PhantomData<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> {
|
pub fn extract_host(act: &APRequestInfo) -> Option<String> {
|
||||||
act.activity
|
act.activity
|
||||||
.as_ref()
|
.as_ref()
|
||||||
|
@ -50,3 +60,26 @@ pub struct MetaItem {
|
||||||
instance_host: Option<String>,
|
instance_host: Option<String>,
|
||||||
attributed_to: 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
10
src/lib.rs
10
src/lib.rs
|
@ -17,6 +17,7 @@ use axum::{
|
||||||
};
|
};
|
||||||
use client::ClientCache;
|
use client::ClientCache;
|
||||||
use evaluate::chain::audit::{Audit, AuditOptions};
|
use evaluate::chain::audit::{Audit, AuditOptions};
|
||||||
|
use evaluate::chain::meta::Meta;
|
||||||
use evaluate::{
|
use evaluate::{
|
||||||
Disposition, Evaluator, ERR_BAD_REQUEST, ERR_INTERNAL_SERVER_ERROR, ERR_PAYLOAD_TOO_LARGE,
|
Disposition, Evaluator, ERR_BAD_REQUEST, ERR_INTERNAL_SERVER_ERROR, ERR_PAYLOAD_TOO_LARGE,
|
||||||
ERR_SERVICE_TEMPORARILY_UNAVAILABLE,
|
ERR_SERVICE_TEMPORARILY_UNAVAILABLE,
|
||||||
|
@ -103,6 +104,15 @@ pub trait HasAppState<E: IntoResponse + 'static>: Clone {
|
||||||
{
|
{
|
||||||
Audit::new(self, opts)
|
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.
|
/// Application state.
|
||||||
|
|
|
@ -29,6 +29,7 @@ async fn build_state<E: IntoResponse + Clone + Serialize + Send + Sync + 'static
|
||||||
_args: &Args,
|
_args: &Args,
|
||||||
) -> impl HasAppState<E> + Evaluator<E> {
|
) -> impl HasAppState<E> + Evaluator<E> {
|
||||||
base.audited(AuditOptions::new(PathBuf::from("inbox_audit")))
|
base.audited(AuditOptions::new(PathBuf::from("inbox_audit")))
|
||||||
|
.extract_meta()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
|
@ -41,9 +42,9 @@ async fn main() {
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
|
|
||||||
let state = build_state::<MisskeyError>(
|
let state = build_state::<MisskeyError>(
|
||||||
Arc::new(BaseAppState::new(
|
Arc::new(
|
||||||
args.backend.parse().expect("Invalid backend URL"),
|
BaseAppState::new(args.backend.parse().expect("Invalid backend URL")).with_empty_ctx(),
|
||||||
)),
|
),
|
||||||
&args,
|
&args,
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|
Loading…
Add table
Reference in a new issue