add: journal note tags and directories

This commit is contained in:
trisua 2025-06-21 19:44:28 -04:00
parent a37312fecf
commit af6fbdf04e
16 changed files with 722 additions and 78 deletions

View file

@ -4,9 +4,12 @@ use axum::{
Extension,
};
use axum_extra::extract::CookieJar;
use tetratto_shared::snow::Snowflake;
use crate::{
get_user_from_token,
routes::api::v1::{UpdateJournalPrivacy, CreateJournal, UpdateJournalTitle},
routes::api::v1::{
AddJournalDir, CreateJournal, RemoveJournalDir, UpdateJournalPrivacy, UpdateJournalTitle,
},
State,
};
use tetratto_core::{
@ -198,3 +201,86 @@ pub async fn delete_request(
Err(e) => Json(e.into()),
}
}
pub async fn add_dir_request(
jar: CookieJar,
Path(id): Path<usize>,
Extension(data): Extension<State>,
Json(props): Json<AddJournalDir>,
) -> impl IntoResponse {
let data = &(data.read().await).0;
let user = match get_user_from_token!(jar, data, oauth::AppScope::UserManageNotes) {
Some(ua) => ua,
None => return Json(Error::NotAllowed.into()),
};
if props.name.len() > 32 {
return Json(Error::DataTooLong("name".to_string()).into());
}
let mut journal = match data.get_journal_by_id(id).await {
Ok(x) => x,
Err(e) => return Json(e.into()),
};
// add dir
journal.dirs.push((
Snowflake::new().to_string().parse::<usize>().unwrap(),
match props.parent.parse() {
Ok(p) => p,
Err(_) => return Json(Error::Unknown.into()),
},
props.name,
));
// ...
match data.update_journal_dirs(id, &user, journal.dirs).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "Journal updated".to_string(),
payload: (),
}),
Err(e) => Json(e.into()),
}
}
pub async fn remove_dir_request(
jar: CookieJar,
Path(id): Path<usize>,
Extension(data): Extension<State>,
Json(props): Json<RemoveJournalDir>,
) -> impl IntoResponse {
let data = &(data.read().await).0;
let user = match get_user_from_token!(jar, data, oauth::AppScope::UserManageNotes) {
Some(ua) => ua,
None => return Json(Error::NotAllowed.into()),
};
let mut journal = match data.get_journal_by_id(id).await {
Ok(x) => x,
Err(e) => return Json(e.into()),
};
// add dir
let dir_id: usize = match props.dir.parse() {
Ok(x) => x,
Err(_) => return Json(Error::Unknown.into()),
};
journal
.dirs
.remove(match journal.dirs.iter().position(|x| x.0 == dir_id) {
Some(idx) => idx,
None => return Json(Error::GeneralNotFound("directory".to_string()).into()),
});
// ...
match data.update_journal_dirs(id, &user, journal.dirs).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "Journal updated".to_string(),
payload: (),
}),
Err(e) => Json(e.into()),
}
}

View file

@ -570,14 +570,22 @@ pub fn routes() -> Router {
"/journals/{id}/privacy",
post(journals::update_privacy_request),
)
.route("/journals/{id}/dirs", post(journals::add_dir_request))
.route("/journals/{id}/dirs", delete(journals::remove_dir_request))
// notes
.route("/notes", post(notes::create_request))
.route("/notes/{id}", get(notes::get_request))
.route("/notes/{id}", delete(notes::delete_request))
.route("/notes/{id}/title", post(notes::update_title_request))
.route("/notes/{id}/content", post(notes::update_content_request))
.route("/notes/{id}/dir", post(notes::update_dir_request))
.route("/notes/{id}/tags", post(notes::update_tags_request))
.route("/notes/from_journal/{id}", get(notes::list_request))
.route("/notes/preview", post(notes::render_markdown_request))
.route(
"/notes/{journal}/dir/{dir}",
delete(notes::delete_by_dir_request),
)
// uploads
.route("/uploads/{id}", get(uploads::get_request))
.route("/uploads/{id}", delete(uploads::delete_request))
@ -926,3 +934,24 @@ pub struct CreateMessageReaction {
pub message: String,
pub emoji: String,
}
#[derive(Deserialize)]
pub struct UpdateNoteDir {
pub dir: String,
}
#[derive(Deserialize)]
pub struct AddJournalDir {
pub name: String,
#[serde(default)]
pub parent: String,
}
#[derive(Deserialize)]
pub struct RemoveJournalDir {
pub dir: String,
}
#[derive(Deserialize)]
pub struct UpdateNoteTags {
pub tags: Vec<String>,
}

View file

