add: journals + notes

This commit is contained in:
trisua 2025-06-19 15:48:04 -04:00
parent c08a26ae8d
commit c1568ad866
26 changed files with 1431 additions and 265 deletions

View file

@ -4,15 +4,17 @@ use axum::{
Extension,
};
use axum_extra::extract::CookieJar;
use tetratto_shared::unix_epoch_timestamp;
use crate::{
get_user_from_token,
routes::api::v1::{CreateNote, UpdateNoteContent, UpdateNoteTitle},
routes::api::v1::{CreateNote, RenderMarkdown, UpdateNoteContent, UpdateNoteTitle},
State,
};
use tetratto_core::model::{
journals::{JournalPrivacyPermission, Note},
oauth,
permissions::FinePermission,
uploads::CustomEmoji,
ApiReturn, Error,
};
@ -110,7 +112,7 @@ pub async fn create_request(
Ok(x) => Json(ApiReturn {
ok: true,
message: "Note created".to_string(),
payload: Some(x),
payload: Some(x.id.to_string()),
}),
Err(e) => Json(e.into()),
}
@ -120,7 +122,7 @@ pub async fn update_title_request(
jar: CookieJar,
Path(id): Path<usize>,
Extension(data): Extension<State>,
Json(props): Json<UpdateNoteTitle>,
Json(mut props): Json<UpdateNoteTitle>,
) -> impl IntoResponse {
let data = &(data.read().await).0;
let user = match get_user_from_token!(jar, data, oauth::AppScope::UserManageNotes) {
@ -128,6 +130,23 @@ pub async fn update_title_request(
None => return Json(Error::NotAllowed.into()),
};
let note = match data.get_note_by_id(id).await {
Ok(n) => n,
Err(e) => return Json(e.into()),
};
props.title = props.title.replace(" ", "_");
// make sure this title isn't already in use
if data
.get_note_by_journal_title(note.journal, &props.title)
.await
.is_ok()
{
return Json(Error::TitleInUse.into());
}
// ...
match data.update_note_title(id, &user, &props.title).await {
Ok(_) => Json(ApiReturn {
ok: true,
@ -151,11 +170,20 @@ pub async fn update_content_request(
};
match data.update_note_content(id, &user, &props.content).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "Note updated".to_string(),
payload: (),
}),
Ok(_) => {
if let Err(e) = data
.update_note_edited(id, unix_epoch_timestamp() as i64)
.await
{
return Json(e.into());
}
Json(ApiReturn {
ok: true,
message: "Note updated".to_string(),
payload: (),
})
}
Err(e) => Json(e.into()),
}
}
@ -180,3 +208,9 @@ pub async fn delete_request(
Err(e) => Json(e.into()),
}
}
pub async fn render_markdown_request(Json(req): Json<RenderMarkdown>) -> impl IntoResponse {
tetratto_shared::markdown::render_markdown(&CustomEmoji::replace(&req.content))
.replace("\\@", "@")
.replace("%5C@", "@")
}