2018-06-20 21:06:34 +02:00
|
|
|
use activitypub::link;
|
2018-06-20 20:22:34 +02:00
|
|
|
use diesel::{self, PgConnection, QueryDsl, RunQueryDsl, ExpressionMethods};
|
|
|
|
|
2018-06-20 22:05:30 +02:00
|
|
|
use activity_pub::{Id, inbox::Notify};
|
2018-06-20 20:22:34 +02:00
|
|
|
use models::{
|
|
|
|
comments::Comment,
|
2018-06-20 22:05:30 +02:00
|
|
|
notifications::*,
|
2018-06-20 21:06:34 +02:00
|
|
|
posts::Post,
|
|
|
|
users::User
|
2018-06-20 20:22:34 +02:00
|
|
|
};
|
|
|
|
use schema::mentions;
|
|
|
|
|
|
|
|
#[derive(Queryable, Identifiable)]
|
|
|
|
pub struct Mention {
|
|
|
|
pub id: i32,
|
|
|
|
pub mentioned_id: i32,
|
|
|
|
pub post_id: Option<i32>,
|
2018-06-20 22:05:30 +02:00
|
|
|
pub comment_id: Option<i32>,
|
|
|
|
pub ap_url: String
|
2018-06-20 20:22:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Insertable)]
|
|
|
|
#[table_name = "mentions"]
|
|
|
|
pub struct NewMention {
|
|
|
|
pub mentioned_id: i32,
|
|
|
|
pub post_id: Option<i32>,
|
2018-06-20 22:05:30 +02:00
|
|
|
pub comment_id: Option<i32>,
|
|
|
|
pub ap_url: String
|
2018-06-20 20:22:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Mention {
|
|
|
|
insert!(mentions, NewMention);
|
|
|
|
get!(mentions);
|
2018-06-20 22:05:30 +02:00
|
|
|
find_by!(mentions, find_by_ap_url, ap_url as String);
|
2018-06-20 20:25:43 +02:00
|
|
|
list_by!(mentions, list_for_user, mentioned_id as i32);
|
2018-06-20 22:58:11 +02:00
|
|
|
list_by!(mentions, list_for_post, post_id as i32);
|
2018-06-20 20:22:34 +02:00
|
|
|
|
2018-06-20 21:06:34 +02:00
|
|
|
pub fn get_mentioned(&self, conn: &PgConnection) -> Option<User> {
|
|
|
|
User::get(conn, self.mentioned_id)
|
|
|
|
}
|
|
|
|
|
2018-06-20 20:22:34 +02:00
|
|
|
pub fn get_post(&self, conn: &PgConnection) -> Option<Post> {
|
|
|
|
self.post_id.and_then(|id| Post::get(conn, id))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_comment(&self, conn: &PgConnection) -> Option<Comment> {
|
|
|
|
self.post_id.and_then(|id| Comment::get(conn, id))
|
|
|
|
}
|
2018-06-20 21:06:34 +02:00
|
|
|
|
2018-06-20 22:58:11 +02:00
|
|
|
pub fn build_activity(conn: &PgConnection, 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("Error setting mention's href");
|
|
|
|
mention.link_props.set_name_string(format!("@{}", ment)).expect("Error setting mention's name");
|
|
|
|
mention
|
|
|
|
}
|
|
|
|
|
2018-06-20 21:06:34 +02:00
|
|
|
pub fn to_activity(&self, conn: &PgConnection) -> 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("Error setting mention's href");
|
|
|
|
mention.link_props.set_name_string(user.map(|u| format!("@{}", u.get_fqn(conn))).unwrap_or(String::new())).expect("Error setting mention's name");
|
|
|
|
mention
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_activity(conn: &PgConnection, ment: link::Mention, inside: Id) -> Option<Self> {
|
|
|
|
let mentioned = User::find_by_ap_url(conn, ment.link_props.href_string().unwrap()).unwrap();
|
|
|
|
|
|
|
|
if let Some(post) = Post::find_by_ap_url(conn, inside.clone().into()) {
|
2018-06-20 22:05:30 +02:00
|
|
|
let res = Some(Mention::insert(conn, NewMention {
|
2018-06-20 21:06:34 +02:00
|
|
|
mentioned_id: mentioned.id,
|
|
|
|
post_id: Some(post.id),
|
2018-06-20 22:05:30 +02:00
|
|
|
comment_id: None,
|
|
|
|
ap_url: ment.link_props.href_string().unwrap_or(String::new())
|
|
|
|
}));
|
|
|
|
Mention::notify(conn, ment, Id::new(String::new()));
|
|
|
|
res
|
2018-06-20 21:06:34 +02:00
|
|
|
} else {
|
|
|
|
if let Some(comment) = Comment::find_by_ap_url(conn, inside.into()) {
|
2018-06-20 22:05:30 +02:00
|
|
|
let res =Some(Mention::insert(conn, NewMention {
|
2018-06-20 21:06:34 +02:00
|
|
|
mentioned_id: mentioned.id,
|
|
|
|
post_id: None,
|
2018-06-20 22:05:30 +02:00
|
|
|
comment_id: Some(comment.id),
|
|
|
|
ap_url: ment.link_props.href_string().unwrap_or(String::new())
|
|
|
|
}));
|
|
|
|
Mention::notify(conn, ment, Id::new(String::new()));
|
|
|
|
res
|
2018-06-20 21:06:34 +02:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-20 20:22:34 +02:00
|
|
|
}
|
2018-06-20 22:05:30 +02:00
|
|
|
|
|
|
|
impl Notify<link::Mention> for Mention {
|
|
|
|
fn notify(conn: &PgConnection, ment: link::Mention, _actor: Id) {
|
|
|
|
match Mention::find_by_ap_url(conn, ment.link_props.href_string().unwrap()) {
|
|
|
|
Some(mention) => {
|
|
|
|
let author = mention.get_comment(conn)
|
|
|
|
.map(|c| c.get_author(conn).display_name.clone())
|
|
|
|
.unwrap_or(mention.get_post(conn).unwrap().get_authors(conn)[0].display_name.clone());
|
|
|
|
|
|
|
|
mention.get_mentioned(conn).map(|m| {
|
|
|
|
Notification::insert(conn, NewNotification {
|
|
|
|
title: "{{ data }} mentioned you.".to_string(),
|
|
|
|
data: Some(author),
|
|
|
|
content: None,
|
|
|
|
link: Some(mention.get_post(conn).map(|p| p.ap_url).unwrap_or(mention.get_comment(conn).unwrap().ap_url.unwrap_or(String::new()))),
|
|
|
|
user_id: m.id
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
None => println!("Couldn't find mention by AP URL, to create a new notification")
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|