lemmy/crates/api_structs/src/lib.rs

189 lines
5.1 KiB
Rust
Raw Normal View History

pub mod comment;
pub mod community;
2021-03-11 04:43:11 +00:00
pub mod person;
pub mod post;
pub mod site;
pub mod websocket;
use diesel::PgConnection;
2021-03-10 22:33:55 +00:00
use lemmy_db_queries::{Crud, DbPool};
use lemmy_db_schema::source::{
comment::Comment,
2021-03-10 22:33:55 +00:00
person::Person,
person_mention::{PersonMention, PersonMentionForm},
2021-03-11 04:43:11 +00:00
post::Post,
};
2021-03-10 22:33:55 +00:00
use lemmy_db_views::local_user_view::LocalUserView;
use lemmy_utils::{email::send_email, settings::structs::Settings, utils::MentionData, LemmyError};
use log::error;
use serde::{Deserialize, Serialize};
use url::Url;
#[derive(Serialize, Deserialize, Debug)]
pub struct WebFingerLink {
pub rel: Option<String>,
#[serde(rename(serialize = "type", deserialize = "type"))]
pub type_: Option<String>,
pub href: Option<Url>,
#[serde(skip_serializing_if = "Option::is_none")]
pub template: Option<String>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct WebFingerResponse {
pub subject: String,
pub aliases: Vec<Url>,
pub links: Vec<WebFingerLink>,
}
pub async fn blocking<F, T>(pool: &DbPool, f: F) -> Result<T, LemmyError>
where
F: FnOnce(&diesel::PgConnection) -> T + Send + 'static,
T: Send + 'static,
{
let pool = pool.clone();
let res = actix_web::web::block(move || {
let conn = pool.get()?;
let res = (f)(&conn);
Ok(res) as Result<_, LemmyError>
})
.await?;
Ok(res)
}
pub async fn send_local_notifs(
mentions: Vec<MentionData>,
comment: Comment,
2021-03-10 22:33:55 +00:00
person: Person,
post: Post,
pool: &DbPool,
do_send_email: bool,
) -> Result<Vec<i32>, LemmyError> {
let ids = blocking(pool, move |conn| {
2021-03-10 22:33:55 +00:00
do_send_local_notifs(conn, &mentions, &comment, &person, &post, do_send_email)
})
.await?;
Ok(ids)
}
2021-03-10 22:33:55 +00:00
// TODO should this really use person_ids as recipient ids? or local_user_ids ?
fn do_send_local_notifs(
conn: &PgConnection,
mentions: &[MentionData],
comment: &Comment,
2021-03-10 22:33:55 +00:00
person: &Person,
post: &Post,
do_send_email: bool,
) -> Vec<i32> {
let mut recipient_ids = Vec::new();
// Send the local mentions
for mention in mentions
.iter()
2021-03-10 22:33:55 +00:00
.filter(|m| m.is_local() && m.name.ne(&person.name))
.collect::<Vec<&MentionData>>()
{
2021-03-10 22:33:55 +00:00
// TODO do a local user fetch
if let Ok(mention_user_view) = LocalUserView::read_from_name(&conn, &mention.name) {
// TODO
// At some point, make it so you can't tag the parent creator either
// This can cause two notifications, one for reply and the other for mention
2021-03-11 22:47:44 +00:00
recipient_ids.push(mention_user_view.local_user.id);
2021-03-10 22:33:55 +00:00
let user_mention_form = PersonMentionForm {
recipient_id: mention_user_view.person.id,
comment_id: comment.id,
read: None,
};
// Allow this to fail softly, since comment edits might re-update or replace it
// Let the uniqueness handle this fail
2021-03-10 22:33:55 +00:00
PersonMention::create(&conn, &user_mention_form).ok();
2021-03-11 22:47:44 +00:00
// Send an email to those local users that have notifications on
2021-03-10 22:33:55 +00:00
if do_send_email && mention_user_view.local_user.send_notifications_to_email {
send_email_to_user(
2021-03-10 22:33:55 +00:00
mention_user_view,
"Mentioned by",
2021-03-10 22:33:55 +00:00
"Person Mention",
&comment.content,
)
}
}
}
// Send notifs to the parent commenter / poster
match comment.parent_id {
Some(parent_id) => {
if let Ok(parent_comment) = Comment::read(&conn, parent_id) {
2021-03-10 22:33:55 +00:00
if parent_comment.creator_id != person.id {
2021-03-11 04:43:11 +00:00
if let Ok(parent_user_view) = LocalUserView::read_person(&conn, parent_comment.creator_id)
{
2021-03-11 22:47:44 +00:00
recipient_ids.push(parent_user_view.local_user.id);
2021-03-10 22:33:55 +00:00
if do_send_email && parent_user_view.local_user.send_notifications_to_email {
2021-03-11 04:43:11 +00:00
send_email_to_user(
parent_user_view,
"Reply from",
"Comment Reply",
&comment.content,
)
}
}
}
}
}
// Its a post
None => {
2021-03-10 22:33:55 +00:00
if post.creator_id != person.id {
2021-03-11 04:43:11 +00:00
if let Ok(parent_user_view) = LocalUserView::read_person(&conn, post.creator_id) {
2021-03-11 22:47:44 +00:00
recipient_ids.push(parent_user_view.local_user.id);
2021-03-10 22:33:55 +00:00
if do_send_email && parent_user_view.local_user.send_notifications_to_email {
2021-03-11 04:43:11 +00:00
send_email_to_user(
parent_user_view,
"Reply from",
"Post Reply",
&comment.content,
)
}
}
}
}
};
recipient_ids
}
2021-03-11 04:43:11 +00:00
pub fn send_email_to_user(
local_user_view: LocalUserView,
subject_text: &str,
body_text: &str,
comment_content: &str,
) {
2021-03-10 22:33:55 +00:00
if local_user_view.person.banned {
return;
}
2021-03-10 22:33:55 +00:00
if let Some(user_email) = local_user_view.local_user.email {
let subject = &format!(
"{} - {} {}",
subject_text,
Settings::get().hostname(),
2021-03-10 22:33:55 +00:00
local_user_view.person.name,
);
let html = &format!(
"<h1>{}</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>",
body_text,
2021-03-10 22:33:55 +00:00
local_user_view.person.name,
comment_content,
Settings::get().get_protocol_and_hostname()
);
2021-03-10 22:33:55 +00:00
match send_email(subject, &user_email, &local_user_view.person.name, html) {
Ok(_o) => _o,
Err(e) => error!("{}", e),
};
}
}