8 use diesel::PgConnection;
9 use lemmy_db_queries::{Crud, DbPool};
10 use lemmy_db_schema::source::{
13 person_mention::{PersonMention, PersonMentionForm},
16 use lemmy_db_views::local_user_view::LocalUserView;
17 use lemmy_utils::{email::send_email, settings::structs::Settings, utils::MentionData, LemmyError};
19 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<Url>,
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<Url>,
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 ids = blocking(pool, move |conn| {
64 do_send_local_notifs(conn, &mentions, &comment, &person, &post, do_send_email)
71 // TODO should this really use person_ids as recipient ids? or local_user_ids ?
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(&person.name))
86 .collect::<Vec<&MentionData>>()
88 // TODO do a local user fetch
89 if let Ok(mention_user_view) = LocalUserView::read_from_name(&conn, &mention.name) {
91 // At some point, make it so you can't tag the parent creator either
92 // This can cause two notifications, one for reply and the other for mention
93 recipient_ids.push(mention_user_view.local_user.id);
95 let user_mention_form = PersonMentionForm {
96 recipient_id: mention_user_view.person.id,
97 comment_id: comment.id,
101 // Allow this to fail softly, since comment edits might re-update or replace it
102 // Let the uniqueness handle this fail
103 PersonMention::create(&conn, &user_mention_form).ok();
105 // Send an email to those local users that have notifications on
106 if do_send_email && mention_user_view.local_user.send_notifications_to_email {
117 // Send notifs to the parent commenter / poster
118 match comment.parent_id {
120 if let Ok(parent_comment) = Comment::read(&conn, parent_id) {
121 if parent_comment.creator_id != person.id {
122 if let Ok(parent_user_view) = LocalUserView::read_person(&conn, parent_comment.creator_id)
124 recipient_ids.push(parent_user_view.local_user.id);
126 if do_send_email && parent_user_view.local_user.send_notifications_to_email {
140 if post.creator_id != person.id {
141 if let Ok(parent_user_view) = LocalUserView::read_person(&conn, post.creator_id) {
142 recipient_ids.push(parent_user_view.local_user.id);
144 if do_send_email && parent_user_view.local_user.send_notifications_to_email {
159 pub fn send_email_to_user(
160 local_user_view: &LocalUserView,
163 comment_content: &str,
165 if local_user_view.person.banned {
169 if let Some(user_email) = &local_user_view.local_user.email {
170 let subject = &format!(
173 Settings::get().hostname(),
174 local_user_view.person.name,
177 "<h1>{}</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>",
179 local_user_view.person.name,
181 Settings::get().get_protocol_and_hostname()
183 match send_email(subject, &user_email, &local_user_view.person.name, html) {
185 Err(e) => error!("{}", e),