Notify when receiving a mention

This commit is contained in:
Bat 2018-06-20 21:05:30 +01:00
parent c9f29955a0
commit c4cc4a4e13
8 changed files with 60 additions and 9 deletions

View File

@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
ALTER TABLE mentions DROP COLUMN ap_url;

View File

@ -0,0 +1,2 @@
-- Your SQL goes here
ALTER TABLE mentions ADD COLUMN ap_url VARCHAR NOT NULL DEFAULT '';

View File

@ -280,3 +280,6 @@ msgstr ""
msgid "You are not author in this blog." msgid "You are not author in this blog."
msgstr "" msgstr ""
msgid "{{ data }} mentioned you."
msgstr ""

View File

@ -280,3 +280,6 @@ msgstr ""
msgid "You are not author in this blog." msgid "You are not author in this blog."
msgstr "" msgstr ""
msgid "{{ data }} mentioned you."
msgstr ""

View File

@ -285,5 +285,9 @@ msgstr ""
msgid "You are not author in this blog." msgid "You are not author in this blog."
msgstr "" msgstr ""
#, fuzzy
msgid "{{ data }} mentioned you."
msgstr "{{ data }} skomentował Twój artykuł"
#~ msgid "Logowanie" #~ msgid "Logowanie"
#~ msgstr "Zaloguj się" #~ msgstr "Zaloguj się"

View File

@ -275,3 +275,6 @@ msgstr ""
msgid "You are not author in this blog." msgid "You are not author in this blog."
msgstr "" msgstr ""
msgid "{{ data }} mentioned you."
msgstr ""

View File

@ -1,9 +1,10 @@
use activitypub::link; use activitypub::link;
use diesel::{self, PgConnection, QueryDsl, RunQueryDsl, ExpressionMethods}; use diesel::{self, PgConnection, QueryDsl, RunQueryDsl, ExpressionMethods};
use activity_pub::Id; use activity_pub::{Id, inbox::Notify};
use models::{ use models::{
comments::Comment, comments::Comment,
notifications::*,
posts::Post, posts::Post,
users::User users::User
}; };
@ -14,7 +15,8 @@ pub struct Mention {
pub id: i32, pub id: i32,
pub mentioned_id: i32, pub mentioned_id: i32,
pub post_id: Option<i32>, pub post_id: Option<i32>,
pub comment_id: Option<i32> pub comment_id: Option<i32>,
pub ap_url: String
} }
#[derive(Insertable)] #[derive(Insertable)]
@ -22,12 +24,14 @@ pub struct Mention {
pub struct NewMention { pub struct NewMention {
pub mentioned_id: i32, pub mentioned_id: i32,
pub post_id: Option<i32>, pub post_id: Option<i32>,
pub comment_id: Option<i32> pub comment_id: Option<i32>,
pub ap_url: String
} }
impl Mention { impl Mention {
insert!(mentions, NewMention); insert!(mentions, NewMention);
get!(mentions); get!(mentions);
find_by!(mentions, find_by_ap_url, ap_url as String);
list_by!(mentions, list_for_user, mentioned_id as i32); list_by!(mentions, list_for_user, mentioned_id as i32);
pub fn get_mentioned(&self, conn: &PgConnection) -> Option<User> { pub fn get_mentioned(&self, conn: &PgConnection) -> Option<User> {
@ -54,21 +58,50 @@ impl Mention {
let mentioned = User::find_by_ap_url(conn, ment.link_props.href_string().unwrap()).unwrap(); 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()) { if let Some(post) = Post::find_by_ap_url(conn, inside.clone().into()) {
Some(Mention::insert(conn, NewMention { let res = Some(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())
}));
Mention::notify(conn, ment, Id::new(String::new()));
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()) {
Some(Mention::insert(conn, NewMention { let res =Some(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())
}));
Mention::notify(conn, ment, Id::new(String::new()));
res
} else { } else {
None None
} }
} }
} }
} }
impl Notify<link::Mention> for Mention {
fn notify(conn: &PgConnection, ment: link::Mention, _actor: Id) {
match Mention::find_by_ap_url(conn, ment.link_props.href_string().unwrap()) {
Some(mention) => {
let author = mention.get_comment(conn)
.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| {
Notification::insert(conn, NewNotification {
title: "{{ data }} mentioned you.".to_string(),
data: Some(author),
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()))),
user_id: m.id
});
});
},
None => println!("Couldn't find mention by AP URL, to create a new notification")
};
}
}

View File

@ -72,6 +72,7 @@ table! {
mentioned_id -> Int4, mentioned_id -> Int4,
post_id -> Nullable<Int4>, post_id -> Nullable<Int4>,
comment_id -> Nullable<Int4>, comment_id -> Nullable<Int4>,
ap_url -> Varchar,
} }
} }