2018-06-20 21:06:34 +02:00
|
|
|
use activitypub::link;
|
2018-11-24 12:44:17 +01:00
|
|
|
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl};
|
2018-06-20 20:22:34 +02:00
|
|
|
|
2018-06-23 18:36:11 +02:00
|
|
|
use comments::Comment;
|
|
|
|
use notifications::*;
|
2018-11-24 12:44:17 +01:00
|
|
|
use plume_common::activity_pub::inbox::Notify;
|
2018-06-23 18:36:11 +02:00
|
|
|
use posts::Post;
|
2018-06-20 20:22:34 +02:00
|
|
|
use schema::mentions;
|
2018-11-24 12:44:17 +01:00
|
|
|
use users::User;
|
2018-12-29 09:36:07 +01:00
|
|
|
use {Connection, Error, Result};
|
2018-06-20 20:22:34 +02:00
|
|
|
|
2018-09-27 23:06:40 +02:00
|
|
|
#[derive(Clone, 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-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>,
|
2018-06-20 20:22:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Mention {
|
|
|
|
insert!(mentions, NewMention);
|
|
|
|
get!(mentions);
|
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-12-29 09:36:07 +01:00
|
|
|
pub fn get_mentioned(&self, conn: &Connection) -> Result<User> {
|
2018-06-20 21:06:34 +02:00
|
|
|
User::get(conn, self.mentioned_id)
|
|
|
|
}
|
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn get_post(&self, conn: &Connection) -> Result<Post> {
|
|
|
|
self.post_id.ok_or(Error::NotFound).and_then(|id| Post::get(conn, id))
|
2018-06-20 20:22:34 +02:00
|
|
|
}
|
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn get_comment(&self, conn: &Connection) -> Result<Comment> {
|
|
|
|
self.comment_id.ok_or(Error::NotFound).and_then(|id| Comment::get(conn, id))
|
2018-06-20 20:22:34 +02:00
|
|
|
}
|
2018-06-20 21:06:34 +02:00
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn get_user(&self, conn: &Connection) -> Result<User> {
|
2018-07-26 15:46:10 +02:00
|
|
|
match self.get_post(conn) {
|
2018-12-29 09:36:07 +01:00
|
|
|
Ok(p) => Ok(p.get_authors(conn)?.into_iter().next()?),
|
|
|
|
Err(_) => self.get_comment(conn).and_then(|c| c.get_author(conn)),
|
2018-07-26 15:46:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn build_activity(conn: &Connection, ment: &str) -> Result<link::Mention> {
|
|
|
|
let user = User::find_by_fqn(conn, ment)?;
|
2018-06-20 22:58:11 +02:00
|
|
|
let mut mention = link::Mention::default();
|
2018-11-24 12:44:17 +01:00
|
|
|
mention
|
|
|
|
.link_props
|
2018-12-29 09:36:07 +01:00
|
|
|
.set_href_string(user.ap_url)?;
|
2018-11-24 12:44:17 +01:00
|
|
|
mention
|
|
|
|
.link_props
|
2018-12-29 09:36:07 +01:00
|
|
|
.set_name_string(format!("@{}", ment))?;
|
|
|
|
Ok(mention)
|
2018-06-20 22:58:11 +02:00
|
|
|
}
|
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn to_activity(&self, conn: &Connection) -> Result<link::Mention> {
|
|
|
|
let user = self.get_mentioned(conn)?;
|
2018-06-20 21:06:34 +02:00
|
|
|
let mut mention = link::Mention::default();
|
2018-11-24 12:44:17 +01:00
|
|
|
mention
|
|
|
|
.link_props
|
2018-12-29 09:36:07 +01:00
|
|
|
.set_href_string(user.ap_url.clone())?;
|
2018-11-24 12:44:17 +01:00
|
|
|
mention
|
|
|
|
.link_props
|
2018-12-29 09:36:07 +01:00
|
|
|
.set_name_string(format!("@{}", user.get_fqn(conn)))?;
|
|
|
|
Ok(mention)
|
2018-06-20 21:06:34 +02:00
|
|
|
}
|
|
|
|
|
2018-11-24 12:44:17 +01:00
|
|
|
pub fn from_activity(
|
|
|
|
conn: &Connection,
|
2018-11-26 10:21:52 +01:00
|
|
|
ment: &link::Mention,
|
2018-11-24 12:44:17 +01:00
|
|
|
inside: i32,
|
|
|
|
in_post: bool,
|
|
|
|
notify: bool,
|
2018-12-29 09:36:07 +01:00
|
|
|
) -> Result<Self> {
|
2018-07-11 22:11:31 +02:00
|
|
|
let ap_url = ment.link_props.href_string().ok()?;
|
2018-11-26 10:21:52 +01:00
|
|
|
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 {
|
2018-12-29 09:36:07 +01:00
|
|
|
Post::get(conn, inside).and_then(|post| {
|
2018-11-24 12:44:17 +01:00
|
|
|
let res = Mention::insert(
|
|
|
|
conn,
|
|
|
|
NewMention {
|
|
|
|
mentioned_id: mentioned.id,
|
|
|
|
post_id: Some(post.id),
|
|
|
|
comment_id: None,
|
|
|
|
},
|
2018-12-29 09:36:07 +01:00
|
|
|
)?;
|
2018-09-08 13:52:27 +02:00
|
|
|
if notify {
|
2018-12-29 09:36:07 +01:00
|
|
|
res.notify(conn)?;
|
2018-09-08 13:52:27 +02:00
|
|
|
}
|
2018-12-29 09:36:07 +01:00
|
|
|
Ok(res)
|
2018-06-21 15:05:35 +02:00
|
|
|
})
|
2018-06-20 21:06:34 +02:00
|
|
|
} else {
|
2018-12-29 09:36:07 +01:00
|
|
|
Comment::get(conn, inside).and_then(|comment| {
|
2018-11-24 12:44:17 +01:00
|
|
|
let res = Mention::insert(
|
|
|
|
conn,
|
|
|
|
NewMention {
|
|
|
|
mentioned_id: mentioned.id,
|
|
|
|
post_id: None,
|
|
|
|
comment_id: Some(comment.id),
|
|
|
|
},
|
2018-12-29 09:36:07 +01:00
|
|
|
)?;
|
2018-09-08 13:52:27 +02:00
|
|
|
if notify {
|
2018-12-29 09:36:07 +01:00
|
|
|
res.notify(conn)?;
|
2018-09-08 13:52:27 +02:00
|
|
|
}
|
2018-12-29 09:36:07 +01:00
|
|
|
Ok(res)
|
2018-06-21 15:05:35 +02:00
|
|
|
})
|
2018-06-20 21:06:34 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-27 21:48:38 +02:00
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn delete(&self, conn: &Connection) -> Result<()> {
|
2018-10-27 21:48:38 +02:00
|
|
|
//find related notifications and delete them
|
2018-12-29 09:36:07 +01:00
|
|
|
if let Ok(n) = Notification::find(conn, notification_kind::MENTION, self.id) {
|
|
|
|
n.delete(conn)?;
|
2018-11-26 10:21:52 +01:00
|
|
|
}
|
2018-11-24 12:44:17 +01:00
|
|
|
diesel::delete(self)
|
|
|
|
.execute(conn)
|
2018-12-29 09:36:07 +01:00
|
|
|
.map(|_| ())
|
|
|
|
.map_err(Error::from)
|
2018-10-27 21:48:38 +02:00
|
|
|
}
|
2018-06-20 20:22:34 +02:00
|
|
|
}
|
2018-06-20 22:05:30 +02:00
|
|
|
|
2018-09-27 23:06:40 +02:00
|
|
|
impl Notify<Connection> for Mention {
|
2018-12-29 09:36:07 +01:00
|
|
|
type Error = Error;
|
|
|
|
fn notify(&self, conn: &Connection) -> Result<()> {
|
|
|
|
let m = self.get_mentioned(conn)?;
|
|
|
|
Notification::insert(
|
|
|
|
conn,
|
|
|
|
NewNotification {
|
|
|
|
kind: notification_kind::MENTION.to_string(),
|
|
|
|
object_id: self.id,
|
|
|
|
user_id: m.id,
|
|
|
|
},
|
|
|
|
).map(|_| ())
|
2018-06-20 22:05:30 +02:00
|
|
|
}
|
|
|
|
}
|