Move send_local_notifs into lemmy_api_structs (ref #1115)

This commit is contained in:
Felix Ableitner 2020-09-04 16:36:45 +02:00
parent 12109974c5
commit d9e62f0886
32 changed files with 279 additions and 216 deletions

47
server/Cargo.lock generated vendored
View file

@ -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",
]

View file

@ -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"] }

View file

@ -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<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,
user: &User_,
post: Post,
pool: &DbPool,
do_send_email: bool,
) -> Result<Vec<i32>, 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<i32> {
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::<Vec<&MentionData>>()
{
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!(
"<h1>User Mention</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>",
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!(
"<h1>Comment Reply</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>",
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!(
"<h1>Post Reply</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>",
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
}

View file

@ -40,6 +40,8 @@ pub mod user_mention;
pub mod user_mention_view;
pub mod user_view;
pub type DbPool = diesel::r2d2::Pool<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;
pub trait Crud<T> {
fn create(conn: &PgConnection, form: &T) -> Result<Self, Error>
where

View file

@ -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<MentionData>,
comment: Comment,
user: &User_,
post: Post,
pool: &DbPool,
do_send_email: bool,
) -> Result<Vec<i32>, 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<i32> {
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::<Vec<&MentionData>>()
{
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!(
"<h1>User Mention</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>",
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!(
"<h1>Comment Reply</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>",
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!(
"<h1>Post Reply</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>",
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
}

View file

@ -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,

View file

@ -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,

View file

@ -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::*,

View file

@ -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::*,

View file

@ -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::*,

View file

@ -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,

View file

@ -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},

View file

@ -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,

View file

@ -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,

View file

@ -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,

View file

@ -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,

View file

@ -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,

View file

@ -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,

View file

@ -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,

View file

@ -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,

View file

@ -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_,

View file

@ -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,

View file

@ -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,

View file

@ -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},

View file

@ -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_,

View file

@ -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_},

View file

@ -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<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;
pub struct LemmyContext {
pub pool: DbPool,
pub chat_server: Addr<ChatServer>,
@ -224,22 +223,6 @@ pub async fn is_image_content_type(client: &Client, test: &str) -> Result<(), Le
}
}
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 fn captcha_espeak_wav_base64(captcha: &str) -> Result<String, LemmyError> {
let mut built_text = String::new();

View file

@ -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,

View file

@ -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,

View file

@ -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};

View file

@ -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,