Give reshare and like ap_url before inserting (#369)

Fix #367
This commit is contained in:
fdb-hiroshima 2018-12-23 11:13:02 +01:00 committed by GitHub
parent 0ea1d57e48
commit 0df9c4d400
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 42 deletions

View File

@ -35,19 +35,6 @@ impl Like {
find_by!(likes, find_by_ap_url, ap_url as &str); 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); 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 { pub fn to_activity(&self, conn: &Connection) -> activity::Like {
let mut act = activity::Like::default(); let mut act = activity::Like::default();
act.like_props act.like_props
@ -170,3 +157,14 @@ impl Deletable<Connection, activity::Undo> 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
}
}
}

View File

@ -40,23 +40,6 @@ impl Reshare {
post_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(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<Reshare> { pub fn get_recents_for_author(conn: &Connection, user: &User, limit: i64) -> Vec<Reshare> {
reshares::table reshares::table
.filter(reshares::user_id.eq(user.id)) .filter(reshares::user_id.eq(user.id))
@ -201,3 +184,14 @@ impl Deletable<Connection, Undo> 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
}
}
}

View File

@ -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)?; let post = Post::find_by_slug(&*conn, &slug, b.id)?;
if !user.has_liked(&*conn, &post) { if !user.has_liked(&*conn, &post) {
let like = likes::Like::insert(&*conn, likes::NewLike { let like = likes::Like::insert(&*conn, likes::NewLike::new(&post ,&user));
post_id: post.id,
user_id: user.id,
ap_url: "".to_string()
});
like.update_ap_url(&*conn);
like.notify(&*conn); like.notify(&*conn);
let dest = User::one_by_instance(&*conn); let dest = User::one_by_instance(&*conn);

View File

@ -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)?; let post = Post::find_by_slug(&*conn, &slug, b.id)?;
if !user.has_reshared(&*conn, &post) { if !user.has_reshared(&*conn, &post) {
let reshare = Reshare::insert(&*conn, NewReshare { let reshare = Reshare::insert(&*conn, NewReshare::new(&post, &user));
post_id: post.id,
user_id: user.id,
ap_url: "".to_string()
});
reshare.update_ap_url(&*conn);
reshare.notify(&*conn); reshare.notify(&*conn);
let dest = User::one_by_instance(&*conn); let dest = User::one_by_instance(&*conn);