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 fn do_send_local_notifs(
73 mentions: &[MentionData],
79 let mut recipient_ids = Vec::new();
81 // Send the local mentions
82 for mention in mentions
84 .filter(|m| m.is_local() && m.name.ne(&person.name))
85 .collect::<Vec<&MentionData>>()
87 if let Ok(mention_user_view) = LocalUserView::read_from_name(&conn, &mention.name) {
89 // At some point, make it so you can't tag the parent creator either
90 // This can cause two notifications, one for reply and the other for mention
91 recipient_ids.push(mention_user_view.local_user.id);
93 let user_mention_form = PersonMentionForm {
94 recipient_id: mention_user_view.person.id,
95 comment_id: comment.id,
99 // Allow this to fail softly, since comment edits might re-update or replace it
100 // Let the uniqueness handle this fail
101 PersonMention::create(&conn, &user_mention_form).ok();
103 // Send an email to those local users that have notifications on
104 if do_send_email && mention_user_view.local_user.send_notifications_to_email {
115 // Send notifs to the parent commenter / poster
116 match comment.parent_id {
118 if let Ok(parent_comment) = Comment::read(&conn, parent_id) {
119 if parent_comment.creator_id != person.id {
120 if let Ok(parent_user_view) = LocalUserView::read_person(&conn, parent_comment.creator_id)
122 recipient_ids.push(parent_user_view.local_user.id);
124 if do_send_email && parent_user_view.local_user.send_notifications_to_email {
138 if post.creator_id != person.id {
139 if let Ok(parent_user_view) = LocalUserView::read_person(&conn, post.creator_id) {
140 recipient_ids.push(parent_user_view.local_user.id);
142 if do_send_email && parent_user_view.local_user.send_notifications_to_email {
157 pub fn send_email_to_user(
158 local_user_view: &LocalUserView,
161 comment_content: &str,
163 if local_user_view.person.banned {
167 if let Some(user_email) = &local_user_view.local_user.email {
168 let subject = &format!(
171 Settings::get().hostname(),
172 local_user_view.person.name,
175 "<h1>{}</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>",
177 local_user_view.person.name,
179 Settings::get().get_protocol_and_hostname()
181 match send_email(subject, &user_email, &local_user_view.person.name, html) {
183 Err(e) => error!("{}", e),