add: global notes

This commit is contained in:
trisua 2025-06-26 02:56:22 -04:00
parent 59581f69c9
commit 2cd04b0db0
24 changed files with 371 additions and 608 deletions

View file

@ -17,10 +17,33 @@ impl DataManager {
edited: get!(x->6(i64)) as usize,
dir: get!(x->7(i64)) as usize,
tags: serde_json::from_str(&get!(x->8(String))).unwrap(),
is_global: get!(x->9(i32)) as i8 == 1,
}
}
auto_method!(get_note_by_id(usize as i64)@get_note_from_row -> "SELECT * FROM notes WHERE id = $1" --name="note" --returns=Note --cache-key-tmpl="atto.note:{}");
auto_method!(get_global_note_by_title(&str)@get_note_from_row -> "SELECT * FROM notes WHERE title = $1 AND is_global = 1" --name="note" --returns=Note --cache-key-tmpl="atto.note:{}");
/// Get the number of global notes a user has.
pub async fn get_user_global_notes_count(&self, owner: usize) -> Result<i32> {
let conn = match self.0.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = query_row!(
&conn,
"SELECT COUNT(*)::int FROM notes WHERE owner = $1 AND is_global = 1",
&[&(owner as i64)],
|x| Ok(x.get::<usize, i32>(0))
);
if let Err(e) = res {
return Err(Error::DatabaseError(e.to_string()));
}
Ok(res.unwrap())
}
/// Get a note by `journal` and `title`.
pub async fn get_note_by_journal_title(&self, journal: usize, title: &str) -> Result<Note> {
@ -94,6 +117,9 @@ impl DataManager {
const MAXIMUM_FREE_NOTES_PER_JOURNAL: usize = 10;
pub const MAXIMUM_FREE_GLOBAL_NOTES: usize = 10;
pub const MAXIMUM_SUPPORTER_GLOBAL_NOTES: usize = 50;
/// Create a new note in the database.
///
/// # Arguments
@ -164,7 +190,7 @@ impl DataManager {
let res = execute!(
&conn,
"INSERT INTO notes VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)",
"INSERT INTO notes VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)",
params![
&(data.id as i64),
&(data.created as i64),
@ -175,6 +201,7 @@ impl DataManager {
&(data.edited as i64),
&(data.dir as i64),
&serde_json::to_string(&data.tags).unwrap(),
&if data.is_global { 1 } else { 0 }
]
);
@ -206,7 +233,7 @@ impl DataManager {
}
// ...
self.0.1.remove(format!("atto.note:{}", id)).await;
self.cache_clear_note(&note).await;
Ok(())
}
@ -246,9 +273,26 @@ impl DataManager {
Ok(())
}
auto_method!(update_note_title(&str)@get_note_by_id:MANAGE_NOTES -> "UPDATE notes SET title = $1 WHERE id = $2" --cache-key-tmpl="atto.note:{}");
auto_method!(update_note_content(&str)@get_note_by_id:MANAGE_NOTES -> "UPDATE notes SET content = $1 WHERE id = $2" --cache-key-tmpl="atto.note:{}");
auto_method!(update_note_dir(i64)@get_note_by_id:MANAGE_NOTES -> "UPDATE notes SET dir = $1 WHERE id = $2" --cache-key-tmpl="atto.note:{}");
auto_method!(update_note_tags(Vec<String>)@get_note_by_id:MANAGE_NOTES -> "UPDATE notes SET tags = $1 WHERE id = $2" --serde --cache-key-tmpl="atto.note:{}");
auto_method!(update_note_edited(i64) -> "UPDATE notes SET edited = $1 WHERE id = $2" --cache-key-tmpl="atto.note:{}");
/// Incremenet note views. Views are only stored in the cache.
///
/// This should only be done for global notes.
pub async fn incr_note_views(&self, id: usize) {
self.0.1.incr(format!("atto.note:{id}/views")).await;
}
pub async fn get_note_views(&self, id: usize) -> Option<String> {
self.0.1.get(format!("atto.note:{id}/views")).await
}
pub async fn cache_clear_note(&self, x: &Note) {
self.0.1.remove(format!("atto.note:{}", x.id)).await;
self.0.1.remove(format!("atto.note:{}", x.title)).await;
}
auto_method!(update_note_title(&str)@get_note_by_id:MANAGE_NOTES -> "UPDATE notes SET title = $1 WHERE id = $2" --cache-key-tmpl=cache_clear_note);
auto_method!(update_note_content(&str)@get_note_by_id:MANAGE_NOTES -> "UPDATE notes SET content = $1 WHERE id = $2" --cache-key-tmpl=cache_clear_note);
auto_method!(update_note_dir(i64)@get_note_by_id:MANAGE_NOTES -> "UPDATE notes SET dir = $1 WHERE id = $2" --cache-key-tmpl=cache_clear_note);
auto_method!(update_note_tags(Vec<String>)@get_note_by_id:MANAGE_NOTES -> "UPDATE notes SET tags = $1 WHERE id = $2" --serde --cache-key-tmpl=cache_clear_note);
auto_method!(update_note_edited(i64)@get_note_by_id -> "UPDATE notes SET edited = $1 WHERE id = $2" --cache-key-tmpl=cache_clear_note);
auto_method!(update_note_is_global(i32)@get_note_by_id -> "UPDATE notes SET is_global = $1 WHERE id = $2" --cache-key-tmpl=cache_clear_note);
}