Plume/plume-models/src/comments.rs

202 lines
7.9 KiB
Rust
Raw Normal View History

2018-06-10 13:13:07 +02:00
use activitypub::{
2018-05-19 00:04:30 +02:00
activity::Create,
2018-06-20 21:42:16 +02:00
link,
2018-06-21 12:38:07 +02:00
object::{Note}
2018-05-19 00:04:30 +02:00
};
2018-05-09 22:35:02 +02:00
use chrono;
2018-06-10 21:33:42 +02:00
use diesel::{self, PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods, dsl::any};
2018-05-10 17:36:32 +02:00
use serde_json;
2018-05-09 22:35:02 +02:00
use plume_common::activity_pub::{
Id, IntoId, PUBLIC_VISIBILTY,
inbox::{FromActivity, Notify}
2018-05-19 09:39:59 +02:00
};
use plume_common::utils;
use {get_next_id, ap_url};
use instance::Instance;
use mentions::Mention;
use notifications::*;
use posts::Post;
use users::User;
2018-05-09 22:35:02 +02:00
use schema::comments;
use safe_string::SafeString;
2018-05-09 22:35:02 +02:00
2018-05-10 12:52:56 +02:00
#[derive(Queryable, Identifiable, Serialize, Clone)]
2018-05-09 22:35:02 +02:00
pub struct Comment {
pub id: i32,
pub content: SafeString,
2018-05-09 22:35:02 +02:00
pub in_response_to_id: Option<i32>,
pub post_id: i32,
pub author_id: i32,
pub creation_date: chrono::NaiveDateTime,
pub ap_url: Option<String>,
pub sensitive: bool,
pub spoiler_text: String
}
#[derive(Insertable, Default)]
2018-05-09 22:35:02 +02:00
#[table_name = "comments"]
pub struct NewComment {
pub content: SafeString,
2018-05-09 22:35:02 +02:00
pub in_response_to_id: Option<i32>,
pub post_id: i32,
pub author_id: i32,
pub ap_url: Option<String>,
pub sensitive: bool,
pub spoiler_text: String
}
impl Comment {
insert!(comments, NewComment);
get!(comments);
2018-06-21 12:38:07 +02:00
list_by!(comments, list_by_post, post_id as i32);
find_by!(comments, find_by_ap_url, ap_url as String);
2018-05-10 12:52:56 +02:00
2018-05-10 11:44:57 +02:00
pub fn get_author(&self, conn: &PgConnection) -> User {
User::get(conn, self.author_id).unwrap()
}
2018-05-10 17:36:32 +02:00
pub fn get_post(&self, conn: &PgConnection) -> Post {
Post::get(conn, self.post_id).unwrap()
}
2018-05-19 00:04:30 +02:00
2018-06-10 21:33:42 +02:00
pub fn count_local(conn: &PgConnection) -> usize {
use schema::users;
let local_authors = users::table.filter(users::instance_id.eq(Instance::local_id(conn))).select(users::id);
comments::table.filter(comments::author_id.eq(any(local_authors)))
.load::<Comment>(conn)
.expect("Couldn't load local comments")
.len()
}
pub fn to_json(&self, conn: &PgConnection, others: &Vec<Comment>) -> serde_json::Value {
let mut json = serde_json::to_value(self).unwrap();
json["author"] = self.get_author(conn).to_json(conn);
let mentions = Mention::list_for_comment(conn, self.id).into_iter()
.map(|m| m.get_mentioned(conn).map(|u| u.get_fqn(conn)).unwrap_or(String::new()))
.collect::<Vec<String>>();
json["mentions"] = serde_json::to_value(mentions).unwrap();
json["responses"] = json!(others.into_iter()
.filter(|c| c.in_response_to_id.map(|id| id == self.id).unwrap_or(false))
.map(|c| c.to_json(conn, others))
.collect::<Vec<_>>());
json
}
pub fn compute_id(&self, conn: &PgConnection) -> String {
ap_url(format!("{}#comment-{}", self.get_post(conn).ap_url, self.id))
}
2018-05-10 17:36:32 +02:00
}
impl FromActivity<Note, PgConnection> for Comment {
fn from_activity(conn: &PgConnection, note: Note, actor: Id) -> Comment {
let previous_url = note.object_props.in_reply_to.clone().unwrap().as_str().unwrap().to_string();
let previous_comment = Comment::find_by_ap_url(conn, previous_url.clone());
2018-06-20 21:42:16 +02:00
let comm = Comment::insert(conn, NewComment {
content: SafeString::new(&note.object_props.content_string().unwrap()),
spoiler_text: note.object_props.summary_string().unwrap_or(String::from("")),
ap_url: note.object_props.id_string().ok(),
in_response_to_id: previous_comment.clone().map(|c| c.id),
post_id: previous_comment
.map(|c| c.post_id)
.unwrap_or_else(|| Post::find_by_ap_url(conn, previous_url).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
});
// save mentions
if let Some(serde_json::Value::Array(tags)) = note.object_props.tag.clone() {
for tag in tags.into_iter() {
serde_json::from_value::<link::Mention>(tag)
.map(|m| Mention::from_activity(conn, m, comm.id, false))
.ok();
}
}
comm.notify(conn);
comm
}
}
impl Notify<PgConnection> for Comment {
fn notify(&self, conn: &PgConnection) {
for author in self.get_post(conn).get_authors(conn) {
Notification::insert(conn, NewNotification {
2018-07-26 15:46:10 +02:00
kind: notification_kind::COMMENT.to_string(),
object_id: self.id,
user_id: author.id
});
}
}
}
impl NewComment {
pub fn build() -> Self {
NewComment::default()
}
pub fn content<T: AsRef<str>>(mut self, val: T) -> Self {
self.content = SafeString::new(val.as_ref());
self
}
pub fn in_response_to_id(mut self, val: Option<i32>) -> Self {
self.in_response_to_id = val;
self
}
pub fn post(mut self, post: Post) -> Self {
self.post_id = post.id;
self
}
pub fn author(mut self, author: User) -> Self {
self.author_id = author.id;
self
}
pub fn create(mut self, conn: &PgConnection) -> (Create, i32) {
let post = Post::get(conn, self.post_id).unwrap();
// We have to manually compute it since the new comment haven't been inserted yet, and it needs the activity we are building to be created
let next_id = get_next_id(conn, "comments_id_seq");
self.ap_url = Some(format!("{}#comment-{}", post.ap_url, next_id));
self.sensitive = false;
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 mut note = Note::default();
let mut to = author.get_followers(conn).into_iter().map(User::into_id).collect::<Vec<Id>>();
to.append(&mut post
.get_authors(conn)
.into_iter()
.flat_map(|a| a.get_followers(conn))
.map(User::into_id)
.collect::<Vec<Id>>());
to.push(Id::new(PUBLIC_VISIBILTY.to_string()));
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_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| {
let comm = Comment::get(conn, id).unwrap();
comm.ap_url.clone().unwrap_or(comm.compute_id(conn))
}))).expect("NewComment::create: note.in_reply_to 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");
2018-06-23 14:06:15 +02:00
note.object_props.set_to_link_vec(to.clone()).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();
act.create_props.set_actor_link(author.into_id()).expect("NewComment::create: actor error");
act.create_props.set_object_object(note).expect("NewComment::create: object error");
act.object_props.set_id_string(format!("{}/activity", self.ap_url.clone().unwrap())).expect("NewComment::create: id error");
2018-06-23 14:06:15 +02:00
act.object_props.set_to_link_vec(to).expect("NewComment::create: to error");
act.object_props.set_cc_link_vec::<Id>(vec![]).expect("NewComment::create: cc error");
(act, next_id)
}
}