diff --git a/server/Cargo.lock b/server/Cargo.lock index 0c50f464d..5f3ac4bdb 100644 --- a/server/Cargo.lock +++ b/server/Cargo.lock @@ -139,12 +139,14 @@ dependencies = [ "actix-utils 2.0.0", "base64 0.12.3", "bitflags 1.2.1", + "brotli2", "bytes", "cookie", "copyless", "derive_more", "either", "encoding_rs", + "flate2", "futures-channel", "futures-core", "futures-util", @@ -698,6 +700,26 @@ dependencies = [ "opaque-debug 0.3.0", ] +[[package]] +name = "brotli-sys" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4445dea95f4c2b41cde57cc9fee236ae4dbae88d8fcbdb4750fc1bb5d86aaecd" +dependencies = [ + "cc", + "libc", +] + +[[package]] +name = "brotli2" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0cb036c3eade309815c15ddbacec5b22c4d1f3983a774ab2eac2e3e9ea85568e" +dependencies = [ + "brotli-sys", + "libc", +] + [[package]] name = "buf-min" version = "0.1.1" @@ -909,6 +931,15 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8aebca1129a03dc6dc2b127edd729435bbc4a37e1d5f4d7513165089ceb02634" +[[package]] +name = "crc32fast" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba125de2af0df55319f41944744ad91c71113bf74a4646efff39afe1f6842db1" +dependencies = [ + "cfg-if", +] + [[package]] name = "crossbeam-channel" version = "0.4.4" @@ -1258,6 +1289,18 @@ dependencies = [ "ascii_utils", ] +[[package]] +name = "flate2" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "766d0e77a2c1502169d4a93ff3b8c15a71fd946cd0126309752104e5f3c46d94" +dependencies = [ + "cfg-if", + "crc32fast", + "libc", + "miniz_oxide", +] + [[package]] name = "fnv" version = "1.0.7" @@ -1773,7 +1816,11 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" name = "lemmy_api_structs" version = "0.1.0" dependencies = [ + "actix-web", + "diesel", "lemmy_db", + "lemmy_utils", + "log", "serde 1.0.115", ] diff --git a/server/lemmy_api_structs/Cargo.toml b/server/lemmy_api_structs/Cargo.toml index 3778422be..64627edff 100644 --- a/server/lemmy_api_structs/Cargo.toml +++ b/server/lemmy_api_structs/Cargo.toml @@ -10,4 +10,8 @@ path = "src/lib.rs" [dependencies] lemmy_db = { path = "../lemmy_db" } +lemmy_utils = { path = "../lemmy_utils" } serde = { version = "1.0.105", features = ["derive"] } +log = "0.4.0" +diesel = "1.4.4" +actix-web = { version = "3.0.0-alpha.3", features = ["rustls"] } diff --git a/server/lemmy_api_structs/src/lib.rs b/server/lemmy_api_structs/src/lib.rs index b3576a0d6..e94a286f7 100644 --- a/server/lemmy_api_structs/src/lib.rs +++ b/server/lemmy_api_structs/src/lib.rs @@ -1,7 +1,159 @@ extern crate serde; +extern crate log; +extern crate diesel; +extern crate actix_web; pub mod comment; pub mod community; pub mod post; pub mod site; pub mod user; + +use lemmy_db::comment::Comment; +use lemmy_db::user::User_; +use lemmy_db::post::Post; +use lemmy_db::user_mention::{UserMentionForm, UserMention}; +use log::error; +use lemmy_db::{Crud, DbPool}; +use lemmy_utils::utils::MentionData; +use lemmy_utils::settings::Settings; +use lemmy_utils::email::send_email; +use diesel::PgConnection; +use lemmy_utils::LemmyError; + +pub async fn blocking(pool: &DbPool, f: F) -> Result + 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, + comment: Comment, + user: &User_, + post: Post, + pool: &DbPool, + do_send_email: bool, +) -> Result, LemmyError> { + let user2 = user.clone(); + let ids = blocking(pool, move |conn| { + do_send_local_notifs(conn, &mentions, &comment, &user2, &post, do_send_email) + }) + .await?; + + Ok(ids) +} + +fn do_send_local_notifs( + conn: &PgConnection, + mentions: &[MentionData], + comment: &Comment, + user: &User_, + post: &Post, + do_send_email: bool, +) -> Vec { + let mut recipient_ids = Vec::new(); + let hostname = &format!("https://{}", Settings::get().hostname); + + // Send the local mentions + for mention in mentions + .iter() + .filter(|m| m.is_local() && m.name.ne(&user.name)) + .collect::>() + { + if let Ok(mention_user) = User_::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 + recipient_ids.push(mention_user.id); + + let user_mention_form = UserMentionForm { + recipient_id: mention_user.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 + match UserMention::create(&conn, &user_mention_form) { + Ok(_mention) => (), + Err(_e) => error!("{}", &_e), + }; + + // Send an email to those users that have notifications on + if do_send_email && mention_user.send_notifications_to_email { + if let Some(mention_email) = mention_user.email { + let subject = &format!("{} - Mentioned by {}", Settings::get().hostname, user.name,); + let html = &format!( + "

User Mention


{} - {}

inbox", + user.name, comment.content, hostname + ); + match send_email(subject, &mention_email, &mention_user.name, html) { + Ok(_o) => _o, + Err(e) => error!("{}", e), + }; + } + } + } + } + + // Send notifs to the parent commenter / poster + match comment.parent_id { + Some(parent_id) => { + if let Ok(parent_comment) = Comment::read(&conn, parent_id) { + if parent_comment.creator_id != user.id { + if let Ok(parent_user) = User_::read(&conn, parent_comment.creator_id) { + recipient_ids.push(parent_user.id); + + if do_send_email && parent_user.send_notifications_to_email { + if let Some(comment_reply_email) = parent_user.email { + let subject = &format!("{} - Reply from {}", Settings::get().hostname, user.name,); + let html = &format!( + "

Comment Reply


{} - {}

inbox", + user.name, comment.content, hostname + ); + match send_email(subject, &comment_reply_email, &parent_user.name, html) { + Ok(_o) => _o, + Err(e) => error!("{}", e), + }; + } + } + } + } + } + } + // Its a post + None => { + if post.creator_id != user.id { + if let Ok(parent_user) = User_::read(&conn, post.creator_id) { + recipient_ids.push(parent_user.id); + + if do_send_email && parent_user.send_notifications_to_email { + if let Some(post_reply_email) = parent_user.email { + let subject = &format!("{} - Reply from {}", Settings::get().hostname, user.name,); + let html = &format!( + "

Post Reply


{} - {}

inbox", + user.name, comment.content, hostname + ); + match send_email(subject, &post_reply_email, &parent_user.name, html) { + Ok(_o) => _o, + Err(e) => error!("{}", e), + }; + } + } + } + } + } + }; + recipient_ids +} diff --git a/server/lemmy_db/src/lib.rs b/server/lemmy_db/src/lib.rs index ed6e1dfb9..fc660208d 100644 --- a/server/lemmy_db/src/lib.rs +++ b/server/lemmy_db/src/lib.rs @@ -40,6 +40,8 @@ pub mod user_mention; pub mod user_mention_view; pub mod user_view; +pub type DbPool = diesel::r2d2::Pool>; + pub trait Crud { fn create(conn: &PgConnection, form: &T) -> Result where diff --git a/server/lemmy_db/src/schema.rs b/server/lemmy_db/src/schema.rs index c446edd9f..a189dbced 100644 --- a/server/lemmy_db/src/schema.rs +++ b/server/lemmy_db/src/schema.rs @@ -523,36 +523,36 @@ joinable!(user_mention -> comment (comment_id)); joinable!(user_mention -> user_ (recipient_id)); allow_tables_to_appear_in_same_query!( - activity, - category, - comment, - comment_aggregates_fast, - comment_like, - comment_saved, - community, - community_aggregates_fast, - community_follower, - community_moderator, - community_user_ban, - mod_add, - mod_add_community, - mod_ban, - mod_ban_from_community, - mod_lock_post, - mod_remove_comment, - mod_remove_community, - mod_remove_post, - mod_sticky_post, - password_reset_request, - post, - post_aggregates_fast, - post_like, - post_read, - post_saved, - private_message, - site, - user_, - user_ban, - user_fast, - user_mention, + activity, + category, + comment, + comment_aggregates_fast, + comment_like, + comment_saved, + community, + community_aggregates_fast, + community_follower, + community_moderator, + community_user_ban, + mod_add, + mod_add_community, + mod_ban, + mod_ban_from_community, + mod_lock_post, + mod_remove_comment, + mod_remove_community, + mod_remove_post, + mod_sticky_post, + password_reset_request, + post, + post_aggregates_fast, + post_like, + post_read, + post_saved, + private_message, + site, + user_, + user_ban, + user_fast, + user_mention, ); diff --git a/server/src/api/comment.rs b/server/src/api/comment.rs index b4510d771..dd1970393 100644 --- a/server/src/api/comment.rs +++ b/server/src/api/comment.rs @@ -8,16 +8,14 @@ use crate::{ Perform, }, apub::{ApubLikeableType, ApubObjectType}, - blocking, websocket::{ messages::{JoinCommunityRoom, SendComment}, UserOperation, }, - DbPool, LemmyContext, }; use actix_web::web::Data; -use lemmy_api_structs::comment::*; +use lemmy_api_structs::{blocking, comment::*, send_local_notifs}; use lemmy_db::{ comment::*, comment_view::*, @@ -25,7 +23,6 @@ use lemmy_db::{ post::*, site_view::*, user::*, - user_mention::*, Crud, Likeable, ListingType, @@ -34,14 +31,11 @@ use lemmy_db::{ }; use lemmy_utils::{ apub::{make_apub_endpoint, EndpointType}, - email::send_email, - settings::Settings, - utils::{remove_slurs, scrape_text_for_mentions, MentionData}, + utils::{remove_slurs, scrape_text_for_mentions}, APIError, ConnectionId, LemmyError, }; -use log::error; use std::str::FromStr; #[async_trait::async_trait(?Send)] @@ -704,124 +698,3 @@ impl Perform for GetComments { Ok(GetCommentsResponse { comments }) } } - -pub async fn send_local_notifs( - mentions: Vec, - comment: Comment, - user: &User_, - post: Post, - pool: &DbPool, - do_send_email: bool, -) -> Result, LemmyError> { - let user2 = user.clone(); - let ids = blocking(pool, move |conn| { - do_send_local_notifs(conn, &mentions, &comment, &user2, &post, do_send_email) - }) - .await?; - - Ok(ids) -} - -fn do_send_local_notifs( - conn: &diesel::PgConnection, - mentions: &[MentionData], - comment: &Comment, - user: &User_, - post: &Post, - do_send_email: bool, -) -> Vec { - let mut recipient_ids = Vec::new(); - let hostname = &format!("https://{}", Settings::get().hostname); - - // Send the local mentions - for mention in mentions - .iter() - .filter(|m| m.is_local() && m.name.ne(&user.name)) - .collect::>() - { - if let Ok(mention_user) = User_::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 - recipient_ids.push(mention_user.id); - - let user_mention_form = UserMentionForm { - recipient_id: mention_user.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 - match UserMention::create(&conn, &user_mention_form) { - Ok(_mention) => (), - Err(_e) => error!("{}", &_e), - }; - - // Send an email to those users that have notifications on - if do_send_email && mention_user.send_notifications_to_email { - if let Some(mention_email) = mention_user.email { - let subject = &format!("{} - Mentioned by {}", Settings::get().hostname, user.name,); - let html = &format!( - "

User Mention


{} - {}

inbox", - user.name, comment.content, hostname - ); - match send_email(subject, &mention_email, &mention_user.name, html) { - Ok(_o) => _o, - Err(e) => error!("{}", e), - }; - } - } - } - } - - // Send notifs to the parent commenter / poster - match comment.parent_id { - Some(parent_id) => { - if let Ok(parent_comment) = Comment::read(&conn, parent_id) { - if parent_comment.creator_id != user.id { - if let Ok(parent_user) = User_::read(&conn, parent_comment.creator_id) { - recipient_ids.push(parent_user.id); - - if do_send_email && parent_user.send_notifications_to_email { - if let Some(comment_reply_email) = parent_user.email { - let subject = &format!("{} - Reply from {}", Settings::get().hostname, user.name,); - let html = &format!( - "

Comment Reply


{} - {}

inbox", - user.name, comment.content, hostname - ); - match send_email(subject, &comment_reply_email, &parent_user.name, html) { - Ok(_o) => _o, - Err(e) => error!("{}", e), - }; - } - } - } - } - } - } - // Its a post - None => { - if post.creator_id != user.id { - if let Ok(parent_user) = User_::read(&conn, post.creator_id) { - recipient_ids.push(parent_user.id); - - if do_send_email && parent_user.send_notifications_to_email { - if let Some(post_reply_email) = parent_user.email { - let subject = &format!("{} - Reply from {}", Settings::get().hostname, user.name,); - let html = &format!( - "

Post Reply


{} - {}

inbox", - user.name, comment.content, hostname - ); - match send_email(subject, &post_reply_email, &parent_user.name, html) { - Ok(_o) => _o, - Err(e) => error!("{}", e), - }; - } - } - } - } - } - }; - recipient_ids -} diff --git a/server/src/api/community.rs b/server/src/api/community.rs index adc5d74f3..8486861c4 100644 --- a/server/src/api/community.rs +++ b/server/src/api/community.rs @@ -1,7 +1,6 @@ use crate::{ api::{get_user_from_jwt, get_user_from_jwt_opt, is_admin, is_mod_or_admin, Perform}, apub::ActorType, - blocking, websocket::{ messages::{GetCommunityUsersOnline, JoinCommunityRoom, SendCommunityRoomMessage}, UserOperation, @@ -10,7 +9,7 @@ use crate::{ }; use actix_web::web::Data; use anyhow::Context; -use lemmy_api_structs::community::*; +use lemmy_api_structs::{blocking, community::*}; use lemmy_db::{ comment::Comment, comment_view::CommentQueryBuilder, diff --git a/server/src/api/mod.rs b/server/src/api/mod.rs index 0ace74321..1aff5d106 100644 --- a/server/src/api/mod.rs +++ b/server/src/api/mod.rs @@ -1,5 +1,6 @@ -use crate::{api::claims::Claims, blocking, DbPool, LemmyContext}; +use crate::{api::claims::Claims, DbPool, LemmyContext}; use actix_web::web::Data; +use lemmy_api_structs::blocking; use lemmy_db::{ community::Community, community_view::CommunityUserBanView, diff --git a/server/src/api/post.rs b/server/src/api/post.rs index c2d07102e..a1801d0f2 100644 --- a/server/src/api/post.rs +++ b/server/src/api/post.rs @@ -1,7 +1,6 @@ use crate::{ api::{check_community_ban, get_user_from_jwt, get_user_from_jwt_opt, is_mod_or_admin, Perform}, apub::{ApubLikeableType, ApubObjectType}, - blocking, fetch_iframely_and_pictrs_data, websocket::{ messages::{GetPostUsersOnline, JoinCommunityRoom, JoinPostRoom, SendPost}, @@ -10,7 +9,7 @@ use crate::{ LemmyContext, }; use actix_web::web::Data; -use lemmy_api_structs::post::*; +use lemmy_api_structs::{blocking, post::*}; use lemmy_db::{ comment_view::*, community_view::*, diff --git a/server/src/api/site.rs b/server/src/api/site.rs index d0996b3f7..727078e27 100644 --- a/server/src/api/site.rs +++ b/server/src/api/site.rs @@ -1,7 +1,6 @@ use crate::{ api::{get_user_from_jwt, get_user_from_jwt_opt, is_admin, Perform}, apub::fetcher::search_by_apub_id, - blocking, version, websocket::{ messages::{GetUsersOnline, SendAllMessage}, @@ -11,7 +10,7 @@ use crate::{ }; use actix_web::web::Data; use anyhow::Context; -use lemmy_api_structs::{site::*, user::Register}; +use lemmy_api_structs::{blocking, site::*, user::Register}; use lemmy_db::{ category::*, comment_view::*, diff --git a/server/src/api/user.rs b/server/src/api/user.rs index 391a74f92..778a225fa 100644 --- a/server/src/api/user.rs +++ b/server/src/api/user.rs @@ -1,7 +1,6 @@ use crate::{ api::{claims::Claims, get_user_from_jwt, get_user_from_jwt_opt, is_admin, Perform}, apub::ApubObjectType, - blocking, captcha_espeak_wav_base64, websocket::{ messages::{CaptchaItem, CheckCaptcha, JoinUserRoom, SendAllMessage, SendUserRoomMessage}, @@ -14,7 +13,7 @@ use anyhow::Context; use bcrypt::verify; use captcha::{gen, Difficulty}; use chrono::Duration; -use lemmy_api_structs::user::*; +use lemmy_api_structs::{blocking, user::*}; use lemmy_db::{ comment::*, comment_view::*, diff --git a/server/src/apub/comment.rs b/server/src/apub/comment.rs index 8d50f8bc8..78fe6f8ab 100644 --- a/server/src/apub/comment.rs +++ b/server/src/apub/comment.rs @@ -17,7 +17,6 @@ use crate::{ FromApub, ToApub, }, - blocking, DbPool, LemmyContext, }; @@ -41,6 +40,7 @@ use activitystreams::{ use actix_web::{body::Body, web, web::Path, HttpResponse}; use anyhow::Context; use itertools::Itertools; +use lemmy_api_structs::blocking; use lemmy_db::{ comment::{Comment, CommentForm}, community::Community, diff --git a/server/src/apub/community.rs b/server/src/apub/community.rs index 5aa3336a6..89819f453 100644 --- a/server/src/apub/community.rs +++ b/server/src/apub/community.rs @@ -14,7 +14,6 @@ use crate::{ GroupExt, ToApub, }, - blocking, DbPool, LemmyContext, }; @@ -39,6 +38,7 @@ use activitystreams_ext::Ext2; use actix_web::{body::Body, web, HttpResponse}; use anyhow::Context; use itertools::Itertools; +use lemmy_api_structs::blocking; use lemmy_db::{ community::{Community, CommunityForm}, community_view::{CommunityFollowerView, CommunityModeratorView}, diff --git a/server/src/apub/fetcher.rs b/server/src/apub/fetcher.rs index e849dbf9f..4891a9705 100644 --- a/server/src/apub/fetcher.rs +++ b/server/src/apub/fetcher.rs @@ -8,7 +8,6 @@ use crate::{ PersonExt, APUB_JSON_CONTENT_TYPE, }, - blocking, request::{retry, RecvError}, LemmyContext, }; @@ -16,7 +15,7 @@ use activitystreams::{base::BaseExt, collection::OrderedCollection, object::Note use anyhow::{anyhow, Context}; use chrono::NaiveDateTime; use diesel::result::Error::NotFound; -use lemmy_api_structs::site::SearchResponse; +use lemmy_api_structs::{blocking, site::SearchResponse}; use lemmy_db::{ comment::{Comment, CommentForm}, comment_view::CommentView, diff --git a/server/src/apub/inbox/activities/create.rs b/server/src/apub/inbox/activities/create.rs index 0bbafbce0..66286ce17 100644 --- a/server/src/apub/inbox/activities/create.rs +++ b/server/src/apub/inbox/activities/create.rs @@ -1,5 +1,4 @@ use crate::{ - api::comment::send_local_notifs, apub::{ inbox::shared_inbox::{ announce_if_community_is_local, @@ -10,7 +9,6 @@ use crate::{ FromApub, PageExt, }, - blocking, websocket::{ messages::{SendComment, SendPost}, UserOperation, @@ -20,7 +18,12 @@ use crate::{ use activitystreams::{activity::Create, base::AnyBase, object::Note, prelude::*}; use actix_web::HttpResponse; use anyhow::Context; -use lemmy_api_structs::{comment::CommentResponse, post::PostResponse}; +use lemmy_api_structs::{ + blocking, + comment::CommentResponse, + post::PostResponse, + send_local_notifs, +}; use lemmy_db::{ comment::{Comment, CommentForm}, comment_view::CommentView, diff --git a/server/src/apub/inbox/activities/delete.rs b/server/src/apub/inbox/activities/delete.rs index 70bf37616..4adc7c33c 100644 --- a/server/src/apub/inbox/activities/delete.rs +++ b/server/src/apub/inbox/activities/delete.rs @@ -11,7 +11,6 @@ use crate::{ GroupExt, PageExt, }, - blocking, websocket::{ messages::{SendComment, SendCommunityRoomMessage, SendPost}, UserOperation, @@ -22,6 +21,7 @@ use activitystreams::{activity::Delete, base::AnyBase, object::Note, prelude::*} use actix_web::HttpResponse; use anyhow::Context; use lemmy_api_structs::{ + blocking, comment::CommentResponse, community::CommunityResponse, post::PostResponse, diff --git a/server/src/apub/inbox/activities/dislike.rs b/server/src/apub/inbox/activities/dislike.rs index 599389b18..9b63e82d8 100644 --- a/server/src/apub/inbox/activities/dislike.rs +++ b/server/src/apub/inbox/activities/dislike.rs @@ -9,7 +9,6 @@ use crate::{ FromApub, PageExt, }, - blocking, websocket::{ messages::{SendComment, SendPost}, UserOperation, @@ -19,7 +18,7 @@ use crate::{ use activitystreams::{activity::Dislike, base::AnyBase, object::Note, prelude::*}; use actix_web::HttpResponse; use anyhow::Context; -use lemmy_api_structs::{comment::CommentResponse, post::PostResponse}; +use lemmy_api_structs::{blocking, comment::CommentResponse, post::PostResponse}; use lemmy_db::{ comment::{CommentForm, CommentLike, CommentLikeForm}, comment_view::CommentView, diff --git a/server/src/apub/inbox/activities/like.rs b/server/src/apub/inbox/activities/like.rs index 2cb95521e..e6f865b93 100644 --- a/server/src/apub/inbox/activities/like.rs +++ b/server/src/apub/inbox/activities/like.rs @@ -9,7 +9,6 @@ use crate::{ FromApub, PageExt, }, - blocking, websocket::{ messages::{SendComment, SendPost}, UserOperation, @@ -19,7 +18,7 @@ use crate::{ use activitystreams::{activity::Like, base::AnyBase, object::Note, prelude::*}; use actix_web::HttpResponse; use anyhow::Context; -use lemmy_api_structs::{comment::CommentResponse, post::PostResponse}; +use lemmy_api_structs::{blocking, comment::CommentResponse, post::PostResponse}; use lemmy_db::{ comment::{CommentForm, CommentLike, CommentLikeForm}, comment_view::CommentView, diff --git a/server/src/apub/inbox/activities/remove.rs b/server/src/apub/inbox/activities/remove.rs index 846842d4e..011f57b95 100644 --- a/server/src/apub/inbox/activities/remove.rs +++ b/server/src/apub/inbox/activities/remove.rs @@ -12,7 +12,6 @@ use crate::{ GroupExt, PageExt, }, - blocking, websocket::{ messages::{SendComment, SendCommunityRoomMessage, SendPost}, UserOperation, @@ -23,6 +22,7 @@ use activitystreams::{activity::Remove, base::AnyBase, object::Note, prelude::*} use actix_web::HttpResponse; use anyhow::{anyhow, Context}; use lemmy_api_structs::{ + blocking, comment::CommentResponse, community::CommunityResponse, post::PostResponse, diff --git a/server/src/apub/inbox/activities/undo.rs b/server/src/apub/inbox/activities/undo.rs index 0b695d32e..bf2073860 100644 --- a/server/src/apub/inbox/activities/undo.rs +++ b/server/src/apub/inbox/activities/undo.rs @@ -11,7 +11,6 @@ use crate::{ GroupExt, PageExt, }, - blocking, websocket::{ messages::{SendComment, SendCommunityRoomMessage, SendPost}, UserOperation, @@ -27,6 +26,7 @@ use activitystreams::{ use actix_web::HttpResponse; use anyhow::{anyhow, Context}; use lemmy_api_structs::{ + blocking, comment::CommentResponse, community::CommunityResponse, post::PostResponse, diff --git a/server/src/apub/inbox/activities/update.rs b/server/src/apub/inbox/activities/update.rs index 124ae8981..f078e67aa 100644 --- a/server/src/apub/inbox/activities/update.rs +++ b/server/src/apub/inbox/activities/update.rs @@ -1,5 +1,4 @@ use crate::{ - api::comment::send_local_notifs, apub::{ fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post}, inbox::shared_inbox::{ @@ -11,7 +10,6 @@ use crate::{ FromApub, PageExt, }, - blocking, websocket::{ messages::{SendComment, SendPost}, UserOperation, @@ -21,7 +19,12 @@ use crate::{ use activitystreams::{activity::Update, base::AnyBase, object::Note, prelude::*}; use actix_web::HttpResponse; use anyhow::Context; -use lemmy_api_structs::{comment::CommentResponse, post::PostResponse}; +use lemmy_api_structs::{ + blocking, + comment::CommentResponse, + post::PostResponse, + send_local_notifs, +}; use lemmy_db::{ comment::{Comment, CommentForm}, comment_view::CommentView, diff --git a/server/src/apub/inbox/community_inbox.rs b/server/src/apub/inbox/community_inbox.rs index 0631f9391..b27e6b6a9 100644 --- a/server/src/apub/inbox/community_inbox.rs +++ b/server/src/apub/inbox/community_inbox.rs @@ -6,7 +6,6 @@ use crate::{ insert_activity, ActorType, }, - blocking, LemmyContext, }; use activitystreams::{ @@ -16,6 +15,7 @@ use activitystreams::{ }; use actix_web::{web, HttpRequest, HttpResponse}; use anyhow::{anyhow, Context}; +use lemmy_api_structs::blocking; use lemmy_db::{ community::{Community, CommunityFollower, CommunityFollowerForm}, user::User_, diff --git a/server/src/apub/inbox/user_inbox.rs b/server/src/apub/inbox/user_inbox.rs index 7ea95833e..ccc4d105a 100644 --- a/server/src/apub/inbox/user_inbox.rs +++ b/server/src/apub/inbox/user_inbox.rs @@ -6,7 +6,6 @@ use crate::{ insert_activity, FromApub, }, - blocking, websocket::{messages::SendUserRoomMessage, UserOperation}, LemmyContext, }; @@ -18,7 +17,7 @@ use activitystreams::{ }; use actix_web::{web, HttpRequest, HttpResponse}; use anyhow::Context; -use lemmy_api_structs::user::PrivateMessageResponse; +use lemmy_api_structs::{blocking, user::PrivateMessageResponse}; use lemmy_db::{ community::{CommunityFollower, CommunityFollowerForm}, naive_now, diff --git a/server/src/apub/mod.rs b/server/src/apub/mod.rs index beeda49e2..e9184c33a 100644 --- a/server/src/apub/mod.rs +++ b/server/src/apub/mod.rs @@ -15,7 +15,6 @@ use crate::{ page_extension::PageExtension, signatures::{PublicKey, PublicKeyExtension}, }, - blocking, request::{retry, RecvError}, routes::webfinger::WebFingerResponse, DbPool, @@ -33,6 +32,7 @@ use activitystreams_ext::{Ext1, Ext2}; use actix_web::{body::Body, HttpResponse}; use anyhow::{anyhow, Context}; use chrono::NaiveDateTime; +use lemmy_api_structs::blocking; use lemmy_db::{activity::do_insert_activity, user::User_}; use lemmy_utils::{ apub::get_apub_protocol_string, diff --git a/server/src/apub/post.rs b/server/src/apub/post.rs index c57292c96..86ac3e600 100644 --- a/server/src/apub/post.rs +++ b/server/src/apub/post.rs @@ -14,7 +14,6 @@ use crate::{ PageExt, ToApub, }, - blocking, DbPool, LemmyContext, }; @@ -36,6 +35,7 @@ use activitystreams::{ use activitystreams_ext::Ext1; use actix_web::{body::Body, web, HttpResponse}; use anyhow::Context; +use lemmy_api_structs::blocking; use lemmy_db::{ community::Community, post::{Post, PostForm}, diff --git a/server/src/apub/private_message.rs b/server/src/apub/private_message.rs index 028a96eec..6ae30a3f4 100644 --- a/server/src/apub/private_message.rs +++ b/server/src/apub/private_message.rs @@ -12,7 +12,6 @@ use crate::{ FromApub, ToApub, }, - blocking, DbPool, LemmyContext, }; @@ -28,6 +27,7 @@ use activitystreams::{ prelude::*, }; use anyhow::Context; +use lemmy_api_structs::blocking; use lemmy_db::{ private_message::{PrivateMessage, PrivateMessageForm}, user::User_, diff --git a/server/src/apub/user.rs b/server/src/apub/user.rs index c1fc8c64b..54daadb96 100644 --- a/server/src/apub/user.rs +++ b/server/src/apub/user.rs @@ -11,7 +11,6 @@ use crate::{ PersonExt, ToApub, }, - blocking, DbPool, LemmyContext, }; @@ -28,6 +27,7 @@ use activitystreams::{ use activitystreams_ext::Ext1; use actix_web::{body::Body, web, HttpResponse}; use anyhow::Context; +use lemmy_api_structs::blocking; use lemmy_db::{ naive_now, user::{UserForm, User_}, diff --git a/server/src/lib.rs b/server/src/lib.rs index 31eaa990f..11b8df7cb 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -36,6 +36,7 @@ use crate::{ use actix::Addr; use anyhow::anyhow; use background_jobs::QueueHandle; +use lemmy_db::DbPool; use lemmy_utils::{apub::get_apub_protocol_string, settings::Settings, LemmyError}; use log::error; use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC}; @@ -43,8 +44,6 @@ use reqwest::Client; use serde::Deserialize; use std::process::Command; -pub type DbPool = diesel::r2d2::Pool>; - pub struct LemmyContext { pub pool: DbPool, pub chat_server: Addr, @@ -224,22 +223,6 @@ pub async fn is_image_content_type(client: &Client, test: &str) -> Result<(), Le } } -pub async fn blocking(pool: &DbPool, f: F) -> Result -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 fn captcha_espeak_wav_base64(captcha: &str) -> Result { let mut built_text = String::new(); diff --git a/server/src/main.rs b/server/src/main.rs index 688e1aa1f..8445a68ba 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -16,11 +16,11 @@ use diesel::{ PgConnection, }; use lazy_static::lazy_static; +use lemmy_api_structs::blocking; use lemmy_db::get_database_url_from_env; use lemmy_rate_limit::{rate_limiter::RateLimiter, RateLimit}; use lemmy_server::{ apub::activity_queue::create_activity_queue, - blocking, code_migrations::run_advanced_migrations, routes::*, websocket::chat_server::ChatServer, diff --git a/server/src/routes/feeds.rs b/server/src/routes/feeds.rs index 3b725ed5b..1d8641490 100644 --- a/server/src/routes/feeds.rs +++ b/server/src/routes/feeds.rs @@ -1,8 +1,9 @@ -use crate::{api::claims::Claims, blocking, LemmyContext}; +use crate::{api::claims::Claims, LemmyContext}; use actix_web::{error::ErrorBadRequest, *}; use anyhow::anyhow; use chrono::{DateTime, NaiveDateTime, Utc}; use diesel::PgConnection; +use lemmy_api_structs::blocking; use lemmy_db::{ comment_view::{ReplyQueryBuilder, ReplyView}, community::Community, diff --git a/server/src/routes/nodeinfo.rs b/server/src/routes/nodeinfo.rs index 44afee6f2..2273fae54 100644 --- a/server/src/routes/nodeinfo.rs +++ b/server/src/routes/nodeinfo.rs @@ -1,6 +1,7 @@ -use crate::{blocking, version, LemmyContext}; +use crate::{version, LemmyContext}; use actix_web::{body::Body, error::ErrorBadRequest, *}; use anyhow::anyhow; +use lemmy_api_structs::blocking; use lemmy_db::site_view::SiteView; use lemmy_utils::{apub::get_apub_protocol_string, settings::Settings, LemmyError}; use serde::{Deserialize, Serialize}; diff --git a/server/src/routes/webfinger.rs b/server/src/routes/webfinger.rs index 22861434c..458fa0f37 100644 --- a/server/src/routes/webfinger.rs +++ b/server/src/routes/webfinger.rs @@ -1,6 +1,7 @@ -use crate::{blocking, LemmyContext}; +use crate::LemmyContext; use actix_web::{error::ErrorBadRequest, web::Query, *}; use anyhow::anyhow; +use lemmy_api_structs::blocking; use lemmy_db::{community::Community, user::User_}; use lemmy_utils::{ settings::Settings,