Add unit tests for main model parts (#310)
Add tests for following models: - Blog - Instance - Media - User
This commit is contained in:
committed by
Baptiste Gelez
parent
0b9727ed28
commit
8a4702df92
@@ -1,13 +1,13 @@
|
||||
use activitypub::link;
|
||||
use diesel::{self, QueryDsl, RunQueryDsl, ExpressionMethods};
|
||||
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl};
|
||||
|
||||
use plume_common::activity_pub::inbox::Notify;
|
||||
use Connection;
|
||||
use comments::Comment;
|
||||
use notifications::*;
|
||||
use plume_common::activity_pub::inbox::Notify;
|
||||
use posts::Post;
|
||||
use users::User;
|
||||
use schema::mentions;
|
||||
use users::User;
|
||||
use Connection;
|
||||
|
||||
#[derive(Clone, Queryable, Identifiable, Serialize, Deserialize)]
|
||||
pub struct Mention {
|
||||
@@ -15,7 +15,7 @@ pub struct Mention {
|
||||
pub mentioned_id: i32,
|
||||
pub post_id: Option<i32>,
|
||||
pub comment_id: Option<i32>,
|
||||
pub ap_url: String // TODO: remove, since mentions don't have an AP URL actually, this field was added by mistake
|
||||
pub ap_url: String, // TODO: remove, since mentions don't have an AP URL actually, this field was added by mistake
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
@@ -24,7 +24,7 @@ pub struct NewMention {
|
||||
pub mentioned_id: i32,
|
||||
pub post_id: Option<i32>,
|
||||
pub comment_id: Option<i32>,
|
||||
pub ap_url: String
|
||||
pub ap_url: String,
|
||||
}
|
||||
|
||||
impl Mention {
|
||||
@@ -50,38 +50,62 @@ impl Mention {
|
||||
pub fn get_user(&self, conn: &Connection) -> Option<User> {
|
||||
match self.get_post(conn) {
|
||||
Some(p) => p.get_authors(conn).into_iter().next(),
|
||||
None => self.get_comment(conn).map(|c| c.get_author(conn))
|
||||
None => self.get_comment(conn).map(|c| c.get_author(conn)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build_activity(conn: &Connection, ment: String) -> link::Mention {
|
||||
let user = User::find_by_fqn(conn, ment.clone());
|
||||
let mut mention = link::Mention::default();
|
||||
mention.link_props.set_href_string(user.clone().map(|u| u.ap_url).unwrap_or(String::new())).expect("Mention::build_activity: href error");
|
||||
mention.link_props.set_name_string(format!("@{}", ment)).expect("Mention::build_activity: name error:");
|
||||
mention
|
||||
.link_props
|
||||
.set_href_string(user.clone().map(|u| u.ap_url).unwrap_or(String::new()))
|
||||
.expect("Mention::build_activity: href error");
|
||||
mention
|
||||
.link_props
|
||||
.set_name_string(format!("@{}", ment))
|
||||
.expect("Mention::build_activity: name error:");
|
||||
mention
|
||||
}
|
||||
|
||||
pub fn to_activity(&self, conn: &Connection) -> link::Mention {
|
||||
let user = self.get_mentioned(conn);
|
||||
let mut mention = link::Mention::default();
|
||||
mention.link_props.set_href_string(user.clone().map(|u| u.ap_url).unwrap_or(String::new())).expect("Mention::to_activity: href error");
|
||||
mention.link_props.set_name_string(user.map(|u| format!("@{}", u.get_fqn(conn))).unwrap_or(String::new())).expect("Mention::to_activity: mention error");
|
||||
mention
|
||||
.link_props
|
||||
.set_href_string(user.clone().map(|u| u.ap_url).unwrap_or(String::new()))
|
||||
.expect("Mention::to_activity: href error");
|
||||
mention
|
||||
.link_props
|
||||
.set_name_string(
|
||||
user.map(|u| format!("@{}", u.get_fqn(conn)))
|
||||
.unwrap_or(String::new()),
|
||||
)
|
||||
.expect("Mention::to_activity: mention error");
|
||||
mention
|
||||
}
|
||||
|
||||
pub fn from_activity(conn: &Connection, ment: link::Mention, inside: i32, in_post: bool, notify: bool) -> Option<Self> {
|
||||
pub fn from_activity(
|
||||
conn: &Connection,
|
||||
ment: link::Mention,
|
||||
inside: i32,
|
||||
in_post: bool,
|
||||
notify: bool,
|
||||
) -> Option<Self> {
|
||||
let ap_url = ment.link_props.href_string().ok()?;
|
||||
let mentioned = User::find_by_ap_url(conn, ap_url)?;
|
||||
|
||||
if in_post {
|
||||
Post::get(conn, inside.clone().into()).map(|post| {
|
||||
let res = Mention::insert(conn, NewMention {
|
||||
mentioned_id: mentioned.id,
|
||||
post_id: Some(post.id),
|
||||
comment_id: None,
|
||||
ap_url: ment.link_props.href_string().unwrap_or(String::new())
|
||||
});
|
||||
let res = Mention::insert(
|
||||
conn,
|
||||
NewMention {
|
||||
mentioned_id: mentioned.id,
|
||||
post_id: Some(post.id),
|
||||
comment_id: None,
|
||||
ap_url: ment.link_props.href_string().unwrap_or(String::new()),
|
||||
},
|
||||
);
|
||||
if notify {
|
||||
res.notify(conn);
|
||||
}
|
||||
@@ -89,12 +113,15 @@ impl Mention {
|
||||
})
|
||||
} else {
|
||||
Comment::get(conn, inside.into()).map(|comment| {
|
||||
let res = Mention::insert(conn, NewMention {
|
||||
mentioned_id: mentioned.id,
|
||||
post_id: None,
|
||||
comment_id: Some(comment.id),
|
||||
ap_url: ment.link_props.href_string().unwrap_or(String::new())
|
||||
});
|
||||
let res = Mention::insert(
|
||||
conn,
|
||||
NewMention {
|
||||
mentioned_id: mentioned.id,
|
||||
post_id: None,
|
||||
comment_id: Some(comment.id),
|
||||
ap_url: ment.link_props.href_string().unwrap_or(String::new()),
|
||||
},
|
||||
);
|
||||
if notify {
|
||||
res.notify(conn);
|
||||
}
|
||||
@@ -106,18 +133,23 @@ impl Mention {
|
||||
pub fn delete(&self, conn: &Connection) {
|
||||
//find related notifications and delete them
|
||||
Notification::find(conn, notification_kind::MENTION, self.id).map(|n| n.delete(conn));
|
||||
diesel::delete(self).execute(conn).expect("Mention::delete: mention deletion error");
|
||||
diesel::delete(self)
|
||||
.execute(conn)
|
||||
.expect("Mention::delete: mention deletion error");
|
||||
}
|
||||
}
|
||||
|
||||
impl Notify<Connection> for Mention {
|
||||
fn notify(&self, conn: &Connection) {
|
||||
self.get_mentioned(conn).map(|m| {
|
||||
Notification::insert(conn, NewNotification {
|
||||
kind: notification_kind::MENTION.to_string(),
|
||||
object_id: self.id,
|
||||
user_id: m.id
|
||||
});
|
||||
Notification::insert(
|
||||
conn,
|
||||
NewNotification {
|
||||
kind: notification_kind::MENTION.to_string(),
|
||||
object_id: self.id,
|
||||
user_id: m.id,
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user