Add some ActivityPub function to Mention
This commit is contained in:
parent
356cc20e34
commit
215b577573
|
@ -40,7 +40,7 @@ pub trait FromActivity<T: Object>: Sized {
|
|||
}
|
||||
}
|
||||
|
||||
pub trait Notify<T: Object> {
|
||||
pub trait Notify<T> {
|
||||
fn notify(conn: &PgConnection, act: T, actor: Id);
|
||||
}
|
||||
|
||||
|
|
|
@ -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> {
|
||||
User::get(conn, self.mentioned_id)
|
||||
}
|
||||
|
||||
pub fn get_post(&self, conn: &PgConnection) -> Option<Post> {
|
||||
self.post_id.and_then(|id| Post::get(conn, id))
|
||||
}
|
||||
|
@ -34,4 +41,35 @@ impl Mention {
|
|||
pub fn get_comment(&self, conn: &PgConnection) -> Option<Comment> {
|
||||
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<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()) {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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,14 +419,7 @@ impl APActor for User {
|
|||
}
|
||||
|
||||
fn from_url(conn: &PgConnection, url: String) -> Option<User> {
|
||||
let in_db = users::table.filter(users::ap_url.eq(url.clone()))
|
||||
.limit(1)
|
||||
.load::<User>(conn)
|
||||
.expect("Error loading user by AP url")
|
||||
.into_iter().nth(0);
|
||||
match in_db {
|
||||
Some(u) => Some(u),
|
||||
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() {
|
||||
|
@ -434,8 +427,7 @@ impl APActor for User {
|
|||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue