Simplify the activity_pub::inbox::Notify trait + Fix notifications
Also fix a bug with the list of mentions that was returned
This commit is contained in:
parent
d7b71848fc
commit
3551bef895
@ -40,8 +40,8 @@ pub trait FromActivity<T: Object>: Sized {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Notify<T> {
|
pub trait Notify {
|
||||||
fn notify(conn: &PgConnection, act: T, actor: Id);
|
fn notify(&self, conn: &PgConnection);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Deletable {
|
pub trait Deletable {
|
||||||
|
@ -137,27 +137,21 @@ impl FromActivity<Note> for Comment {
|
|||||||
author_id: User::from_url(conn, actor.clone().into()).unwrap().id,
|
author_id: User::from_url(conn, actor.clone().into()).unwrap().id,
|
||||||
sensitive: false // "sensitive" is not a standard property, we need to think about how to support it with the activitypub crate
|
sensitive: false // "sensitive" is not a standard property, we need to think about how to support it with the activitypub crate
|
||||||
});
|
});
|
||||||
Comment::notify(conn, note, actor);
|
comm.notify(conn);
|
||||||
comm
|
comm
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Notify<Note> for Comment {
|
impl Notify for Comment {
|
||||||
fn notify(conn: &PgConnection, note: Note, _actor: Id) {
|
fn notify(&self, conn: &PgConnection) {
|
||||||
match Comment::find_by_ap_url(conn, note.object_props.id_string().unwrap()) {
|
for author in self.get_post(conn).get_authors(conn) {
|
||||||
Some(comment) => {
|
Notification::insert(conn, NewNotification {
|
||||||
for author in comment.clone().get_post(conn).get_authors(conn) {
|
title: "{{ data }} commented your article".to_string(),
|
||||||
let comment = comment.clone();
|
data: Some(self.get_author(conn).display_name.clone()),
|
||||||
Notification::insert(conn, NewNotification {
|
content: Some(self.get_post(conn).title),
|
||||||
title: "{{ data }} commented your article".to_string(),
|
link: self.ap_url.clone(),
|
||||||
data: Some(comment.get_author(conn).display_name.clone()),
|
user_id: author.id
|
||||||
content: Some(comment.get_post(conn).title),
|
});
|
||||||
link: comment.ap_url,
|
}
|
||||||
user_id: author.id
|
|
||||||
});
|
|
||||||
}
|
|
||||||
},
|
|
||||||
None => println!("Couldn't find comment by AP id, to create a new notification")
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -62,15 +62,15 @@ impl FromActivity<FollowAct> for Follow {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Notify<FollowAct> for Follow {
|
impl Notify for Follow {
|
||||||
fn notify(conn: &PgConnection, follow: FollowAct, actor: Id) {
|
fn notify(&self, conn: &PgConnection) {
|
||||||
let follower = User::from_url(conn, actor.into()).unwrap();
|
let follower = User::get(conn, self.follower_id).unwrap();
|
||||||
Notification::insert(conn, NewNotification {
|
Notification::insert(conn, NewNotification {
|
||||||
title: "{{ data }} started following you".to_string(),
|
title: "{{ data }} started following you".to_string(),
|
||||||
data: Some(follower.display_name.clone()),
|
data: Some(follower.display_name.clone()),
|
||||||
content: None,
|
content: None,
|
||||||
link: Some(follower.ap_url),
|
link: Some(follower.ap_url),
|
||||||
user_id: User::from_url(conn, follow.follow_props.object_link::<Id>().unwrap().into()).unwrap().id
|
user_id: self.following_id
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -77,7 +77,7 @@ impl Like {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl FromActivity<activity::Like> for Like {
|
impl FromActivity<activity::Like> for Like {
|
||||||
fn from_activity(conn: &PgConnection, like: activity::Like, actor: Id) -> Like {
|
fn from_activity(conn: &PgConnection, like: activity::Like, _actor: Id) -> Like {
|
||||||
let liker = User::from_url(conn, like.like_props.actor.as_str().unwrap().to_string());
|
let liker = User::from_url(conn, like.like_props.actor.as_str().unwrap().to_string());
|
||||||
let post = Post::find_by_ap_url(conn, like.like_props.object.as_str().unwrap().to_string());
|
let post = Post::find_by_ap_url(conn, like.like_props.object.as_str().unwrap().to_string());
|
||||||
let res = Like::insert(conn, NewLike {
|
let res = Like::insert(conn, NewLike {
|
||||||
@ -85,15 +85,15 @@ impl FromActivity<activity::Like> for Like {
|
|||||||
user_id: liker.unwrap().id,
|
user_id: liker.unwrap().id,
|
||||||
ap_url: like.object_props.id_string().unwrap_or(String::from(""))
|
ap_url: like.object_props.id_string().unwrap_or(String::from(""))
|
||||||
});
|
});
|
||||||
Like::notify(conn, like, actor);
|
res.notify(conn);
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Notify<activity::Like> for Like {
|
impl Notify for Like {
|
||||||
fn notify(conn: &PgConnection, like: activity::Like, actor: Id) {
|
fn notify(&self, conn: &PgConnection) {
|
||||||
let liker = User::from_url(conn, actor.into()).unwrap();
|
let liker = User::get(conn, self.user_id).unwrap();
|
||||||
let post = Post::find_by_ap_url(conn, like.like_props.object_link::<Id>().unwrap().into()).unwrap();
|
let post = Post::get(conn, self.post_id).unwrap();
|
||||||
for author in post.get_authors(conn) {
|
for author in post.get_authors(conn) {
|
||||||
let post = post.clone();
|
let post = post.clone();
|
||||||
Notification::insert(conn, NewNotification {
|
Notification::insert(conn, NewNotification {
|
||||||
|
@ -49,6 +49,7 @@ impl Mention {
|
|||||||
|
|
||||||
pub fn build_activity(conn: &PgConnection, ment: String) -> link::Mention {
|
pub fn build_activity(conn: &PgConnection, ment: String) -> link::Mention {
|
||||||
let user = User::find_by_fqn(conn, ment.clone());
|
let user = User::find_by_fqn(conn, ment.clone());
|
||||||
|
println!("building act : {} -> {:?}", ment, user);
|
||||||
let mut mention = link::Mention::default();
|
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_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.link_props.set_name_string(format!("@{}", ment)).expect("Error setting mention's name");
|
||||||
@ -64,27 +65,28 @@ impl Mention {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_activity(conn: &PgConnection, ment: link::Mention, inside: Id) -> Option<Self> {
|
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();
|
let ap_url = ment.link_props.href_string().unwrap();
|
||||||
|
let mentioned = User::find_by_ap_url(conn, ap_url).unwrap();
|
||||||
|
|
||||||
if let Some(post) = Post::find_by_ap_url(conn, inside.clone().into()) {
|
if let Some(post) = Post::find_by_ap_url(conn, inside.clone().into()) {
|
||||||
let res = Some(Mention::insert(conn, NewMention {
|
let res = Mention::insert(conn, NewMention {
|
||||||
mentioned_id: mentioned.id,
|
mentioned_id: mentioned.id,
|
||||||
post_id: Some(post.id),
|
post_id: Some(post.id),
|
||||||
comment_id: None,
|
comment_id: None,
|
||||||
ap_url: ment.link_props.href_string().unwrap_or(String::new())
|
ap_url: ment.link_props.href_string().unwrap_or(String::new())
|
||||||
}));
|
});
|
||||||
Mention::notify(conn, ment, Id::new(String::new()));
|
res.notify(conn);
|
||||||
res
|
Some(res)
|
||||||
} else {
|
} else {
|
||||||
if let Some(comment) = Comment::find_by_ap_url(conn, inside.into()) {
|
if let Some(comment) = Comment::find_by_ap_url(conn, inside.into()) {
|
||||||
let res =Some(Mention::insert(conn, NewMention {
|
let res = Mention::insert(conn, NewMention {
|
||||||
mentioned_id: mentioned.id,
|
mentioned_id: mentioned.id,
|
||||||
post_id: None,
|
post_id: None,
|
||||||
comment_id: Some(comment.id),
|
comment_id: Some(comment.id),
|
||||||
ap_url: ment.link_props.href_string().unwrap_or(String::new())
|
ap_url: ment.link_props.href_string().unwrap_or(String::new())
|
||||||
}));
|
});
|
||||||
Mention::notify(conn, ment, Id::new(String::new()));
|
res.notify(conn);
|
||||||
res
|
Some(res)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
@ -92,25 +94,20 @@ impl Mention {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Notify<link::Mention> for Mention {
|
impl Notify for Mention {
|
||||||
fn notify(conn: &PgConnection, ment: link::Mention, _actor: Id) {
|
fn notify(&self, conn: &PgConnection) {
|
||||||
match Mention::find_by_ap_url(conn, ment.link_props.href_string().unwrap()) {
|
let author = self.get_comment(conn)
|
||||||
Some(mention) => {
|
.map(|c| c.get_author(conn).display_name.clone())
|
||||||
let author = mention.get_comment(conn)
|
.unwrap_or(self.get_post(conn).unwrap().get_authors(conn)[0].display_name.clone());
|
||||||
.map(|c| c.get_author(conn).display_name.clone())
|
|
||||||
.unwrap_or(mention.get_post(conn).unwrap().get_authors(conn)[0].display_name.clone());
|
|
||||||
|
|
||||||
mention.get_mentioned(conn).map(|m| {
|
self.get_mentioned(conn).map(|m| {
|
||||||
Notification::insert(conn, NewNotification {
|
Notification::insert(conn, NewNotification {
|
||||||
title: "{{ data }} mentioned you.".to_string(),
|
title: "{{ data }} mentioned you.".to_string(),
|
||||||
data: Some(author),
|
data: Some(author),
|
||||||
content: None,
|
content: None,
|
||||||
link: Some(mention.get_post(conn).map(|p| p.ap_url).unwrap_or(mention.get_comment(conn).unwrap().ap_url.unwrap_or(String::new()))),
|
link: Some(self.get_post(conn).map(|p| p.ap_url).unwrap_or_else(|| self.get_comment(conn).unwrap().ap_url.unwrap_or(String::new()))),
|
||||||
user_id: m.id
|
user_id: m.id
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
|
||||||
None => println!("Couldn't find mention by AP URL, to create a new notification")
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -73,7 +73,7 @@ impl Reshare {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl FromActivity<Announce> for Reshare {
|
impl FromActivity<Announce> for Reshare {
|
||||||
fn from_activity(conn: &PgConnection, announce: Announce, actor: Id) -> Reshare {
|
fn from_activity(conn: &PgConnection, announce: Announce, _actor: Id) -> Reshare {
|
||||||
let user = User::from_url(conn, announce.announce_props.actor.as_str().unwrap().to_string());
|
let user = User::from_url(conn, announce.announce_props.actor.as_str().unwrap().to_string());
|
||||||
let post = Post::find_by_ap_url(conn, announce.announce_props.object.as_str().unwrap().to_string());
|
let post = Post::find_by_ap_url(conn, announce.announce_props.object.as_str().unwrap().to_string());
|
||||||
let reshare = Reshare::insert(conn, NewReshare {
|
let reshare = Reshare::insert(conn, NewReshare {
|
||||||
@ -81,15 +81,15 @@ impl FromActivity<Announce> for Reshare {
|
|||||||
user_id: user.unwrap().id,
|
user_id: user.unwrap().id,
|
||||||
ap_url: announce.object_props.id_string().unwrap_or(String::from(""))
|
ap_url: announce.object_props.id_string().unwrap_or(String::from(""))
|
||||||
});
|
});
|
||||||
Reshare::notify(conn, announce, actor);
|
reshare.notify(conn);
|
||||||
reshare
|
reshare
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Notify<Announce> for Reshare {
|
impl Notify for Reshare {
|
||||||
fn notify(conn: &PgConnection, announce: Announce, actor: Id) {
|
fn notify(&self, conn: &PgConnection) {
|
||||||
let actor = User::from_url(conn, actor.into()).unwrap();
|
let actor = User::get(conn, self.user_id).unwrap();
|
||||||
let post = Post::find_by_ap_url(conn, announce.announce_props.object_link::<Id>().unwrap().into()).unwrap();
|
let post = self.get_post(conn).unwrap();
|
||||||
for author in post.get_authors(conn) {
|
for author in post.get_authors(conn) {
|
||||||
let post = post.clone();
|
let post = post.clone();
|
||||||
Notification::insert(conn, NewNotification {
|
Notification::insert(conn, NewNotification {
|
||||||
|
@ -47,7 +47,7 @@ use safe_string::SafeString;
|
|||||||
|
|
||||||
pub const AUTH_COOKIE: &'static str = "user_id";
|
pub const AUTH_COOKIE: &'static str = "user_id";
|
||||||
|
|
||||||
#[derive(Queryable, Identifiable, Serialize, Deserialize, Clone)]
|
#[derive(Queryable, Identifiable, Serialize, Deserialize, Clone, Debug)]
|
||||||
pub struct User {
|
pub struct User {
|
||||||
pub id: i32,
|
pub id: i32,
|
||||||
pub username: String,
|
pub username: String,
|
||||||
|
@ -4,7 +4,7 @@ use rocket::{
|
|||||||
};
|
};
|
||||||
use rocket_contrib::Template;
|
use rocket_contrib::Template;
|
||||||
|
|
||||||
use activity_pub::{broadcast, IntoId, inbox::Notify};
|
use activity_pub::{broadcast, inbox::Notify};
|
||||||
use db_conn::DbConn;
|
use db_conn::DbConn;
|
||||||
use models::{
|
use models::{
|
||||||
blogs::Blog,
|
blogs::Blog,
|
||||||
@ -53,12 +53,12 @@ fn create(blog_name: String, slug: String, query: CommentQuery, data: Form<NewCo
|
|||||||
in_response_to_id: query.responding_to,
|
in_response_to_id: query.responding_to,
|
||||||
post_id: post.id,
|
post_id: post.id,
|
||||||
author_id: user.id,
|
author_id: user.id,
|
||||||
ap_url: None,
|
ap_url: None, // TODO: set it
|
||||||
sensitive: false,
|
sensitive: false,
|
||||||
spoiler_text: "".to_string()
|
spoiler_text: "".to_string()
|
||||||
});
|
});
|
||||||
|
comment.notify(&*conn);
|
||||||
|
|
||||||
Comment::notify(&*conn, comment.into_activity(&*conn), user.clone().into_id());
|
|
||||||
broadcast(&*conn, &user, comment.create_activity(&*conn), user.get_followers(&*conn));
|
broadcast(&*conn, &user, comment.create_activity(&*conn), user.get_followers(&*conn));
|
||||||
|
|
||||||
Redirect::to(format!("/~/{}/{}/#comment-{}", blog_name, slug, comment.id))
|
Redirect::to(format!("/~/{}/{}/#comment-{}", blog_name, slug, comment.id))
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use rocket::response::{Redirect, Flash};
|
use rocket::response::{Redirect, Flash};
|
||||||
|
|
||||||
use activity_pub::{broadcast, IntoId, inbox::Notify};
|
use activity_pub::{broadcast, inbox::Notify};
|
||||||
use db_conn::DbConn;
|
use db_conn::DbConn;
|
||||||
use models::{
|
use models::{
|
||||||
blogs::Blog,
|
blogs::Blog,
|
||||||
@ -23,8 +23,8 @@ fn create(blog: String, slug: String, user: User, conn: DbConn) -> Redirect {
|
|||||||
ap_url: "".to_string()
|
ap_url: "".to_string()
|
||||||
});
|
});
|
||||||
like.update_ap_url(&*conn);
|
like.update_ap_url(&*conn);
|
||||||
|
like.notify(&*conn);
|
||||||
|
|
||||||
likes::Like::notify(&*conn, like.into_activity(&*conn), user.clone().into_id());
|
|
||||||
broadcast(&*conn, &user, like.into_activity(&*conn), user.get_followers(&*conn));
|
broadcast(&*conn, &user, like.into_activity(&*conn), user.get_followers(&*conn));
|
||||||
} else {
|
} else {
|
||||||
let like = likes::Like::find_by_user_on_post(&*conn, user.id, post.id).unwrap();
|
let like = likes::Like::find_by_user_on_post(&*conn, user.id, post.id).unwrap();
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use rocket::response::{Redirect, Flash};
|
use rocket::response::{Redirect, Flash};
|
||||||
|
|
||||||
use activity_pub::{broadcast, IntoId, inbox::Notify};
|
use activity_pub::{broadcast, inbox::Notify};
|
||||||
use db_conn::DbConn;
|
use db_conn::DbConn;
|
||||||
use models::{
|
use models::{
|
||||||
blogs::Blog,
|
blogs::Blog,
|
||||||
@ -23,8 +23,8 @@ fn create(blog: String, slug: String, user: User, conn: DbConn) -> Redirect {
|
|||||||
ap_url: "".to_string()
|
ap_url: "".to_string()
|
||||||
});
|
});
|
||||||
reshare.update_ap_url(&*conn);
|
reshare.update_ap_url(&*conn);
|
||||||
|
reshare.notify(&*conn);
|
||||||
|
|
||||||
Reshare::notify(&*conn, reshare.into_activity(&*conn), user.clone().into_id());
|
|
||||||
broadcast(&*conn, &user, reshare.into_activity(&*conn), user.get_followers(&*conn));
|
broadcast(&*conn, &user, reshare.into_activity(&*conn), user.get_followers(&*conn));
|
||||||
} else {
|
} else {
|
||||||
let reshare = Reshare::find_by_user_on_post(&*conn, user.id, post.id).unwrap();
|
let reshare = Reshare::find_by_user_on_post(&*conn, user.id, post.id).unwrap();
|
||||||
|
@ -71,16 +71,17 @@ fn dashboard_auth() -> Flash<Redirect> {
|
|||||||
#[get("/@/<name>/follow")]
|
#[get("/@/<name>/follow")]
|
||||||
fn follow(name: String, conn: DbConn, user: User) -> Redirect {
|
fn follow(name: String, conn: DbConn, user: User) -> Redirect {
|
||||||
let target = User::find_by_fqn(&*conn, name.clone()).unwrap();
|
let target = User::find_by_fqn(&*conn, name.clone()).unwrap();
|
||||||
follows::Follow::insert(&*conn, follows::NewFollow {
|
let f = follows::Follow::insert(&*conn, follows::NewFollow {
|
||||||
follower_id: user.id,
|
follower_id: user.id,
|
||||||
following_id: target.id
|
following_id: target.id
|
||||||
});
|
});
|
||||||
|
f.notify(&*conn);
|
||||||
|
|
||||||
let mut act = Follow::default();
|
let mut act = Follow::default();
|
||||||
act.follow_props.set_actor_link::<Id>(user.clone().into_id()).unwrap();
|
act.follow_props.set_actor_link::<Id>(user.clone().into_id()).unwrap();
|
||||||
act.follow_props.set_object_object(user.into_activity(&*conn)).unwrap();
|
act.follow_props.set_object_object(user.into_activity(&*conn)).unwrap();
|
||||||
act.object_props.set_id_string(format!("{}/follow/{}", user.ap_url, target.ap_url)).unwrap();
|
act.object_props.set_id_string(format!("{}/follow/{}", user.ap_url, target.ap_url)).unwrap();
|
||||||
|
|
||||||
follows::Follow::notify(&*conn, act.clone(), user.clone().into_id());
|
|
||||||
broadcast(&*conn, &user, act, vec![target]);
|
broadcast(&*conn, &user, act, vec![target]);
|
||||||
Redirect::to(uri!(details: name = name))
|
Redirect::to(uri!(details: name = name))
|
||||||
}
|
}
|
||||||
|
@ -60,7 +60,7 @@ pub fn md_to_html(md: &str) -> (String, Vec<String>) {
|
|||||||
_ => (vec![evt], vec![])
|
_ => (vec![evt], vec![])
|
||||||
}).unzip();
|
}).unzip();
|
||||||
let parser = parser.into_iter().flatten();
|
let parser = parser.into_iter().flatten();
|
||||||
let mentions = mentions.into_iter().flatten();
|
let mentions = mentions.into_iter().flatten().map(|m| String::from(m.trim()));
|
||||||
|
|
||||||
// TODO: fetch mentionned profiles in background, if needed
|
// TODO: fetch mentionned profiles in background, if needed
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user