From c4cc4a4e139cdc3da3c9ffced2b46b47fc3bf79c Mon Sep 17 00:00:00 2001 From: Bat Date: Wed, 20 Jun 2018 21:05:30 +0100 Subject: [PATCH] Notify when receiving a mention --- .../down.sql | 2 + .../up.sql | 2 + po/en.po | 3 ++ po/fr.po | 3 ++ po/pl.po | 4 ++ po/plume.pot | 3 ++ src/models/mentions.rs | 51 +++++++++++++++---- src/schema.rs | 1 + 8 files changed, 60 insertions(+), 9 deletions(-) create mode 100644 migrations/2018-06-20-194538_add_mentions_ap_url/down.sql create mode 100644 migrations/2018-06-20-194538_add_mentions_ap_url/up.sql diff --git a/migrations/2018-06-20-194538_add_mentions_ap_url/down.sql b/migrations/2018-06-20-194538_add_mentions_ap_url/down.sql new file mode 100644 index 00000000..4e626f3b --- /dev/null +++ b/migrations/2018-06-20-194538_add_mentions_ap_url/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +ALTER TABLE mentions DROP COLUMN ap_url; diff --git a/migrations/2018-06-20-194538_add_mentions_ap_url/up.sql b/migrations/2018-06-20-194538_add_mentions_ap_url/up.sql new file mode 100644 index 00000000..df9c3c79 --- /dev/null +++ b/migrations/2018-06-20-194538_add_mentions_ap_url/up.sql @@ -0,0 +1,2 @@ +-- Your SQL goes here +ALTER TABLE mentions ADD COLUMN ap_url VARCHAR NOT NULL DEFAULT ''; diff --git a/po/en.po b/po/en.po index 899ae1a8..cd7fe411 100644 --- a/po/en.po +++ b/po/en.po @@ -280,3 +280,6 @@ msgstr "" msgid "You are not author in this blog." msgstr "" + +msgid "{{ data }} mentioned you." +msgstr "" diff --git a/po/fr.po b/po/fr.po index ebf655b2..1442a26f 100644 --- a/po/fr.po +++ b/po/fr.po @@ -280,3 +280,6 @@ msgstr "" msgid "You are not author in this blog." msgstr "" + +msgid "{{ data }} mentioned you." +msgstr "" diff --git a/po/pl.po b/po/pl.po index f2910acd..2724bb48 100644 --- a/po/pl.po +++ b/po/pl.po @@ -285,5 +285,9 @@ msgstr "" msgid "You are not author in this blog." msgstr "" +#, fuzzy +msgid "{{ data }} mentioned you." +msgstr "{{ data }} skomentował Twój artykuł" + #~ msgid "Logowanie" #~ msgstr "Zaloguj się" diff --git a/po/plume.pot b/po/plume.pot index 0ba8c259..8f0cd9b7 100644 --- a/po/plume.pot +++ b/po/plume.pot @@ -275,3 +275,6 @@ msgstr "" msgid "You are not author in this blog." msgstr "" + +msgid "{{ data }} mentioned you." +msgstr "" diff --git a/src/models/mentions.rs b/src/models/mentions.rs index f43a76f4..f40f9f37 100644 --- a/src/models/mentions.rs +++ b/src/models/mentions.rs @@ -1,9 +1,10 @@ use activitypub::link; use diesel::{self, PgConnection, QueryDsl, RunQueryDsl, ExpressionMethods}; -use activity_pub::Id; +use activity_pub::{Id, inbox::Notify}; use models::{ comments::Comment, + notifications::*, posts::Post, users::User }; @@ -14,7 +15,8 @@ pub struct Mention { pub id: i32, pub mentioned_id: i32, pub post_id: Option, - pub comment_id: Option + pub comment_id: Option, + pub ap_url: String } #[derive(Insertable)] @@ -22,12 +24,14 @@ pub struct Mention { pub struct NewMention { pub mentioned_id: i32, pub post_id: Option, - pub comment_id: Option + pub comment_id: Option, + pub ap_url: String } impl Mention { insert!(mentions, NewMention); get!(mentions); + find_by!(mentions, find_by_ap_url, ap_url as String); list_by!(mentions, list_for_user, mentioned_id as i32); pub fn get_mentioned(&self, conn: &PgConnection) -> Option { @@ -54,21 +58,50 @@ impl Mention { 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 { + let res = Some(Mention::insert(conn, NewMention { mentioned_id: mentioned.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 { 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, 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 { None } } } } + +impl Notify 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") + }; + } +} diff --git a/src/schema.rs b/src/schema.rs index 87563fd5..42184146 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -72,6 +72,7 @@ table! { mentioned_id -> Int4, post_id -> Nullable, comment_id -> Nullable, + ap_url -> Varchar, } }