7 use diesel::PgConnection;
13 user_mention::{UserMention, UserMentionForm},
18 use lemmy_utils::{email::send_email, settings::Settings, utils::MentionData, LemmyError};
20 use serde::{Deserialize, Serialize};
22 #[derive(Serialize, Deserialize, Debug)]
23 pub struct WebFingerLink {
24 pub rel: Option<String>,
25 #[serde(rename(serialize = "type", deserialize = "type"))]
26 pub type_: Option<String>,
27 pub href: Option<String>,
28 #[serde(skip_serializing_if = "Option::is_none")]
29 pub template: Option<String>,
32 #[derive(Serialize, Deserialize, Debug)]
33 pub struct WebFingerResponse {
35 pub aliases: Vec<String>,
36 pub links: Vec<WebFingerLink>,
39 pub async fn blocking<F, T>(pool: &DbPool, f: F) -> Result<T, LemmyError>
41 F: FnOnce(&diesel::PgConnection) -> T + Send + 'static,
44 let pool = pool.clone();
45 let res = actix_web::web::block(move || {
46 let conn = pool.get()?;
48 Ok(res) as Result<_, LemmyError>
55 pub async fn send_local_notifs(
56 mentions: Vec<MentionData>,
62 ) -> Result<Vec<i32>, LemmyError> {
63 let user2 = user.clone();
64 let ids = blocking(pool, move |conn| {
65 do_send_local_notifs(conn, &mentions, &comment, &user2, &post, do_send_email)
72 fn do_send_local_notifs(
74 mentions: &[MentionData],
80 let mut recipient_ids = Vec::new();
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 let _ = UserMention::create(&conn, &user_mention_form);
104 // Send an email to those users that have notifications on
105 if do_send_email && mention_user.send_notifications_to_email {
116 // Send notifs to the parent commenter / poster
117 match comment.parent_id {
119 if let Ok(parent_comment) = Comment::read(&conn, parent_id) {
120 if parent_comment.creator_id != user.id {
121 if let Ok(parent_user) = User_::read(&conn, parent_comment.creator_id) {
122 recipient_ids.push(parent_user.id);
124 if do_send_email && parent_user.send_notifications_to_email {
125 send_email_to_user(parent_user, "Reply from", "Comment Reply", &comment.content)
133 if post.creator_id != user.id {
134 if let Ok(parent_user) = User_::read(&conn, post.creator_id) {
135 recipient_ids.push(parent_user.id);
137 if do_send_email && parent_user.send_notifications_to_email {
138 send_email_to_user(parent_user, "Reply from", "Post Reply", &comment.content)
147 pub fn send_email_to_user(user: User_, subject_text: &str, body_text: &str, comment_content: &str) {
152 if let Some(user_email) = user.email {
153 let subject = &format!(
156 Settings::get().hostname,
160 "<h1>{}</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>",
164 Settings::get().get_protocol_and_hostname()
166 match send_email(subject, &user_email, &user.name, html) {
168 Err(e) => error!("{}", e),