8 use diesel::PgConnection;
13 user_mention::{UserMention, UserMentionForm},
17 use lemmy_utils::{email::send_email, settings::Settings, utils::MentionData, LemmyError};
19 use serde::{Deserialize, Serialize};
21 #[derive(Serialize, Deserialize, Debug)]
22 pub struct WebFingerLink {
23 pub rel: Option<String>,
24 #[serde(rename(serialize = "type", deserialize = "type"))]
25 pub type_: Option<String>,
26 pub href: Option<String>,
27 #[serde(skip_serializing_if = "Option::is_none")]
28 pub template: Option<String>,
31 #[derive(Serialize, Deserialize, Debug)]
32 pub struct WebFingerResponse {
34 pub aliases: Vec<String>,
35 pub links: Vec<WebFingerLink>,
38 pub async fn blocking<F, T>(pool: &DbPool, f: F) -> Result<T, LemmyError>
40 F: FnOnce(&diesel::PgConnection) -> T + Send + 'static,
43 let pool = pool.clone();
44 let res = actix_web::web::block(move || {
45 let conn = pool.get()?;
47 Ok(res) as Result<_, LemmyError>
54 pub async fn send_local_notifs(
55 mentions: Vec<MentionData>,
61 ) -> Result<Vec<i32>, LemmyError> {
62 let user2 = user.clone();
63 let ids = blocking(pool, move |conn| {
64 do_send_local_notifs(conn, &mentions, &comment, &user2, &post, do_send_email)
71 fn do_send_local_notifs(
73 mentions: &[MentionData],
79 let mut recipient_ids = Vec::new();
80 let hostname = &format!("https://{}", Settings::get().hostname);
82 // Send the local mentions
83 for mention in mentions
85 .filter(|m| m.is_local() && m.name.ne(&user.name))
86 .collect::<Vec<&MentionData>>()
88 if let Ok(mention_user) = User_::read_from_name(&conn, &mention.name) {
90 // At some point, make it so you can't tag the parent creator either
91 // This can cause two notifications, one for reply and the other for mention
92 recipient_ids.push(mention_user.id);
94 let user_mention_form = UserMentionForm {
95 recipient_id: mention_user.id,
96 comment_id: comment.id,
100 // Allow this to fail softly, since comment edits might re-update or replace it
101 // Let the uniqueness handle this fail
102 match UserMention::create(&conn, &user_mention_form) {
104 Err(_e) => error!("{}", &_e),
107 // Send an email to those users that have notifications on
108 if do_send_email && mention_user.send_notifications_to_email {
109 if let Some(mention_email) = mention_user.email {
110 let subject = &format!("{} - Mentioned by {}", Settings::get().hostname, user.name,);
112 "<h1>User Mention</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>",
113 user.name, comment.content, hostname
115 match send_email(subject, &mention_email, &mention_user.name, html) {
117 Err(e) => error!("{}", e),
124 // Send notifs to the parent commenter / poster
125 match comment.parent_id {
127 if let Ok(parent_comment) = Comment::read(&conn, parent_id) {
128 if parent_comment.creator_id != user.id {
129 if let Ok(parent_user) = User_::read(&conn, parent_comment.creator_id) {
130 recipient_ids.push(parent_user.id);
132 if do_send_email && parent_user.send_notifications_to_email {
133 if let Some(comment_reply_email) = parent_user.email {
134 let subject = &format!("{} - Reply from {}", Settings::get().hostname, user.name,);
136 "<h1>Comment Reply</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>",
137 user.name, comment.content, hostname
139 match send_email(subject, &comment_reply_email, &parent_user.name, html) {
141 Err(e) => error!("{}", e),
151 if post.creator_id != user.id {
152 if let Ok(parent_user) = User_::read(&conn, post.creator_id) {
153 recipient_ids.push(parent_user.id);
155 if do_send_email && parent_user.send_notifications_to_email {
156 if let Some(post_reply_email) = parent_user.email {
157 let subject = &format!("{} - Reply from {}", Settings::get().hostname, user.name,);
159 "<h1>Post Reply</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>",
160 user.name, comment.content, hostname
162 match send_email(subject, &post_reply_email, &parent_user.name, html) {
164 Err(e) => error!("{}", e),