8 use crate::site::FederatedInstances;
9 use diesel::PgConnection;
10 use lemmy_db_queries::{
12 community::{CommunityModerator_, Community_},
13 person_block::PersonBlock_,
20 use lemmy_db_schema::{
23 community::{Community, CommunityModerator},
25 person_block::PersonBlock,
26 person_mention::{PersonMention, PersonMentionForm},
27 post::{Post, PostRead, PostReadForm},
35 use lemmy_db_views::local_user_view::{LocalUserSettingsView, LocalUserView};
36 use lemmy_db_views_actor::{
37 community_person_ban_view::CommunityPersonBanView,
38 community_view::CommunityView,
43 settings::structs::Settings,
49 use serde::{Deserialize, Serialize};
52 #[derive(Serialize, Deserialize, Debug)]
53 pub struct WebFingerLink {
54 pub rel: Option<String>,
55 #[serde(rename(serialize = "type", deserialize = "type"))]
56 pub type_: Option<String>,
57 pub href: Option<Url>,
58 #[serde(skip_serializing_if = "Option::is_none")]
59 pub template: Option<String>,
62 #[derive(Serialize, Deserialize, Debug)]
63 pub struct WebFingerResponse {
65 pub aliases: Vec<Url>,
66 pub links: Vec<WebFingerLink>,
69 pub async fn blocking<F, T>(pool: &DbPool, f: F) -> Result<T, LemmyError>
71 F: FnOnce(&diesel::PgConnection) -> T + Send + 'static,
74 let pool = pool.clone();
75 let res = actix_web::web::block(move || {
76 let conn = pool.get()?;
78 Ok(res) as Result<T, LemmyError>
85 pub async fn send_local_notifs(
86 mentions: Vec<MentionData>,
92 ) -> Result<Vec<LocalUserId>, LemmyError> {
93 let ids = blocking(pool, move |conn| {
94 do_send_local_notifs(conn, &mentions, &comment, &person, &post, do_send_email)
101 fn do_send_local_notifs(
103 mentions: &[MentionData],
108 ) -> Vec<LocalUserId> {
109 let mut recipient_ids = Vec::new();
111 // Send the local mentions
112 for mention in mentions
114 .filter(|m| m.is_local() && m.name.ne(&person.name))
115 .collect::<Vec<&MentionData>>()
117 if let Ok(mention_user_view) = LocalUserView::read_from_name(conn, &mention.name) {
119 // At some point, make it so you can't tag the parent creator either
120 // This can cause two notifications, one for reply and the other for mention
121 recipient_ids.push(mention_user_view.local_user.id);
123 let user_mention_form = PersonMentionForm {
124 recipient_id: mention_user_view.person.id,
125 comment_id: comment.id,
129 // Allow this to fail softly, since comment edits might re-update or replace it
130 // Let the uniqueness handle this fail
131 PersonMention::create(conn, &user_mention_form).ok();
133 // Send an email to those local users that have notifications on
145 // Send notifs to the parent commenter / poster
146 match comment.parent_id {
148 if let Ok(parent_comment) = Comment::read(conn, parent_id) {
149 // Don't send a notif to yourself
150 if parent_comment.creator_id != person.id {
151 // Get the parent commenter local_user
152 if let Ok(parent_user_view) = LocalUserView::read_person(conn, parent_comment.creator_id)
154 recipient_ids.push(parent_user_view.local_user.id);
170 if post.creator_id != person.id {
171 if let Ok(parent_user_view) = LocalUserView::read_person(conn, post.creator_id) {
172 recipient_ids.push(parent_user_view.local_user.id);
189 pub fn send_email_to_user(
190 local_user_view: &LocalUserView,
193 comment_content: &str,
195 if local_user_view.person.banned || !local_user_view.local_user.send_notifications_to_email {
199 if let Some(user_email) = &local_user_view.local_user.email {
200 let subject = &format!(
203 Settings::get().hostname,
204 local_user_view.person.name,
207 "<h1>{}</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>",
209 local_user_view.person.name,
211 Settings::get().get_protocol_and_hostname()
213 match send_email(subject, user_email, &local_user_view.person.name, html) {
215 Err(e) => error!("{}", e),
220 pub async fn is_mod_or_admin(
223 community_id: CommunityId,
224 ) -> Result<(), LemmyError> {
225 let is_mod_or_admin = blocking(pool, move |conn| {
226 CommunityView::is_mod_or_admin(conn, person_id, community_id)
229 if !is_mod_or_admin {
230 return Err(ApiError::err("not_a_mod_or_admin").into());
235 pub fn is_admin(local_user_view: &LocalUserView) -> Result<(), LemmyError> {
236 if !local_user_view.person.admin {
237 return Err(ApiError::err("not_an_admin").into());
242 pub async fn get_post(post_id: PostId, pool: &DbPool) -> Result<Post, LemmyError> {
243 blocking(pool, move |conn| Post::read(conn, post_id))
245 .map_err(|_| ApiError::err("couldnt_find_post").into())
248 pub async fn mark_post_as_read(
252 ) -> Result<PostRead, LemmyError> {
253 let post_read_form = PostReadForm { post_id, person_id };
255 blocking(pool, move |conn| {
256 PostRead::mark_as_read(conn, &post_read_form)
259 .map_err(|_| ApiError::err("couldnt_mark_post_as_read").into())
262 pub async fn get_local_user_view_from_jwt(
265 ) -> Result<LocalUserView, LemmyError> {
266 let claims = Claims::decode(jwt)
267 .map_err(|_| ApiError::err("not_logged_in"))?
269 let local_user_id = LocalUserId(claims.sub);
270 let local_user_view =
271 blocking(pool, move |conn| LocalUserView::read(conn, local_user_id)).await??;
272 // Check for a site ban
273 if local_user_view.person.banned {
274 return Err(ApiError::err("site_ban").into());
277 // Check for user deletion
278 if local_user_view.person.deleted {
279 return Err(ApiError::err("deleted").into());
282 check_validator_time(&local_user_view.local_user.validator_time, &claims)?;
287 /// Checks if user's token was issued before user's password reset.
288 pub fn check_validator_time(
289 validator_time: &chrono::NaiveDateTime,
291 ) -> Result<(), LemmyError> {
292 let user_validation_time = validator_time.timestamp();
293 if user_validation_time > claims.iat {
294 Err(ApiError::err("not_logged_in").into())
300 pub async fn get_local_user_view_from_jwt_opt(
301 jwt: &Option<String>,
303 ) -> Result<Option<LocalUserView>, LemmyError> {
305 Some(jwt) => Ok(Some(get_local_user_view_from_jwt(jwt, pool).await?)),
310 pub async fn get_local_user_settings_view_from_jwt(
313 ) -> Result<LocalUserSettingsView, LemmyError> {
314 let claims = Claims::decode(jwt)
315 .map_err(|_| ApiError::err("not_logged_in"))?
317 let local_user_id = LocalUserId(claims.sub);
318 let local_user_view = blocking(pool, move |conn| {
319 LocalUserSettingsView::read(conn, local_user_id)
322 // Check for a site ban
323 if local_user_view.person.banned {
324 return Err(ApiError::err("site_ban").into());
327 check_validator_time(&local_user_view.local_user.validator_time, &claims)?;
332 pub async fn get_local_user_settings_view_from_jwt_opt(
333 jwt: &Option<String>,
335 ) -> Result<Option<LocalUserSettingsView>, LemmyError> {
337 Some(jwt) => Ok(Some(
338 get_local_user_settings_view_from_jwt(jwt, pool).await?,
344 pub async fn check_community_ban(
346 community_id: CommunityId,
348 ) -> Result<(), LemmyError> {
350 move |conn: &'_ _| CommunityPersonBanView::get(conn, person_id, community_id).is_ok();
351 if blocking(pool, is_banned).await? {
352 Err(ApiError::err("community_ban").into())
358 pub async fn check_person_block(
360 potential_blocker_id: PersonId,
362 ) -> Result<(), LemmyError> {
363 let is_blocked = move |conn: &'_ _| PersonBlock::read(conn, potential_blocker_id, my_id).is_ok();
364 if blocking(pool, is_blocked).await? {
365 Err(ApiError::err("person_block").into())
371 pub async fn check_downvotes_enabled(score: i16, pool: &DbPool) -> Result<(), LemmyError> {
373 let site = blocking(pool, move |conn| Site::read_simple(conn)).await??;
374 if !site.enable_downvotes {
375 return Err(ApiError::err("downvotes_disabled").into());
381 /// Returns a list of communities that the user moderates
382 /// or if a community_id is supplied validates the user is a moderator
383 /// of that community and returns the community id in a vec
385 /// * `person_id` - the person id of the moderator
386 /// * `community_id` - optional community id to check for moderator privileges
387 /// * `pool` - the diesel db pool
388 pub async fn collect_moderated_communities(
390 community_id: Option<CommunityId>,
392 ) -> Result<Vec<CommunityId>, LemmyError> {
393 if let Some(community_id) = community_id {
394 // if the user provides a community_id, just check for mod/admin privileges
395 is_mod_or_admin(pool, person_id, community_id).await?;
396 Ok(vec![community_id])
398 let ids = blocking(pool, move |conn: &'_ _| {
399 CommunityModerator::get_person_moderated_communities(conn, person_id)
406 pub async fn build_federated_instances(
408 ) -> Result<Option<FederatedInstances>, LemmyError> {
409 if Settings::get().federation.enabled {
410 let distinct_communities = blocking(pool, move |conn| {
411 Community::distinct_federated_communities(conn)
415 let allowed = Settings::get().federation.allowed_instances;
416 let blocked = Settings::get().federation.blocked_instances;
418 let mut linked = distinct_communities
420 .map(|actor_id| Ok(Url::parse(actor_id)?.host_str().unwrap_or("").to_string()))
421 .collect::<Result<Vec<String>, LemmyError>>()?;
423 if let Some(allowed) = allowed.as_ref() {
424 linked.extend_from_slice(allowed);
427 if let Some(blocked) = blocked.as_ref() {
428 linked.retain(|a| !blocked.contains(a) && !a.eq(&Settings::get().hostname));
431 // Sort and remove dupes
432 linked.sort_unstable();
435 Ok(Some(FederatedInstances {
445 /// Checks the password length
446 pub fn password_length_check(pass: &str) -> Result<(), LemmyError> {
448 Err(ApiError::err("invalid_password").into())
454 /// Checks the site description length
455 pub fn site_description_length_check(description: &str) -> Result<(), LemmyError> {
456 if description.len() > 150 {
457 Err(ApiError::err("site_description_length_overflow").into())