diff --git a/src/models/blog_authors.rs b/src/models/blog_authors.rs index 04be669..106832a 100644 --- a/src/models/blog_authors.rs +++ b/src/models/blog_authors.rs @@ -19,12 +19,6 @@ pub struct NewBlogAuthor { } impl BlogAuthor { - pub fn insert (conn: &PgConnection, new: NewBlogAuthor) -> BlogAuthor { - diesel::insert_into(blog_authors::table) - .values(new) - .get_result(conn) - .expect("Error saving new blog author") - } - + insert!(blog_authors, NewBlogAuthor); get!(blog_authors); } diff --git a/src/models/blogs.rs b/src/models/blogs.rs index ce253d3..71e7bee 100644 --- a/src/models/blogs.rs +++ b/src/models/blogs.rs @@ -22,7 +22,7 @@ use activity_pub::{ sign, webfinger::* }; -use models::instance::Instance; +use models::instance::*; use schema::blogs; @@ -56,13 +56,7 @@ pub struct NewBlog { } impl Blog { - pub fn insert (conn: &PgConnection, new: NewBlog) -> Blog { - diesel::insert_into(blogs::table) - .values(new) - .get_result(conn) - .expect("Error saving new blog") - } - + insert!(blogs, NewBlog); get!(blogs); pub fn find_for_author(conn: &PgConnection, author_id: i32) -> Vec { @@ -130,7 +124,11 @@ impl Blog { let instance = match Instance::find_by_domain(conn, inst.clone()) { Some(instance) => instance, None => { - Instance::insert(conn, inst.clone(), inst.clone(), false) + Instance::insert(conn, NewInstance { + public_domain: inst.clone(), + name: inst.clone(), + local: false + }) } }; Blog::insert(conn, NewBlog { diff --git a/src/models/comments.rs b/src/models/comments.rs index be75b23..a24ac40 100644 --- a/src/models/comments.rs +++ b/src/models/comments.rs @@ -47,13 +47,7 @@ pub struct NewComment { } impl Comment { - pub fn insert (conn: &PgConnection, new: NewComment) -> Comment { - diesel::insert_into(comments::table) - .values(new) - .get_result(conn) - .expect("Error saving new comment") - } - + insert!(comments, NewComment); get!(comments); find_by!(comments, find_by_post, post_id as i32); diff --git a/src/models/follows.rs b/src/models/follows.rs index 111cc6e..0654862 100644 --- a/src/models/follows.rs +++ b/src/models/follows.rs @@ -25,13 +25,7 @@ pub struct NewFollow { } impl Follow { - pub fn insert(conn: &PgConnection, new: NewFollow) -> Follow { - diesel::insert_into(follows::table) - .values(new) - .get_result(conn) - .expect("Unable to insert new follow") - } - + insert!(follows, NewFollow); get!(follows); pub fn accept_follow( diff --git a/src/models/instance.rs b/src/models/instance.rs index ac7ad9d..b7128da 100644 --- a/src/models/instance.rs +++ b/src/models/instance.rs @@ -44,19 +44,8 @@ impl Instance { Instance::get_local(conn).unwrap().id } - pub fn insert<'a>(conn: &PgConnection, pub_dom: String, name: String, local: bool) -> Instance { - diesel::insert_into(instances::table) - .values(NewInstance { - public_domain: pub_dom, - name: name, - local: local - }) - .get_result(conn) - .expect("Error saving new instance") - } - + insert!(instances, NewInstance); get!(instances); - find_by!(instances, find_by_domain, public_domain as String); pub fn block(&self) { diff --git a/src/models/likes.rs b/src/models/likes.rs index e1ad3c1..537a457 100644 --- a/src/models/likes.rs +++ b/src/models/likes.rs @@ -35,12 +35,9 @@ pub struct NewLike { } impl Like { - pub fn insert(conn: &PgConnection, new: NewLike) -> Like { - diesel::insert_into(likes::table) - .values(new) - .get_result(conn) - .expect("Unable to insert new like") - } + insert!(likes, NewLike); + get!(likes); + find_by!(likes, find_by_ap_url, ap_url as String); pub fn update_ap_url(&self, conn: &PgConnection) { if self.ap_url.len() == 0 { @@ -50,10 +47,6 @@ impl Like { } } - get!(likes); - - find_by!(likes, find_by_ap_url, ap_url as String); - pub fn find_by_user_on_post(conn: &PgConnection, user: &User, post: &Post) -> Option { likes::table.filter(likes::post_id.eq(post.id)) .filter(likes::user_id.eq(user.id)) diff --git a/src/models/mod.rs b/src/models/mod.rs index 1bab1a7..e4bc0d7 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -24,6 +24,17 @@ macro_rules! get { }; } +macro_rules! insert { + ($table:ident, $from:ident) => { + pub fn insert(conn: &PgConnection, new: $from) -> Self { + diesel::insert_into($table::table) + .values(new) + .get_result(conn) + .expect("Error saving new $table") + } + }; +} + pub mod blog_authors; pub mod blogs; pub mod comments; diff --git a/src/models/notifications.rs b/src/models/notifications.rs index 6857e1f..c5b7d59 100644 --- a/src/models/notifications.rs +++ b/src/models/notifications.rs @@ -26,13 +26,7 @@ pub struct NewNotification { } impl Notification { - pub fn insert(conn: &PgConnection, new: NewNotification) -> Notification { - diesel::insert_into(notifications::table) - .values(new) - .get_result(conn) - .expect("Couldn't save notification") - } - + insert!(notifications, NewNotification); get!(notifications); pub fn find_for_user(conn: &PgConnection, user: &User) -> Vec { diff --git a/src/models/post_authors.rs b/src/models/post_authors.rs index 4806f02..174507c 100644 --- a/src/models/post_authors.rs +++ b/src/models/post_authors.rs @@ -23,12 +23,6 @@ pub struct NewPostAuthor { } impl PostAuthor { - pub fn insert(conn: &PgConnection, new: NewPostAuthor) -> PostAuthor { - diesel::insert_into(post_authors::table) - .values(new) - .get_result(conn) - .expect("Error saving new blog author") - } - + insert!(post_authors, NewPostAuthor); get!(post_authors); } diff --git a/src/models/posts.rs b/src/models/posts.rs index dafbbbd..67068bd 100644 --- a/src/models/posts.rs +++ b/src/models/posts.rs @@ -50,13 +50,7 @@ pub struct NewPost { } impl Post { - pub fn insert(conn: &PgConnection, new: NewPost) -> Post { - diesel::insert_into(posts::table) - .values(new) - .get_result(conn) - .expect("Error saving new post") - } - + insert!(posts, NewPost); get!(posts); pub fn count_local(conn: &PgConnection) -> usize { diff --git a/src/models/reshares.rs b/src/models/reshares.rs index f694170..6a4b240 100644 --- a/src/models/reshares.rs +++ b/src/models/reshares.rs @@ -24,13 +24,7 @@ pub struct NewReshare { } impl Reshare { - pub fn insert(conn: &PgConnection, new: NewReshare) -> Reshare { - diesel::insert_into(reshares::table) - .values(new) - .get_result(conn) - .expect("Couldn't save reshare") - } - + insert!(reshares, NewReshare); get!(reshares); pub fn update_ap_url(&self, conn: &PgConnection) { diff --git a/src/models/users.rs b/src/models/users.rs index 2b7708d..2a956fa 100644 --- a/src/models/users.rs +++ b/src/models/users.rs @@ -38,7 +38,7 @@ use models::{ blogs::Blog, blog_authors::BlogAuthor, follows::Follow, - instance::Instance, + instance::*, post_authors::PostAuthor, posts::Post }; @@ -85,6 +85,8 @@ pub struct NewUser { } impl User { + insert!(users, NewUser); + pub fn grant_admin_rights(&self, conn: &PgConnection) { diesel::update(self) .set(users::is_admin.eq(true)) @@ -92,13 +94,6 @@ impl User { .expect("Couldn't grant admin rights"); } - pub fn insert(conn: &PgConnection, new: NewUser) -> User { - diesel::insert_into(users::table) - .values(new) - .get_result(conn) - .expect("Error saving new user") - } - pub fn update(&self, conn: &PgConnection, name: String, email: String, summary: String) -> User { diesel::update(self) .set(( @@ -178,7 +173,11 @@ impl User { let instance = match Instance::find_by_domain(conn, inst.clone()) { Some(instance) => instance, None => { - Instance::insert(conn, inst.clone(), inst.clone(), false) + Instance::insert(conn, NewInstance { + name: inst.clone(), + public_domain: inst.clone(), + local: false + }) } }; User::insert(conn, NewUser { diff --git a/src/routes/instance.rs b/src/routes/instance.rs index f366a39..a5dd54d 100644 --- a/src/routes/instance.rs +++ b/src/routes/instance.rs @@ -58,11 +58,11 @@ struct NewInstanceForm { #[post("/configure", data = "")] fn post_config(conn: DbConn, data: Form) -> Redirect { let form = data.get(); - let inst = Instance::insert( - &*conn, - BASE_URL.as_str().to_string(), - form.name.to_string(), - true); + let inst = Instance::insert(&*conn, NewInstance { + public_domain: BASE_URL.as_str().to_string(), + name: form.name.to_string(), + local: true + }); if inst.has_admin(&*conn) { Redirect::to("/") } else {