add: profile and full search
This commit is contained in:
parent
b8b0ef7f21
commit
3e4ee8126a
52 changed files with 897 additions and 484 deletions
|
@ -1,12 +1,15 @@
|
|||
use super::{PaginatedQuery, render_error};
|
||||
use crate::{State, assets::initial_context, get_lang, get_user_from_token};
|
||||
use crate::{
|
||||
assets::initial_context, check_user_blocked_or_private, get_lang, get_user_from_token, State,
|
||||
};
|
||||
use axum::{
|
||||
extract::{Path, Query},
|
||||
response::{Html, IntoResponse},
|
||||
Extension,
|
||||
};
|
||||
use axum_extra::extract::CookieJar;
|
||||
use tetratto_core::model::{requests::ActionType, Error};
|
||||
use serde::Deserialize;
|
||||
use tetratto_core::model::{permissions::FinePermission, requests::ActionType, Error};
|
||||
use std::fs::read_to_string;
|
||||
use pathbufd::PathBufD;
|
||||
|
||||
|
@ -497,3 +500,96 @@ pub async fn markdown_document_request(
|
|||
// return
|
||||
Ok(Html(data.1.render("misc/markdown.html", &context).unwrap()))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct SearchQuery {
|
||||
#[serde(default)]
|
||||
pub query: String,
|
||||
#[serde(default)]
|
||||
pub profile: usize,
|
||||
#[serde(default)]
|
||||
pub page: usize,
|
||||
}
|
||||
|
||||
/// `/search`
|
||||
pub async fn search_request(
|
||||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
Query(mut req): Query<SearchQuery>,
|
||||
) -> impl IntoResponse {
|
||||
let data = data.read().await;
|
||||
let user = match get_user_from_token!(jar, data.0) {
|
||||
Some(ua) => ua,
|
||||
None => {
|
||||
return Err(Html(
|
||||
render_error(Error::NotAllowed, &jar, &data, &None).await,
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
if req.profile == 0 && !user.permissions.check(FinePermission::SUPPORTER) {
|
||||
req.query = String::new();
|
||||
}
|
||||
|
||||
req.query = req.query.trim().replace(" ", " & "); // change spaces into & for tsquery
|
||||
|
||||
let ignore_users = data.0.get_userblocks_receivers(user.id).await;
|
||||
|
||||
let list = if req.query.is_empty() {
|
||||
Vec::new()
|
||||
} else {
|
||||
if req.profile != 0 {
|
||||
match data
|
||||
.0
|
||||
.get_posts_by_user_searched(req.profile, 12, req.page, &req.query, &Some(&user))
|
||||
.await
|
||||
{
|
||||
Ok(l) => match data
|
||||
.0
|
||||
.fill_posts_with_community(l, user.id, &ignore_users, &Some(user.clone()))
|
||||
.await
|
||||
{
|
||||
Ok(l) => l,
|
||||
Err(_) => Vec::new(),
|
||||
},
|
||||
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
|
||||
}
|
||||
} else {
|
||||
match data.0.get_posts_searched(12, req.page, &req.query).await {
|
||||
Ok(l) => match data
|
||||
.0
|
||||
.fill_posts_with_community(l, user.id, &ignore_users, &Some(user.clone()))
|
||||
.await
|
||||
{
|
||||
Ok(l) => l,
|
||||
Err(_) => Vec::new(),
|
||||
},
|
||||
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let profile = if req.profile != 0 {
|
||||
Some(match data.0.get_user_by_id(req.profile).await {
|
||||
Ok(ua) => {
|
||||
check_user_blocked_or_private!(Some(user.clone()), ua, data, jar);
|
||||
ua
|
||||
}
|
||||
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
|
||||
})
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let lang = get_lang!(jar, data.0);
|
||||
let mut context = initial_context(&data.0.0, lang, &Some(user)).await;
|
||||
|
||||
context.insert("list", &list);
|
||||
context.insert("profile", &profile);
|
||||
context.insert("query", &req.query);
|
||||
context.insert("page", &req.page);
|
||||
|
||||
Ok(Html(
|
||||
data.1.render("timelines/search.html", &context).unwrap(),
|
||||
))
|
||||
}
|
||||
|
|
|
@ -28,6 +28,7 @@ pub fn routes() -> Router {
|
|||
.route("/popular", get(misc::popular_request))
|
||||
.route("/following", get(misc::following_request))
|
||||
.route("/all", get(misc::all_request))
|
||||
.route("/search", get(misc::search_request))
|
||||
// question timelines
|
||||
.route("/questions", get(misc::index_questions_request))
|
||||
.route("/popular/questions", get(misc::popular_questions_request))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue