Add support for markdown in comments + Correctly federate mentions in comments

This commit is contained in:
Bat 2018-06-21 12:00:30 +01:00
parent 5a5c1a8d99
commit 0fd181e7ea
2 changed files with 6 additions and 2 deletions

View File

@ -22,6 +22,7 @@ use models::{
}; };
use schema::comments; use schema::comments;
use safe_string::SafeString; use safe_string::SafeString;
use utils;
#[derive(Queryable, Identifiable, Serialize, Clone)] #[derive(Queryable, Identifiable, Serialize, Clone)]
pub struct Comment { pub struct Comment {
@ -159,6 +160,8 @@ impl NewComment {
self.sensitive = false; self.sensitive = false;
self.spoiler_text = String::new(); self.spoiler_text = String::new();
let (html, mentions) = utils::md_to_html(self.content.get().as_ref());
let author = User::get(conn, self.author_id).unwrap(); let author = User::get(conn, self.author_id).unwrap();
let mut note = Note::default(); let mut note = Note::default();
let mut to = author.get_followers(conn).into_iter().map(User::into_id).collect::<Vec<Id>>(); let mut to = author.get_followers(conn).into_iter().map(User::into_id).collect::<Vec<Id>>();
@ -172,7 +175,7 @@ impl NewComment {
note.object_props.set_id_string(self.ap_url.clone().unwrap_or(String::new())).expect("NewComment::create: note.id error"); note.object_props.set_id_string(self.ap_url.clone().unwrap_or(String::new())).expect("NewComment::create: note.id error");
note.object_props.set_summary_string(self.spoiler_text.clone()).expect("NewComment::create: note.summary error"); note.object_props.set_summary_string(self.spoiler_text.clone()).expect("NewComment::create: note.summary error");
note.object_props.set_content_string(self.content.get().clone()).expect("NewComment::create: note.content error"); note.object_props.set_content_string(html).expect("NewComment::create: note.content error");
note.object_props.set_in_reply_to_link(Id::new(self.in_response_to_id.map_or_else(|| Post::get(conn, self.post_id).unwrap().ap_url, |id| { note.object_props.set_in_reply_to_link(Id::new(self.in_response_to_id.map_or_else(|| Post::get(conn, self.post_id).unwrap().ap_url, |id| {
let comm = Comment::get(conn, id).unwrap(); let comm = Comment::get(conn, id).unwrap();
comm.ap_url.clone().unwrap_or(comm.compute_id(conn)) comm.ap_url.clone().unwrap_or(comm.compute_id(conn))
@ -180,6 +183,8 @@ impl NewComment {
note.object_props.set_published_string(chrono::Utc::now().to_rfc3339()).expect("NewComment::create: note.published error"); note.object_props.set_published_string(chrono::Utc::now().to_rfc3339()).expect("NewComment::create: note.published error");
note.object_props.set_attributed_to_link(author.clone().into_id()).expect("NewComment::create: note.attributed_to error"); note.object_props.set_attributed_to_link(author.clone().into_id()).expect("NewComment::create: note.attributed_to error");
note.object_props.set_to_link_vec(to).expect("NewComment::create: note.to error"); note.object_props.set_to_link_vec(to).expect("NewComment::create: note.to error");
note.object_props.set_tag_link_vec(mentions.into_iter().map(|m| Mention::build_activity(conn, m)).collect::<Vec<link::Mention>>())
.expect("NewComment::create: note.tag error");
let mut act = Create::default(); let mut act = Create::default();
act.create_props.set_actor_link(author.into_id()).expect("NewComment::create: actor error"); act.create_props.set_actor_link(author.into_id()).expect("NewComment::create: actor error");

View File

@ -49,7 +49,6 @@ 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");