Replace PlumeRocket.conn with DbConn
This commit is contained in:
+55
-51
@@ -14,21 +14,25 @@ use crate::template_utils::{IntoContext, Ructe};
|
||||
use plume_common::activity_pub::{ActivityStream, ApRequest};
|
||||
use plume_common::utils;
|
||||
use plume_models::{
|
||||
blog_authors::*, blogs::*, instance::Instance, medias::*, posts::Post, safe_string::SafeString,
|
||||
users::User, Connection, PlumeRocket,
|
||||
blog_authors::*, blogs::*, db_conn::DbConn, instance::Instance, medias::*, posts::Post,
|
||||
safe_string::SafeString, users::User, Connection, PlumeRocket,
|
||||
};
|
||||
|
||||
#[get("/~/<name>?<page>", rank = 2)]
|
||||
pub fn details(name: String, page: Option<Page>, rockets: PlumeRocket) -> Result<Ructe, ErrorPage> {
|
||||
pub fn details(
|
||||
name: String,
|
||||
page: Option<Page>,
|
||||
conn: DbConn,
|
||||
rockets: PlumeRocket,
|
||||
) -> Result<Ructe, ErrorPage> {
|
||||
let page = page.unwrap_or_default();
|
||||
let conn = &*rockets.conn;
|
||||
let blog = Blog::find_by_fqn(&rockets, &name)?;
|
||||
let posts = Post::blog_page(conn, &blog, page.limits())?;
|
||||
let articles_count = Post::count_for_blog(conn, &blog)?;
|
||||
let authors = &blog.list_authors(conn)?;
|
||||
let blog = Blog::find_by_fqn(&conn, &name)?;
|
||||
let posts = Post::blog_page(&conn, &blog, page.limits())?;
|
||||
let articles_count = Post::count_for_blog(&conn, &blog)?;
|
||||
let authors = &blog.list_authors(&conn)?;
|
||||
|
||||
Ok(render!(blogs::details(
|
||||
&rockets.to_context(),
|
||||
&(&conn, &rockets).to_context(),
|
||||
blog,
|
||||
authors,
|
||||
page.0,
|
||||
@@ -40,17 +44,17 @@ pub fn details(name: String, page: Option<Page>, rockets: PlumeRocket) -> Result
|
||||
#[get("/~/<name>", rank = 1)]
|
||||
pub fn activity_details(
|
||||
name: String,
|
||||
rockets: PlumeRocket,
|
||||
conn: DbConn,
|
||||
_ap: ApRequest,
|
||||
) -> Option<ActivityStream<CustomGroup>> {
|
||||
let blog = Blog::find_by_fqn(&rockets, &name).ok()?;
|
||||
Some(ActivityStream::new(blog.to_activity(&*rockets.conn).ok()?))
|
||||
let blog = Blog::find_by_fqn(&conn, &name).ok()?;
|
||||
Some(ActivityStream::new(blog.to_activity(&conn).ok()?))
|
||||
}
|
||||
|
||||
#[get("/blogs/new")]
|
||||
pub fn new(rockets: PlumeRocket, _user: User) -> Ructe {
|
||||
pub fn new(conn: DbConn, rockets: PlumeRocket, _user: User) -> Ructe {
|
||||
render!(blogs::new(
|
||||
&rockets.to_context(),
|
||||
&(&conn, &rockets).to_context(),
|
||||
&NewBlogForm::default(),
|
||||
ValidationErrors::default()
|
||||
))
|
||||
@@ -83,9 +87,12 @@ fn valid_slug(title: &str) -> Result<(), ValidationError> {
|
||||
}
|
||||
|
||||
#[post("/blogs/new", data = "<form>")]
|
||||
pub fn create(form: LenientForm<NewBlogForm>, rockets: PlumeRocket) -> RespondOrRedirect {
|
||||
pub fn create(
|
||||
form: LenientForm<NewBlogForm>,
|
||||
conn: DbConn,
|
||||
rockets: PlumeRocket,
|
||||
) -> RespondOrRedirect {
|
||||
let slug = utils::make_actor_id(&form.title);
|
||||
let conn = &*rockets.conn;
|
||||
let intl = &rockets.intl.catalog;
|
||||
let user = rockets.user.clone().unwrap();
|
||||
|
||||
@@ -93,7 +100,7 @@ pub fn create(form: LenientForm<NewBlogForm>, rockets: PlumeRocket) -> RespondOr
|
||||
Ok(_) => ValidationErrors::new(),
|
||||
Err(e) => e,
|
||||
};
|
||||
if Blog::find_by_fqn(&rockets, &slug).is_ok() {
|
||||
if Blog::find_by_fqn(&conn, &slug).is_ok() {
|
||||
errors.add(
|
||||
"title",
|
||||
ValidationError {
|
||||
@@ -108,11 +115,11 @@ pub fn create(form: LenientForm<NewBlogForm>, rockets: PlumeRocket) -> RespondOr
|
||||
}
|
||||
|
||||
if !errors.is_empty() {
|
||||
return render!(blogs::new(&rockets.to_context(), &*form, errors)).into();
|
||||
return render!(blogs::new(&(&conn, &rockets).to_context(), &*form, errors)).into();
|
||||
}
|
||||
|
||||
let blog = Blog::insert(
|
||||
&*conn,
|
||||
&conn,
|
||||
NewBlog::new_local(
|
||||
slug.clone(),
|
||||
form.title.to_string(),
|
||||
@@ -126,7 +133,7 @@ pub fn create(form: LenientForm<NewBlogForm>, rockets: PlumeRocket) -> RespondOr
|
||||
.expect("blog::create: error");
|
||||
|
||||
BlogAuthor::insert(
|
||||
&*conn,
|
||||
&conn,
|
||||
NewBlogAuthor {
|
||||
blog_id: blog.id,
|
||||
author_id: user.id,
|
||||
@@ -143,17 +150,16 @@ pub fn create(form: LenientForm<NewBlogForm>, rockets: PlumeRocket) -> RespondOr
|
||||
}
|
||||
|
||||
#[post("/~/<name>/delete")]
|
||||
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");
|
||||
pub fn delete(name: String, conn: DbConn, rockets: PlumeRocket) -> RespondOrRedirect {
|
||||
let blog = Blog::find_by_fqn(&conn, &name).expect("blog::delete: blog not found");
|
||||
|
||||
if rockets
|
||||
.user
|
||||
.clone()
|
||||
.and_then(|u| u.is_author_in(&*conn, &blog).ok())
|
||||
.and_then(|u| u.is_author_in(&conn, &blog).ok())
|
||||
.unwrap_or(false)
|
||||
{
|
||||
blog.delete(&conn).expect("blog::expect: deletion error");
|
||||
blog.delete(&*conn).expect("blog::expect: deletion error");
|
||||
Flash::success(
|
||||
Redirect::to(uri!(super::instance::index)),
|
||||
i18n!(rockets.intl.catalog, "Your blog was deleted."),
|
||||
@@ -162,7 +168,7 @@ pub fn delete(name: String, rockets: PlumeRocket) -> RespondOrRedirect {
|
||||
} else {
|
||||
// TODO actually return 403 error code
|
||||
render!(errors::not_authorized(
|
||||
&rockets.to_context(),
|
||||
&(&conn, &rockets).to_context(),
|
||||
i18n!(
|
||||
rockets.intl.catalog,
|
||||
"You are not allowed to delete this blog."
|
||||
@@ -183,22 +189,21 @@ pub struct EditForm {
|
||||
}
|
||||
|
||||
#[get("/~/<name>/edit")]
|
||||
pub fn edit(name: String, rockets: PlumeRocket) -> Result<Ructe, ErrorPage> {
|
||||
let conn = &*rockets.conn;
|
||||
let blog = Blog::find_by_fqn(&rockets, &name)?;
|
||||
pub fn edit(name: String, conn: DbConn, rockets: PlumeRocket) -> Result<Ructe, ErrorPage> {
|
||||
let blog = Blog::find_by_fqn(&conn, &name)?;
|
||||
if rockets
|
||||
.user
|
||||
.clone()
|
||||
.and_then(|u| u.is_author_in(conn, &blog).ok())
|
||||
.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");
|
||||
let medias = Media::for_user(conn, user.id).expect("Couldn't list media");
|
||||
let medias = Media::for_user(&conn, user.id).expect("Couldn't list media");
|
||||
Ok(render!(blogs::edit(
|
||||
&rockets.to_context(),
|
||||
&(&conn, &rockets).to_context(),
|
||||
&blog,
|
||||
medias,
|
||||
&EditForm {
|
||||
@@ -213,7 +218,7 @@ pub fn edit(name: String, rockets: PlumeRocket) -> Result<Ructe, ErrorPage> {
|
||||
} else {
|
||||
// TODO actually return 403 error code
|
||||
Ok(render!(errors::not_authorized(
|
||||
&rockets.to_context(),
|
||||
&(&conn, &rockets).to_context(),
|
||||
i18n!(
|
||||
rockets.intl.catalog,
|
||||
"You are not allowed to edit this blog."
|
||||
@@ -235,20 +240,20 @@ fn check_media(conn: &Connection, id: i32, user: &User) -> bool {
|
||||
pub fn update(
|
||||
name: String,
|
||||
form: LenientForm<EditForm>,
|
||||
conn: DbConn,
|
||||
rockets: PlumeRocket,
|
||||
) -> 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");
|
||||
let mut blog = Blog::find_by_fqn(&conn, &name).expect("blog::update: blog not found");
|
||||
if !rockets
|
||||
.user
|
||||
.clone()
|
||||
.and_then(|u| u.is_author_in(&*conn, &blog).ok())
|
||||
.and_then(|u| u.is_author_in(&conn, &blog).ok())
|
||||
.unwrap_or(false)
|
||||
{
|
||||
// TODO actually return 403 error code
|
||||
return render!(errors::not_authorized(
|
||||
&rockets.to_context(),
|
||||
&(&conn, &rockets).to_context(),
|
||||
i18n!(
|
||||
rockets.intl.catalog,
|
||||
"You are not allowed to edit this blog."
|
||||
@@ -264,7 +269,7 @@ pub fn update(
|
||||
form.validate()
|
||||
.and_then(|_| {
|
||||
if let Some(icon) = form.icon {
|
||||
if !check_media(&*conn, icon, &user) {
|
||||
if !check_media(&conn, icon, &user) {
|
||||
let mut errors = ValidationErrors::new();
|
||||
errors.add(
|
||||
"",
|
||||
@@ -282,7 +287,7 @@ pub fn update(
|
||||
}
|
||||
|
||||
if let Some(banner) = form.banner {
|
||||
if !check_media(&*conn, banner, &user) {
|
||||
if !check_media(&conn, banner, &user) {
|
||||
let mut errors = ValidationErrors::new();
|
||||
errors.add(
|
||||
"",
|
||||
@@ -327,9 +332,9 @@ pub fn update(
|
||||
))
|
||||
})
|
||||
.map_err(|err| {
|
||||
let medias = Media::for_user(&*conn, user.id).expect("Couldn't list media");
|
||||
let medias = Media::for_user(&conn, user.id).expect("Couldn't list media");
|
||||
render!(blogs::edit(
|
||||
&rockets.to_context(),
|
||||
&(&conn, &rockets).to_context(),
|
||||
&blog,
|
||||
medias,
|
||||
&*form,
|
||||
@@ -341,31 +346,30 @@ pub fn update(
|
||||
}
|
||||
|
||||
#[get("/~/<name>/outbox")]
|
||||
pub fn outbox(name: String, rockets: PlumeRocket) -> Option<ActivityStream<OrderedCollection>> {
|
||||
let blog = Blog::find_by_fqn(&rockets, &name).ok()?;
|
||||
Some(blog.outbox(&*rockets.conn).ok()?)
|
||||
pub fn outbox(name: String, conn: DbConn) -> Option<ActivityStream<OrderedCollection>> {
|
||||
let blog = Blog::find_by_fqn(&conn, &name).ok()?;
|
||||
Some(blog.outbox(&conn).ok()?)
|
||||
}
|
||||
#[allow(unused_variables)]
|
||||
#[get("/~/<name>/outbox?<page>")]
|
||||
pub fn outbox_page(
|
||||
name: String,
|
||||
page: Page,
|
||||
rockets: PlumeRocket,
|
||||
conn: DbConn,
|
||||
) -> Option<ActivityStream<OrderedCollectionPage>> {
|
||||
let blog = Blog::find_by_fqn(&rockets, &name).ok()?;
|
||||
Some(blog.outbox_page(&*rockets.conn, page.limits()).ok()?)
|
||||
let blog = Blog::find_by_fqn(&conn, &name).ok()?;
|
||||
Some(blog.outbox_page(&conn, page.limits()).ok()?)
|
||||
}
|
||||
#[get("/~/<name>/atom.xml")]
|
||||
pub fn atom_feed(name: String, rockets: PlumeRocket) -> Option<Content<String>> {
|
||||
let blog = Blog::find_by_fqn(&rockets, &name).ok()?;
|
||||
let conn = &*rockets.conn;
|
||||
pub fn atom_feed(name: String, conn: DbConn) -> Option<Content<String>> {
|
||||
let blog = Blog::find_by_fqn(&conn, &name).ok()?;
|
||||
let entries = Post::get_recents_for_blog(&*conn, &blog, 15).ok()?;
|
||||
let uri = Instance::get_local()
|
||||
.ok()?
|
||||
.compute_box("~", &name, "atom.xml");
|
||||
let title = &blog.title;
|
||||
let default_updated = &blog.creation_date;
|
||||
let feed = super::build_atom_feed(entries, &uri, title, default_updated, conn);
|
||||
let feed = super::build_atom_feed(entries, &uri, title, default_updated, &conn);
|
||||
Some(Content(
|
||||
ContentType::new("application", "atom+xml"),
|
||||
feed.to_string(),
|
||||
|
||||
Reference in New Issue
Block a user