8 use diesel::PgConnection;
9 use lemmy_db_queries::{Crud, DbPool};
10 use lemmy_db_schema::{
14 person_mention::{PersonMention, PersonMentionForm},
19 use lemmy_db_views::local_user_view::LocalUserView;
20 use lemmy_utils::{email::send_email, settings::structs::Settings, utils::MentionData, LemmyError};
22 use serde::{Deserialize, Serialize};
25 #[derive(Serialize, Deserialize, Debug)]
26 pub struct WebFingerLink {
27 pub rel: Option<String>,
28 #[serde(rename(serialize = "type", deserialize = "type"))]
29 pub type_: Option<String>,
30 pub href: Option<Url>,
31 #[serde(skip_serializing_if = "Option::is_none")]
32 pub template: Option<String>,
35 #[derive(Serialize, Deserialize, Debug)]
36 pub struct WebFingerResponse {
38 pub aliases: Vec<Url>,
39 pub links: Vec<WebFingerLink>,
42 pub async fn blocking<F, T>(pool: &DbPool, f: F) -> Result<T, LemmyError>
44 F: FnOnce(&diesel::PgConnection) -> T + Send + 'static,
47 let pool = pool.clone();
48 let res = actix_web::web::block(move || {
49 let conn = pool.get()?;
51 Ok(res) as Result<_, LemmyError>
58 pub async fn send_local_notifs(
59 mentions: Vec<MentionData>,
65 ) -> Result<Vec<LocalUserId>, LemmyError> {
66 let ids = blocking(pool, move |conn| {
67 do_send_local_notifs(conn, &mentions, &comment, &person, &post, do_send_email)
74 fn do_send_local_notifs(
76 mentions: &[MentionData],
81 ) -> Vec<LocalUserId> {
82 let mut recipient_ids = Vec::new();
84 // Send the local mentions
85 for mention in mentions
87 .filter(|m| m.is_local() && m.name.ne(&person.name))
88 .collect::<Vec<&MentionData>>()
90 if let Ok(mention_user_view) = LocalUserView::read_from_name(&conn, &mention.name) {
92 // At some point, make it so you can't tag the parent creator either
93 // This can cause two notifications, one for reply and the other for mention
94 recipient_ids.push(mention_user_view.local_user.id);
96 let user_mention_form = PersonMentionForm {
97 recipient_id: mention_user_view.person.id,
98 comment_id: comment.id,
102 // Allow this to fail softly, since comment edits might re-update or replace it
103 // Let the uniqueness handle this fail
104 PersonMention::create(&conn, &user_mention_form).ok();
106 // Send an email to those local users that have notifications on
118 // Send notifs to the parent commenter / poster
119 match comment.parent_id {
121 if let Ok(parent_comment) = Comment::read(&conn, parent_id) {
122 // Don't send a notif to yourself
123 if parent_comment.creator_id != person.id {
124 // Get the parent commenter local_user
125 if let Ok(parent_user_view) = LocalUserView::read_person(&conn, parent_comment.creator_id)
127 recipient_ids.push(parent_user_view.local_user.id);
143 if post.creator_id != person.id {
144 if let Ok(parent_user_view) = LocalUserView::read_person(&conn, post.creator_id) {
145 recipient_ids.push(parent_user_view.local_user.id);
162 pub fn send_email_to_user(
163 local_user_view: &LocalUserView,
166 comment_content: &str,
168 if local_user_view.person.banned || !local_user_view.local_user.send_notifications_to_email {
172 if let Some(user_email) = &local_user_view.local_user.email {
173 let subject = &format!(
176 Settings::get().hostname(),
177 local_user_view.person.name,
180 "<h1>{}</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>",
182 local_user_view.person.name,
184 Settings::get().get_protocol_and_hostname()
186 match send_email(subject, &user_email, &local_user_view.person.name, html) {
188 Err(e) => error!("{}", e),