@ -7,7 +7,10 @@ use axum_extra::extract::CookieJar;
use tetratto_shared::unix_epoch_timestamp;
use crate::{
get_user_from_token,
routes::api::v1::{CreateNote, RenderMarkdown, UpdateNoteContent, UpdateNoteTitle},
routes::api::v1::{
CreateNote, RenderMarkdown, UpdateNoteContent, UpdateNoteDir, UpdateNoteTags,
UpdateNoteTitle,
},
State,
};
use tetratto_core::{
@ -222,8 +225,96 @@ pub async fn delete_request(
}
}
pub async fn delete_by_dir_request(
jar: CookieJar,
Path((journal, id)): Path<(usize, usize)>,
Extension(data): Extension<State>,
) -> impl IntoResponse {
let data = &(data.read().await).0;
let user = match get_user_from_token!(jar, data, oauth::AppScope::UserManageNotes) {
Some(ua) => ua,
None => return Json(Error::NotAllowed.into()),
};
match data.delete_notes_by_journal_dir(journal, id, &user).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "Notes deleted".to_string(),
payload: (),
}),
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@", "@")
}
pub async fn update_dir_request(
jar: CookieJar,
Path(id): Path<usize>,
Extension(data): Extension<State>,
Json(props): Json<UpdateNoteDir>,
) -> impl IntoResponse {
let data = &(data.read().await).0;
let user = match get_user_from_token!(jar, data, oauth::AppScope::UserManageNotes) {
Some(ua) => ua,
None => return Json(Error::NotAllowed.into()),
};
let note = match data.get_note_by_id(id).await {
Ok(x) => x,
Err(e) => return Json(e.into()),
};
let journal = match data.get_journal_by_id(note.journal).await {
Ok(x) => x,
Err(e) => return Json(e.into()),
};
// make sure dir exists
let dir = match props.dir.parse::<usize>() {
Ok(d) => d,
Err(_) => return Json(Error::Unknown.into()),
};
if dir != 0 {
if journal.dirs.iter().find(|x| x.0 == dir).is_none() {
return Json(Error::GeneralNotFound("directory".to_string()).into());
}
}
// ...
match data.update_note_dir(id, &user, dir as i64).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "Note updated".to_string(),
payload: (),
}),
Err(e) => Json(e.into()),
}
}
pub async fn update_tags_request(
jar: CookieJar,
Path(id): Path<usize>,
Extension(data): Extension<State>,
Json(props): Json<UpdateNoteTags>,
) -> impl IntoResponse {
let data = &(data.read().await).0;
let user = match get_user_from_token!(jar, data, oauth::AppScope::UserManageNotes) {
Some(ua) => ua,
None => return Json(Error::NotAllowed.into()),
};
match data.update_note_tags(id, &user, props.tags).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "Note updated".to_string(),
payload: (),
}),
Err(e) => Json(e.into()),
}
}

View file

@ -158,14 +158,6 @@ pub async fn view_request(
}
}
// ...
let notes = match data.0.get_notes_by_journal(journal.id).await {
Ok(p) => Some(p),
Err(e) => {
return Err(Html(render_error(e, &jar, &data, &user).await));
}
};
// ...
let note = if !selected_note.is_empty() {
match data
@ -199,7 +191,7 @@ pub async fn view_request(
context.insert("note", &note);
context.insert("owner", &owner);
context.insert("notes", &notes);
context.insert::<[i8; 0], &str>("notes", &[]);
context.insert("view_mode", &true);
context.insert("is_editor", &false);
@ -213,6 +205,7 @@ pub async fn index_view_request(
jar: CookieJar,
Extension(data): Extension<State>,
Path((owner, selected_journal)): Path<(String, String)>,
Query(props): Query<JournalsAppQuery>,
) -> impl IntoResponse {
let data = data.read().await;
let user = match get_user_from_token!(jar, data.0) {
@ -257,10 +250,23 @@ pub async fn index_view_request(
}
// ...
let notes = match data.0.get_notes_by_journal(journal.id).await {
Ok(p) => Some(p),
Err(e) => {
return Err(Html(render_error(e, &jar, &data, &user).await));
let notes = if props.tag.is_empty() {
match data.0.get_notes_by_journal(journal.id).await {
Ok(p) => Some(p),
Err(e) => {
return Err(Html(render_error(e, &jar, &data, &user).await));
}
}
} else {
match data
.0
.get_notes_by_journal_tag(journal.id, &props.tag)
.await
{
Ok(p) => Some(p),
Err(e) => {
return Err(Html(render_error(e, &jar, &data, &user).await));
}
}
};
@ -281,6 +287,7 @@ pub async fn index_view_request(
context.insert("view_mode", &true);
context.insert("is_editor", &false);
context.insert("tag", &props.tag);
// return
Ok(Html(data.1.render("journals/app.html", &context).unwrap()))

View file

@ -196,4 +196,6 @@ pub struct RepostsQuery {
pub struct JournalsAppQuery {
#[serde(default)]
pub view: bool,
#[serde(default)]
pub tag: String,
}