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-23 18:36:11 +02:00
|
|
|
use plume_common::activity_pub::inbox::Notify;
|
2018-09-26 17:22:42 +02:00
|
|
|
use Connection;
|
2018-06-23 18:36:11 +02:00
|
|
|
use comments::Comment;
|
|
|
|
use notifications::*;
|
|
|
|
use posts::Post;
|
|
|
|
use users::User;
|
2018-06-20 20:22:34 +02:00
|
|
|
use schema::mentions;
|
|
|
|
|
2018-06-21 15:05:35 +02:00
|
|
|
#[derive(Queryable, Identifiable, Serialize, Deserialize)]
|
2018-06-20 20:22:34 +02:00
|
|
|
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>,
|
2018-06-21 15:05:35 +02:00
|
|
|
pub ap_url: String // TODO: remove, since mentions don't have an AP URL actually, this field was added by mistake
|
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-21 15:05:35 +02:00
|
|
|
list_by!(mentions, list_for_comment, comment_id as i32);
|
2018-06-20 20:22:34 +02:00
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn get_mentioned(&self, conn: &Connection) -> Option<User> {
|
2018-06-20 21:06:34 +02:00
|
|
|
User::get(conn, self.mentioned_id)
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn get_post(&self, conn: &Connection) -> Option<Post> {
|
2018-06-20 20:22:34 +02:00
|
|
|
self.post_id.and_then(|id| Post::get(conn, id))
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn get_comment(&self, conn: &Connection) -> Option<Comment> {
|
2018-06-21 15:05:35 +02:00
|
|
|
self.comment_id.and_then(|id| Comment::get(conn, id))
|
2018-06-20 20:22:34 +02:00
|
|
|
}
|
2018-06-20 21:06:34 +02:00
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn get_user(&self, conn: &Connection) -> Option<User> {
|
2018-07-26 15:46:10 +02:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn build_activity(conn: &Connection, ment: String) -> link::Mention {
|
2018-06-20 22:58:11 +02:00
|
|
|
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-09-26 17:22:42 +02:00
|
|
|
pub fn to_activity(&self, conn: &Connection) -> link::Mention {
|
2018-06-20 21:06:34 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:22:42 +02:00
|
|
|
pub fn from_activity(conn: &Connection, ment: link::Mention, inside: i32, in_post: bool, notify: bool) -> Option<Self> {
|
2018-07-11 22:11:31 +02:00
|
|
|
let ap_url = ment.link_props.href_string().ok()?;
|
|
|
|
let mentioned = User::find_by_ap_url(conn, ap_url)?;
|
2018-06-20 21:06:34 +02:00
|
|
|
|
2018-06-21 15:05:35 +02:00
|
|
|
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())
|
|
|
|
});
|
2018-09-08 13:52:27 +02:00
|
|
|
if notify {
|
|
|
|
res.notify(conn);
|
|
|
|
}
|
2018-06-21 15:05:35 +02:00
|
|
|
res
|
|
|
|
})
|
2018-06-20 21:06:34 +02:00
|
|
|
} else {
|
2018-06-21 15:05:35 +02:00
|
|
|
Comment::get(conn, inside.into()).map(|comment| {
|
2018-06-20 23:51:47 +02:00
|
|
|
let res = 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())
|
2018-06-20 23:51:47 +02:00
|
|
|
});
|
2018-09-08 13:52:27 +02:00
|
|
|
if notify {
|
|
|
|
res.notify(conn);
|
|
|
|
}
|
2018-06-21 15:05:35 +02:00
|
|
|
res
|
|
|
|
})
|
2018-06-20 21:06:34 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-20 20:22:34 +02:00
|
|
|
}
|
2018-06-20 22:05:30 +02:00
|
|
|
|
2018-06-23 18:36:11 +02:00
|
|
|
impl Notify<PgConnection> for Mention {
|
2018-09-26 17:22:42 +02:00
|
|
|
fn notify(&self, conn: &Connection) {
|
2018-06-20 23:51:47 +02:00
|
|
|
self.get_mentioned(conn).map(|m| {
|
|
|
|
Notification::insert(conn, NewNotification {
|
2018-07-26 15:46:10 +02:00
|
|
|
kind: notification_kind::MENTION.to_string(),
|
|
|
|
object_id: self.id,
|
2018-06-20 23:51:47 +02:00
|
|
|
user_id: m.id
|
|
|
|
});
|
|
|
|
});
|
2018-06-20 22:05:30 +02:00
|
|
|
}
|
|
|
|
}
|