From b4270003649a60f6620044ccb7ae19325b71e30d Mon Sep 17 00:00:00 2001 From: alemi Date: Sun, 11 Aug 2024 14:46:15 +0200 Subject: [PATCH] feat: search endpoint it's a bit rushed and ugly but maybe works? --- upub/core/src/config.rs | 11 +++--- upub/routes/src/activitypub/application.rs | 42 +++++++++++++++++++++- upub/routes/src/activitypub/mod.rs | 9 +++++ upub/routes/src/builders.rs | 1 + 4 files changed, 58 insertions(+), 5 deletions(-) diff --git a/upub/core/src/config.rs b/upub/core/src/config.rs index a26cd2a..9967448 100644 --- a/upub/core/src/config.rs +++ b/upub/core/src/config.rs @@ -74,6 +74,9 @@ pub struct SecurityConfig { #[serde(default)] pub allow_public_debugger: bool, + #[serde(default)] + pub allow_public_search: bool, + #[serde_inline_default("changeme".to_string())] pub proxy_secret: String, @@ -87,16 +90,16 @@ pub struct SecurityConfig { pub session_duration_hours: i64, #[serde_inline_default(2)] - pub max_id_redirects: u32, + pub max_id_redirects: u32, // TODO not sure it fits here #[serde_inline_default(20)] - pub thread_crawl_depth: u32, + pub thread_crawl_depth: u32, // TODO doesn't really fit here #[serde_inline_default(30)] - pub job_expiration_days: u32, + pub job_expiration_days: u32, // TODO doesn't really fit here #[serde_inline_default(100)] - pub reinsertion_attempt_limit: u32, + pub reinsertion_attempt_limit: u32, // TODO doesn't really fit here } #[serde_inline_default::serde_inline_default] diff --git a/upub/routes/src/activitypub/application.rs b/upub/routes/src/activitypub/application.rs index 45ec290..b20ae19 100644 --- a/upub/routes/src/activitypub/application.rs +++ b/upub/routes/src/activitypub/application.rs @@ -1,9 +1,12 @@ use apb::{LD, ActorMut, BaseMut, ObjectMut, PublicKeyMut}; use axum::{extract::{Path, Query, State}, http::HeaderMap, response::{IntoResponse, Redirect, Response}}; use reqwest::Method; +use sea_orm::{Condition, ColumnTrait}; use upub::{traits::{Cloaker, Fetcher}, Context}; -use crate::{builders::JsonLD, ApiError, AuthIdentity}; +use crate::{builders::JsonLD, ApiError, AuthIdentity, Identity}; + +use super::{PaginatedSearch, Pagination}; pub async fn view( @@ -39,6 +42,43 @@ pub async fn view( ).into_response()) } +pub async fn search( + State(ctx): State, + AuthIdentity(auth): AuthIdentity, + Query(page): Query, +) -> crate::ApiResult> { + if !auth.is_local() && ctx.cfg().security.allow_public_search { + return Err(crate::ApiError::forbidden()); + } + + let mut filter = Condition::any() + .add(auth.filter()); + + if let Identity::Local { ref id, .. } = auth { + filter = filter.add(upub::model::object::Column::AttributedTo.eq(id)); + } + + filter = Condition::all() + .add(upub::model::object::Column::Content.like(page.q)) + .add(filter); + + // TODO lmao rethink this all + let page = Pagination { + offset: page.offset, + batch: page.batch, + }; + + crate::builders::paginate_feed( + upub::url!(ctx, "/search"), + filter, + ctx.db(), + page, + auth.my_id(), + false, + ) + .await +} + #[derive(Debug, serde::Deserialize)] pub struct ProxyQuery { uri: String, diff --git a/upub/routes/src/activitypub/mod.rs b/upub/routes/src/activitypub/mod.rs index e2c18c2..ed401d9 100644 --- a/upub/routes/src/activitypub/mod.rs +++ b/upub/routes/src/activitypub/mod.rs @@ -22,6 +22,7 @@ impl ActivityPubRouter for Router { // core server inbox/outbox, maybe for feeds? TODO do we need these? .route("/", get(ap::application::view)) // fetch route, to debug and retreive remote objects + .route("/search", get(ap::application::search)) .route("/fetch", get(ap::application::ap_fetch)) .route("/proxy/:hmac/:uri", get(ap::application::cloak_proxy)) .route("/inbox", post(ap::inbox::post)) @@ -90,6 +91,14 @@ pub struct Pagination { pub batch: Option, } +#[derive(Debug, serde::Deserialize)] +// TODO i don't really like how pleroma/mastodon do it actually, maybe change this? +pub struct PaginatedSearch { + pub q: String, + pub offset: Option, + pub batch: Option, +} + pub struct CreationResult(pub String); impl IntoResponse for CreationResult { fn into_response(self) -> axum::response::Response { diff --git a/upub/routes/src/builders.rs b/upub/routes/src/builders.rs index d7b1425..49125e7 100644 --- a/upub/routes/src/builders.rs +++ b/upub/routes/src/builders.rs @@ -5,6 +5,7 @@ use upub::selector::{BatchFillable, RichActivity}; use crate::activitypub::Pagination; +#[deprecated = "just query directly maybe?"] pub async fn paginate_feed( id: String, filter: Condition,