@ -16,7 +16,7 @@ use plume_models::{
blog_authors ::* , blogs ::* , instance ::Instance , medias ::* , posts ::Post , safe_string ::SafeString ,
users ::User , Connection , PlumeRocket ,
} ;
use routes ::{ errors ::ErrorPage , Page };
use routes ::{ errors ::ErrorPage , Page , RespondOrRedirect };
use template_utils ::{ IntoContext , Ructe } ;
#[ get( " /~/<name>?<page> " , rank = 2) ]
@ -84,10 +84,7 @@ fn valid_slug(title: &str) -> Result<(), ValidationError> {
}
#[ post( " /blogs/new " , data = " <form> " ) ]
pub fn create (
form : LenientForm < NewBlogForm > ,
rockets : PlumeRocket ,
) -> Result < Flash < Redirect > , Ructe > {
pub fn create ( form : LenientForm < NewBlogForm > , rockets : PlumeRocket ) -> RespondOrRedirect {
let slug = utils ::make_actor_id ( & form . title ) ;
let conn = & * rockets . conn ;
let intl = & rockets . intl . catalog ;
@ -111,42 +108,43 @@ pub fn create(
) ;
}
if errors . is_empty ( ) {
let blog = Blog ::insert (
& * conn ,
NewBlog ::new_local (
slug . clone ( ) ,
form . title . to_string ( ) ,
String ::from ( "" ) ,
Instance ::get_local ( )
. expect ( "blog::create: instance error" )
. id ,
)
. expect ( "blog::create: new local error" ) ,
)
. expect ( "blog::create: error" ) ;
if ! errors . is_empty ( ) {
return render ! ( blogs ::new ( & rockets . to_context ( ) , & * form , errors ) ) . into ( ) ;
}
BlogAuthor ::insert (
& * conn ,
NewBlogAuthor {
blog_id : blog . id ,
author_id : user . id ,
is_owner : true ,
} ,
let blog = Blog ::insert (
& * conn ,
NewBlog ::new_local (
slug . clone ( ) ,
form . title . to_string ( ) ,
String ::from ( "" ) ,
Instance ::get_local ( )
. expect ( "blog::create: instance error" )
. id ,
)
. expect ( "blog::create: author error" ) ;
. expect ( "blog::create: new local error" ) ,
)
. expect ( "blog::create: error" ) ;
Ok ( Flash ::success (
Redirect ::to ( uri ! ( details : name = slug . clone ( ) , page = _ ) ) ,
& i18n ! ( intl , "Your blog was successfully created!" ) ,
) )
} else {
Err ( render ! ( blogs ::new ( & rockets . to_context ( ) , & * form , errors ) ) )
}
BlogAuthor ::insert (
& * conn ,
NewBlogAuthor {
blog_id : blog . id ,
author_id : user . id ,
is_owner : true ,
} ,
)
. expect ( "blog::create: author error" ) ;
Flash ::success (
Redirect ::to ( uri ! ( details : name = slug . clone ( ) , page = _ ) ) ,
& i18n ! ( intl , "Your blog was successfully created!" ) ,
)
. into ( )
}
#[ post( " /~/<name>/delete " ) ]
pub fn delete ( name : String , rockets : PlumeRocket ) -> Result < Flash < Redirect > , Ructe > {
pub fn delete ( name : String , rockets : PlumeRocket ) -> RespondOrRedirect {
let conn = & * rockets . conn ;
let blog = Blog ::find_by_fqn ( & rockets , & name ) . expect ( "blog::delete: blog not found" ) ;
@ -158,19 +156,21 @@ pub fn delete(name: String, rockets: PlumeRocket) -> Result<Flash<Redirect>, Ruc
{
blog . delete ( & conn , & rockets . searcher )
. expect ( "blog::expect: deletion error" ) ;
Ok ( Flash ::success (
Flash ::success (
Redirect ::to ( uri ! ( super ::instance ::index ) ) ,
i18n ! ( rockets . intl . catalog , "Your blog was deleted." ) ,
) )
)
. into ( )
} else {
// TODO actually return 403 error code
Err ( render ! ( errors ::not_authorized (
render ! ( errors ::not_authorized (
& rockets . to_context ( ) ,
i18n ! (
rockets . intl . catalog ,
"You are not allowed to delete this blog."
)
) ) )
) )
. into ( )
}
}
@ -236,104 +236,107 @@ pub fn update(
name : String ,
form : LenientForm < EditForm > ,
rockets : PlumeRocket ,
) -> Result < Flash < Redirect > , Ructe > {
) -> RespondOrRedirect {
let conn = & * rockets . conn ;
let intl = & rockets . intl . catalog ;
let mut blog = Blog ::find_by_fqn ( & rockets , & name ) . expect ( "blog::update: blog not found" ) ;
if rockets
if ! rockets
. user
. clone ( )
. and_then ( | u | u . is_author_in ( & * conn , & blog ) . ok ( ) )
. unwrap_or ( false )
{
let user = rockets
. user
. clone ( )
. expect ( "blogs::edit: User was None while it shouldn't" ) ;
form . validate ( )
. and_then ( | _ | {
if let Some ( icon ) = form . icon {
if ! check_media ( & * conn , icon , & user ) {
let mut errors = ValidationErrors ::new ( ) ;
errors . add (
"" ,
ValidationError {
code : Cow ::from ( "icon" ) ,
message : Some ( Cow ::from ( i18n ! (
intl ,
"You can't use this media as a blog icon."
) ) ) ,
params : HashMap ::new ( ) ,
} ,
) ;
return Err ( errors ) ;
}
}
if let Some ( banner ) = form . banner {
if ! check_media ( & * conn , banner , & user ) {
let mut errors = ValidationErrors ::new ( ) ;
errors . add (
"" ,
ValidationError {
code : Cow ::from ( "banner" ) ,
message : Some ( Cow ::from ( i18n ! (
intl ,
"You can't use this media as a blog banner."
) ) ) ,
params : HashMap ::new ( ) ,
} ,
) ;
return Err ( errors ) ;
}
}
blog . title = form . title . clone ( ) ;
blog . summary = form . summary . clone ( ) ;
blog . summary_html = SafeString ::new (
& utils ::md_to_html (
& form . summary ,
None ,
true ,
Some ( Media ::get_media_processor (
& conn ,
blog . list_authors ( & conn )
. expect ( "Couldn't get list of authors" )
. iter ( )
. collect ( ) ,
) ) ,
)
. 0 ,
) ;
blog . icon_id = form . icon ;
blog . banner_id = form . banner ;
blog . save_changes ::< Blog > ( & * conn )
. expect ( "Couldn't save blog changes" ) ;
Ok ( Flash ::success (
Redirect ::to ( uri ! ( details : name = name , page = _ ) ) ,
i18n ! ( intl , "Your blog information have been updated." ) ,
) )
} )
. map_err ( | err | {
let medias = Media ::for_user ( & * conn , user . id ) . expect ( "Couldn't list media" ) ;
render ! ( blogs ::edit (
& rockets . to_context ( ) ,
& blog ,
medias ,
& * form ,
err
) )
} )
} else {
// TODO actually return 403 error code
Err ( render ! ( errors ::not_authorized (
return render ! ( errors ::not_authorized (
& rockets . to_context ( ) ,
i18n ! (
rockets . intl . catalog ,
"You are not allowed to edit this blog."
)
) ) )
) )
. into ( ) ;
}
let user = rockets
. user
. clone ( )
. expect ( "blogs::edit: User was None while it shouldn't" ) ;
form . validate ( )
. and_then ( | _ | {
if let Some ( icon ) = form . icon {
if ! check_media ( & * conn , icon , & user ) {
let mut errors = ValidationErrors ::new ( ) ;
errors . add (
"" ,
ValidationError {
code : Cow ::from ( "icon" ) ,
message : Some ( Cow ::from ( i18n ! (
intl ,
"You can't use this media as a blog icon."
) ) ) ,
params : HashMap ::new ( ) ,
} ,
) ;
return Err ( errors ) ;
}
}
if let Some ( banner ) = form . banner {
if ! check_media ( & * conn , banner , & user ) {
let mut errors = ValidationErrors ::new ( ) ;
errors . add (
"" ,
ValidationError {
code : Cow ::from ( "banner" ) ,
message : Some ( Cow ::from ( i18n ! (
intl ,
"You can't use this media as a blog banner."
) ) ) ,
params : HashMap ::new ( ) ,
} ,
) ;
return Err ( errors ) ;
}
}
blog . title = form . title . clone ( ) ;
blog . summary = form . summary . clone ( ) ;
blog . summary_html = SafeString ::new (
& utils ::md_to_html (
& form . summary ,
None ,
true ,
Some ( Media ::get_media_processor (
& conn ,
blog . list_authors ( & conn )
. expect ( "Couldn't get list of authors" )
. iter ( )
. collect ( ) ,
) ) ,
)
. 0 ,
) ;
blog . icon_id = form . icon ;
blog . banner_id = form . banner ;
blog . save_changes ::< Blog > ( & * conn )
. expect ( "Couldn't save blog changes" ) ;
Ok ( Flash ::success (
Redirect ::to ( uri ! ( details : name = name , page = _ ) ) ,
i18n ! ( intl , "Your blog information have been updated." ) ,
) )
} )
. map_err ( | err | {
let medias = Media ::for_user ( & * conn , user . id ) . expect ( "Couldn't list media" ) ;
render ! ( blogs ::edit (
& rockets . to_context ( ) ,
& blog ,
medias ,
& * form ,
err
) )
} )
. unwrap ( )
. into ( )
}
#[ get( " /~/<name>/outbox " ) ]