add: community search

This commit is contained in:
trisua 2025-04-10 21:37:33 -04:00
parent 1b77fef470
commit 5109ac65f4
10 changed files with 167 additions and 12 deletions

View file

@ -52,6 +52,7 @@ pub const COMMUNITIES_FEED: &str = include_str!("./public/html/communities/feed.
pub const COMMUNITIES_POST: &str = include_str!("./public/html/communities/post.html"); pub const COMMUNITIES_POST: &str = include_str!("./public/html/communities/post.html");
pub const COMMUNITIES_SETTINGS: &str = include_str!("./public/html/communities/settings.html"); pub const COMMUNITIES_SETTINGS: &str = include_str!("./public/html/communities/settings.html");
pub const COMMUNITIES_MEMBERS: &str = include_str!("./public/html/communities/members.html"); pub const COMMUNITIES_MEMBERS: &str = include_str!("./public/html/communities/members.html");
pub const COMMUNITIES_SEARCH: &str = include_str!("./public/html/communities/search.html");
pub const TIMELINES_HOME: &str = include_str!("./public/html/timelines/home.html"); pub const TIMELINES_HOME: &str = include_str!("./public/html/timelines/home.html");
pub const TIMELINES_FOLLOWING: &str = include_str!("./public/html/timelines/following.html"); pub const TIMELINES_FOLLOWING: &str = include_str!("./public/html/timelines/following.html");
@ -180,6 +181,7 @@ pub(crate) async fn write_assets(config: &Config) -> PathBufD {
write_template!(html_path->"communities/post.html"(crate::assets::COMMUNITIES_POST) --config=config); write_template!(html_path->"communities/post.html"(crate::assets::COMMUNITIES_POST) --config=config);
write_template!(html_path->"communities/settings.html"(crate::assets::COMMUNITIES_SETTINGS) --config=config); write_template!(html_path->"communities/settings.html"(crate::assets::COMMUNITIES_SETTINGS) --config=config);
write_template!(html_path->"communities/members.html"(crate::assets::COMMUNITIES_MEMBERS) --config=config); write_template!(html_path->"communities/members.html"(crate::assets::COMMUNITIES_MEMBERS) --config=config);
write_template!(html_path->"communities/search.html"(crate::assets::COMMUNITIES_SEARCH) --config=config);
write_template!(html_path->"timelines/home.html"(crate::assets::TIMELINES_HOME) -d "timelines" --config=config); write_template!(html_path->"timelines/home.html"(crate::assets::TIMELINES_HOME) -d "timelines" --config=config);
write_template!(html_path->"timelines/following.html"(crate::assets::TIMELINES_FOLLOWING) --config=config); write_template!(html_path->"timelines/following.html"(crate::assets::TIMELINES_FOLLOWING) --config=config);

View file

@ -80,6 +80,10 @@ version = "1.0.0"
"communities:label.repost" = "Repost" "communities:label.repost" = "Repost"
"communities:label.quote_post" = "Quote post" "communities:label.quote_post" = "Quote post"
"communities:label.expand_original" = "Expand original" "communities:label.expand_original" = "Expand original"
"communities:label.search" = "Search"
"communities:label.search_results" = "Search results"
"communities:label.query" = "Query"
"communities:label.join_new" = "Join new"
"notifs:action.mark_as_read" = "Mark as read" "notifs:action.mark_as_read" = "Mark as read"
"notifs:action.mark_as_unread" = "Mark as unread" "notifs:action.mark_as_unread" = "Mark as unread"

View file

@ -157,6 +157,10 @@ p {
margin-bottom: 0; margin-bottom: 0;
} }
.no_p_margin img {
display: block !important;
}
.name { .name {
max-width: 250px; max-width: 250px;
overflow: hidden; overflow: hidden;

View file

@ -33,9 +33,16 @@
{% endif %} {% endif %}
<div class="card-nest w-full"> <div class="card-nest w-full">
<div class="card small flex items-center gap-2"> <div class="card small flex items-center justify-between gap-2">
{{ icon "award" }} <div class="flex items-center gap-2">
<span>{{ text "communities:label.my_communities" }}</span> {{ icon "award" }}
<span>{{ text "communities:label.my_communities" }}</span>
</div>
<a href="/communities/search" class="button quaternary small">
{{ icon "search" }}
<span>{{ text "communities:label.join_new" }}</span>
</a>
</div> </div>
<div class="card flex flex-col gap-2"> <div class="card flex flex-col gap-2">
@ -45,9 +52,16 @@
</div> </div>
<div class="card-nest w-full"> <div class="card-nest w-full">
<div class="card small flex items-center gap-2"> <div class="card small flex items-center justify-between gap-2">
{{ icon "trending-up" }} <div class="flex items-center gap-2">
<span>{{ text "communities:label.popular_communities" }}</span> {{ icon "trending-up" }}
<span>{{ text "communities:label.popular_communities" }}</span>
</div>
<a href="/communities/search" class="button quaternary small">
{{ icon "search" }}
<span>{{ text "communities:label.search" }}</span>
</a>
</div> </div>
<div class="card flex flex-col gap-2"> <div class="card flex flex-col gap-2">

View file

@ -0,0 +1,45 @@
{% extends "root.html" %} {% block head %}
<title>Search communities - {{ config.name }}</title>
{% endblock %} {% block body %} {{ macros::nav(selected="communities") }}
<main class="flex flex-col gap-2">
<div class="card-nest">
<div class="card small flex items-center gap-2">
{{ icon "search" }}
<span>{{ text "communities:label.search" }}</span>
</div>
<form class="card flex flex-col gap-4">
<div class="flex flex-col gap-1">
<label for="text">{{ text "communities:label.query" }}</label>
<input
type="text"
name="text"
id="text"
placeholder="text"
required
maxlength="32"
value="{{ text }}"
/>
</div>
<button class="primary">{{ text "dialog:action.continue" }}</button>
</form>
</div>
<div class="card-nest">
<div class="card small flex items-center gap-2">
{{ icon "book-marked" }}
<span>{{ text "communities:label.search_results" }}</span>
</div>
<!-- prettier-ignore -->
<div class="card flex flex-col gap-4">
{% for item in list %}
{{ components::community_listing_card(community=item) }}
{% endfor %}
{{ components::pagination(page=page, items=list|length, key="&text=", value=text) }}
</div>
</div>
</main>
{% endblock %}

View file

@ -392,17 +392,23 @@ show_community and post.community != config.town_square %}
</div> </div>
</div> </div>
</a> </a>
{%- endmacro %} {% macro pagination(page=0, items=0) -%} {%- endmacro %} {% macro pagination(page=0, items=0, key="", value="") -%}
<div class="flex justify-between gap-2 w-full"> <div class="flex justify-between gap-2 w-full">
{% if page > 0 %} {% if page > 0 %}
<a class="button quaternary" href="?page={{ page - 1 }}"> <a
class="button quaternary"
href="?page={{ page - 1 }}{{ key }}{{ value }}"
>
{{ icon "arrow-left" }} {{ icon "arrow-left" }}
<span>{{ text "general:link.previous" }}</span> <span>{{ text "general:link.previous" }}</span>
</a> </a>
{% else %} {% else %}
<div></div> <div></div>
{% endif %} {% if items != 0 %} {% endif %} {% if items != 0 %}
<a class="button quaternary" href="?page={{ page + 1 }}"> <a
class="button quaternary"
href="?page={{ page + 1 }}{{ key }}{{ value }}"
>
<span>{{ text "general:link.next" }}</span> <span>{{ text "general:link.next" }}</span>
{{ icon "arrow-right"}} {{ icon "arrow-right"}}
</a> </a>

View file

@ -11,8 +11,14 @@
<b>✨ Welcome to <i>{{ config.name }}</i>!</b> <b>✨ Welcome to <i>{{ config.name }}</i>!</b>
</div> </div>
<div class="card"> <div class="card no_p_margin">
Join some communities to populate your home timeline! <p>Join some communities to populate your home timeline!</p>
<p>
You can get started by
<a href="/communities/search"
>searching for a community to join!</a
>
</p>
</div> </div>
</div> </div>
{% else %} {% else %}

View file

@ -1,4 +1,4 @@
use super::{PaginatedQuery, render_error}; use super::{render_error, PaginatedQuery, SearchedQuery};
use crate::{assets::initial_context, get_lang, get_user_from_token, sanitize::clean_context, State}; use crate::{assets::initial_context, get_lang, get_user_from_token, sanitize::clean_context, State};
use axum::{ use axum::{
Extension, Extension,
@ -166,6 +166,44 @@ pub async fn list_request(jar: CookieJar, Extension(data): Extension<State>) ->
)) ))
} }
/// `/communities/search`
pub async fn search_request(
jar: CookieJar,
Extension(data): Extension<State>,
Query(req): Query<SearchedQuery>,
) -> 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,
));
}
};
let communities = match data
.0
.get_communities_searched(&req.text, 12, req.page)
.await
{
Ok(p) => p,
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
};
let lang = get_lang!(jar, data.0);
let mut context = initial_context(&data.0.0, lang, &Some(user)).await;
context.insert("list", &communities);
context.insert("page", &req.page);
context.insert("text", &req.text);
// return
Ok(Html(
data.1.render("communities/search.html", &context).unwrap(),
))
}
pub fn community_context( pub fn community_context(
context: &mut Context, context: &mut Context,
community: &Community, community: &Community,

View file

@ -45,6 +45,7 @@ pub fn routes() -> Router {
.route("/@{username}/followers", get(profile::followers_request)) .route("/@{username}/followers", get(profile::followers_request))
// communities // communities
.route("/communities", get(communities::list_request)) .route("/communities", get(communities::list_request))
.route("/communities/search", get(communities::search_request))
.route("/community/{title}", get(communities::feed_request)) .route("/community/{title}", get(communities::feed_request))
.route( .route(
"/community/{title}/manage", "/community/{title}/manage",
@ -74,3 +75,11 @@ pub struct PaginatedQuery {
#[serde(default)] #[serde(default)]
pub page: usize, pub page: usize,
} }
#[derive(Deserialize)]
pub struct SearchedQuery {
#[serde(default)]
pub text: String,
#[serde(default)]
pub page: usize,
}

View file

@ -145,6 +145,33 @@ impl DataManager {
Ok(res.unwrap()) Ok(res.unwrap())
} }
/// Get all communities, filtering their title.
/// Communities are sorted by popularity first, creation date second.
pub async fn get_communities_searched(
&self,
query: &str,
batch: usize,
page: usize,
) -> Result<Vec<Community>> {
let conn = match self.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = query_rows!(
&conn,
"SELECT * FROM communities WHERE title LIKE $1 ORDER BY member_count DESC, created DESC LIMIT $2 OFFSET $3",
params![&format!("%{query}%"), &(batch as i64), &(page as i64)],
|x| { Self::get_community_from_row(x) }
);
if res.is_err() {
return Err(Error::GeneralNotFound("communities".to_string()));
}
Ok(res.unwrap())
}
/// Create a new community in the database. /// Create a new community in the database.
/// ///
/// # Arguments /// # Arguments