add: better mobile chats state

add: move channels list to turbo-frame
fix: don't spam _render (socket)
This commit is contained in:
trisua 2025-04-30 16:45:31 -04:00
parent 0dbf660399
commit c549fdd274
5 changed files with 265 additions and 170 deletions

View file

@ -1,4 +1,4 @@
use super::{render_error, PaginatedQuery};
use super::{render_error, ChatsAppQuery, PaginatedQuery};
use crate::{State, assets::initial_context, get_lang, get_user_from_token};
use axum::{
extract::{Path, Query},
@ -28,7 +28,7 @@ pub async fn app_request(
jar: CookieJar,
Extension(data): Extension<State>,
Path((selected_community, selected_channel)): Path<(usize, usize)>,
Query(props): Query<PaginatedQuery>,
Query(props): Query<ChatsAppQuery>,
) -> impl IntoResponse {
let data = data.read().await;
let user = match get_user_from_token!(jar, data.0) {
@ -60,17 +60,19 @@ pub async fn app_request(
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
};
let channels = if selected_community == 0 {
match data.0.get_channels_by_user(user.id).await {
if selected_community != 0 && selected_channel == 0 {
let channels = match data.0.get_channels_by_community(selected_community).await {
Ok(p) => p,
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
};
if let Some(ref channel) = channels.get(0) {
return Ok(Html(format!(
"<!doctype html><html><head><meta http-equiv=\"refresh\" content=\"0; url=/chats/{}/{}?nav={}\" /></head></html>",
selected_community, channel.id, props.nav
)));
}
} else {
match data.0.get_channels_by_community(selected_community).await {
Ok(p) => p,
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
}
};
}
let community = if selected_community != 0 {
match data.0.get_community_by_id(selected_community).await {
@ -130,9 +132,7 @@ pub async fn app_request(
context.insert("community", &community);
context.insert("channel", &channel);
context.insert("communities", &communities);
context.insert("channels", &channels);
// return
Ok(Html(data.1.render("chats/app.html", &context).unwrap()))
@ -249,3 +249,47 @@ pub async fn message_request(
// return
Ok(Html(data.1.render("chats/message.html", &context).unwrap()))
}
/// `/chats/{community}/{channel/_channels`
pub async fn channels_request(
jar: CookieJar,
Extension(data): Extension<State>,
Path((community, channel)): Path<(usize, usize)>,
Query(props): Query<PaginatedQuery>,
) -> 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 channels = if community == 0 {
match data.0.get_channels_by_user(user.id).await {
Ok(p) => p,
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
}
} else {
match data.0.get_channels_by_community(community).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("channels", &channels);
context.insert("page", &props.page);
context.insert("selected_community", &community);
context.insert("selected_channel", &channel);
// return
Ok(Html(
data.1.render("chats/channels.html", &context).unwrap(),
))
}

View file

@ -99,6 +99,10 @@ pub fn routes() -> Router {
"/chats/{community}/{channel}/_render",
post(chats::message_request),
)
.route(
"/chats/{community}/{channel}/_channels",
get(chats::channels_request),
)
}
pub async fn render_error(
@ -119,6 +123,14 @@ pub struct PaginatedQuery {
pub page: usize,
}
#[derive(Deserialize)]
pub struct ChatsAppQuery {
#[serde(default)]
pub page: usize,
#[serde(default)]
pub nav: bool,
}
#[derive(Deserialize)]
pub struct ProfileQuery {
#[serde(default)]