feat: search endpoint
it's a bit rushed and ugly but maybe works?
This commit is contained in:
parent
7a9a6fc245
commit
b427000364
4 changed files with 58 additions and 5 deletions
|
@ -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]
|
||||
|
|
|
@ -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<Context>,
|
||||
AuthIdentity(auth): AuthIdentity,
|
||||
Query(page): Query<PaginatedSearch>,
|
||||
) -> crate::ApiResult<JsonLD<serde_json::Value>> {
|
||||
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,
|
||||
|
|
|
@ -22,6 +22,7 @@ impl ActivityPubRouter for Router<upub::Context> {
|
|||
// 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<u64>,
|
||||
}
|
||||
|
||||
#[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<u64>,
|
||||
pub batch: Option<u64>,
|
||||
}
|
||||
|
||||
pub struct CreationResult(pub String);
|
||||
impl IntoResponse for CreationResult {
|
||||
fn into_response(self) -> axum::response::Response {
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue