2025-03-24 19:55:08 -04:00
use super ::* ;
use crate ::cache ::Cache ;
2025-03-25 21:19:55 -04:00
use crate ::model ::journal ::JournalPostContext ;
2025-03-24 20:19:12 -04:00
use crate ::model ::{
2025-03-25 21:19:55 -04:00
Error , Result , auth ::User , journal ::JournalPost , journal ::JournalWriteAccess ,
2025-03-24 20:19:12 -04:00
permissions ::FinePermission ,
} ;
2025-03-26 21:46:21 -04:00
use crate ::{ auto_method , execute , get , query_row , query_rows } ;
2025-03-24 19:55:08 -04:00
#[ cfg(feature = " sqlite " ) ]
use rusqlite ::Row ;
#[ cfg(feature = " postgres " ) ]
use tokio_postgres ::Row ;
impl DataManager {
/// Get a [`JournalEntry`] from an SQL row.
2025-03-25 21:19:55 -04:00
pub ( crate ) fn get_post_from_row (
2025-03-24 19:55:08 -04:00
#[ cfg(feature = " sqlite " ) ] x : & Row < '_ > ,
#[ cfg(feature = " postgres " ) ] x : & Row ,
2025-03-25 21:19:55 -04:00
) -> JournalPost {
JournalPost {
2025-03-25 18:18:33 -04:00
id : get ! ( x ->0 ( i64 ) ) as usize ,
created : get ! ( x ->1 ( i64 ) ) as usize ,
2025-03-24 19:55:08 -04:00
content : get ! ( x ->2 ( String ) ) ,
2025-03-25 18:18:33 -04:00
owner : get ! ( x ->3 ( i64 ) ) as usize ,
journal : get ! ( x ->4 ( i64 ) ) as usize ,
2025-03-24 20:23:52 -04:00
context : serde_json ::from_str ( & get! ( x ->5 ( String ) ) ) . unwrap ( ) ,
2025-03-25 22:52:47 -04:00
replying_to : if let Some ( id ) = get! ( x ->6 ( Option < i64 > ) ) {
Some ( id as usize )
} else {
None
} ,
2025-03-24 22:42:33 -04:00
// likes
2025-03-25 22:52:47 -04:00
likes : get ! ( x ->7 ( i64 ) ) as isize ,
dislikes : get ! ( x ->8 ( i64 ) ) as isize ,
// other counts
comment_count : get ! ( x ->9 ( i64 ) ) as usize ,
2025-03-24 19:55:08 -04:00
}
}
2025-03-25 22:52:47 -04:00
auto_method! ( get_post_by_id ( ) @ get_post_from_row -> " SELECT * FROM posts WHERE id = $1 " - - name = " post " - - returns = JournalPost - - cache - key - tmpl = " atto.post:{} " ) ;
/// Get all posts which are comments on the given post by ID.
///
/// # Arguments
/// * `id` - the ID of the post the requested posts are commenting on
/// * `batch` - the limit of posts in each page
/// * `page` - the page number
pub async fn get_post_comments (
& self ,
id : usize ,
batch : usize ,
page : usize ,
) -> Result < JournalPost > {
let conn = match self . connect ( ) . await {
Ok ( c ) = > c ,
Err ( e ) = > return Err ( Error ::DatabaseConnection ( e . to_string ( ) ) ) ,
} ;
let res = query_row! (
& conn ,
2025-03-26 21:46:21 -04:00
" SELECT * FROM posts WHERE replying_to = $1 ORDER BY created DESC LIMIT $2 OFFSET $3 " ,
2025-03-25 22:52:47 -04:00
& [ & ( id as i64 ) , & ( batch as i64 ) , & ( ( page * batch ) as i64 ) ] ,
| x | { Ok ( Self ::get_post_from_row ( x ) ) }
) ;
if res . is_err ( ) {
return Err ( Error ::GeneralNotFound ( " post " . to_string ( ) ) ) ;
}
Ok ( res . unwrap ( ) )
}
2025-03-24 19:55:08 -04:00
2025-03-26 21:46:21 -04:00
/// Get all posts from the given user (from most recent).
///
/// # Arguments
/// * `id` - the ID of the user the requested posts belong to
/// * `batch` - the limit of posts in each page
/// * `page` - the page number
pub async fn get_posts_by_user (
& self ,
id : usize ,
batch : usize ,
page : usize ,
) -> Result < Vec < JournalPost > > {
let conn = match self . connect ( ) . await {
Ok ( c ) = > c ,
Err ( e ) = > return Err ( Error ::DatabaseConnection ( e . to_string ( ) ) ) ,
} ;
let res = query_rows! (
& conn ,
" SELECT * FROM posts WHERE owner = $1 ORDER BY created DESC LIMIT $2 OFFSET $3 " ,
& [ & ( id as i64 ) , & ( batch as i64 ) , & ( ( page * batch ) as i64 ) ] ,
| x | { Self ::get_post_from_row ( x ) }
) ;
if res . is_err ( ) {
return Err ( Error ::GeneralNotFound ( " post " . to_string ( ) ) ) ;
}
Ok ( res . unwrap ( ) )
}
2025-03-24 19:55:08 -04:00
/// Create a new journal entry in the database.
///
/// # Arguments
/// * `data` - a mock [`JournalEntry`] object to insert
2025-03-25 22:52:47 -04:00
pub async fn create_post ( & self , data : JournalPost ) -> Result < ( ) > {
2025-03-24 19:55:08 -04:00
// check values
if data . content . len ( ) < 2 {
return Err ( Error ::DataTooShort ( " content " . to_string ( ) ) ) ;
} else if data . content . len ( ) > 4096 {
return Err ( Error ::DataTooLong ( " username " . to_string ( ) ) ) ;
}
2025-03-24 20:19:12 -04:00
// check permission in page
let page = match self . get_page_by_id ( data . journal ) . await {
Ok ( p ) = > p ,
Err ( e ) = > return Err ( e ) ,
} ;
match page . write_access {
2025-03-25 21:19:55 -04:00
JournalWriteAccess ::Owner = > {
2025-03-24 20:19:12 -04:00
if data . owner ! = page . owner {
return Err ( Error ::NotAllowed ) ;
}
}
2025-03-25 21:19:55 -04:00
JournalWriteAccess ::Joined = > {
2025-03-24 20:19:12 -04:00
if let Err ( _ ) = self
. get_membership_by_owner_journal ( data . owner , page . id )
. await
{
return Err ( Error ::NotAllowed ) ;
}
}
_ = > ( ) ,
} ;
2025-03-24 19:55:08 -04:00
// ...
let conn = match self . connect ( ) . await {
Ok ( c ) = > c ,
Err ( e ) = > return Err ( Error ::DatabaseConnection ( e . to_string ( ) ) ) ,
} ;
let res = execute! (
& conn ,
2025-03-25 22:52:47 -04:00
" INSERT INTO posts VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) " ,
2025-03-24 19:55:08 -04:00
& [
2025-03-25 22:52:47 -04:00
& Some ( data . id . to_string ( ) ) ,
& Some ( data . created . to_string ( ) ) ,
& Some ( data . content ) ,
& Some ( data . owner . to_string ( ) ) ,
& Some ( data . journal . to_string ( ) ) ,
& Some ( serde_json ::to_string ( & data . context ) . unwrap ( ) ) ,
& if let Some ( id ) = data . replying_to {
Some ( id . to_string ( ) )
} else {
None
} ,
& Some ( 0. to_string ( ) ) ,
& Some ( 0. to_string ( ) ) ,
& Some ( 0. to_string ( ) )
2025-03-24 19:55:08 -04:00
]
) ;
if let Err ( e ) = res {
return Err ( Error ::DatabaseError ( e . to_string ( ) ) ) ;
}
2025-03-25 22:52:47 -04:00
// incr comment count
if let Some ( id ) = data . replying_to {
self . incr_post_comments ( id ) . await . unwrap ( ) ;
}
// return
2025-03-24 19:55:08 -04:00
Ok ( ( ) )
}
2025-03-25 22:52:47 -04:00
auto_method! ( delete_post ( ) @ get_post_by_id :MANAGE_JOURNAL_ENTRIES -> " DELETE FROM posts WHERE id = $1 " - - cache - key - tmpl = " atto.post:{} " ) ;
auto_method! ( update_post_content ( String ) @ get_post_by_id :MANAGE_JOURNAL_ENTRIES -> " UPDATE posts SET content = $1 WHERE id = $2 " - - cache - key - tmpl = " atto.post:{} " ) ;
auto_method! ( update_post_context ( JournalPostContext ) @ get_post_by_id :MANAGE_JOURNAL_ENTRIES -> " UPDATE posts SET context = $1 WHERE id = $2 " - - serde - - cache - key - tmpl = " atto.post:{} " ) ;
auto_method! ( incr_post_likes ( ) -> " UPDATE posts SET likes = likes + 1 WHERE id = $1 " - - cache - key - tmpl = " atto.post:{} " - - incr ) ;
auto_method! ( incr_post_dislikes ( ) -> " UPDATE posts SET likes = dislikes + 1 WHERE id = $1 " - - cache - key - tmpl = " atto.post:{} " - - incr ) ;
auto_method! ( decr_post_likes ( ) -> " UPDATE posts SET likes = likes - 1 WHERE id = $1 " - - cache - key - tmpl = " atto.post:{} " - - decr ) ;
auto_method! ( decr_post_dislikes ( ) -> " UPDATE posts SET likes = dislikes - 1 WHERE id = $1 " - - cache - key - tmpl = " atto.post:{} " - - decr ) ;
2025-03-24 22:42:33 -04:00
2025-03-25 22:52:47 -04:00
auto_method! ( incr_post_comments ( ) -> " UPDATE posts SET comment_count = comment_count + 1 WHERE id = $1 " - - cache - key - tmpl = " atto.post:{} " - - incr ) ;
auto_method! ( decr_post_comments ( ) -> " UPDATE posts SET comment_count = comment_count - 1 WHERE id = $1 " - - cache - key - tmpl = " atto.post:{} " - - decr ) ;
2025-03-24 19:55:08 -04:00
}