add: journal entry context

This commit is contained in:
trisua 2025-03-24 20:23:52 -04:00
parent e87ad74d43
commit 1a6b48078a
5 changed files with 57 additions and 3 deletions

View file

@ -4,7 +4,7 @@ use tetratto_core::model::{ApiReturn, Error, journal::JournalEntry};
use crate::{ use crate::{
State, get_user_from_token, State, get_user_from_token,
routes::api::v1::{CreateJournalEntry, UpdateJournalEntryContent}, routes::api::v1::{CreateJournalEntry, UpdateJournalEntryContent, UpdateJournalEntryContext},
}; };
pub async fn create_request( pub async fn create_request(
@ -73,3 +73,25 @@ pub async fn update_content_request(
Err(e) => return Json(e.into()), Err(e) => return Json(e.into()),
} }
} }
pub async fn update_context_request(
jar: CookieJar,
Extension(data): Extension<State>,
Path(id): Path<usize>,
Json(req): Json<UpdateJournalEntryContext>,
) -> impl IntoResponse {
let data = &(data.read().await).0;
let user = match get_user_from_token!(jar, data) {
Some(ua) => ua,
None => return Json(Error::NotAllowed.into()),
};
match data.update_entry_context(id, user, req.context).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "Entry updated".to_string(),
payload: (),
}),
Err(e) => return Json(e.into()),
}
}

View file

@ -6,7 +6,9 @@ use axum::{
routing::{delete, get, post}, routing::{delete, get, post},
}; };
use serde::Deserialize; use serde::Deserialize;
use tetratto_core::model::journal::{JournalPageReadAccess, JournalPageWriteAccess}; use tetratto_core::model::journal::{
JournalEntryContext, JournalPageReadAccess, JournalPageWriteAccess,
};
pub fn routes() -> Router { pub fn routes() -> Router {
Router::new() Router::new()
@ -36,6 +38,10 @@ pub fn routes() -> Router {
"/entries/{id}/content", "/entries/{id}/content",
post(journal::entries::update_content_request), post(journal::entries::update_content_request),
) )
.route(
"/entries/{id}/context",
post(journal::entries::update_context_request),
)
// auth // auth
// global // global
.route("/auth/register", post(auth::register_request)) .route("/auth/register", post(auth::register_request))
@ -102,3 +108,8 @@ pub struct CreateJournalEntry {
pub struct UpdateJournalEntryContent { pub struct UpdateJournalEntryContent {
pub content: String, pub content: String,
} }
#[derive(Deserialize)]
pub struct UpdateJournalEntryContext {
pub context: JournalEntryContext,
}

View file

@ -3,5 +3,6 @@ CREATE TABLE IF NOT EXISTS entries (
created INTEGER NOT NULL, created INTEGER NOT NULL,
content TEXT NOT NULL, content TEXT NOT NULL,
owner INTEGER NOT NULL, owner INTEGER NOT NULL,
journal INTEGER NOT NULL journal INTEGER NOT NULL,
context TEXT NOT NULL
) )

View file

@ -1,5 +1,6 @@
use super::*; use super::*;
use crate::cache::Cache; use crate::cache::Cache;
use crate::model::journal::JournalEntryContext;
use crate::model::{ use crate::model::{
Error, Result, auth::User, journal::JournalEntry, journal::JournalPageWriteAccess, Error, Result, auth::User, journal::JournalEntry, journal::JournalPageWriteAccess,
permissions::FinePermission, permissions::FinePermission,
@ -24,6 +25,7 @@ impl DataManager {
content: get!(x->2(String)), content: get!(x->2(String)),
owner: get!(x->3(u64)) as usize, owner: get!(x->3(u64)) as usize,
journal: get!(x->4(u64)) as usize, journal: get!(x->4(u64)) as usize,
context: serde_json::from_str(&get!(x->5(String))).unwrap(),
} }
} }
@ -79,6 +81,7 @@ impl DataManager {
&data.content.as_str(), &data.content.as_str(),
&data.owner.to_string().as_str(), &data.owner.to_string().as_str(),
&data.journal.to_string().as_str(), &data.journal.to_string().as_str(),
&serde_json::to_string(&data.context).unwrap().as_str(),
] ]
); );
@ -91,4 +94,5 @@ impl DataManager {
auto_method!(delete_entry()@get_entry_by_id:MANAGE_JOURNAL_ENTRIES -> "DELETE FROM entries WHERE id = $1" --cache-key-tmpl="atto.entry:{}"); auto_method!(delete_entry()@get_entry_by_id:MANAGE_JOURNAL_ENTRIES -> "DELETE FROM entries WHERE id = $1" --cache-key-tmpl="atto.entry:{}");
auto_method!(update_entry_content(String)@get_entry_by_id:MANAGE_JOURNAL_ENTRIES -> "UPDATE entries SET content = $1 WHERE id = $2" --cache-key-tmpl="atto.entry:{}"); auto_method!(update_entry_content(String)@get_entry_by_id:MANAGE_JOURNAL_ENTRIES -> "UPDATE entries SET content = $1 WHERE id = $2" --cache-key-tmpl="atto.entry:{}");
auto_method!(update_entry_context(JournalEntryContext)@get_entry_by_id:MANAGE_JOURNAL_ENTRIES -> "UPDATE entries SET context = $1 WHERE id = $2" --serde --cache-key-tmpl="atto.entry:{}");
} }

View file

@ -100,6 +100,19 @@ impl JournalPageMembership {
} }
} }
#[derive(Serialize, Deserialize)]
pub struct JournalEntryContext {
pub comments_enabled: bool,
}
impl Default for JournalEntryContext {
fn default() -> Self {
Self {
comments_enabled: true,
}
}
}
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct JournalEntry { pub struct JournalEntry {
pub id: usize, pub id: usize,
@ -109,6 +122,8 @@ pub struct JournalEntry {
pub owner: usize, pub owner: usize,
/// The ID of the [`JournalPage`] this entry belongs to. /// The ID of the [`JournalPage`] this entry belongs to.
pub journal: usize, pub journal: usize,
/// Extra information about the journal entry.
pub context: JournalEntryContext,
} }
impl JournalEntry { impl JournalEntry {
@ -123,6 +138,7 @@ impl JournalEntry {
content, content,
owner, owner,
journal, journal,
context: JournalEntryContext::default(),
} }
} }
} }