Fix follow IDs (#455)

* Generate valid IDs for Follow

Fixes #449

* Use the new post-insert hook for all the models

* Fix plume-cli build
This commit is contained in:
Baptiste Gelez
2019-03-04 21:35:03 +01:00
committed by GitHub
parent 9b48b8a846
commit 2a188abfa1
12 changed files with 162 additions and 181 deletions
+40 -3
View File
@@ -3,7 +3,7 @@ use activitypub::{
actor::Person,
Actor,
};
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl};
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl, SaveChangesDsl};
use blogs::Blog;
use notifications::*;
@@ -17,7 +17,7 @@ use schema::follows;
use users::User;
use {ap_url, Connection, BASE_URL, Error, Result};
#[derive(Clone, Queryable, Identifiable, Associations)]
#[derive(Clone, Queryable, Identifiable, Associations, AsChangeset)]
#[belongs_to(User, foreign_key = "following_id")]
pub struct Follow {
pub id: i32,
@@ -35,7 +35,14 @@ pub struct NewFollow {
}
impl Follow {
insert!(follows, NewFollow);
insert!(follows, NewFollow, |inserted, conn| {
if inserted.ap_url.is_empty() {
inserted.ap_url = ap_url(&format!("{}/follows/{}", *BASE_URL, inserted.id));
inserted.save_changes(conn).map_err(Error::from)
} else {
Ok(inserted)
}
});
get!(follows);
find_by!(follows, find_by_ap_url, ap_url as &str);
@@ -200,3 +207,33 @@ impl IntoId for Follow {
Id::new(self.ap_url)
}
}
#[cfg(test)]
mod tests {
use diesel::Connection;
use super::*;
use tests::db;
use users::tests as user_tests;
#[test]
fn test_id() {
let conn = db();
conn.test_transaction::<_, (), _>(|| {
let users = user_tests::fill_database(&conn);
let follow = Follow::insert(&conn, NewFollow {
follower_id: users[0].id,
following_id: users[1].id,
ap_url: String::new(),
}).expect("Couldn't insert new follow");
assert_eq!(follow.ap_url, format!("https://{}/follows/{}", *BASE_URL, follow.id));
let follow = Follow::insert(&conn, NewFollow {
follower_id: users[1].id,
following_id: users[0].id,
ap_url: String::from("https://some.url/"),
}).expect("Couldn't insert new follow");
assert_eq!(follow.ap_url, String::from("https://some.url/"));
Ok(())
});
}
}