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

@ -6,7 +6,7 @@ use axum::{
use axum_extra::extract::CookieJar;
use crate::{
get_user_from_token,
routes::api::v1::{UpdateJournalView, CreateJournal, UpdateJournalTitle},
routes::api::v1::{UpdateJournalPrivacy, CreateJournal, UpdateJournalTitle},
State,
};
use tetratto_core::model::{
@ -81,7 +81,7 @@ pub async fn create_request(
Ok(x) => Json(ApiReturn {
ok: true,
message: "Journal created".to_string(),
payload: Some(x),
payload: Some(x.id.to_string()),
}),
Err(e) => Json(e.into()),
}
@ -91,7 +91,7 @@ pub async fn update_title_request(
jar: CookieJar,
Path(id): Path<usize>,
Extension(data): Extension<State>,
Json(props): Json<UpdateJournalTitle>,
Json(mut props): Json<UpdateJournalTitle>,
) -> impl IntoResponse {
let data = &(data.read().await).0;
let user = match get_user_from_token!(jar, data, oauth::AppScope::UserManageJournals) {
@ -99,6 +99,18 @@ pub async fn update_title_request(
None => return Json(Error::NotAllowed.into()),
};
props.title = props.title.replace(" ", "_");
// make sure this title isn't already in use
if data
.get_journal_by_owner_title(user.id, &props.title)
.await
.is_ok()
{
return Json(Error::TitleInUse.into());
}
// ...
match data.update_journal_title(id, &user, &props.title).await {
Ok(_) => Json(ApiReturn {
ok: true,
@ -113,7 +125,7 @@ pub async fn update_privacy_request(
jar: CookieJar,
Path(id): Path<usize>,
Extension(data): Extension<State>,
Json(props): Json<UpdateJournalView>,
Json(props): Json<UpdateJournalPrivacy>,
) -> impl IntoResponse {
let data = &(data.read().await).0;
let user = match get_user_from_token!(jar, data, oauth::AppScope::UserManageJournals) {
@ -121,7 +133,7 @@ pub async fn update_privacy_request(
None => return Json(Error::NotAllowed.into()),
};
match data.update_journal_privacy(id, &user, props.view).await {
match data.update_journal_privacy(id, &user, props.privacy).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "Journal updated".to_string(),

View file

@ -563,6 +563,7 @@ pub fn routes() -> Router {
.route("/notes/{id}/title", post(notes::update_title_request))
.route("/notes/{id}/content", post(notes::update_content_request))
.route("/notes/from_journal/{id}", get(notes::list_request))
.route("/notes/preview", post(notes::render_markdown_request))
// uploads
.route("/uploads/{id}", get(uploads::get_request))
.route("/uploads/{id}", delete(uploads::delete_request))
@ -887,8 +888,8 @@ pub struct UpdateJournalTitle {
}
#[derive(Deserialize)]
pub struct UpdateJournalView {
pub view: JournalPrivacyPermission,
pub struct UpdateJournalPrivacy {
pub privacy: JournalPrivacyPermission,
}
#[derive(Deserialize)]
@ -900,3 +901,8 @@ pub struct UpdateNoteTitle {
pub struct UpdateNoteContent {
pub content: String,
}
#[derive(Deserialize)]
pub struct RenderMarkdown {
pub content: String,
}

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@", "@")
}

View file

@ -12,6 +12,7 @@ serve_asset!(favicon_request: FAVICON("image/svg+xml"));
serve_asset!(style_css_request: STYLE_CSS("text/css"));
serve_asset!(root_css_request: ROOT_CSS("text/css"));
serve_asset!(utility_css_request: UTILITY_CSS("text/css"));
serve_asset!(chats_css_request: CHATS_CSS("text/css"));
serve_asset!(loader_js_request: LOADER_JS("text/javascript"));
serve_asset!(atto_js_request: ATTO_JS("text/javascript"));

View file

@ -14,6 +14,7 @@ pub fn routes(config: &Config) -> Router {
.route("/css/style.css", get(assets::style_css_request))
.route("/css/root.css", get(assets::root_css_request))
.route("/css/utility.css", get(assets::utility_css_request))
.route("/css/chats.css", get(assets::chats_css_request))
.route("/js/loader.js", get(assets::loader_js_request))
.route("/js/atto.js", get(assets::atto_js_request))
.route("/js/me.js", get(assets::me_js_request))

View file

@ -0,0 +1,209 @@
use axum::{
extract::{Path, Query},
response::{Html, IntoResponse, Redirect},
Extension,
};
use axum_extra::extract::CookieJar;
use crate::{
assets::initial_context,
check_user_blocked_or_private, get_lang, get_user_from_token,
routes::pages::{render_error, JournalsAppQuery},
State,
};
use tetratto_core::model::{journals::JournalPrivacyPermission, Error};
pub async fn redirect_request() -> impl IntoResponse {
Redirect::to("/journals/0/0")
}
/// `/journals/{journal}/{note}`
pub async fn app_request(
jar: CookieJar,
Extension(data): Extension<State>,
Path((selected_journal, selected_note)): Path<(usize, usize)>,
Query(props): Query<JournalsAppQuery>,
) -> impl IntoResponse {
let data = data.read().await;
let user = match get_user_from_token!(jar, data.0) {
Some(ua) => ua,
None => {
return Err(Html(
render_error(Error::NotAllowed, &jar, &data, &None).await,
));
}
};
let journals = match data.0.get_journals_by_user(user.id).await {
Ok(p) => Some(p),
Err(e) => {
return Err(Html(
render_error(e, &jar, &data, &Some(user.to_owned())).await,
));
}
};
let notes = match data.0.get_notes_by_journal(selected_journal).await {
Ok(p) => Some(p),
Err(e) => {
return Err(Html(render_error(e, &jar, &data, &Some(user)).await));
}
};
// get journal and check privacy settings
let journal = if selected_journal != 0 {
match data.0.get_journal_by_id(selected_journal).await {
Ok(p) => Some(p),
Err(e) => {
return Err(Html(render_error(e, &jar, &data, &Some(user)).await));
}
}
} else {
None
};
if let Some(ref j) = journal {
// if we're not the owner, we shouldn't be viewing this journal from this endpoint
if user.id != j.owner {
return Err(Html(
render_error(Error::NotAllowed, &jar, &data, &Some(user.to_owned())).await,
));
}
}
// ...
let note = if selected_note != 0 {
match data.0.get_note_by_id(selected_note).await {
Ok(p) => Some(p),
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
}
} else {
None
};
let lang = get_lang!(jar, data.0);
let mut context = initial_context(&data.0.0.0, lang, &Some(user)).await;
context.insert("selected_journal", &selected_journal);
context.insert("selected_note", &selected_note);
context.insert("journal", &journal);
context.insert("note", &note);
context.insert("journals", &journals);
context.insert("notes", &notes);
context.insert("view_mode", &props.view);
context.insert("is_editor", &true);
// return
Ok(Html(data.1.render("journals/app.html", &context).unwrap()))
}
/// `/@{owner}/{journal}/{note}`
pub async fn view_request(
jar: CookieJar,
Extension(data): Extension<State>,
Path((owner, selected_journal, mut selected_note)): Path<(String, String, String)>,
) -> impl IntoResponse {
let data = data.read().await;
let user = match get_user_from_token!(jar, data.0) {
Some(ua) => Some(ua),
None => None,
};
if selected_note == "index" {
selected_note = String::new();
}
// if we don't have a selected journal, we shouldn't be here probably
if selected_journal.is_empty() {
return Err(Html(
render_error(Error::NotAllowed, &jar, &data, &user).await,
));
}
// get owner
let owner = match data.0.get_user_by_username(&owner).await {
Ok(ua) => ua,
Err(e) => {
return Err(Html(render_error(e, &jar, &data, &user).await));
}
};
check_user_blocked_or_private!(user, owner, data, jar);
// get journal and check privacy settings
let journal = match data
.0
.get_journal_by_owner_title(owner.id, &selected_journal)
.await
{
Ok(p) => p,
Err(e) => {
return Err(Html(render_error(e, &jar, &data, &user).await));
}
};
if journal.privacy == JournalPrivacyPermission::Private {
if let Some(ref user) = user {
if user.id != journal.owner {
return Err(Html(
render_error(Error::NotAllowed, &jar, &data, &Some(user.to_owned())).await,
));
}
} else {
return Err(Html(
render_error(Error::NotAllowed, &jar, &data, &user).await,
));
}
}
// ...
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
.0
.get_note_by_journal_title(journal.id, &selected_note)
.await
{
Ok(p) => Some(p),
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
}
} else {
None
};
let lang = get_lang!(jar, data.0);
let mut context = initial_context(&data.0.0.0, lang, &user).await;
if selected_journal.is_empty() {
context.insert("selected_journal", &0);
} else {
context.insert("selected_journal", &selected_journal);
}
if selected_note.is_empty() {
context.insert("selected_note", &0);
} else {
context.insert("selected_note", &selected_note);
}
context.insert("journal", &journal);
context.insert("note", &note);
context.insert("owner", &owner);
context.insert("notes", &notes);
context.insert("view_mode", &true);
context.insert("is_editor", &false);
// return
Ok(Html(data.1.render("journals/app.html", &context).unwrap()))
}

View file

@ -3,6 +3,7 @@ pub mod chats;
pub mod communities;
pub mod developer;
pub mod forge;
pub mod journals;
pub mod misc;
pub mod mod_panel;
pub mod profile;
@ -130,6 +131,11 @@ pub fn routes() -> Router {
.route("/stacks", get(stacks::list_request))
.route("/stacks/{id}", get(stacks::feed_request))
.route("/stacks/{id}/manage", get(stacks::manage_request))
// journals
.route("/journals", get(journals::redirect_request))
.route("/journals/{journal}/{note}", get(journals::app_request))
.route("/@{owner}/{journal}", get(journals::view_request))
.route("/@{owner}/{journal}/{note}", get(journals::view_request))
}
pub async fn render_error(
@ -185,3 +191,9 @@ pub struct RepostsQuery {
#[serde(default)]
pub page: usize,
}
#[derive(Deserialize)]
pub struct JournalsAppQuery {
#[serde(default)]
pub view: bool,
}