mirror of
https://github.com/perstarkse/minne.git
synced 2026-07-11 07:12:50 +02:00
fix: improved performance by truncating not displayed text
This commit is contained in:
@@ -2,6 +2,7 @@ pub mod html_state;
|
|||||||
pub mod middlewares;
|
pub mod middlewares;
|
||||||
pub mod router_factory;
|
pub mod router_factory;
|
||||||
pub mod routes;
|
pub mod routes;
|
||||||
|
pub mod utils;
|
||||||
|
|
||||||
use axum::{extract::FromRef, Router};
|
use axum::{extract::FromRef, Router};
|
||||||
use axum_session::{Session, SessionStore};
|
use axum_session::{Session, SessionStore};
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ use crate::{
|
|||||||
auth_middleware::RequireUser,
|
auth_middleware::RequireUser,
|
||||||
response_middleware::{HtmlError, TemplateResponse},
|
response_middleware::{HtmlError, TemplateResponse},
|
||||||
},
|
},
|
||||||
|
utils::text_content_preview::truncate_text_contents,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
@@ -58,6 +59,8 @@ pub async fn show_content_page(
|
|||||||
User::get_text_contents(&user.id, &state.db).await?
|
User::get_text_contents(&user.id, &state.db).await?
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let text_contents = truncate_text_contents(text_contents);
|
||||||
|
|
||||||
let conversation_archive = User::get_user_conversations(&user.id, &state.db).await?;
|
let conversation_archive = User::get_user_conversations(&user.id, &state.db).await?;
|
||||||
let data = ContentPageData {
|
let data = ContentPageData {
|
||||||
user,
|
user,
|
||||||
@@ -117,7 +120,8 @@ pub async fn patch_text_content(
|
|||||||
TextContent::patch(&id, &form.context, &form.category, &form.text, &state.db).await?;
|
TextContent::patch(&id, &form.context, &form.category, &form.text, &state.db).await?;
|
||||||
|
|
||||||
if target.as_deref() == Some("latest_content_section") {
|
if target.as_deref() == Some("latest_content_section") {
|
||||||
let text_contents = User::get_latest_text_contents(&user.id, &state.db).await?;
|
let text_contents =
|
||||||
|
truncate_text_contents(User::get_latest_text_contents(&user.id, &state.db).await?);
|
||||||
|
|
||||||
return Ok(TemplateResponse::new_template(
|
return Ok(TemplateResponse::new_template(
|
||||||
"dashboard/recent_content.html",
|
"dashboard/recent_content.html",
|
||||||
@@ -128,7 +132,7 @@ pub async fn patch_text_content(
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let text_contents = User::get_text_contents(&user.id, &state.db).await?;
|
let text_contents = truncate_text_contents(User::get_text_contents(&user.id, &state.db).await?);
|
||||||
let categories = User::get_user_categories(&user.id, &state.db).await?;
|
let categories = User::get_user_categories(&user.id, &state.db).await?;
|
||||||
let conversation_archive = User::get_user_conversations(&user.id, &state.db).await?;
|
let conversation_archive = User::get_user_conversations(&user.id, &state.db).await?;
|
||||||
|
|
||||||
@@ -166,7 +170,7 @@ pub async fn delete_text_content(
|
|||||||
state.db.delete_item::<TextContent>(&id).await?;
|
state.db.delete_item::<TextContent>(&id).await?;
|
||||||
|
|
||||||
// Get updated content, categories and return the refreshed list
|
// Get updated content, categories and return the refreshed list
|
||||||
let text_contents = User::get_text_contents(&user.id, &state.db).await?;
|
let text_contents = truncate_text_contents(User::get_text_contents(&user.id, &state.db).await?);
|
||||||
let categories = User::get_user_categories(&user.id, &state.db).await?;
|
let categories = User::get_user_categories(&user.id, &state.db).await?;
|
||||||
let conversation_archive = User::get_user_conversations(&user.id, &state.db).await?;
|
let conversation_archive = User::get_user_conversations(&user.id, &state.db).await?;
|
||||||
|
|
||||||
@@ -205,7 +209,8 @@ pub async fn show_recent_content(
|
|||||||
State(state): State<HtmlState>,
|
State(state): State<HtmlState>,
|
||||||
RequireUser(user): RequireUser,
|
RequireUser(user): RequireUser,
|
||||||
) -> Result<impl IntoResponse, HtmlError> {
|
) -> Result<impl IntoResponse, HtmlError> {
|
||||||
let text_contents = User::get_latest_text_contents(&user.id, &state.db).await?;
|
let text_contents =
|
||||||
|
truncate_text_contents(User::get_latest_text_contents(&user.id, &state.db).await?);
|
||||||
|
|
||||||
Ok(TemplateResponse::new_template(
|
Ok(TemplateResponse::new_template(
|
||||||
"dashboard/recent_content.html",
|
"dashboard/recent_content.html",
|
||||||
|
|||||||
@@ -9,10 +9,12 @@ use serde::Serialize;
|
|||||||
use tokio::join;
|
use tokio::join;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
html_state::HtmlState,
|
||||||
middlewares::{
|
middlewares::{
|
||||||
auth_middleware::RequireUser,
|
auth_middleware::RequireUser,
|
||||||
response_middleware::{HtmlError, TemplateResponse},
|
response_middleware::{HtmlError, TemplateResponse},
|
||||||
},
|
},
|
||||||
|
utils::text_content_preview::truncate_text_contents,
|
||||||
AuthSessionType,
|
AuthSessionType,
|
||||||
};
|
};
|
||||||
use common::storage::store;
|
use common::storage::store;
|
||||||
@@ -26,8 +28,6 @@ use common::{
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::html_state::HtmlState;
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
pub struct IndexPageData {
|
pub struct IndexPageData {
|
||||||
user: Option<User>,
|
user: Option<User>,
|
||||||
@@ -52,6 +52,8 @@ pub async fn index_handler(
|
|||||||
User::get_unfinished_ingestion_tasks(&user.id, &state.db)
|
User::get_unfinished_ingestion_tasks(&user.id, &state.db)
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
|
let text_contents = truncate_text_contents(text_contents);
|
||||||
|
|
||||||
Ok(TemplateResponse::new_template(
|
Ok(TemplateResponse::new_template(
|
||||||
"dashboard/base.html",
|
"dashboard/base.html",
|
||||||
IndexPageData {
|
IndexPageData {
|
||||||
@@ -94,7 +96,8 @@ pub async fn delete_text_content(
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Render updated content
|
// Render updated content
|
||||||
let latest_text_contents = User::get_latest_text_contents(&user.id, &state.db).await?;
|
let latest_text_contents =
|
||||||
|
truncate_text_contents(User::get_latest_text_contents(&user.id, &state.db).await?);
|
||||||
|
|
||||||
Ok(TemplateResponse::new_partial(
|
Ok(TemplateResponse::new_partial(
|
||||||
"index/signed_in/recent_content.html",
|
"index/signed_in/recent_content.html",
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
pub mod text_content_preview;
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
use common::storage::types::text_content::TextContent;
|
||||||
|
|
||||||
|
const TEXT_PREVIEW_LENGTH: usize = 50;
|
||||||
|
|
||||||
|
fn maybe_truncate(value: &str) -> Option<String> {
|
||||||
|
let mut char_count = 0;
|
||||||
|
|
||||||
|
for (idx, _) in value.char_indices() {
|
||||||
|
if char_count == TEXT_PREVIEW_LENGTH {
|
||||||
|
return Some(value[..idx].to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
char_count += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn truncate_text_content(mut content: TextContent) -> TextContent {
|
||||||
|
if let Some(truncated) = maybe_truncate(&content.text) {
|
||||||
|
content.text = truncated;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(context) = content.context.as_mut() {
|
||||||
|
if let Some(truncated) = maybe_truncate(context) {
|
||||||
|
*context = truncated;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
content
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn truncate_text_contents(contents: Vec<TextContent>) -> Vec<TextContent> {
|
||||||
|
contents.into_iter().map(truncate_text_content).collect()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user