WIP: recent content

This commit is contained in:
Per Stark
2025-01-27 09:37:48 +01:00
parent 078b4040bb
commit 9a3a8f3a1e
7 changed files with 4656 additions and 23 deletions

View File

@@ -70,6 +70,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
notifier.set_fast_reload(true);
notifier.watch_path(&template_path, true);
minijinja_contrib::add_to_environment(&mut env);
Ok(env)
});

View File

@@ -9,13 +9,14 @@ use crate::{
error::HtmlError,
page_data,
server::{routes::html::render_template, AppState},
storage::types::user::User,
storage::types::{text_content::TextContent, user::User},
};
page_data!(IndexData, "index/index.html", {
gdpr_accepted: bool,
queue_length: u32,
user: Option<User>
user: Option<User>,
latest_text_contents: Vec<TextContent>
});
pub async fn index_handler(
@@ -37,17 +38,29 @@ pub async fn index_handler(
false => 0,
};
// let latest_text_contents = match auth.current_user.is_some() {
// true =>
// }
let latest_text_contents = match auth.current_user.is_some() {
true => User::get_latest_text_contents(
auth.current_user.clone().unwrap().id.as_str(),
&state.surreal_db_client,
)
.await
.map_err(|e| HtmlError::new(e, state.templates.clone()))?,
false => vec![],
};
// let knowledge_entities = User::get_knowledge_entities(
// &auth.current_user.clone().unwrap().id,
// &state.surreal_db_client,
// )
// .await?;
info!("{:?}", latest_text_contents);
// info!("{:?}", knowledge_entities);
let latest_knowledge_entities = match auth.current_user.is_some() {
true => User::get_latest_knowledge_entities(
auth.current_user.clone().unwrap().id.as_str(),
&state.surreal_db_client,
)
.await
.map_err(|e| HtmlError::new(e, state.templates.clone()))?,
false => vec![],
};
info!("{:?}", latest_knowledge_entities);
let output = render_template(
IndexData::template_name(),
@@ -55,6 +68,7 @@ pub async fn index_handler(
queue_length: queue_length.try_into().unwrap(),
gdpr_accepted,
user: auth.current_user,
latest_text_contents,
},
state.templates.clone(),
)?;

View File

@@ -170,13 +170,13 @@ impl User {
}
pub async fn get_knowledge_entities(
id: &str,
user_id: &str,
db: &SurrealDbClient,
) -> Result<Vec<KnowledgeEntity>, AppError> {
let entities: Vec<KnowledgeEntity> = db
.client
.query("SELECT * FROM knowledge_entity WHERE user_id = $user_id")
.bind(("user_id", id.to_owned()))
.bind(("user_id", user_id.to_owned()))
.await?
.take(0)?;
@@ -184,13 +184,31 @@ impl User {
}
pub async fn get_latest_text_contents(
id: &str,
user_id: &str,
db: &SurrealDbClient,
) -> Result<Vec<TextContent>, AppError> {
let items: Vec<TextContent> = db
.client
.query("SELECT * FROM text_content WHERE user_id = $user_id ORDER BY created_at DESC LIMIT 5")
.bind(("user_id", id.to_owned()))
.query("SELECT * FROM type::table($table_name) WHERE user_id = $user_id ORDER BY created_at DESC LIMIT 5")
.bind(("user_id", user_id.to_owned()))
.bind(("table_name", TextContent::table_name()))
.await?
.take(0)?;
Ok(items)
}
pub async fn get_latest_knowledge_entities(
user_id: &str,
db: &SurrealDbClient,
) -> Result<Vec<KnowledgeEntity>, AppError> {
let items: Vec<KnowledgeEntity> = db
.client
.query(
"SELECT * FROM type::table($table_name) WHERE user_id = $user_id ORDER BY created_at DESC LIMIT 5",
)
.bind(("user_id", user_id.to_owned()))
.bind(("table_name", KnowledgeEntity::table_name()))
.await?
.take(0)?;