@ -1,23 +1,10 @@
use crate ::{
newtypes ::{ CommunityId , DbUrl , PersonId , PostId } ,
schema ::post ::dsl ::{
ap_id ,
body ,
community_id ,
creator_id ,
deleted ,
featured_community ,
local ,
name ,
post ,
published ,
removed ,
thumbnail_url ,
updated ,
url ,
} ,
schema ::{ post , post_hide , post_like , post_read , post_saved } ,
source ::post ::{
Post ,
PostHide ,
PostHideForm ,
PostInsertForm ,
PostLike ,
PostLikeForm ,
@ -53,9 +40,9 @@ impl Crud for Post {
async fn create ( pool : & mut DbPool < ' _ > , form : & Self ::InsertForm ) -> Result < Self , Error > {
let conn = & mut get_conn ( pool ) . await ? ;
insert_into ( post )
insert_into ( post ::table )
. values ( form )
. on_conflict ( ap_id)
. on_conflict ( post:: ap_id)
. do_update ( )
. set ( form )
. get_result ::< Self > ( conn )
@ -68,7 +55,7 @@ impl Crud for Post {
new_post : & Self ::UpdateForm ,
) -> Result < Self , Error > {
let conn = & mut get_conn ( pool ) . await ? ;
diesel ::update ( post . find ( post_id ) )
diesel ::update ( post ::table . find ( post_id ) )
. set ( new_post )
. get_result ::< Self > ( conn )
. await
@ -81,12 +68,12 @@ impl Post {
the_community_id : CommunityId ,
) -> Result < Vec < Self > , Error > {
let conn = & mut get_conn ( pool ) . await ? ;
post
. filter ( community_id. eq ( the_community_id ) )
. filter ( deleted. eq ( false ) )
. filter ( removed. eq ( false ) )
. then_order_by ( featured_community. desc ( ) )
. then_order_by ( p ublished. desc ( ) )
post ::table
. filter ( post:: community_id. eq ( the_community_id ) )
. filter ( post:: deleted. eq ( false ) )
. filter ( post:: removed. eq ( false ) )
. then_order_by ( post:: featured_community. desc ( ) )
. then_order_by ( p ost::p ublished. desc ( ) )
. limit ( FETCH_LIMIT_MAX )
. load ::< Self > ( conn )
. await
@ -97,12 +84,12 @@ impl Post {
the_community_id : CommunityId ,
) -> Result < Vec < Self > , Error > {
let conn = & mut get_conn ( pool ) . await ? ;
post
. filter ( community_id. eq ( the_community_id ) )
. filter ( deleted. eq ( false ) )
. filter ( removed. eq ( false ) )
. filter ( featured_community. eq ( true ) )
. then_order_by ( p ublished. desc ( ) )
post ::table
. filter ( post:: community_id. eq ( the_community_id ) )
. filter ( post:: deleted. eq ( false ) )
. filter ( post:: removed. eq ( false ) )
. filter ( post:: featured_community. eq ( true ) )
. then_order_by ( p ost::p ublished. desc ( ) )
. limit ( FETCH_LIMIT_MAX )
. load ::< Self > ( conn )
. await
@ -112,13 +99,13 @@ impl Post {
pool : & mut DbPool < ' _ > ,
) -> Result < Vec < ( DbUrl , chrono ::DateTime < Utc > ) > , Error > {
let conn = & mut get_conn ( pool ) . await ? ;
post
. select ( ( ap_id, coalesce ( updated, published ) ) )
. filter ( local. eq ( true ) )
. filter ( deleted. eq ( false ) )
. filter ( removed. eq ( false ) )
. filter ( p ublished. ge ( Utc ::now ( ) . naive_utc ( ) - Duration ::days ( SITEMAP_DAYS ) ) )
. order ( p ublished. desc ( ) )
post ::table
. select ( ( post:: ap_id, coalesce ( post:: updated, post ::published ) ) )
. filter ( post:: local. eq ( true ) )
. filter ( post:: deleted. eq ( false ) )
. filter ( post:: removed. eq ( false ) )
. filter ( p ost::p ublished. ge ( Utc ::now ( ) . naive_utc ( ) - Duration ::days ( SITEMAP_DAYS ) ) )
. order ( p ost::p ublished. desc ( ) )
. limit ( SITEMAP_LIMIT )
. load ::< ( DbUrl , chrono ::DateTime < Utc > ) > ( conn )
. await
@ -130,13 +117,13 @@ impl Post {
) -> Result < Vec < Self > , Error > {
let conn = & mut get_conn ( pool ) . await ? ;
diesel ::update ( post . filter ( creator_id . eq ( for_creator_id ) ) )
diesel ::update ( post ::table . filter ( post ::creator_id . eq ( for_creator_id ) ) )
. set ( (
name. eq ( DELETED_REPLACEMENT_TEXT ) ,
url. eq ( Option ::< & str > ::None ) ,
body. eq ( DELETED_REPLACEMENT_TEXT ) ,
deleted. eq ( true ) ,
updated. eq ( naive_now ( ) ) ,
post:: name. eq ( DELETED_REPLACEMENT_TEXT ) ,
post:: url. eq ( Option ::< & str > ::None ) ,
post:: body. eq ( DELETED_REPLACEMENT_TEXT ) ,
post:: deleted. eq ( true ) ,
post:: updated. eq ( naive_now ( ) ) ,
) )
. get_results ::< Self > ( conn )
. await
@ -150,15 +137,15 @@ impl Post {
) -> Result < Vec < Self > , Error > {
let conn = & mut get_conn ( pool ) . await ? ;
let mut update = diesel ::update ( post ) . into_boxed ( ) ;
update = update . filter ( creator_id. eq ( for_creator_id ) ) ;
let mut update = diesel ::update ( post ::table ) . into_boxed ( ) ;
update = update . filter ( post:: creator_id. eq ( for_creator_id ) ) ;
if let Some ( for_community_id ) = for_community_id {
update = update . filter ( community_id. eq ( for_community_id ) ) ;
update = update . filter ( post:: community_id. eq ( for_community_id ) ) ;
}
update
. set ( ( removed. eq ( new_removed ) , updated . eq ( naive_now ( ) ) ) )
. set ( ( post:: removed. eq ( new_removed ) , post ::updated . eq ( naive_now ( ) ) ) )
. get_results ::< Self > ( conn )
. await
}
@ -174,8 +161,8 @@ impl Post {
let conn = & mut get_conn ( pool ) . await ? ;
let object_id : DbUrl = object_id . into ( ) ;
Ok (
post
. filter ( ap_id. eq ( object_id ) )
post ::table
. filter ( post:: ap_id. eq ( object_id ) )
. first ::< Post > ( conn )
. await
. ok ( )
@ -190,9 +177,9 @@ impl Post {
let conn = & mut get_conn ( pool ) . await ? ;
let pictrs_search = "%pictrs/image%" ;
post
. filter ( creator_id. eq ( for_creator_id ) )
. filter ( url. like ( pictrs_search ) )
post ::table
. filter ( post:: creator_id. eq ( for_creator_id ) )
. filter ( post:: url. like ( pictrs_search ) )
. load ::< Self > ( conn )
. await
}
@ -206,13 +193,13 @@ impl Post {
let pictrs_search = "%pictrs/image%" ;
diesel ::update (
post
. filter ( creator_id. eq ( for_creator_id ) )
. filter ( url. like ( pictrs_search ) ) ,
post ::table
. filter ( post:: creator_id. eq ( for_creator_id ) )
. filter ( post:: url. like ( pictrs_search ) ) ,
)
. set ( (
url. eq ::< Option < String > > ( None ) ,
thumbnail_url. eq ::< Option < String > > ( None ) ,
post:: url. eq ::< Option < String > > ( None ) ,
post:: thumbnail_url. eq ::< Option < String > > ( None ) ,
) )
. get_results ::< Self > ( conn )
. await
@ -224,9 +211,9 @@ impl Post {
) -> Result < Vec < Self > , Error > {
let conn = & mut get_conn ( pool ) . await ? ;
let pictrs_search = "%pictrs/image%" ;
post
. filter ( community_id. eq ( for_community_id ) )
. filter ( url. like ( pictrs_search ) )
post ::table
. filter ( post:: community_id. eq ( for_community_id ) )
. filter ( post:: url. like ( pictrs_search ) )
. load ::< Self > ( conn )
. await
}
@ -240,13 +227,13 @@ impl Post {
let pictrs_search = "%pictrs/image%" ;
diesel ::update (
post
. filter ( community_id. eq ( for_community_id ) )
. filter ( url. like ( pictrs_search ) ) ,
post ::table
. filter ( post:: community_id. eq ( for_community_id ) )
. filter ( post:: url. like ( pictrs_search ) ) ,
)
. set ( (
url. eq ::< Option < String > > ( None ) ,
thumbnail_url. eq ::< Option < String > > ( None ) ,
post:: url. eq ::< Option < String > > ( None ) ,
post:: thumbnail_url. eq ::< Option < String > > ( None ) ,
) )
. get_results ::< Self > ( conn )
. await
@ -258,11 +245,10 @@ impl Likeable for PostLike {
type Form = PostLikeForm ;
type IdType = PostId ;
async fn like ( pool : & mut DbPool < ' _ > , post_like_form : & PostLikeForm ) -> Result < Self , Error > {
use crate ::schema ::post_like ::dsl ::{ person_id , post_id , post_like } ;
let conn = & mut get_conn ( pool ) . await ? ;
insert_into ( post_like )
insert_into ( post_like ::table )
. values ( post_like_form )
. on_conflict ( ( post_ id, person_id ) )
. on_conflict ( ( post_ like::post_ id, post_like ::person_id ) )
. do_update ( )
. set ( post_like_form )
. get_result ::< Self > ( conn )
@ -273,9 +259,8 @@ impl Likeable for PostLike {
person_id : PersonId ,
post_id : PostId ,
) -> Result < usize , Error > {
use crate ::schema ::post_like ::dsl ;
let conn = & mut get_conn ( pool ) . await ? ;
diesel ::delete ( dsl:: post_like. find ( ( person_id , post_id ) ) )
diesel ::delete ( post_like::tabl e. find ( ( person_id , post_id ) ) )
. execute ( conn )
. await
}
@ -285,20 +270,18 @@ impl Likeable for PostLike {
impl Saveable for PostSaved {
type Form = PostSavedForm ;
async fn save ( pool : & mut DbPool < ' _ > , post_saved_form : & PostSavedForm ) -> Result < Self , Error > {
use crate ::schema ::post_saved ::dsl ::{ person_id , post_id , post_saved } ;
let conn = & mut get_conn ( pool ) . await ? ;
insert_into ( post_saved )
insert_into ( post_saved ::table )
. values ( post_saved_form )
. on_conflict ( ( post_ id, person_id ) )
. on_conflict ( ( post_ saved::post_ id, post_saved ::person_id ) )
. do_update ( )
. set ( post_saved_form )
. get_result ::< Self > ( conn )
. await
}
async fn unsave ( pool : & mut DbPool < ' _ > , post_saved_form : & PostSavedForm ) -> Result < usize , Error > {
use crate ::schema ::post_saved ::dsl ::post_saved ;
let conn = & mut get_conn ( pool ) . await ? ;
diesel ::delete ( post_saved . find ( ( post_saved_form . person_id , post_saved_form . post_id ) ) )
diesel ::delete ( post_saved ::table . find ( ( post_saved_form . person_id , post_saved_form . post_id ) ) )
. execute ( conn )
. await
}
@ -310,14 +293,13 @@ impl PostRead {
post_ids : HashSet < PostId > ,
person_id : PersonId ,
) -> Result < usize , Error > {
use crate ::schema ::post_read ::dsl ::post_read ;
let conn = & mut get_conn ( pool ) . await ? ;
let forms = post_ids
. into_iter ( )
. map ( | post_id | PostReadForm { post_id , person_id } )
. collect ::< Vec < PostReadForm > > ( ) ;
insert_into ( post_read )
insert_into ( post_read ::table )
. values ( forms )
. on_conflict_do_nothing ( )
. execute ( conn )
@ -329,13 +311,48 @@ impl PostRead {
post_id_ : HashSet < PostId > ,
person_id_ : PersonId ,
) -> Result < usize , Error > {
use crate ::schema ::post_read ::dsl ::{ person_id , post_id , post_read } ;
let conn = & mut get_conn ( pool ) . await ? ;
diesel ::delete (
post_read
. filter ( post_id . eq_any ( post_id_ ) )
. filter ( person_id . eq ( person_id_ ) ) ,
post_read ::table
. filter ( post_read ::post_id . eq_any ( post_id_ ) )
. filter ( post_read ::person_id . eq ( person_id_ ) ) ,
)
. execute ( conn )
. await
}
}
impl PostHide {
pub async fn hide (
pool : & mut DbPool < ' _ > ,
post_ids : HashSet < PostId > ,
person_id : PersonId ,
) -> Result < usize , Error > {
let conn = & mut get_conn ( pool ) . await ? ;
let forms = post_ids
. into_iter ( )
. map ( | post_id | PostHideForm { post_id , person_id } )
. collect ::< Vec < PostHideForm > > ( ) ;
insert_into ( post_hide ::table )
. values ( forms )
. on_conflict_do_nothing ( )
. execute ( conn )
. await
}
pub async fn unhide (
pool : & mut DbPool < ' _ > ,
post_id_ : HashSet < PostId > ,
person_id_ : PersonId ,
) -> Result < usize , Error > {
let conn = & mut get_conn ( pool ) . await ? ;
diesel ::delete (
post_hide ::table
. filter ( post_hide ::post_id . eq_any ( post_id_ ) )
. filter ( post_hide ::person_id . eq ( person_id_ ) ) ,
)
. execute ( conn )
. await