feat: show content and wip editing

This commit is contained in:
Per Stark
2025-02-13 11:12:32 +01:00
parent 09b2451021
commit c055245a48
15 changed files with 326 additions and 33 deletions

File diff suppressed because one or more lines are too long

View File

@@ -29,6 +29,7 @@ use zettle_db::{
html::{ html::{
account::{delete_account, set_api_key, show_account_page, update_timezone}, account::{delete_account, set_api_key, show_account_page, update_timezone},
admin_panel::{show_admin_panel, toggle_registration_status}, admin_panel::{show_admin_panel, toggle_registration_status},
content::{patch_text_content, show_content_page, show_text_content_edit_form},
documentation::{ documentation::{
show_documentation_index, show_get_started, show_mobile_friendly, show_documentation_index, show_get_started, show_mobile_friendly,
show_privacy_policy, show_privacy_policy,
@@ -38,7 +39,8 @@ use zettle_db::{
ingress_form::{hide_ingress_form, process_ingress_form, show_ingress_form}, ingress_form::{hide_ingress_form, process_ingress_form, show_ingress_form},
knowledge::{ knowledge::{
delete_knowledge_entity, delete_knowledge_relationship, patch_knowledge_entity, delete_knowledge_entity, delete_knowledge_relationship, patch_knowledge_entity,
show_edit_knowledge_entity_form, show_knowledge_page, save_knowledge_relationship, show_edit_knowledge_entity_form,
show_knowledge_page,
}, },
search_result::search_result_handler, search_result::search_result_handler,
signin::{authenticate_user, show_signin_form}, signin::{authenticate_user, show_signin_form},
@@ -177,6 +179,11 @@ fn html_routes(
.route("/text-content/:id", delete(delete_text_content)) .route("/text-content/:id", delete(delete_text_content))
.route("/jobs/:job_id", delete(delete_job)) .route("/jobs/:job_id", delete(delete_job))
.route("/active-jobs", get(show_active_jobs)) .route("/active-jobs", get(show_active_jobs))
.route("/content", get(show_content_page))
.route(
"/content/:id",
get(show_text_content_edit_form).patch(patch_text_content),
)
.route("/knowledge", get(show_knowledge_page)) .route("/knowledge", get(show_knowledge_page))
.route( .route(
"/knowledge-entity/:id", "/knowledge-entity/:id",
@@ -184,6 +191,7 @@ fn html_routes(
.delete(delete_knowledge_entity) .delete(delete_knowledge_entity)
.patch(patch_knowledge_entity), .patch(patch_knowledge_entity),
) )
.route("/knowledge-relationship", post(save_knowledge_relationship))
.route( .route(
"/knowledge-relationship/:id", "/knowledge-relationship/:id",
delete(delete_knowledge_relationship), delete(delete_knowledge_relationship),

View File

@@ -0,0 +1,108 @@
use axum::{
extract::{Path, State},
response::{IntoResponse, Redirect},
};
use axum_session_auth::AuthSession;
use axum_session_surreal::SessionSurrealPool;
use surrealdb::{engine::any::Any, Surreal};
use crate::{
error::HtmlError,
page_data,
server::AppState,
storage::types::{text_content::TextContent, user::User},
};
use super::render_template;
page_data!(ContentPageData, "content/base.html", {
user: User,
text_contents: Vec<TextContent>
});
pub async fn show_content_page(
State(state): State<AppState>,
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
) -> Result<impl IntoResponse, HtmlError> {
// Early return if the user is not authenticated
let user = match auth.current_user {
Some(user) => user,
None => return Ok(Redirect::to("/signin").into_response()),
};
let text_contents = User::get_text_contents(&user.id, &state.surreal_db_client)
.await
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
let output = render_template(
ContentPageData::template_name(),
ContentPageData {
user,
text_contents,
},
state.templates,
)?;
Ok(output.into_response())
}
#[derive(Serialize)]
pub struct TextContentEditModal {
pub user: User,
pub text_content: TextContent,
}
pub async fn show_text_content_edit_form(
State(state): State<AppState>,
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
Path(id): Path<String>,
) -> Result<impl IntoResponse, HtmlError> {
// Early return if the user is not authenticated
let user = match auth.current_user {
Some(user) => user,
None => return Ok(Redirect::to("/signin").into_response()),
};
let text_content = User::get_and_validate_text_content(&id, &user.id, &state.surreal_db_client)
.await
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
let output = render_template(
"content/edit_text_content_modal.html",
TextContentEditModal { user, text_content },
state.templates,
)?;
Ok(output.into_response())
}
pub async fn patch_text_content(
State(state): State<AppState>,
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
Path(id): Path<String>,
) -> Result<impl IntoResponse, HtmlError> {
// Early return if the user is not authenticated
let user = match auth.current_user {
Some(user) => user,
None => return Ok(Redirect::to("/signin").into_response()),
};
let text_content = User::get_and_validate_text_content(&id, &user.id, &state.surreal_db_client)
.await
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
let text_contents = User::get_text_contents(&user.id, &state.surreal_db_client)
.await
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
let output = render_template(
"content/content_list.html",
ContentPageData {
user,
text_contents,
},
state.templates,
)?;
Ok(output.into_response())
}

View File

@@ -281,6 +281,8 @@ pub async fn delete_knowledge_relationship(
None => return Ok(Redirect::to("/signin").into_response()), None => return Ok(Redirect::to("/signin").into_response()),
}; };
// GOTTA ADD AUTH VALIDATION
KnowledgeRelationship::delete_relationship_by_id(&id, &state.surreal_db_client) KnowledgeRelationship::delete_relationship_by_id(&id, &state.surreal_db_client)
.await .await
.map_err(|e| HtmlError::new(e, state.templates.clone()))?; .map_err(|e| HtmlError::new(e, state.templates.clone()))?;
@@ -305,3 +307,56 @@ pub async fn delete_knowledge_relationship(
Ok(output.into_response()) Ok(output.into_response())
} }
#[derive(Deserialize)]
pub struct SaveKnowledgeRelationshipInput {
pub in_: String,
pub out: String,
pub relationship_type: String,
}
pub async fn save_knowledge_relationship(
State(state): State<AppState>,
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
Form(form): Form<SaveKnowledgeRelationshipInput>,
) -> Result<impl IntoResponse, HtmlError> {
// Early return if the user is not authenticated
let user = match auth.current_user {
Some(user) => user,
None => return Ok(Redirect::to("/signin").into_response()),
};
// Construct relationship
let relationship = KnowledgeRelationship::new(
form.in_,
form.out,
user.id.clone(),
"manual".into(),
form.relationship_type,
);
relationship
.store_relationship(&state.surreal_db_client)
.await
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
let entities = User::get_knowledge_entities(&user.id, &state.surreal_db_client)
.await
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
let relationships = User::get_knowledge_relationships(&user.id, &state.surreal_db_client)
.await
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
// Render updated list
let output = render_template(
"knowledge/relationship_table.html",
RelationshipTableData {
entities,
relationships,
},
state.templates,
)?;
Ok(output.into_response())
}

View File

@@ -7,6 +7,7 @@ use crate::error::{HtmlError, IntoHtmlError};
pub mod account; pub mod account;
pub mod admin_panel; pub mod admin_panel;
pub mod content;
pub mod documentation; pub mod documentation;
pub mod gdpr; pub mod gdpr;
pub mod index; pub mod index;

View File

@@ -1,7 +1,6 @@
use crate::storage::types::file_info::deserialize_flexible_id; use crate::storage::types::file_info::deserialize_flexible_id;
use crate::{error::AppError, storage::db::SurrealDbClient}; use crate::{error::AppError, storage::db::SurrealDbClient};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use surrealdb::{engine::any::Any, Surreal};
use uuid::Uuid; use uuid::Uuid;
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
@@ -40,7 +39,7 @@ impl KnowledgeRelationship {
}, },
} }
} }
pub async fn store_relationship(&self, db_client: &Surreal<Any>) -> Result<(), AppError> { pub async fn store_relationship(&self, db_client: &SurrealDbClient) -> Result<(), AppError> {
let query = format!( let query = format!(
r#"RELATE knowledge_entity:`{}`->relates_to:`{}`->knowledge_entity:`{}` r#"RELATE knowledge_entity:`{}`->relates_to:`{}`->knowledge_entity:`{}`
SET SET

View File

@@ -239,6 +239,21 @@ impl User {
Ok(items) Ok(items)
} }
pub async fn get_text_contents(
user_id: &str,
db: &SurrealDbClient,
) -> Result<Vec<TextContent>, AppError> {
let items: Vec<TextContent> = db
.client
.query("SELECT * FROM type::table($table_name) WHERE user_id = $user_id ORDER BY created_at DESC")
.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( pub async fn get_latest_knowledge_entities(
user_id: &str, user_id: &str,
db: &SurrealDbClient, db: &SurrealDbClient,
@@ -286,6 +301,7 @@ impl User {
Ok(categories) Ok(categories)
} }
pub async fn get_and_validate_knowledge_entity( pub async fn get_and_validate_knowledge_entity(
id: &str, id: &str,
user_id: &str, user_id: &str,
@@ -301,4 +317,20 @@ impl User {
Ok(entity) Ok(entity)
} }
pub async fn get_and_validate_text_content(
id: &str,
user_id: &str,
db: &SurrealDbClient,
) -> Result<TextContent, AppError> {
let text_content: TextContent = get_item(db, &id)
.await?
.ok_or_else(|| AppError::NotFound("Content not found".into()))?;
if text_content.user_id != user_id {
return Err(AppError::Auth("Access denied".into()));
}
Ok(text_content)
}
} }

View File

@@ -0,0 +1,12 @@
{% extends 'body_base.html' %}
{% block main %}
<main class="flex justify-center grow mt-2 sm:mt-4 gap-6 mb-10">
<div class="container">
<h2 class="text-2xl font-bold mb-2">Text Contents</h2>
{% include "content/content_list.html" %}
</div>
</main>
{% endblock %}

View File

@@ -0,0 +1,40 @@
<div class="grid sm:grid-cols-2 lg:grid-cols-3 gap-4" id="text_content_cards">
{% for text_content in text_contents %}
<div class="card min-w-72 bg-base-100 shadow">
<div class="card-body">
<div class="flex items-center space-x-2">
<div class="flex-shrink-0">
{% if text_content.url %}
{% include "icons/globe_icon.html" %}
{% elif text_content.file_info %}
{% include "icons/document_icon.html" %}
{% else %}
{% include "icons/chat_icon.html" %}
{% endif %}
</div>
<h2 class="card-title truncate">
{{ text_content.text }}
</h2>
</div>
<div class="flex items-center">
<p class="text-xs opacity-60">
{{ text_content.created_at | datetimeformat(format="short", tz=user.timezone) }}
</p>
<div class="flex gap-2">
<button hx-get="/content/{{ text_content.id }}" hx-target="#modal" hx-swap="innerHTML"
class="btn btn-square btn-ghost btn-sm">
{% include "icons/edit_icon.html" %}
</button>
<button hx-delete="/content/{{ text_content.id }}" hx-target="#text_content_cards" hx-swap="outerHTML"
class="btn btn-square btn-ghost btn-sm">
{% include "icons/delete_icon.html" %}
</button>
</div>
</div>
<p class="mt-2">
{{ text_content.instructions }}
</p>
</div>
</div>
{% endfor %}
</div>

View File

@@ -0,0 +1,33 @@
{% extends "modal_base.html" %}
{% block form_attributes %}
hx-patch="/content/{{text_content.id}}"
hx-target="#text_content_cards"
hx-swap="outerHTML"
{% endblock %}
{% block modal_content %}
<h3 class="text-lg font-bold mb-4">Edit Content</h3>
<div class="form-control">
<label class="floating-label">
<span class="label-text">Content Name</span>
<input type="text" name="name" value="{{ text_content.text }}" class="w-full input input-bordered">
</label>
</div>
<input type="text" name="id" value="{{ text_content.id }}" class="hidden">
<div class="form-control mt-4 ">
<label class="floating-label">
<span class="label-text">Description</span>
<textarea name="description" class="textarea textarea-bordered h-32 w-full">{{ text_content.text}}</textarea>
</label>
</div>
{% endblock %}
{% block primary_actions %}
<button type="submit" class="btn btn-primary">
Save Changes
</button>
{% endblock %}

View File

@@ -1,5 +1,6 @@
<div class="flex gap-4 flex-col sm:flex-row"> <div class="flex gap-4 flex-col sm:flex-row">
<a class="btn btn-secondary" href="/knowledge" hx-boost="true">View Knowledge</a> <a class="btn btn-secondary" href="/knowledge" hx-boost="true">View Knowledge</a>
<a class="btn btn-accent" href="/content" hx-boost="true">View Content</a>
<button class="btn btn-primary" hx-get="/ingress-form" hx-target="#modal" hx-swap="innerHTML">Add <button class="btn btn-primary" hx-get="/ingress-form" hx-target="#modal" hx-swap="innerHTML">Add
Content</button> Content</button>
</div> </div>

View File

@@ -10,19 +10,19 @@ hx-swap="outerHTML"
<h3 class="text-lg font-bold mb-4">Edit Entity</h3> <h3 class="text-lg font-bold mb-4">Edit Entity</h3>
<div class="form-control"> <div class="form-control">
<label class="label"> <label class="floating-label">
<span class="label-text">Entity Name</span> <span class="label-text">Entity Name</span>
<input type="text" name="name" value="{{ entity.name }}" class="input input-bordered w-full">
</label> </label>
<input type="text" name="name" value="{{ entity.name }}" class="input input-bordered">
</div> </div>
<input type="text" name="id" value="{{ entity.id }}" class="hidden"> <input type="text" name="id" value="{{ entity.id }}" class="hidden">
<div class="form-control mt-4"> <div class="form-control mt-4">
<label class="label"> <label class="floating-label">
<span class="label-text">Description</span> <span class="label-text">Description</span>
<textarea name="description" class="w-full textarea textarea-bordered h-32">{{ entity.description }}</textarea>
</label> </label>
<textarea name="description" class="textarea textarea-bordered h-32">{{ entity.description }}</textarea>
</div> </div>
{% endblock %} {% endblock %}

View File

@@ -15,29 +15,22 @@
<!-- Origin column --> <!-- Origin column -->
<td> <td>
{% for entity in entities if entity.id == relationship.in %} {% for entity in entities if entity.id == relationship.in %}
<span class="cursor-pointer tooltip tooltip-info" data-tip="Click for more details" <span> {{ entity.name }}
hx-get="/knowledge-entity/{{entity.id}}" hx-trigger="click" hx-target="#entity_detail_modal"
hx-swap="innerHTML">
{{ entity.name }}
</span> </span>
{% else %} {% else %}
{{ relationship.in }} {{ relationship.in }}
{% endfor %} {% endfor %}
</td> </td>
<!-- Target column --> <!-- Target column -->
<td> <td>
{% for entity in entities if entity.id == relationship.out %} {% for entity in entities if entity.id == relationship.out %}
<span class="cursor-pointer tooltip tooltip-info" data-tip="Click for more details" <span>
hx-get="/knowledge-entity/{{entity.id}}" hx-trigger="click" hx-target="#entity_detail_modal"
hx-swap="innerHTML">
{{ entity.name }} {{ entity.name }}
</span> </span>
{% else %} {% else %}
{{ relationship.out }} {{ relationship.out }}
{% endfor %} {% endfor %}
</td> </td>
<td>{{ relationship.metadata.relationship_type }}</td> <td>{{ relationship.metadata.relationship_type }}</td>
<td> <td>
<button class="btn btn-sm btn-outline" hx-delete="/knowledge-relationship/{{ relationship.id }}" <button class="btn btn-sm btn-outline" hx-delete="/knowledge-relationship/{{ relationship.id }}"
@@ -50,15 +43,17 @@
<!-- New linking row --> <!-- New linking row -->
<tr id="new_relationship"> <tr id="new_relationship">
<td> <td>
<select name="origin_id" class="select select-bordered w-full new_relationship_input"> <select name="in_" class="select select-bordered w-full new_relationship_input">
<option disabled selected>Select Origin</option> <option disabled selected>Select Origin</option>
{% for entity in entities %} {% for entity in entities %}
<option value="{{ entity.id }}">{{ entity.name }}</option> <option value="{{ entity.id }}">
{{ entity.name }}
</option>
{% endfor %} {% endfor %}
</select> </select>
</td> </td>
<td> <td>
<select name="target_id" class="select select-bordered w-full new_relationship_input"> <select name="out" class="select select-bordered w-full new_relationship_input">
<option disabled selected>Select Target</option> <option disabled selected>Select Target</option>
{% for entity in entities %} {% for entity in entities %}
<option value="{{ entity.id }}">{{ entity.name }}</option> <option value="{{ entity.id }}">{{ entity.name }}</option>
@@ -66,12 +61,13 @@
</select> </select>
</td> </td>
<td> <td>
<input name="relationship_type" type="text" placeholder="RelatedTo" <input id="relationship_type_input" name="relationship_type" type="text" placeholder="RelatedTo"
class="input input-bordered w-full new_relationship_input" /> class="input input-bordered w-full new_relationship_input" />
</td> </td>
<td> <td>
<button type="button" class="btn btn-primary btn-sm" hx-post="/relationship/create" <button id="save_relationship_button" type="button" class="btn btn-primary btn-sm"
hx-target="#relationship_table" hx-swap="outerHTML" hx-include=".new_relationship_input"> hx-post="/knowledge-relationship" hx-target="#relationship_table_section" hx-swap="outerHTML"
hx-include=".new_relationship_input">
Save Save
</button> </button>
</td> </td>
@@ -79,8 +75,11 @@
</tbody> </tbody>
</table> </table>
</div> </div>
<script>
document.getElementById('relationship_type_input').addEventListener('keydown', function (event) {
<!-- Modal containers for dynamic content --> if (event.key === 'Enter') {
<div id="entity_detail_modal" class="mt-4"></div> event.preventDefault(); // Prevent form submission if within a form
<div id="modal_container"></div> document.getElementById('save_relationship_button').click();
}
});
</script>

View File

@@ -18,7 +18,6 @@
</div> </div>
</form> </form>
</div> </div>
<script> <script>
// Auto-open modal when injected // Auto-open modal when injected
document.getElementById('body_modal').showModal(); document.getElementById('body_modal').showModal();
@@ -29,5 +28,10 @@
document.getElementById('body_modal').close(); document.getElementById('body_modal').close();
} }
}); });
// Clear modal content on close to prevent browser back from reopening it
document.getElementById('body_modal').addEventListener('close', (evt) => {
evt.target.innerHTML = '';
});
</script> </script>
</dialog> </dialog>

View File

@@ -1,10 +1,8 @@
\[\] admin controls re registration
\[\] archive ingressed webpage \[\] archive ingressed webpage
\[\] configs primarily get envs \[\] configs primarily get envs
\[\] view content \[\] on updates of knowledgeentity create new embeddings
\[\] view graph map
\[\] view latest
\[x\] add user_id to ingress objects \[x\] add user_id to ingress objects
\[x\] admin controls re registration
\[x\] gdpr \[x\] gdpr
\[x\] html ingression \[x\] html ingression
\[x\] hx-redirect \[x\] hx-redirect
@@ -16,3 +14,6 @@
\[x\] smoothie_dom test \[x\] smoothie_dom test
\[x\] templating \[x\] templating
\[x\] user id to fileinfo and data path? \[x\] user id to fileinfo and data path?
\[x\] view content
\[x\] view graph map
\[x\] view latest