From 0df9c4d4000bd78ae661a6454331e726d66e34cc Mon Sep 17 00:00:00 2001 From: fdb-hiroshima <35889323+fdb-hiroshima@users.noreply.github.com> Date: Sun, 23 Dec 2018 11:13:02 +0100 Subject: [PATCH] Give reshare and like ap_url before inserting (#369) Fix #367 --- plume-models/src/likes.rs | 24 +++++++++++------------- plume-models/src/reshares.rs | 28 +++++++++++----------------- src/routes/likes.rs | 7 +------ src/routes/reshares.rs | 7 +------ 4 files changed, 24 insertions(+), 42 deletions(-) diff --git a/plume-models/src/likes.rs b/plume-models/src/likes.rs index eb043b6e..0480e1e6 100644 --- a/plume-models/src/likes.rs +++ b/plume-models/src/likes.rs @@ -35,19 +35,6 @@ impl Like { find_by!(likes, find_by_ap_url, ap_url as &str); find_by!(likes, find_by_user_on_post, user_id as i32, post_id as i32); - pub fn update_ap_url(&self, conn: &Connection) { - if self.ap_url.is_empty() { - diesel::update(self) - .set(likes::ap_url.eq(format!( - "{}/like/{}", - User::get(conn, self.user_id).expect("Like::update_ap_url: user error").ap_url, - Post::get(conn, self.post_id).expect("Like::update_ap_url: post error").ap_url - ))) - .execute(conn) - .expect("Like::update_ap_url: update error"); - } - } - pub fn to_activity(&self, conn: &Connection) -> activity::Like { let mut act = activity::Like::default(); act.like_props @@ -170,3 +157,14 @@ impl Deletable for Like { } } } + +impl NewLike { + pub fn new(p: &Post, u: &User) -> Self { + let ap_url = format!("{}/like/{}", u.ap_url, p.ap_url); + NewLike { + post_id: p.id, + user_id: u.id, + ap_url + } + } +} diff --git a/plume-models/src/reshares.rs b/plume-models/src/reshares.rs index 1517112e..3ee771f8 100644 --- a/plume-models/src/reshares.rs +++ b/plume-models/src/reshares.rs @@ -40,23 +40,6 @@ impl Reshare { post_id as i32 ); - pub fn update_ap_url(&self, conn: &Connection) { - if self.ap_url.is_empty() { - diesel::update(self) - .set(reshares::ap_url.eq(format!( - "{}/reshare/{}", - User::get(conn, self.user_id) - .expect("Reshare::update_ap_url: user error") - .ap_url, - Post::get(conn, self.post_id) - .expect("Reshare::update_ap_url: post error") - .ap_url - ))) - .execute(conn) - .expect("Reshare::update_ap_url: update error"); - } - } - pub fn get_recents_for_author(conn: &Connection, user: &User, limit: i64) -> Vec { reshares::table .filter(reshares::user_id.eq(user.id)) @@ -201,3 +184,14 @@ impl Deletable for Reshare { } } } + +impl NewReshare { + pub fn new(p: &Post, u: &User) -> Self { + let ap_url = format!("{}/reshare/{}", u.ap_url, p.ap_url); + NewReshare { + post_id: p.id, + user_id: u.id, + ap_url + } + } +} diff --git a/src/routes/likes.rs b/src/routes/likes.rs index 9ad95c95..2c49afc9 100644 --- a/src/routes/likes.rs +++ b/src/routes/likes.rs @@ -18,12 +18,7 @@ pub fn create(blog: String, slug: String, user: User, conn: DbConn, worker: Work let post = Post::find_by_slug(&*conn, &slug, b.id)?; if !user.has_liked(&*conn, &post) { - let like = likes::Like::insert(&*conn, likes::NewLike { - post_id: post.id, - user_id: user.id, - ap_url: "".to_string() - }); - like.update_ap_url(&*conn); + let like = likes::Like::insert(&*conn, likes::NewLike::new(&post ,&user)); like.notify(&*conn); let dest = User::one_by_instance(&*conn); diff --git a/src/routes/reshares.rs b/src/routes/reshares.rs index 4ad5455d..9c3dda73 100644 --- a/src/routes/reshares.rs +++ b/src/routes/reshares.rs @@ -18,12 +18,7 @@ pub fn create(blog: String, slug: String, user: User, conn: DbConn, worker: Work let post = Post::find_by_slug(&*conn, &slug, b.id)?; if !user.has_reshared(&*conn, &post) { - let reshare = Reshare::insert(&*conn, NewReshare { - post_id: post.id, - user_id: user.id, - ap_url: "".to_string() - }); - reshare.update_ap_url(&*conn); + let reshare = Reshare::insert(&*conn, NewReshare::new(&post, &user)); reshare.notify(&*conn); let dest = User::one_by_instance(&*conn);