From 215b5775738b0834454d01a52eca18dccb617898 Mon Sep 17 00:00:00 2001 From: Bat Date: Wed, 20 Jun 2018 20:06:34 +0100 Subject: [PATCH] Add some ActivityPub function to Mention --- src/activity_pub/inbox.rs | 2 +- src/models/mentions.rs | 40 ++++++++++++++++++++++++++++++++++++++- src/models/users.rs | 26 +++++++++---------------- 3 files changed, 49 insertions(+), 19 deletions(-) diff --git a/src/activity_pub/inbox.rs b/src/activity_pub/inbox.rs index a20af1ea..8b0856f2 100644 --- a/src/activity_pub/inbox.rs +++ b/src/activity_pub/inbox.rs @@ -40,7 +40,7 @@ pub trait FromActivity: Sized { } } -pub trait Notify { +pub trait Notify { fn notify(conn: &PgConnection, act: T, actor: Id); } diff --git a/src/models/mentions.rs b/src/models/mentions.rs index 0a2964ed..934618fa 100644 --- a/src/models/mentions.rs +++ b/src/models/mentions.rs @@ -1,8 +1,11 @@ +use activitypub::link; use diesel::{self, PgConnection, QueryDsl, RunQueryDsl, ExpressionMethods}; +use activity_pub::Id; use models::{ comments::Comment, - posts::Post + posts::Post, + users::User }; use schema::mentions; @@ -27,6 +30,10 @@ impl Mention { get!(mentions); list_by!(mentions, list_for_user, mentioned_id as i32); + pub fn get_mentioned(&self, conn: &PgConnection) -> Option { + User::get(conn, self.mentioned_id) + } + pub fn get_post(&self, conn: &PgConnection) -> Option { self.post_id.and_then(|id| Post::get(conn, id)) } @@ -34,4 +41,35 @@ impl Mention { pub fn get_comment(&self, conn: &PgConnection) -> Option { self.post_id.and_then(|id| Comment::get(conn, id)) } + + 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 { + 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()) { + Some(Mention::insert(conn, NewMention { + mentioned_id: mentioned.id, + post_id: Some(post.id), + comment_id: None + })) + } else { + if let Some(comment) = Comment::find_by_ap_url(conn, inside.into()) { + Some(Mention::insert(conn, NewMention { + mentioned_id: mentioned.id, + post_id: None, + comment_id: Some(comment.id) + })) + } else { + None + } + } + } } + diff --git a/src/models/users.rs b/src/models/users.rs index c0dc4902..5a075487 100644 --- a/src/models/users.rs +++ b/src/models/users.rs @@ -89,7 +89,7 @@ impl User { get!(users); find_by!(users, find_by_email, email as String); find_by!(users, find_by_name, username as String, instance_id as i32); - + find_by!(users, find_by_ap_url, ap_url as String); pub fn grant_admin_rights(&self, conn: &PgConnection) { diesel::update(self) @@ -419,23 +419,15 @@ impl APActor for User { } fn from_url(conn: &PgConnection, url: String) -> Option { - let in_db = users::table.filter(users::ap_url.eq(url.clone())) - .limit(1) - .load::(conn) - .expect("Error loading user by AP url") - .into_iter().nth(0); - match in_db { - Some(u) => Some(u), - None => { - // The requested user was not in the DB - // We try to fetch it if it is remote - if Url::parse(url.as_ref()).unwrap().host_str().unwrap() != BASE_URL.as_str() { - Some(User::fetch_from_url(conn, url).unwrap()) - } else { - None - } + User::find_by_ap_url(conn, url.clone()).or_else(|| { + // The requested user was not in the DB + // We try to fetch it if it is remote + if Url::parse(url.as_ref()).unwrap().host_str().unwrap() != BASE_URL.as_str() { + Some(User::fetch_from_url(conn, url).unwrap()) + } else { + None } - } + }) } }