Plume/plume-models/src/mentions.rs

144 lines
4.3 KiB
Rust
Raw Normal View History

2020-01-21 07:02:03 +01:00
use crate::{
comments::Comment, notifications::*, posts::Post, schema::mentions, users::User, Connection,
Error, PlumeRocket, Result,
};
use activitypub::link;
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl};
use plume_common::activity_pub::inbox::AsActor;
2018-06-20 20:22:34 +02:00
#[derive(Clone, Queryable, Identifiable)]
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);
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);
list_by!(mentions, list_for_comment, comment_id as i32);
2018-06-20 20:22:34 +02:00
pub fn get_mentioned(&self, conn: &Connection) -> Result<User> {
User::get(conn, self.mentioned_id)
}
pub fn get_post(&self, conn: &Connection) -> Result<Post> {
2019-03-20 17:56:17 +01:00
self.post_id
.ok_or(Error::NotFound)
.and_then(|id| Post::get(conn, id))
2018-06-20 20:22:34 +02:00
}
pub fn get_comment(&self, conn: &Connection) -> Result<Comment> {
2019-03-20 17:56:17 +01:00
self.comment_id
.ok_or(Error::NotFound)
.and_then(|id| Comment::get(conn, id))
2018-06-20 20:22:34 +02:00
}
pub fn get_user(&self, conn: &Connection) -> Result<User> {
2018-07-26 15:46:10 +02:00
match self.get_post(conn) {
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
}
}
Big refactoring of the Inbox (#443) * Big refactoring of the Inbox We now have a type that routes an activity through the registered handlers until one of them matches. Each Actor/Activity/Object combination is represented by an implementation of AsObject These combinations are then registered on the Inbox type, which will try to deserialize the incoming activity in the requested types. Advantages: - nicer syntax: the final API is clearer and more idiomatic - more generic: only two traits (`AsActor` and `AsObject`) instead of one for each kind of activity - it is easier to see which activities we handle and which one we don't * Small fixes - Avoid panics - Don't search for AP ID infinitely - Code style issues * Fix tests * Introduce a new trait: FromId It should be implemented for any AP object. It allows to look for an object in database using its AP ID, or to dereference it if it was not present in database Also moves the inbox code to plume-models to test it (and write a basic test for each activity type we handle) * Use if let instead of match * Don't require PlumeRocket::intl for tests * Return early and remove a forgotten dbg! * Add more tests to try to understand where the issues come from * Also add a test for comment federation * Don't check creation_date is the same for blogs * Make user and blog federation more tolerant to errors/missing fields * Make clippy happy * Use the correct Accept header when dereferencing * Fix follow approval with Mastodon * Add spaces to characters that should not be in usernames And validate blog names too * Smarter dereferencing: only do it once for each actor/object * Forgot some files * Cargo fmt * Delete plume_test * Delete plume_tests * Update get_id docs + Remove useless : Sized * Appease cargo fmt * Remove dbg! + Use as_ref instead of clone when possible + Use and_then instead of map when possible * Remove .po~ * send unfollow to local instance * read cover from update activity * Make sure "cc" and "to" are never empty and fix a typo in a constant name * Cargo fmt
2019-04-17 19:31:47 +02:00
pub fn build_activity(c: &PlumeRocket, ment: &str) -> Result<link::Mention> {
let user = User::find_by_fqn(c, ment)?;
2018-06-20 22:58:11 +02:00
let mut mention = link::Mention::default();
2019-03-20 17:56:17 +01:00
mention.link_props.set_href_string(user.ap_url)?;
mention.link_props.set_name_string(format!("@{}", ment))?;
Ok(mention)
2018-06-20 22:58:11 +02:00
}
pub fn to_activity(&self, conn: &Connection) -> Result<link::Mention> {
let user = self.get_mentioned(conn)?;
let mut mention = link::Mention::default();
2019-03-20 17:56:17 +01:00
mention.link_props.set_href_string(user.ap_url.clone())?;
mention
.link_props
.set_name_string(format!("@{}", user.fqn))?;
Ok(mention)
}
pub fn from_activity(
conn: &Connection,
ment: &link::Mention,
inside: i32,
in_post: bool,
notify: bool,
) -> Result<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).and_then(|post| {
let res = Mention::insert(
conn,
NewMention {
mentioned_id: mentioned.id,
post_id: Some(post.id),
comment_id: None,
},
)?;
if notify {
res.notify(conn)?;
}
Ok(res)
})
} else {
Comment::get(conn, inside).and_then(|comment| {
let res = Mention::insert(
conn,
NewMention {
mentioned_id: mentioned.id,
post_id: None,
comment_id: Some(comment.id),
},
)?;
if notify {
res.notify(conn)?;
}
Ok(res)
})
}
}
pub fn delete(&self, conn: &Connection) -> Result<()> {
//find related notifications and delete them
if let Ok(n) = Notification::find(conn, notification_kind::MENTION, self.id) {
n.delete(conn)?;
}
diesel::delete(self)
.execute(conn)
.map(|_| ())
.map_err(Error::from)
}
2018-06-20 22:05:30 +02:00
fn notify(&self, conn: &Connection) -> Result<()> {
let m = self.get_mentioned(conn)?;
if m.is_local() {
Notification::insert(
conn,
NewNotification {
kind: notification_kind::MENTION.to_string(),
object_id: self.id,
user_id: m.id,
},
)
.map(|_| ())
} else {
Ok(())
}
2018-06-20 22:05:30 +02:00
}
}