2020-09-01 14:25:34 +00:00
|
|
|
pub mod comment;
|
|
|
|
pub mod community;
|
|
|
|
pub mod post;
|
|
|
|
pub mod site;
|
2021-03-10 22:33:55 +00:00
|
|
|
pub mod person;
|
2021-01-18 21:57:31 +00:00
|
|
|
pub mod websocket;
|
2020-09-14 15:29:50 +00:00
|
|
|
|
|
|
|
use diesel::PgConnection;
|
2021-03-10 22:33:55 +00:00
|
|
|
use lemmy_db_queries::{Crud, DbPool};
|
2020-12-21 13:38:34 +00:00
|
|
|
use lemmy_db_schema::source::{
|
|
|
|
comment::Comment,
|
|
|
|
post::Post,
|
2021-03-10 22:33:55 +00:00
|
|
|
person::Person,
|
|
|
|
person_mention::{PersonMention, PersonMentionForm},
|
2020-09-15 19:26:47 +00:00
|
|
|
};
|
2021-03-10 22:33:55 +00:00
|
|
|
use lemmy_db_views::local_user_view::LocalUserView;
|
2021-03-01 17:24:11 +00:00
|
|
|
use lemmy_utils::{email::send_email, settings::structs::Settings, utils::MentionData, LemmyError};
|
2020-09-15 19:26:47 +00:00
|
|
|
use log::error;
|
2020-09-24 13:53:21 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-01-27 16:42:23 +00:00
|
|
|
use url::Url;
|
2020-09-24 13:53:21 +00:00
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
pub struct WebFingerLink {
|
|
|
|
pub rel: Option<String>,
|
|
|
|
#[serde(rename(serialize = "type", deserialize = "type"))]
|
|
|
|
pub type_: Option<String>,
|
2021-01-27 16:42:23 +00:00
|
|
|
pub href: Option<Url>,
|
2020-09-24 13:53:21 +00:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub template: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
pub struct WebFingerResponse {
|
|
|
|
pub subject: String,
|
2021-01-27 16:42:23 +00:00
|
|
|
pub aliases: Vec<Url>,
|
2020-09-24 13:53:21 +00:00
|
|
|
pub links: Vec<WebFingerLink>,
|
|
|
|
}
|
2020-09-14 15:29:50 +00:00
|
|
|
|
|
|
|
pub async fn blocking<F, T>(pool: &DbPool, f: F) -> Result<T, LemmyError>
|
2020-09-15 19:26:47 +00:00
|
|
|
where
|
|
|
|
F: FnOnce(&diesel::PgConnection) -> T + Send + 'static,
|
|
|
|
T: Send + 'static,
|
2020-09-14 15:29:50 +00:00
|
|
|
{
|
|
|
|
let pool = pool.clone();
|
|
|
|
let res = actix_web::web::block(move || {
|
|
|
|
let conn = pool.get()?;
|
|
|
|
let res = (f)(&conn);
|
|
|
|
Ok(res) as Result<_, LemmyError>
|
|
|
|
})
|
2020-09-15 19:26:47 +00:00
|
|
|
.await?;
|
2020-09-14 15:29:50 +00:00
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn send_local_notifs(
|
|
|
|
mentions: Vec<MentionData>,
|
|
|
|
comment: Comment,
|
2021-03-10 22:33:55 +00:00
|
|
|
person: Person,
|
2020-09-14 15:29:50 +00:00
|
|
|
post: Post,
|
|
|
|
pool: &DbPool,
|
|
|
|
do_send_email: bool,
|
|
|
|
) -> Result<Vec<i32>, LemmyError> {
|
|
|
|
let ids = blocking(pool, move |conn| {
|
2021-03-10 22:33:55 +00:00
|
|
|
do_send_local_notifs(conn, &mentions, &comment, &person, &post, do_send_email)
|
2020-09-14 15:29:50 +00:00
|
|
|
})
|
2020-09-15 19:26:47 +00:00
|
|
|
.await?;
|
2020-09-14 15:29:50 +00:00
|
|
|
|
|
|
|
Ok(ids)
|
|
|
|
}
|
|
|
|
|
2021-03-10 22:33:55 +00:00
|
|
|
// TODO should this really use person_ids as recipient ids? or local_user_ids ?
|
2020-09-14 15:29:50 +00:00
|
|
|
fn do_send_local_notifs(
|
|
|
|
conn: &PgConnection,
|
|
|
|
mentions: &[MentionData],
|
|
|
|
comment: &Comment,
|
2021-03-10 22:33:55 +00:00
|
|
|
person: &Person,
|
2020-09-14 15:29:50 +00:00
|
|
|
post: &Post,
|
|
|
|
do_send_email: bool,
|
|
|
|
) -> Vec<i32> {
|
|
|
|
let mut recipient_ids = Vec::new();
|
|
|
|
|
|
|
|
// Send the local mentions
|
|
|
|
for mention in mentions
|
|
|
|
.iter()
|
2021-03-10 22:33:55 +00:00
|
|
|
.filter(|m| m.is_local() && m.name.ne(&person.name))
|
2020-09-14 15:29:50 +00:00
|
|
|
.collect::<Vec<&MentionData>>()
|
|
|
|
{
|
2021-03-10 22:33:55 +00:00
|
|
|
// TODO do a local user fetch
|
|
|
|
if let Ok(mention_user_view) = LocalUserView::read_from_name(&conn, &mention.name) {
|
2020-09-14 15:29:50 +00:00
|
|
|
// TODO
|
|
|
|
// At some point, make it so you can't tag the parent creator either
|
|
|
|
// This can cause two notifications, one for reply and the other for mention
|
2021-03-10 22:33:55 +00:00
|
|
|
recipient_ids.push(mention_user_view.person.id);
|
2020-09-14 15:29:50 +00:00
|
|
|
|
2021-03-10 22:33:55 +00:00
|
|
|
let user_mention_form = PersonMentionForm {
|
|
|
|
recipient_id: mention_user_view.person.id,
|
2020-09-14 15:29:50 +00:00
|
|
|
comment_id: comment.id,
|
|
|
|
read: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Allow this to fail softly, since comment edits might re-update or replace it
|
|
|
|
// Let the uniqueness handle this fail
|
2021-03-10 22:33:55 +00:00
|
|
|
PersonMention::create(&conn, &user_mention_form).ok();
|
2020-09-14 15:29:50 +00:00
|
|
|
|
|
|
|
// Send an email to those users that have notifications on
|
2021-03-10 22:33:55 +00:00
|
|
|
if do_send_email && mention_user_view.local_user.send_notifications_to_email {
|
2020-11-05 12:43:46 +00:00
|
|
|
send_email_to_user(
|
2021-03-10 22:33:55 +00:00
|
|
|
mention_user_view,
|
2020-11-05 12:43:46 +00:00
|
|
|
"Mentioned by",
|
2021-03-10 22:33:55 +00:00
|
|
|
"Person Mention",
|
2020-11-05 12:43:46 +00:00
|
|
|
&comment.content,
|
|
|
|
)
|
2020-09-14 15:29:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send notifs to the parent commenter / poster
|
|
|
|
match comment.parent_id {
|
|
|
|
Some(parent_id) => {
|
|
|
|
if let Ok(parent_comment) = Comment::read(&conn, parent_id) {
|
2021-03-10 22:33:55 +00:00
|
|
|
if parent_comment.creator_id != person.id {
|
|
|
|
if let Ok(parent_user_view) = LocalUserView::read(&conn, parent_comment.creator_id) {
|
|
|
|
recipient_ids.push(parent_user_view.person.id);
|
2020-09-14 15:29:50 +00:00
|
|
|
|
2021-03-10 22:33:55 +00:00
|
|
|
if do_send_email && parent_user_view.local_user.send_notifications_to_email {
|
|
|
|
send_email_to_user(parent_user_view, "Reply from", "Comment Reply", &comment.content)
|
2020-09-14 15:29:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Its a post
|
|
|
|
None => {
|
2021-03-10 22:33:55 +00:00
|
|
|
if post.creator_id != person.id {
|
|
|
|
if let Ok(parent_user_view) = LocalUserView::read(&conn, post.creator_id) {
|
|
|
|
recipient_ids.push(parent_user_view.person.id);
|
2020-09-14 15:29:50 +00:00
|
|
|
|
2021-03-10 22:33:55 +00:00
|
|
|
if do_send_email && parent_user_view.local_user.send_notifications_to_email {
|
|
|
|
send_email_to_user(parent_user_view, "Reply from", "Post Reply", &comment.content)
|
2020-09-14 15:29:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
recipient_ids
|
|
|
|
}
|
2020-11-05 12:43:46 +00:00
|
|
|
|
2021-03-10 22:33:55 +00:00
|
|
|
pub fn send_email_to_user(local_user_view: LocalUserView, subject_text: &str, body_text: &str, comment_content: &str) {
|
|
|
|
if local_user_view.person.banned {
|
2020-11-05 12:43:46 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-10 22:33:55 +00:00
|
|
|
if let Some(user_email) = local_user_view.local_user.email {
|
2020-11-05 12:43:46 +00:00
|
|
|
let subject = &format!(
|
|
|
|
"{} - {} {}",
|
|
|
|
subject_text,
|
2021-03-01 17:24:11 +00:00
|
|
|
Settings::get().hostname(),
|
2021-03-10 22:33:55 +00:00
|
|
|
local_user_view.person.name,
|
2020-11-05 12:43:46 +00:00
|
|
|
);
|
|
|
|
let html = &format!(
|
|
|
|
"<h1>{}</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>",
|
|
|
|
body_text,
|
2021-03-10 22:33:55 +00:00
|
|
|
local_user_view.person.name,
|
2020-11-05 12:43:46 +00:00
|
|
|
comment_content,
|
|
|
|
Settings::get().get_protocol_and_hostname()
|
|
|
|
);
|
2021-03-10 22:33:55 +00:00
|
|
|
match send_email(subject, &user_email, &local_user_view.person.name, html) {
|
2020-11-05 12:43:46 +00:00
|
|
|
Ok(_o) => _o,
|
|
|
|
Err(e) => error!("{}", e),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|