Attempt to create custom GetConn and RunQueryDsl traits

pull/3420/head
dull b 2023-07-05 01:37:55 +00:00
parent 48e12de921
commit 7aeeec6810
79 changed files with 1480 additions and 1418 deletions

View File

@ -95,10 +95,10 @@ mod tests {
#[serial]
async fn test_should_not_validate_user_token_after_password_change() {
let mut conn = build_db_conn_for_tests().await;
let secret = Secret::init(&mut *conn).await.unwrap();
let secret = Secret::init(conn).await.unwrap();
let settings = &SETTINGS.to_owned();
let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string())
let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string())
.await
.unwrap();
@ -108,14 +108,14 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_person = Person::create(&mut *conn, &new_person).await.unwrap();
let inserted_person = Person::create(conn, &new_person).await.unwrap();
let local_user_form = LocalUserInsertForm::builder()
.person_id(inserted_person.id)
.password_encrypted("123456".to_string())
.build();
let inserted_local_user = LocalUser::create(&mut *conn, &local_user_form)
let inserted_local_user = LocalUser::create(conn, &local_user_form)
.await
.unwrap();
@ -131,13 +131,13 @@ mod tests {
// The check should fail, since the validator time is now newer than the jwt issue time
let updated_local_user =
LocalUser::update_password(&mut *conn, inserted_local_user.id, "password111")
LocalUser::update_password(conn, inserted_local_user.id, "password111")
.await
.unwrap();
let check_after = check_validator_time(&updated_local_user.validator_time, &claims);
assert!(check_after.is_err());
let num_deleted = Person::delete(&mut *conn, inserted_person.id)
let num_deleted = Person::delete(conn, inserted_person.id)
.await
.unwrap();
assert_eq!(1, num_deleted);

View File

@ -24,7 +24,7 @@ use lemmy_db_schema::{
registration_application::RegistrationApplication,
},
traits::{Crud, Readable},
utils::{get_conn, DbConn, DbPool},
utils::{get_conn, DbPool, GetConn},
RegistrationMode,
};
use lemmy_db_views::{comment_view::CommentQuery, structs::LocalUserView};
@ -50,7 +50,7 @@ use url::{ParseError, Url};
#[tracing::instrument(skip_all)]
pub async fn is_mod_or_admin(
conn: impl DbConn,
conn: impl GetConn,
person_id: PersonId,
community_id: CommunityId,
) -> Result<(), LemmyError> {
@ -63,7 +63,7 @@ pub async fn is_mod_or_admin(
#[tracing::instrument(skip_all)]
pub async fn is_mod_or_admin_opt(
conn: impl DbConn,
conn: impl GetConn,
local_user_view: Option<&LocalUserView>,
community_id: Option<CommunityId>,
) -> Result<(), LemmyError> {
@ -101,7 +101,7 @@ pub fn is_top_mod(
}
#[tracing::instrument(skip_all)]
pub async fn get_post(post_id: PostId, conn: impl DbConn) -> Result<Post, LemmyError> {
pub async fn get_post(post_id: PostId, conn: impl GetConn) -> Result<Post, LemmyError> {
Post::read(conn, post_id)
.await
.map_err(|e| LemmyError::from_error_message(e, "couldnt_find_post"))
@ -111,7 +111,7 @@ pub async fn get_post(post_id: PostId, conn: impl DbConn) -> Result<Post, LemmyE
pub async fn mark_post_as_read(
person_id: PersonId,
post_id: PostId,
conn: impl DbConn,
conn: impl GetConn,
) -> Result<PostRead, LemmyError> {
let post_read_form = PostReadForm { post_id, person_id };
@ -124,7 +124,7 @@ pub async fn mark_post_as_read(
pub async fn mark_post_as_unread(
person_id: PersonId,
post_id: PostId,
conn: impl DbConn,
conn: impl GetConn,
) -> Result<usize, LemmyError> {
let post_read_form = PostReadForm { post_id, person_id };
@ -197,7 +197,7 @@ pub fn check_user_valid(
pub async fn check_community_ban(
person_id: PersonId,
community_id: CommunityId,
conn: impl DbConn,
conn: impl GetConn,
) -> Result<(), LemmyError> {
let is_banned = CommunityPersonBanView::get(conn, person_id, community_id)
.await
@ -212,7 +212,7 @@ pub async fn check_community_ban(
#[tracing::instrument(skip_all)]
pub async fn check_community_deleted_or_removed(
community_id: CommunityId,
conn: impl DbConn,
conn: impl GetConn,
) -> Result<(), LemmyError> {
let community = Community::read(conn, community_id)
.await
@ -236,7 +236,7 @@ pub fn check_post_deleted_or_removed(post: &Post) -> Result<(), LemmyError> {
pub async fn check_person_block(
my_id: PersonId,
potential_blocker_id: PersonId,
conn: impl DbConn,
conn: impl GetConn,
) -> Result<(), LemmyError> {
let is_blocked = PersonBlock::read(conn, potential_blocker_id, my_id)
.await
@ -279,9 +279,9 @@ pub async fn build_federated_instances(
if local_site.federation_enabled {
// TODO I hate that this requires 3 queries
let (linked, allowed, blocked) = try_join!(
Instance::linked(&mut *conn_0),
Instance::allowlist(&mut *conn_1),
Instance::blocklist(&mut *conn_2)
Instance::linked(conn_0),
Instance::allowlist(conn_1),
Instance::blocklist(conn_2)
)?;
Ok(Some(FederatedInstances {
@ -338,7 +338,7 @@ pub fn send_email_to_user(
pub async fn send_password_reset_email(
user: &LocalUserView,
conn: impl DbConn,
conn: impl GetConn,
settings: &Settings,
) -> Result<(), LemmyError> {
// Generate a random token
@ -362,7 +362,7 @@ pub async fn send_password_reset_email(
pub async fn send_verification_email(
user: &LocalUserView,
new_email: &str,
conn: impl DbConn,
conn: impl GetConn,
settings: &Settings,
) -> Result<(), LemmyError> {
let form = EmailVerificationForm {
@ -453,7 +453,7 @@ pub fn send_application_approved_email(
/// Send a new applicant email notification to all admins
pub async fn send_new_applicant_email_to_admins(
applicant_username: &str,
conn: impl DbConn,
conn: impl GetConn,
settings: &Settings,
) -> Result<(), LemmyError> {
// Collect the admins with emails
@ -478,7 +478,7 @@ pub async fn send_new_applicant_email_to_admins(
pub async fn send_new_report_email_to_admins(
reporter_username: &str,
reported_username: &str,
conn: impl DbConn,
conn: impl GetConn,
settings: &Settings,
) -> Result<(), LemmyError> {
// Collect the admins with emails
@ -499,7 +499,7 @@ pub async fn send_new_report_email_to_admins(
pub async fn check_registration_application(
local_user_view: &LocalUserView,
local_site: &LocalSite,
conn: impl DbConn,
conn: impl GetConn,
) -> Result<(), LemmyError> {
if (local_site.registration_mode == RegistrationMode::RequireApplication
|| local_site.registration_mode == RegistrationMode::Closed)

View File

@ -21,7 +21,7 @@ use lemmy_api_common::{
use lemmy_db_schema::{
source::{community::Community, person::Person, site::Site},
traits::Crud,
utils::DbConn,
utils::GetConn,
};
use lemmy_db_views::structs::SiteView;
use lemmy_utils::{error::LemmyError, utils::time::naive_from_unix};
@ -118,7 +118,7 @@ impl SiteOrCommunity {
}
}
async fn generate_cc(target: &SiteOrCommunity, conn: impl DbConn) -> Result<Vec<Url>, LemmyError> {
async fn generate_cc(target: &SiteOrCommunity, conn: impl GetConn) -> Result<Vec<Url>, LemmyError> {
Ok(match target {
SiteOrCommunity::Site(_) => Site::read_remote_sites(conn)
.await?

View File

@ -7,7 +7,7 @@ use lemmy_api_common::{
site::{ResolveObject, ResolveObjectResponse},
utils::{check_private_instance, local_user_view_from_jwt},
};
use lemmy_db_schema::{newtypes::PersonId, source::local_site::LocalSite, utils::DbConn};
use lemmy_db_schema::{newtypes::PersonId, source::local_site::LocalSite, utils::GetConn};
use lemmy_db_views::structs::{CommentView, PostView};
use lemmy_db_views_actor::structs::{CommunityView, PersonView};
use lemmy_utils::error::LemmyError;
@ -33,7 +33,7 @@ pub async fn resolve_object(
async fn convert_response(
object: SearchableObjects,
user_id: PersonId,
conn: impl DbConn,
conn: impl GetConn,
) -> Result<Json<ResolveObjectResponse>, LemmyError> {
use SearchableObjects::*;
let removed_or_deleted;

View File

@ -25,7 +25,7 @@ use lemmy_db_schema::{
site::{Site, SiteInsertForm},
},
traits::Crud,
utils::{naive_now, DbConn},
utils::{naive_now, GetConn},
};
use lemmy_utils::{
error::LemmyError,
@ -195,9 +195,11 @@ pub(in crate::objects) async fn fetch_instance_actor_for_object<T: Into<Url> + C
}
}
pub(crate) async fn remote_instance_inboxes(mut conn: impl DbConn) -> Result<Vec<Url>, LemmyError> {
pub(crate) async fn remote_instance_inboxes(
mut conn: impl GetConn,
) -> Result<Vec<Url>, LemmyError> {
Ok(
Site::read_remote_sites(&mut *conn)
Site::read_remote_sites(conn)
.await?
.into_iter()
.map(|s| ApubSite::from(s).shared_inbox_or_inbox())

View File

@ -2,7 +2,7 @@ use lemmy_db_schema::{
impls::actor_language::UNDETERMINED_ID,
newtypes::LanguageId,
source::language::Language,
utils::DbConn,
utils::GetConn,
};
use lemmy_utils::error::LemmyError;
use serde::{Deserialize, Serialize};
@ -33,9 +33,9 @@ pub(crate) struct LanguageTag {
impl LanguageTag {
pub(crate) async fn new_single(
lang: LanguageId,
mut conn: impl DbConn,
mut conn: impl GetConn,
) -> Result<Option<LanguageTag>, LemmyError> {
let lang = Language::read_from_id(&mut *conn, lang).await?;
let lang = Language::read_from_id(conn, lang).await?;
// undetermined
if lang.id == UNDETERMINED_ID {
@ -50,12 +50,12 @@ impl LanguageTag {
pub(crate) async fn new_multiple(
lang_ids: Vec<LanguageId>,
mut conn: impl DbConn,
mut conn: impl GetConn,
) -> Result<Vec<LanguageTag>, LemmyError> {
let mut langs = Vec::<Language>::new();
for l in lang_ids {
langs.push(Language::read_from_id(&mut *conn, l).await?);
langs.push(Language::read_from_id(conn, l).await?);
}
let langs = langs
@ -70,23 +70,23 @@ impl LanguageTag {
pub(crate) async fn to_language_id_single(
lang: Option<Self>,
mut conn: impl DbConn,
mut conn: impl GetConn,
) -> Result<Option<LanguageId>, LemmyError> {
let identifier = lang.map(|l| l.identifier);
let language = Language::read_id_from_code(&mut *conn, identifier.as_deref()).await?;
let language = Language::read_id_from_code(conn, identifier.as_deref()).await?;
Ok(language)
}
pub(crate) async fn to_language_id_multiple(
langs: Vec<Self>,
mut conn: impl DbConn,
mut conn: impl GetConn,
) -> Result<Vec<LanguageId>, LemmyError> {
let mut language_ids = Vec::new();
for l in langs {
let id = l.identifier;
language_ids.push(Language::read_id_from_code(&mut *conn, Some(&id)).await?);
language_ids.push(Language::read_id_from_code(conn, Some(&id)).await?);
}
Ok(language_ids.into_iter().flatten().collect())

View File

@ -2,21 +2,21 @@ use crate::{
aggregates::structs::CommentAggregates,
newtypes::CommentId,
schema::comment_aggregates,
utils::{functions::hot_rank, DbConn},
utils::{functions::hot_rank, GetConn},
};
use diesel::{result::Error, ExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::utils::RunQueryDsl;
impl CommentAggregates {
pub async fn read(mut conn: impl DbConn, comment_id: CommentId) -> Result<Self, Error> {
pub async fn read(mut conn: impl GetConn, comment_id: CommentId) -> Result<Self, Error> {
comment_aggregates::table
.filter(comment_aggregates::comment_id.eq(comment_id))
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await
}
pub async fn update_hot_rank(
mut conn: impl DbConn,
mut conn: impl GetConn,
comment_id: CommentId,
) -> Result<Self, Error> {
diesel::update(comment_aggregates::table)
@ -25,7 +25,7 @@ impl CommentAggregates {
comment_aggregates::score,
comment_aggregates::published,
)))
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
}
@ -51,7 +51,7 @@ mod tests {
async fn test_crud() {
let mut conn = build_db_conn_for_tests().await;
let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string())
let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string())
.await
.unwrap();
@ -61,7 +61,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_person = Person::create(&mut *conn, &new_person).await.unwrap();
let inserted_person = Person::create(conn, &new_person).await.unwrap();
let another_person = PersonInsertForm::builder()
.name("jerry_comment_agg".into())
@ -69,7 +69,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let another_inserted_person = Person::create(&mut *conn, &another_person).await.unwrap();
let another_inserted_person = Person::create(conn, &another_person).await.unwrap();
let new_community = CommunityInsertForm::builder()
.name("TIL_comment_agg".into())
@ -78,7 +78,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_community = Community::create(&mut *conn, &new_community).await.unwrap();
let inserted_community = Community::create(conn, &new_community).await.unwrap();
let new_post = PostInsertForm::builder()
.name("A test post".into())
@ -86,7 +86,7 @@ mod tests {
.community_id(inserted_community.id)
.build();
let inserted_post = Post::create(&mut *conn, &new_post).await.unwrap();
let inserted_post = Post::create(conn, &new_post).await.unwrap();
let comment_form = CommentInsertForm::builder()
.content("A test comment".into())
@ -94,7 +94,7 @@ mod tests {
.post_id(inserted_post.id)
.build();
let inserted_comment = Comment::create(&mut *conn, &comment_form, None)
let inserted_comment = Comment::create(conn, &comment_form, None)
.await
.unwrap();
@ -105,7 +105,7 @@ mod tests {
.build();
let _inserted_child_comment = Comment::create(
&mut *conn,
conn,
&child_comment_form,
Some(&inserted_comment.path),
)
@ -119,9 +119,9 @@ mod tests {
score: 1,
};
CommentLike::like(&mut *conn, &comment_like).await.unwrap();
CommentLike::like(conn, &comment_like).await.unwrap();
let comment_aggs_before_delete = CommentAggregates::read(&mut *conn, inserted_comment.id)
let comment_aggs_before_delete = CommentAggregates::read(conn, inserted_comment.id)
.await
.unwrap();
@ -137,11 +137,11 @@ mod tests {
score: -1,
};
CommentLike::like(&mut *conn, &comment_dislike)
CommentLike::like(conn, &comment_dislike)
.await
.unwrap();
let comment_aggs_after_dislike = CommentAggregates::read(&mut *conn, inserted_comment.id)
let comment_aggs_after_dislike = CommentAggregates::read(conn, inserted_comment.id)
.await
.unwrap();
@ -150,10 +150,10 @@ mod tests {
assert_eq!(1, comment_aggs_after_dislike.downvotes);
// Remove the first comment like
CommentLike::remove(&mut *conn, inserted_person.id, inserted_comment.id)
CommentLike::remove(conn, inserted_person.id, inserted_comment.id)
.await
.unwrap();
let after_like_remove = CommentAggregates::read(&mut *conn, inserted_comment.id)
let after_like_remove = CommentAggregates::read(conn, inserted_comment.id)
.await
.unwrap();
assert_eq!(-1, after_like_remove.score);
@ -161,28 +161,28 @@ mod tests {
assert_eq!(1, after_like_remove.downvotes);
// Remove the parent post
Post::delete(&mut *conn, inserted_post.id).await.unwrap();
Post::delete(conn, inserted_post.id).await.unwrap();
// Should be none found, since the post was deleted
let after_delete = CommentAggregates::read(&mut *conn, inserted_comment.id).await;
let after_delete = CommentAggregates::read(conn, inserted_comment.id).await;
assert!(after_delete.is_err());
// This should delete all the associated rows, and fire triggers
Person::delete(&mut *conn, another_inserted_person.id)
Person::delete(conn, another_inserted_person.id)
.await
.unwrap();
let person_num_deleted = Person::delete(&mut *conn, inserted_person.id)
let person_num_deleted = Person::delete(conn, inserted_person.id)
.await
.unwrap();
assert_eq!(1, person_num_deleted);
// Delete the community
let community_num_deleted = Community::delete(&mut *conn, inserted_community.id)
let community_num_deleted = Community::delete(conn, inserted_community.id)
.await
.unwrap();
assert_eq!(1, community_num_deleted);
Instance::delete(&mut *conn, inserted_instance.id)
Instance::delete(conn, inserted_instance.id)
.await
.unwrap();
}

View File

@ -2,16 +2,16 @@ use crate::{
aggregates::structs::CommunityAggregates,
newtypes::CommunityId,
schema::community_aggregates,
utils::DbConn,
utils::GetConn,
};
use diesel::{result::Error, ExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::utils::RunQueryDsl;
impl CommunityAggregates {
pub async fn read(mut conn: impl DbConn, community_id: CommunityId) -> Result<Self, Error> {
pub async fn read(mut conn: impl GetConn, community_id: CommunityId) -> Result<Self, Error> {
community_aggregates::table
.filter(community_aggregates::community_id.eq(community_id))
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await
}
}
@ -37,7 +37,7 @@ mod tests {
async fn test_crud() {
let mut conn = build_db_conn_for_tests().await;
let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string())
let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string())
.await
.unwrap();
@ -47,7 +47,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_person = Person::create(&mut *conn, &new_person).await.unwrap();
let inserted_person = Person::create(conn, &new_person).await.unwrap();
let another_person = PersonInsertForm::builder()
.name("jerry_community_agg".into())
@ -55,7 +55,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let another_inserted_person = Person::create(&mut *conn, &another_person).await.unwrap();
let another_inserted_person = Person::create(conn, &another_person).await.unwrap();
let new_community = CommunityInsertForm::builder()
.name("TIL_community_agg".into())
@ -64,7 +64,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_community = Community::create(&mut *conn, &new_community).await.unwrap();
let inserted_community = Community::create(conn, &new_community).await.unwrap();
let another_community = CommunityInsertForm::builder()
.name("TIL_community_agg_2".into())
@ -73,7 +73,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let another_inserted_community = Community::create(&mut *conn, &another_community)
let another_inserted_community = Community::create(conn, &another_community)
.await
.unwrap();
@ -83,7 +83,7 @@ mod tests {
pending: false,
};
CommunityFollower::follow(&mut *conn, &first_person_follow)
CommunityFollower::follow(conn, &first_person_follow)
.await
.unwrap();
@ -93,7 +93,7 @@ mod tests {
pending: false,
};
CommunityFollower::follow(&mut *conn, &second_person_follow)
CommunityFollower::follow(conn, &second_person_follow)
.await
.unwrap();
@ -103,7 +103,7 @@ mod tests {
pending: false,
};
CommunityFollower::follow(&mut *conn, &another_community_follow)
CommunityFollower::follow(conn, &another_community_follow)
.await
.unwrap();
@ -113,7 +113,7 @@ mod tests {
.community_id(inserted_community.id)
.build();
let inserted_post = Post::create(&mut *conn, &new_post).await.unwrap();
let inserted_post = Post::create(conn, &new_post).await.unwrap();
let comment_form = CommentInsertForm::builder()
.content("A test comment".into())
@ -121,7 +121,7 @@ mod tests {
.post_id(inserted_post.id)
.build();
let inserted_comment = Comment::create(&mut *conn, &comment_form, None)
let inserted_comment = Comment::create(conn, &comment_form, None)
.await
.unwrap();
@ -132,7 +132,7 @@ mod tests {
.build();
let _inserted_child_comment = Comment::create(
&mut *conn,
conn,
&child_comment_form,
Some(&inserted_comment.path),
)
@ -140,7 +140,7 @@ mod tests {
.unwrap();
let community_aggregates_before_delete =
CommunityAggregates::read(&mut *conn, inserted_community.id)
CommunityAggregates::read(conn, inserted_community.id)
.await
.unwrap();
@ -150,7 +150,7 @@ mod tests {
// Test the other community
let another_community_aggs =
CommunityAggregates::read(&mut *conn, another_inserted_community.id)
CommunityAggregates::read(conn, another_inserted_community.id)
.await
.unwrap();
assert_eq!(1, another_community_aggs.subscribers);
@ -158,60 +158,60 @@ mod tests {
assert_eq!(0, another_community_aggs.comments);
// Unfollow test
CommunityFollower::unfollow(&mut *conn, &second_person_follow)
CommunityFollower::unfollow(conn, &second_person_follow)
.await
.unwrap();
let after_unfollow = CommunityAggregates::read(&mut *conn, inserted_community.id)
let after_unfollow = CommunityAggregates::read(conn, inserted_community.id)
.await
.unwrap();
assert_eq!(1, after_unfollow.subscribers);
// Follow again just for the later tests
CommunityFollower::follow(&mut *conn, &second_person_follow)
CommunityFollower::follow(conn, &second_person_follow)
.await
.unwrap();
let after_follow_again = CommunityAggregates::read(&mut *conn, inserted_community.id)
let after_follow_again = CommunityAggregates::read(conn, inserted_community.id)
.await
.unwrap();
assert_eq!(2, after_follow_again.subscribers);
// Remove a parent comment (the comment count should also be 0)
Post::delete(&mut *conn, inserted_post.id).await.unwrap();
let after_parent_post_delete = CommunityAggregates::read(&mut *conn, inserted_community.id)
Post::delete(conn, inserted_post.id).await.unwrap();
let after_parent_post_delete = CommunityAggregates::read(conn, inserted_community.id)
.await
.unwrap();
assert_eq!(0, after_parent_post_delete.comments);
assert_eq!(0, after_parent_post_delete.posts);
// Remove the 2nd person
Person::delete(&mut *conn, another_inserted_person.id)
Person::delete(conn, another_inserted_person.id)
.await
.unwrap();
let after_person_delete = CommunityAggregates::read(&mut *conn, inserted_community.id)
let after_person_delete = CommunityAggregates::read(conn, inserted_community.id)
.await
.unwrap();
assert_eq!(1, after_person_delete.subscribers);
// This should delete all the associated rows, and fire triggers
let person_num_deleted = Person::delete(&mut *conn, inserted_person.id)
let person_num_deleted = Person::delete(conn, inserted_person.id)
.await
.unwrap();
assert_eq!(1, person_num_deleted);
// Delete the community
let community_num_deleted = Community::delete(&mut *conn, inserted_community.id)
let community_num_deleted = Community::delete(conn, inserted_community.id)
.await
.unwrap();
assert_eq!(1, community_num_deleted);
let another_community_num_deleted =
Community::delete(&mut *conn, another_inserted_community.id)
Community::delete(conn, another_inserted_community.id)
.await
.unwrap();
assert_eq!(1, another_community_num_deleted);
// Should be none found, since the creator was deleted
let after_delete = CommunityAggregates::read(&mut *conn, inserted_community.id).await;
let after_delete = CommunityAggregates::read(conn, inserted_community.id).await;
assert!(after_delete.is_err());
}
}

View File

@ -2,16 +2,16 @@ use crate::{
aggregates::structs::PersonAggregates,
newtypes::PersonId,
schema::person_aggregates,
utils::DbConn,
utils::GetConn,
};
use diesel::{result::Error, ExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::utils::RunQueryDsl;
impl PersonAggregates {
pub async fn read(mut conn: impl DbConn, person_id: PersonId) -> Result<Self, Error> {
pub async fn read(mut conn: impl GetConn, person_id: PersonId) -> Result<Self, Error> {
person_aggregates::table
.filter(person_aggregates::person_id.eq(person_id))
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await
}
}
@ -37,7 +37,7 @@ mod tests {
async fn test_crud() {
let mut conn = build_db_conn_for_tests().await;
let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string())
let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string())
.await
.unwrap();
@ -47,7 +47,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_person = Person::create(&mut *conn, &new_person).await.unwrap();
let inserted_person = Person::create(conn, &new_person).await.unwrap();
let another_person = PersonInsertForm::builder()
.name("jerry_user_agg".into())
@ -55,7 +55,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let another_inserted_person = Person::create(&mut *conn, &another_person).await.unwrap();
let another_inserted_person = Person::create(conn, &another_person).await.unwrap();
let new_community = CommunityInsertForm::builder()
.name("TIL_site_agg".into())
@ -64,7 +64,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_community = Community::create(&mut *conn, &new_community).await.unwrap();
let inserted_community = Community::create(conn, &new_community).await.unwrap();
let new_post = PostInsertForm::builder()
.name("A test post".into())
@ -72,7 +72,7 @@ mod tests {
.community_id(inserted_community.id)
.build();
let inserted_post = Post::create(&mut *conn, &new_post).await.unwrap();
let inserted_post = Post::create(conn, &new_post).await.unwrap();
let post_like = PostLikeForm {
post_id: inserted_post.id,
@ -80,7 +80,7 @@ mod tests {
score: 1,
};
let _inserted_post_like = PostLike::like(&mut *conn, &post_like).await.unwrap();
let _inserted_post_like = PostLike::like(conn, &post_like).await.unwrap();
let comment_form = CommentInsertForm::builder()
.content("A test comment".into())
@ -88,7 +88,7 @@ mod tests {
.post_id(inserted_post.id)
.build();
let inserted_comment = Comment::create(&mut *conn, &comment_form, None)
let inserted_comment = Comment::create(conn, &comment_form, None)
.await
.unwrap();
@ -99,7 +99,7 @@ mod tests {
score: 1,
};
let _inserted_comment_like = CommentLike::like(&mut *conn, &comment_like).await.unwrap();
let _inserted_comment_like = CommentLike::like(conn, &comment_like).await.unwrap();
let child_comment_form = CommentInsertForm::builder()
.content("A test comment".into())
@ -108,7 +108,7 @@ mod tests {
.build();
let inserted_child_comment = Comment::create(
&mut *conn,
conn,
&child_comment_form,
Some(&inserted_comment.path),
)
@ -122,11 +122,11 @@ mod tests {
score: 1,
};
let _inserted_child_comment_like = CommentLike::like(&mut *conn, &child_comment_like)
let _inserted_child_comment_like = CommentLike::like(conn, &child_comment_like)
.await
.unwrap();
let person_aggregates_before_delete = PersonAggregates::read(&mut *conn, inserted_person.id)
let person_aggregates_before_delete = PersonAggregates::read(conn, inserted_person.id)
.await
.unwrap();
@ -136,69 +136,69 @@ mod tests {
assert_eq!(2, person_aggregates_before_delete.comment_score);
// Remove a post like
PostLike::remove(&mut *conn, inserted_person.id, inserted_post.id)
PostLike::remove(conn, inserted_person.id, inserted_post.id)
.await
.unwrap();
let after_post_like_remove = PersonAggregates::read(&mut *conn, inserted_person.id)
let after_post_like_remove = PersonAggregates::read(conn, inserted_person.id)
.await
.unwrap();
assert_eq!(0, after_post_like_remove.post_score);
Comment::update(
&mut *conn,
conn,
inserted_comment.id,
&CommentUpdateForm::builder().removed(Some(true)).build(),
)
.await
.unwrap();
Comment::update(
&mut *conn,
conn,
inserted_child_comment.id,
&CommentUpdateForm::builder().removed(Some(true)).build(),
)
.await
.unwrap();
let after_parent_comment_removed = PersonAggregates::read(&mut *conn, inserted_person.id)
let after_parent_comment_removed = PersonAggregates::read(conn, inserted_person.id)
.await
.unwrap();
assert_eq!(0, after_parent_comment_removed.comment_count);
assert_eq!(0, after_parent_comment_removed.comment_score);
// Remove a parent comment (the scores should also be removed)
Comment::delete(&mut *conn, inserted_comment.id)
Comment::delete(conn, inserted_comment.id)
.await
.unwrap();
Comment::delete(&mut *conn, inserted_child_comment.id)
Comment::delete(conn, inserted_child_comment.id)
.await
.unwrap();
let after_parent_comment_delete = PersonAggregates::read(&mut *conn, inserted_person.id)
let after_parent_comment_delete = PersonAggregates::read(conn, inserted_person.id)
.await
.unwrap();
assert_eq!(0, after_parent_comment_delete.comment_count);
assert_eq!(0, after_parent_comment_delete.comment_score);
// Add in the two comments again, then delete the post.
let new_parent_comment = Comment::create(&mut *conn, &comment_form, None)
let new_parent_comment = Comment::create(conn, &comment_form, None)
.await
.unwrap();
let _new_child_comment = Comment::create(
&mut *conn,
conn,
&child_comment_form,
Some(&new_parent_comment.path),
)
.await
.unwrap();
comment_like.comment_id = new_parent_comment.id;
CommentLike::like(&mut *conn, &comment_like).await.unwrap();
let after_comment_add = PersonAggregates::read(&mut *conn, inserted_person.id)
CommentLike::like(conn, &comment_like).await.unwrap();
let after_comment_add = PersonAggregates::read(conn, inserted_person.id)
.await
.unwrap();
assert_eq!(2, after_comment_add.comment_count);
assert_eq!(1, after_comment_add.comment_score);
Post::delete(&mut *conn, inserted_post.id).await.unwrap();
let after_post_delete = PersonAggregates::read(&mut *conn, inserted_person.id)
Post::delete(conn, inserted_post.id).await.unwrap();
let after_post_delete = PersonAggregates::read(conn, inserted_person.id)
.await
.unwrap();
assert_eq!(0, after_post_delete.comment_score);
@ -207,25 +207,25 @@ mod tests {
assert_eq!(0, after_post_delete.post_count);
// This should delete all the associated rows, and fire triggers
let person_num_deleted = Person::delete(&mut *conn, inserted_person.id)
let person_num_deleted = Person::delete(conn, inserted_person.id)
.await
.unwrap();
assert_eq!(1, person_num_deleted);
Person::delete(&mut *conn, another_inserted_person.id)
Person::delete(conn, another_inserted_person.id)
.await
.unwrap();
// Delete the community
let community_num_deleted = Community::delete(&mut *conn, inserted_community.id)
let community_num_deleted = Community::delete(conn, inserted_community.id)
.await
.unwrap();
assert_eq!(1, community_num_deleted);
// Should be none found
let after_delete = PersonAggregates::read(&mut *conn, inserted_person.id).await;
let after_delete = PersonAggregates::read(conn, inserted_person.id).await;
assert!(after_delete.is_err());
Instance::delete(&mut *conn, inserted_instance.id)
Instance::delete(conn, inserted_instance.id)
.await
.unwrap();
}

View File

@ -3,14 +3,14 @@ use crate::{
diesel::BoolExpressionMethods,
newtypes::{PersonId, PostId},
schema::person_post_aggregates::dsl::{person_id, person_post_aggregates, post_id},
utils::DbConn,
utils::GetConn,
};
use diesel::{insert_into, result::Error, ExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::utils::RunQueryDsl;
impl PersonPostAggregates {
pub async fn upsert(
mut conn: impl DbConn,
mut conn: impl GetConn,
form: &PersonPostAggregatesForm,
) -> Result<Self, Error> {
insert_into(person_post_aggregates)
@ -18,17 +18,17 @@ impl PersonPostAggregates {
.on_conflict((person_id, post_id))
.do_update()
.set(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
pub async fn read(
mut conn: impl DbConn,
mut conn: impl GetConn,
person_id_: PersonId,
post_id_: PostId,
) -> Result<Self, Error> {
person_post_aggregates
.filter(post_id.eq(post_id_).and(person_id.eq(person_id_)))
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await
}
}

View File

@ -2,20 +2,20 @@ use crate::{
aggregates::structs::PostAggregates,
newtypes::PostId,
schema::post_aggregates,
utils::{functions::hot_rank, DbConn},
utils::{functions::hot_rank, GetConn},
};
use diesel::{result::Error, ExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::utils::RunQueryDsl;
impl PostAggregates {
pub async fn read(mut conn: impl DbConn, post_id: PostId) -> Result<Self, Error> {
pub async fn read(mut conn: impl GetConn, post_id: PostId) -> Result<Self, Error> {
post_aggregates::table
.filter(post_aggregates::post_id.eq(post_id))
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await
}
pub async fn update_hot_rank(mut conn: impl DbConn, post_id: PostId) -> Result<Self, Error> {
pub async fn update_hot_rank(mut conn: impl GetConn, post_id: PostId) -> Result<Self, Error> {
diesel::update(post_aggregates::table)
.filter(post_aggregates::post_id.eq(post_id))
.set((
@ -25,7 +25,7 @@ impl PostAggregates {
post_aggregates::newest_comment_time_necro,
)),
))
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
}
@ -51,7 +51,7 @@ mod tests {
async fn test_crud() {
let mut conn = build_db_conn_for_tests().await;
let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string())
let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string())
.await
.unwrap();
@ -61,7 +61,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_person = Person::create(&mut *conn, &new_person).await.unwrap();
let inserted_person = Person::create(conn, &new_person).await.unwrap();
let another_person = PersonInsertForm::builder()
.name("jerry_community_agg".into())
@ -69,7 +69,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let another_inserted_person = Person::create(&mut *conn, &another_person).await.unwrap();
let another_inserted_person = Person::create(conn, &another_person).await.unwrap();
let new_community = CommunityInsertForm::builder()
.name("TIL_community_agg".into())
@ -78,7 +78,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_community = Community::create(&mut *conn, &new_community).await.unwrap();
let inserted_community = Community::create(conn, &new_community).await.unwrap();
let new_post = PostInsertForm::builder()
.name("A test post".into())
@ -86,7 +86,7 @@ mod tests {
.community_id(inserted_community.id)
.build();
let inserted_post = Post::create(&mut *conn, &new_post).await.unwrap();
let inserted_post = Post::create(conn, &new_post).await.unwrap();
let comment_form = CommentInsertForm::builder()
.content("A test comment".into())
@ -94,7 +94,7 @@ mod tests {
.post_id(inserted_post.id)
.build();
let inserted_comment = Comment::create(&mut *conn, &comment_form, None)
let inserted_comment = Comment::create(conn, &comment_form, None)
.await
.unwrap();
@ -105,7 +105,7 @@ mod tests {
.build();
let inserted_child_comment = Comment::create(
&mut *conn,
conn,
&child_comment_form,
Some(&inserted_comment.path),
)
@ -118,9 +118,9 @@ mod tests {
score: 1,
};
PostLike::like(&mut *conn, &post_like).await.unwrap();
PostLike::like(conn, &post_like).await.unwrap();
let post_aggs_before_delete = PostAggregates::read(&mut *conn, inserted_post.id)
let post_aggs_before_delete = PostAggregates::read(conn, inserted_post.id)
.await
.unwrap();
@ -136,9 +136,9 @@ mod tests {
score: -1,
};
PostLike::like(&mut *conn, &post_dislike).await.unwrap();
PostLike::like(conn, &post_dislike).await.unwrap();
let post_aggs_after_dislike = PostAggregates::read(&mut *conn, inserted_post.id)
let post_aggs_after_dislike = PostAggregates::read(conn, inserted_post.id)
.await
.unwrap();
@ -148,13 +148,13 @@ mod tests {
assert_eq!(1, post_aggs_after_dislike.downvotes);
// Remove the comments
Comment::delete(&mut *conn, inserted_comment.id)
Comment::delete(conn, inserted_comment.id)
.await
.unwrap();
Comment::delete(&mut *conn, inserted_child_comment.id)
Comment::delete(conn, inserted_child_comment.id)
.await
.unwrap();
let after_comment_delete = PostAggregates::read(&mut *conn, inserted_post.id)
let after_comment_delete = PostAggregates::read(conn, inserted_post.id)
.await
.unwrap();
assert_eq!(0, after_comment_delete.comments);
@ -163,10 +163,10 @@ mod tests {
assert_eq!(1, after_comment_delete.downvotes);
// Remove the first post like
PostLike::remove(&mut *conn, inserted_person.id, inserted_post.id)
PostLike::remove(conn, inserted_person.id, inserted_post.id)
.await
.unwrap();
let after_like_remove = PostAggregates::read(&mut *conn, inserted_post.id)
let after_like_remove = PostAggregates::read(conn, inserted_post.id)
.await
.unwrap();
assert_eq!(0, after_like_remove.comments);
@ -175,25 +175,25 @@ mod tests {
assert_eq!(1, after_like_remove.downvotes);
// This should delete all the associated rows, and fire triggers
Person::delete(&mut *conn, another_inserted_person.id)
Person::delete(conn, another_inserted_person.id)
.await
.unwrap();
let person_num_deleted = Person::delete(&mut *conn, inserted_person.id)
let person_num_deleted = Person::delete(conn, inserted_person.id)
.await
.unwrap();
assert_eq!(1, person_num_deleted);
// Delete the community
let community_num_deleted = Community::delete(&mut *conn, inserted_community.id)
let community_num_deleted = Community::delete(conn, inserted_community.id)
.await
.unwrap();
assert_eq!(1, community_num_deleted);
// Should be none found, since the creator was deleted
let after_delete = PostAggregates::read(&mut *conn, inserted_post.id).await;
let after_delete = PostAggregates::read(conn, inserted_post.id).await;
assert!(after_delete.is_err());
Instance::delete(&mut *conn, inserted_instance.id)
Instance::delete(conn, inserted_instance.id)
.await
.unwrap();
}

View File

@ -1,10 +1,10 @@
use crate::{aggregates::structs::SiteAggregates, schema::site_aggregates, utils::DbConn};
use crate::{aggregates::structs::SiteAggregates, schema::site_aggregates, utils::GetConn};
use diesel::result::Error;
use diesel_async::RunQueryDsl;
use lemmy_db_schema::utils::RunQueryDsl;
impl SiteAggregates {
pub async fn read(mut conn: impl DbConn) -> Result<Self, Error> {
site_aggregates::table.first::<Self>(&mut *conn).await
pub async fn read(mut conn: impl GetConn) -> Result<Self, Error> {
site_aggregates::table.first::<Self>(conn).await
}
}
@ -30,7 +30,7 @@ mod tests {
async fn test_crud() {
let mut conn = build_db_conn_for_tests().await;
let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string())
let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string())
.await
.unwrap();
@ -40,14 +40,14 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_person = Person::create(&mut *conn, &new_person).await.unwrap();
let inserted_person = Person::create(conn, &new_person).await.unwrap();
let site_form = SiteInsertForm::builder()
.name("test_site".into())
.instance_id(inserted_instance.id)
.build();
let inserted_site = Site::create(&mut *conn, &site_form).await.unwrap();
let inserted_site = Site::create(conn, &site_form).await.unwrap();
let new_community = CommunityInsertForm::builder()
.name("TIL_site_agg".into())
@ -56,7 +56,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_community = Community::create(&mut *conn, &new_community).await.unwrap();
let inserted_community = Community::create(conn, &new_community).await.unwrap();
let new_post = PostInsertForm::builder()
.name("A test post".into())
@ -65,8 +65,8 @@ mod tests {
.build();
// Insert two of those posts
let inserted_post = Post::create(&mut *conn, &new_post).await.unwrap();
let _inserted_post_again = Post::create(&mut *conn, &new_post).await.unwrap();
let inserted_post = Post::create(conn, &new_post).await.unwrap();
let _inserted_post_again = Post::create(conn, &new_post).await.unwrap();
let comment_form = CommentInsertForm::builder()
.content("A test comment".into())
@ -75,7 +75,7 @@ mod tests {
.build();
// Insert two of those comments
let inserted_comment = Comment::create(&mut *conn, &comment_form, None)
let inserted_comment = Comment::create(conn, &comment_form, None)
.await
.unwrap();
@ -86,14 +86,14 @@ mod tests {
.build();
let _inserted_child_comment = Comment::create(
&mut *conn,
conn,
&child_comment_form,
Some(&inserted_comment.path),
)
.await
.unwrap();
let site_aggregates_before_delete = SiteAggregates::read(&mut *conn).await.unwrap();
let site_aggregates_before_delete = SiteAggregates::read(conn).await.unwrap();
// TODO: this is unstable, sometimes it returns 0 users, sometimes 1
//assert_eq!(0, site_aggregates_before_delete.users);
@ -102,32 +102,32 @@ mod tests {
assert_eq!(2, site_aggregates_before_delete.comments);
// Try a post delete
Post::delete(&mut *conn, inserted_post.id).await.unwrap();
let site_aggregates_after_post_delete = SiteAggregates::read(&mut *conn).await.unwrap();
Post::delete(conn, inserted_post.id).await.unwrap();
let site_aggregates_after_post_delete = SiteAggregates::read(conn).await.unwrap();
assert_eq!(1, site_aggregates_after_post_delete.posts);
assert_eq!(0, site_aggregates_after_post_delete.comments);
// This shouuld delete all the associated rows, and fire triggers
let person_num_deleted = Person::delete(&mut *conn, inserted_person.id)
let person_num_deleted = Person::delete(conn, inserted_person.id)
.await
.unwrap();
assert_eq!(1, person_num_deleted);
// Delete the community
let community_num_deleted = Community::delete(&mut *conn, inserted_community.id)
let community_num_deleted = Community::delete(conn, inserted_community.id)
.await
.unwrap();
assert_eq!(1, community_num_deleted);
// Site should still exist, it can without a site creator.
let after_delete_creator = SiteAggregates::read(&mut *conn).await;
let after_delete_creator = SiteAggregates::read(conn).await;
assert!(after_delete_creator.is_ok());
Site::delete(&mut *conn, inserted_site.id).await.unwrap();
let after_delete_site = SiteAggregates::read(&mut *conn).await;
Site::delete(conn, inserted_site.id).await.unwrap();
let after_delete_site = SiteAggregates::read(conn).await;
assert!(after_delete_site.is_err());
Instance::delete(&mut *conn, inserted_instance.id)
Instance::delete(conn, inserted_instance.id)
.await
.unwrap();
}

View File

@ -3,52 +3,52 @@ use crate::{
schema::activity::dsl::{activity, ap_id},
source::activity::{Activity, ActivityInsertForm, ActivityUpdateForm},
traits::Crud,
utils::DbConn,
utils::GetConn,
};
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::utils::RunQueryDsl;
#[async_trait]
impl Crud for Activity {
type InsertForm = ActivityInsertForm;
type UpdateForm = ActivityUpdateForm;
type IdType = i32;
async fn read(mut conn: impl DbConn, activity_id: i32) -> Result<Self, Error> {
activity.find(activity_id).first::<Self>(&mut *conn).await
async fn read(mut conn: impl GetConn, activity_id: i32) -> Result<Self, Error> {
activity.find(activity_id).first::<Self>(conn).await
}
async fn create(mut conn: impl DbConn, new_activity: &Self::InsertForm) -> Result<Self, Error> {
async fn create(mut conn: impl GetConn, new_activity: &Self::InsertForm) -> Result<Self, Error> {
insert_into(activity)
.values(new_activity)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn update(
mut conn: impl DbConn,
mut conn: impl GetConn,
activity_id: i32,
new_activity: &Self::UpdateForm,
) -> Result<Self, Error> {
diesel::update(activity.find(activity_id))
.set(new_activity)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn delete(mut conn: impl DbConn, activity_id: i32) -> Result<usize, Error> {
async fn delete(mut conn: impl GetConn, activity_id: i32) -> Result<usize, Error> {
diesel::delete(activity.find(activity_id))
.execute(&mut *conn)
.execute(conn)
.await
}
}
impl Activity {
pub async fn read_from_apub_id(
mut conn: impl DbConn,
mut conn: impl GetConn,
object_id: &DbUrl,
) -> Result<Activity, Error> {
activity
.filter(ap_id.eq(object_id))
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await
}
}
@ -74,7 +74,7 @@ mod tests {
async fn test_crud() {
let mut conn = build_db_conn_for_tests().await;
let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string())
let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string())
.await
.unwrap();
@ -84,7 +84,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_creator = Person::create(&mut *conn, &creator_form).await.unwrap();
let inserted_creator = Person::create(conn, &creator_form).await.unwrap();
let ap_id_: DbUrl = Url::parse(
"https://enterprise.lemmy.ml/activities/delete/f1b5d57c-80f8-4e03-a615-688d552e946c",
@ -113,7 +113,7 @@ mod tests {
updated: None,
};
let inserted_activity = Activity::create(&mut *conn, &activity_form).await.unwrap();
let inserted_activity = Activity::create(conn, &activity_form).await.unwrap();
let expected_activity = Activity {
ap_id: ap_id_.clone(),
@ -125,16 +125,16 @@ mod tests {
updated: None,
};
let read_activity = Activity::read(&mut *conn, inserted_activity.id)
let read_activity = Activity::read(conn, inserted_activity.id)
.await
.unwrap();
let read_activity_by_apub_id = Activity::read_from_apub_id(&mut *conn, &ap_id_)
let read_activity_by_apub_id = Activity::read_from_apub_id(conn, &ap_id_)
.await
.unwrap();
Person::delete(&mut *conn, inserted_creator.id)
Person::delete(conn, inserted_creator.id)
.await
.unwrap();
Activity::delete(&mut *conn, inserted_activity.id)
Activity::delete(conn, inserted_activity.id)
.await
.unwrap();

View File

@ -14,7 +14,7 @@ use crate::{
language::Language,
site::Site,
},
utils::DbConn,
utils::GetConn,
};
use diesel::{
delete,
@ -25,7 +25,7 @@ use diesel::{
ExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::utils::RunQueryDsl;
use lemmy_utils::error::LemmyError;
use tokio::sync::OnceCell;
@ -33,7 +33,7 @@ pub const UNDETERMINED_ID: LanguageId = LanguageId(0);
impl LocalUserLanguage {
pub async fn read(
mut conn: impl DbConn,
mut conn: impl GetConn,
for_local_user_id: LocalUserId,
) -> Result<Vec<LanguageId>, Error> {
use crate::schema::local_user_language::dsl::{
@ -50,9 +50,9 @@ impl LocalUserLanguage {
.filter(local_user_id.eq(for_local_user_id))
.order(language_id)
.select(language_id)
.get_results(&mut *conn)
.get_results(conn)
.await?;
convert_read_languages(&mut *conn, langs).await
convert_read_languages(conn, langs).await
}) as _
})
.await
@ -62,14 +62,14 @@ impl LocalUserLanguage {
///
/// If no language_id vector is given, it will show all languages
pub async fn update(
mut conn: impl DbConn,
mut conn: impl GetConn,
language_ids: Vec<LanguageId>,
for_local_user_id: LocalUserId,
) -> Result<(), Error> {
let mut lang_ids = convert_update_languages(&mut *conn, language_ids).await?;
let mut lang_ids = convert_update_languages(conn, language_ids).await?;
// No need to update if languages are unchanged
let current = LocalUserLanguage::read(&mut *conn, for_local_user_id).await?;
let current = LocalUserLanguage::read(conn, for_local_user_id).await?;
if current == lang_ids {
return Ok(());
}
@ -91,7 +91,7 @@ impl LocalUserLanguage {
use crate::schema::local_user_language::dsl::{local_user_id, local_user_language};
// Clear the current user languages
delete(local_user_language.filter(local_user_id.eq(for_local_user_id)))
.execute(&mut *conn)
.execute(conn)
.await?;
for l in lang_ids {
@ -101,7 +101,7 @@ impl LocalUserLanguage {
};
insert_into(local_user_language)
.values(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await?;
}
Ok(())
@ -112,42 +112,42 @@ impl LocalUserLanguage {
}
impl SiteLanguage {
pub async fn read_local_raw(mut conn: impl DbConn) -> Result<Vec<LanguageId>, Error> {
pub async fn read_local_raw(mut conn: impl GetConn) -> Result<Vec<LanguageId>, Error> {
site::table
.inner_join(local_site::table)
.inner_join(site_language::table)
.order(site_language::id)
.select(site_language::language_id)
.load(&mut *conn)
.load(conn)
.await
}
async fn read_raw(mut conn: impl DbConn, for_site_id: SiteId) -> Result<Vec<LanguageId>, Error> {
async fn read_raw(mut conn: impl GetConn, for_site_id: SiteId) -> Result<Vec<LanguageId>, Error> {
site_language::table
.filter(site_language::site_id.eq(for_site_id))
.order(site_language::language_id)
.select(site_language::language_id)
.load(&mut *conn)
.load(conn)
.await
}
pub async fn read(mut conn: impl DbConn, for_site_id: SiteId) -> Result<Vec<LanguageId>, Error> {
let langs = Self::read_raw(&mut *conn, for_site_id).await?;
pub async fn read(mut conn: impl GetConn, for_site_id: SiteId) -> Result<Vec<LanguageId>, Error> {
let langs = Self::read_raw(conn, for_site_id).await?;
convert_read_languages(&mut *conn, langs).await
convert_read_languages(conn, langs).await
}
pub async fn update(
mut conn: impl DbConn,
mut conn: impl GetConn,
language_ids: Vec<LanguageId>,
site: &Site,
) -> Result<(), Error> {
let for_site_id = site.id;
let instance_id = site.instance_id;
let lang_ids = convert_update_languages(&mut *conn, language_ids).await?;
let lang_ids = convert_update_languages(conn, language_ids).await?;
// No need to update if languages are unchanged
let current = SiteLanguage::read(&mut *conn, site.id).await?;
let current = SiteLanguage::read(conn, site.id).await?;
if current == lang_ids {
return Ok(());
}
@ -160,7 +160,7 @@ impl SiteLanguage {
// Clear the current languages
delete(site_language.filter(site_id.eq(for_site_id)))
.execute(&mut *conn)
.execute(conn)
.await?;
for l in lang_ids {
@ -170,11 +170,11 @@ impl SiteLanguage {
};
insert_into(site_language)
.values(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await?;
}
CommunityLanguage::limit_languages(&mut *conn, instance_id).await?;
CommunityLanguage::limit_languages(conn, instance_id).await?;
Ok(())
}) as _
@ -186,7 +186,7 @@ impl SiteLanguage {
impl CommunityLanguage {
/// Returns true if the given language is one of configured languages for given community
pub async fn is_allowed_community_language(
mut conn: impl DbConn,
mut conn: impl GetConn,
for_language_id: Option<LanguageId>,
for_community_id: CommunityId,
) -> Result<(), LemmyError> {
@ -198,7 +198,7 @@ impl CommunityLanguage {
.filter(language_id.eq(for_language_id))
.filter(community_id.eq(for_community_id)),
))
.get_result(&mut *conn)
.get_result(conn)
.await?;
if is_allowed {
@ -216,7 +216,7 @@ impl CommunityLanguage {
/// community language, and it shouldnt be possible to post content in languages which are not
/// allowed by local site.
async fn limit_languages(
mut conn: impl DbConn,
mut conn: impl GetConn,
for_instance_id: InstanceId,
) -> Result<(), Error> {
use crate::schema::{
@ -230,19 +230,19 @@ impl CommunityLanguage {
.filter(c::instance_id.eq(for_instance_id))
.filter(sl::language_id.is_null())
.select(cl::language_id)
.get_results(&mut *conn)
.get_results(conn)
.await?;
for c in community_languages {
delete(cl::community_language.filter(cl::language_id.eq(c)))
.execute(&mut *conn)
.execute(conn)
.await?;
}
Ok(())
}
async fn read_raw(
mut conn: impl DbConn,
mut conn: impl GetConn,
for_community_id: CommunityId,
) -> Result<Vec<LanguageId>, Error> {
use crate::schema::community_language::dsl::{community_id, community_language, language_id};
@ -250,30 +250,30 @@ impl CommunityLanguage {
.filter(community_id.eq(for_community_id))
.order(language_id)
.select(language_id)
.get_results(&mut *conn)
.get_results(conn)
.await
}
pub async fn read(
mut conn: impl DbConn,
mut conn: impl GetConn,
for_community_id: CommunityId,
) -> Result<Vec<LanguageId>, Error> {
let langs = Self::read_raw(&mut *conn, for_community_id).await?;
convert_read_languages(&mut *conn, langs).await
let langs = Self::read_raw(conn, for_community_id).await?;
convert_read_languages(conn, langs).await
}
pub async fn update(
mut conn: impl DbConn,
mut conn: impl GetConn,
mut language_ids: Vec<LanguageId>,
for_community_id: CommunityId,
) -> Result<(), Error> {
if language_ids.is_empty() {
language_ids = SiteLanguage::read_local_raw(&mut *conn).await?;
language_ids = SiteLanguage::read_local_raw(conn).await?;
}
let lang_ids = convert_update_languages(&mut *conn, language_ids).await?;
let lang_ids = convert_update_languages(conn, language_ids).await?;
// No need to update if languages are unchanged
let current = CommunityLanguage::read_raw(&mut *conn, for_community_id).await?;
let current = CommunityLanguage::read_raw(conn, for_community_id).await?;
if current == lang_ids {
return Ok(());
}
@ -285,7 +285,7 @@ impl CommunityLanguage {
use crate::schema::community_language::dsl::{community_id, community_language};
// Clear the current languages
delete(community_language.filter(community_id.eq(for_community_id)))
.execute(&mut *conn)
.execute(conn)
.await?;
for l in lang_ids {
@ -295,7 +295,7 @@ impl CommunityLanguage {
};
insert_into(community_language)
.values(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await?;
}
Ok(())
@ -306,7 +306,7 @@ impl CommunityLanguage {
}
pub async fn default_post_language(
mut conn: impl DbConn,
mut conn: impl GetConn,
community_id: CommunityId,
local_user_id: LocalUserId,
) -> Result<Option<LanguageId>, Error> {
@ -316,7 +316,7 @@ pub async fn default_post_language(
.filter(ul::local_user_id.eq(local_user_id))
.filter(cl::community_id.eq(community_id))
.select(cl::language_id)
.get_results::<LanguageId>(&mut *conn)
.get_results::<LanguageId>(conn)
.await?;
if intersection.len() == 1 {
@ -331,12 +331,12 @@ pub async fn default_post_language(
/// If no language is given, set all languages
async fn convert_update_languages(
mut conn: impl DbConn,
mut conn: impl GetConn,
language_ids: Vec<LanguageId>,
) -> Result<Vec<LanguageId>, Error> {
if language_ids.is_empty() {
Ok(
Language::read_all_conn(&mut *conn)
Language::read_all_conn(conn)
.await?
.into_iter()
.map(|l| l.id)
@ -349,7 +349,7 @@ async fn convert_update_languages(
/// If all languages are returned, return empty vec instead
async fn convert_read_languages(
mut conn: impl DbConn,
mut conn: impl GetConn,
language_ids: Vec<LanguageId>,
) -> Result<Vec<LanguageId>, Error> {
static ALL_LANGUAGES_COUNT: OnceCell<usize> = OnceCell::const_new();
@ -358,7 +358,7 @@ async fn convert_read_languages(
use crate::schema::language::dsl::{id, language};
let count: i64 = language
.select(count(id))
.first(&mut *conn)
.first(conn)
.await
.expect("read number of languages");
count as usize
@ -381,7 +381,7 @@ mod tests {
convert_update_languages,
default_post_language,
CommunityLanguage,
DbConn,
GetConn,
Language,
LanguageId,
LocalUserLanguage,
@ -402,37 +402,37 @@ mod tests {
};
use serial_test::serial;
async fn test_langs1(mut conn: impl DbConn) -> Vec<LanguageId> {
async fn test_langs1(mut conn: impl GetConn) -> Vec<LanguageId> {
vec![
Language::read_id_from_code(&mut *conn, Some("en"))
Language::read_id_from_code(conn, Some("en"))
.await
.unwrap()
.unwrap(),
Language::read_id_from_code(&mut *conn, Some("fr"))
Language::read_id_from_code(conn, Some("fr"))
.await
.unwrap()
.unwrap(),
Language::read_id_from_code(&mut *conn, Some("ru"))
Language::read_id_from_code(conn, Some("ru"))
.await
.unwrap()
.unwrap(),
]
}
async fn test_langs2(mut conn: impl DbConn) -> Vec<LanguageId> {
async fn test_langs2(mut conn: impl GetConn) -> Vec<LanguageId> {
vec![
Language::read_id_from_code(&mut *conn, Some("fi"))
Language::read_id_from_code(conn, Some("fi"))
.await
.unwrap()
.unwrap(),
Language::read_id_from_code(&mut *conn, Some("se"))
Language::read_id_from_code(conn, Some("se"))
.await
.unwrap()
.unwrap(),
]
}
async fn create_test_site(mut conn: impl DbConn) -> (Site, Instance) {
let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string())
async fn create_test_site(mut conn: impl GetConn) -> (Site, Instance) {
let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string())
.await
.unwrap();
@ -440,11 +440,11 @@ mod tests {
.name("test site".to_string())
.instance_id(inserted_instance.id)
.build();
let site = Site::create(&mut *conn, &site_form).await.unwrap();
let site = Site::create(conn, &site_form).await.unwrap();
// Create a local site, since this is necessary for local languages
let local_site_form = LocalSiteInsertForm::builder().site_id(site.id).build();
LocalSite::create(&mut *conn, &local_site_form)
LocalSite::create(conn, &local_site_form)
.await
.unwrap();
@ -457,12 +457,12 @@ mod tests {
let mut conn = build_db_conn_for_tests().await;
// call with empty vec, returns all languages
let converted1 = convert_update_languages(&mut *conn, vec![]).await.unwrap();
let converted1 = convert_update_languages(conn, vec![]).await.unwrap();
assert_eq!(184, converted1.len());
// call with nonempty vec, returns same vec
let test_langs = test_langs1(&mut *conn).await;
let converted2 = convert_update_languages(&mut *conn, test_langs.clone())
let test_langs = test_langs1(conn).await;
let converted2 = convert_update_languages(conn, test_langs.clone())
.await
.unwrap();
assert_eq!(test_langs, converted2);
@ -474,13 +474,13 @@ mod tests {
let mut conn = build_db_conn_for_tests().await;
// call with all languages, returns empty vec
let all_langs = language.select(id).get_results(&mut *conn).await.unwrap();
let converted1: Vec<LanguageId> = convert_read_languages(&mut *conn, all_langs).await.unwrap();
let all_langs = language.select(id).get_results(conn).await.unwrap();
let converted1: Vec<LanguageId> = convert_read_languages(conn, all_langs).await.unwrap();
assert_eq!(0, converted1.len());
// call with nonempty vec, returns same vec
let test_langs = test_langs1(&mut *conn).await;
let converted2 = convert_read_languages(&mut *conn, test_langs.clone())
let test_langs = test_langs1(conn).await;
let converted2 = convert_read_languages(conn, test_langs.clone())
.await
.unwrap();
assert_eq!(test_langs, converted2);
@ -491,23 +491,23 @@ mod tests {
async fn test_site_languages() {
let mut conn = build_db_conn_for_tests().await;
let (site, instance) = create_test_site(&mut *conn).await;
let site_languages1 = SiteLanguage::read_local_raw(&mut *conn).await.unwrap();
let (site, instance) = create_test_site(conn).await;
let site_languages1 = SiteLanguage::read_local_raw(conn).await.unwrap();
// site is created with all languages
assert_eq!(184, site_languages1.len());
let test_langs = test_langs1(&mut *conn).await;
SiteLanguage::update(&mut *conn, test_langs.clone(), &site)
let test_langs = test_langs1(conn).await;
SiteLanguage::update(conn, test_langs.clone(), &site)
.await
.unwrap();
let site_languages2 = SiteLanguage::read_local_raw(&mut *conn).await.unwrap();
let site_languages2 = SiteLanguage::read_local_raw(conn).await.unwrap();
// after update, site only has new languages
assert_eq!(test_langs, site_languages2);
Site::delete(&mut *conn, site.id).await.unwrap();
Instance::delete(&mut *conn, instance.id).await.unwrap();
LocalSite::delete(&mut *conn).await.unwrap();
Site::delete(conn, site.id).await.unwrap();
Instance::delete(conn, instance.id).await.unwrap();
LocalSite::delete(conn).await.unwrap();
}
#[tokio::test]
@ -515,9 +515,9 @@ mod tests {
async fn test_user_languages() {
let mut conn = build_db_conn_for_tests().await;
let (site, instance) = create_test_site(&mut *conn).await;
let mut test_langs = test_langs1(&mut *conn).await;
SiteLanguage::update(&mut *conn, test_langs.clone(), &site)
let (site, instance) = create_test_site(conn).await;
let mut test_langs = test_langs1(conn).await;
SiteLanguage::update(conn, test_langs.clone(), &site)
.await
.unwrap();
@ -526,16 +526,16 @@ mod tests {
.public_key("pubkey".to_string())
.instance_id(instance.id)
.build();
let person = Person::create(&mut *conn, &person_form).await.unwrap();
let person = Person::create(conn, &person_form).await.unwrap();
let local_user_form = LocalUserInsertForm::builder()
.person_id(person.id)
.password_encrypted("my_pw".to_string())
.build();
let local_user = LocalUser::create(&mut *conn, &local_user_form)
let local_user = LocalUser::create(conn, &local_user_form)
.await
.unwrap();
let local_user_langs1 = LocalUserLanguage::read(&mut *conn, local_user.id)
let local_user_langs1 = LocalUserLanguage::read(conn, local_user.id)
.await
.unwrap();
@ -546,37 +546,37 @@ mod tests {
assert_eq!(test_langs, local_user_langs1);
// update user languages
let test_langs2 = test_langs2(&mut *conn).await;
LocalUserLanguage::update(&mut *conn, test_langs2, local_user.id)
let test_langs2 = test_langs2(conn).await;
LocalUserLanguage::update(conn, test_langs2, local_user.id)
.await
.unwrap();
let local_user_langs2 = LocalUserLanguage::read(&mut *conn, local_user.id)
let local_user_langs2 = LocalUserLanguage::read(conn, local_user.id)
.await
.unwrap();
assert_eq!(3, local_user_langs2.len());
Person::delete(&mut *conn, person.id).await.unwrap();
LocalUser::delete(&mut *conn, local_user.id).await.unwrap();
Site::delete(&mut *conn, site.id).await.unwrap();
LocalSite::delete(&mut *conn).await.unwrap();
Instance::delete(&mut *conn, instance.id).await.unwrap();
Person::delete(conn, person.id).await.unwrap();
LocalUser::delete(conn, local_user.id).await.unwrap();
Site::delete(conn, site.id).await.unwrap();
LocalSite::delete(conn).await.unwrap();
Instance::delete(conn, instance.id).await.unwrap();
}
#[tokio::test]
#[serial]
async fn test_community_languages() {
let mut conn = build_db_conn_for_tests().await;
let (site, instance) = create_test_site(&mut *conn).await;
let test_langs = test_langs1(&mut *conn).await;
SiteLanguage::update(&mut *conn, test_langs.clone(), &site)
let (site, instance) = create_test_site(conn).await;
let test_langs = test_langs1(conn).await;
SiteLanguage::update(conn, test_langs.clone(), &site)
.await
.unwrap();
let read_site_langs = SiteLanguage::read(&mut *conn, site.id).await.unwrap();
let read_site_langs = SiteLanguage::read(conn, site.id).await.unwrap();
assert_eq!(test_langs, read_site_langs);
// Test the local ones are the same
let read_local_site_langs = SiteLanguage::read_local_raw(&mut *conn).await.unwrap();
let read_local_site_langs = SiteLanguage::read_local_raw(conn).await.unwrap();
assert_eq!(test_langs, read_local_site_langs);
let community_form = CommunityInsertForm::builder()
@ -585,10 +585,10 @@ mod tests {
.public_key("pubkey".to_string())
.instance_id(instance.id)
.build();
let community = Community::create(&mut *conn, &community_form)
let community = Community::create(conn, &community_form)
.await
.unwrap();
let community_langs1 = CommunityLanguage::read(&mut *conn, community.id)
let community_langs1 = CommunityLanguage::read(conn, community.id)
.await
.unwrap();
@ -596,16 +596,16 @@ mod tests {
assert_eq!(test_langs, community_langs1);
let allowed_lang1 = CommunityLanguage::is_allowed_community_language(
&mut *conn,
conn,
Some(test_langs[0]),
community.id,
)
.await;
assert!(allowed_lang1.is_ok());
let test_langs2 = test_langs2(&mut *conn).await;
let test_langs2 = test_langs2(conn).await;
let allowed_lang2 = CommunityLanguage::is_allowed_community_language(
&mut *conn,
conn,
Some(test_langs2[0]),
community.id,
)
@ -614,36 +614,36 @@ mod tests {
// limit site languages to en, fi. after this, community languages should be updated to
// intersection of old languages (en, fr, ru) and (en, fi), which is only fi.
SiteLanguage::update(&mut *conn, vec![test_langs[0], test_langs2[0]], &site)
SiteLanguage::update(conn, vec![test_langs[0], test_langs2[0]], &site)
.await
.unwrap();
let community_langs2 = CommunityLanguage::read(&mut *conn, community.id)
let community_langs2 = CommunityLanguage::read(conn, community.id)
.await
.unwrap();
assert_eq!(vec![test_langs[0]], community_langs2);
// update community languages to different ones
CommunityLanguage::update(&mut *conn, test_langs2.clone(), community.id)
CommunityLanguage::update(conn, test_langs2.clone(), community.id)
.await
.unwrap();
let community_langs3 = CommunityLanguage::read(&mut *conn, community.id)
let community_langs3 = CommunityLanguage::read(conn, community.id)
.await
.unwrap();
assert_eq!(test_langs2, community_langs3);
Community::delete(&mut *conn, community.id).await.unwrap();
Site::delete(&mut *conn, site.id).await.unwrap();
LocalSite::delete(&mut *conn).await.unwrap();
Instance::delete(&mut *conn, instance.id).await.unwrap();
Community::delete(conn, community.id).await.unwrap();
Site::delete(conn, site.id).await.unwrap();
LocalSite::delete(conn).await.unwrap();
Instance::delete(conn, instance.id).await.unwrap();
}
#[tokio::test]
#[serial]
async fn test_default_post_language() {
let mut conn = build_db_conn_for_tests().await;
let (site, instance) = create_test_site(&mut *conn).await;
let test_langs = test_langs1(&mut *conn).await;
let test_langs2 = test_langs2(&mut *conn).await;
let (site, instance) = create_test_site(conn).await;
let test_langs = test_langs1(conn).await;
let test_langs2 = test_langs2(conn).await;
let community_form = CommunityInsertForm::builder()
.name("test community".to_string())
@ -651,10 +651,10 @@ mod tests {
.public_key("pubkey".to_string())
.instance_id(instance.id)
.build();
let community = Community::create(&mut *conn, &community_form)
let community = Community::create(conn, &community_form)
.await
.unwrap();
CommunityLanguage::update(&mut *conn, test_langs, community.id)
CommunityLanguage::update(conn, test_langs, community.id)
.await
.unwrap();
@ -663,55 +663,55 @@ mod tests {
.public_key("pubkey".to_string())
.instance_id(instance.id)
.build();
let person = Person::create(&mut *conn, &person_form).await.unwrap();
let person = Person::create(conn, &person_form).await.unwrap();
let local_user_form = LocalUserInsertForm::builder()
.person_id(person.id)
.password_encrypted("my_pw".to_string())
.build();
let local_user = LocalUser::create(&mut *conn, &local_user_form)
let local_user = LocalUser::create(conn, &local_user_form)
.await
.unwrap();
LocalUserLanguage::update(&mut *conn, test_langs2, local_user.id)
LocalUserLanguage::update(conn, test_langs2, local_user.id)
.await
.unwrap();
// no overlap in user/community languages, so defaults to undetermined
let def1 = default_post_language(&mut *conn, community.id, local_user.id)
let def1 = default_post_language(conn, community.id, local_user.id)
.await
.unwrap();
assert_eq!(None, def1);
let ru = Language::read_id_from_code(&mut *conn, Some("ru"))
let ru = Language::read_id_from_code(conn, Some("ru"))
.await
.unwrap()
.unwrap();
let test_langs3 = vec![
ru,
Language::read_id_from_code(&mut *conn, Some("fi"))
Language::read_id_from_code(conn, Some("fi"))
.await
.unwrap()
.unwrap(),
Language::read_id_from_code(&mut *conn, Some("se"))
Language::read_id_from_code(conn, Some("se"))
.await
.unwrap()
.unwrap(),
UNDETERMINED_ID,
];
LocalUserLanguage::update(&mut *conn, test_langs3, local_user.id)
LocalUserLanguage::update(conn, test_langs3, local_user.id)
.await
.unwrap();
// this time, both have ru as common lang
let def2 = default_post_language(&mut *conn, community.id, local_user.id)
let def2 = default_post_language(conn, community.id, local_user.id)
.await
.unwrap();
assert_eq!(Some(ru), def2);
Person::delete(&mut *conn, person.id).await.unwrap();
Community::delete(&mut *conn, community.id).await.unwrap();
LocalUser::delete(&mut *conn, local_user.id).await.unwrap();
Site::delete(&mut *conn, site.id).await.unwrap();
LocalSite::delete(&mut *conn).await.unwrap();
Instance::delete(&mut *conn, instance.id).await.unwrap();
Person::delete(conn, person.id).await.unwrap();
Community::delete(conn, community.id).await.unwrap();
LocalUser::delete(conn, local_user.id).await.unwrap();
Site::delete(conn, site.id).await.unwrap();
LocalSite::delete(conn).await.unwrap();
Instance::delete(conn, instance.id).await.unwrap();
}
}

View File

@ -1,7 +1,7 @@
use crate::{
schema::captcha_answer::dsl::{answer, captcha_answer, uuid},
source::captcha_answer::{CaptchaAnswer, CaptchaAnswerForm, CheckCaptchaAnswer},
utils::{functions::lower, DbConn},
utils::{functions::lower, GetConn},
};
use diesel::{
delete,
@ -12,18 +12,18 @@ use diesel::{
ExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::utils::RunQueryDsl;
impl CaptchaAnswer {
pub async fn insert(mut conn: impl DbConn, captcha: &CaptchaAnswerForm) -> Result<Self, Error> {
pub async fn insert(mut conn: impl GetConn, captcha: &CaptchaAnswerForm) -> Result<Self, Error> {
insert_into(captcha_answer)
.values(captcha)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
pub async fn check_captcha(
mut conn: impl DbConn,
mut conn: impl GetConn,
to_check: CheckCaptchaAnswer,
) -> Result<bool, Error> {
// fetch requested captcha
@ -32,12 +32,12 @@ impl CaptchaAnswer {
.filter((uuid).eq(to_check.uuid))
.filter(lower(answer).eq(to_check.answer.to_lowercase().clone())),
))
.get_result::<bool>(&mut *conn)
.get_result::<bool>(conn)
.await?;
// delete checked captcha
delete(captcha_answer.filter(uuid.eq(to_check.uuid)))
.execute(&mut *conn)
.execute(conn)
.await?;
Ok(captcha_exists)
@ -58,7 +58,7 @@ mod tests {
let mut conn = build_db_conn_for_tests().await;
let inserted = CaptchaAnswer::insert(
&mut *conn,
conn,
&CaptchaAnswerForm {
answer: "XYZ".to_string(),
},
@ -67,7 +67,7 @@ mod tests {
.expect("should not fail to insert captcha");
let result = CaptchaAnswer::check_captcha(
&mut *conn,
conn,
CheckCaptchaAnswer {
uuid: inserted.uuid,
answer: "xyz".to_string(),
@ -85,7 +85,7 @@ mod tests {
let mut conn = build_db_conn_for_tests().await;
let inserted = CaptchaAnswer::insert(
&mut *conn,
conn,
&CaptchaAnswerForm {
answer: "XYZ".to_string(),
},
@ -94,7 +94,7 @@ mod tests {
.expect("should not fail to insert captcha");
let _result = CaptchaAnswer::check_captcha(
&mut *conn,
conn,
CheckCaptchaAnswer {
uuid: inserted.uuid,
answer: "xyz".to_string(),
@ -103,7 +103,7 @@ mod tests {
.await;
let result_repeat = CaptchaAnswer::check_captcha(
&mut *conn,
conn,
CheckCaptchaAnswer {
uuid: inserted.uuid,
answer: "xyz".to_string(),

View File

@ -11,7 +11,7 @@ use crate::{
CommentUpdateForm,
},
traits::{Crud, Likeable, Saveable},
utils::{naive_now, DbConn, DELETED_REPLACEMENT_TEXT},
utils::{naive_now, GetConn, DELETED_REPLACEMENT_TEXT},
};
use diesel::{
dsl::{insert_into, sql_query},
@ -19,13 +19,13 @@ use diesel::{
ExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl;
use diesel_ltree::Ltree;
use lemmy_db_schema::utils::RunQueryDsl;
use url::Url;
impl Comment {
pub async fn permadelete_for_creator(
mut conn: impl DbConn,
mut conn: impl GetConn,
for_creator_id: PersonId,
) -> Result<Vec<Self>, Error> {
diesel::update(comment.filter(creator_id.eq(for_creator_id)))
@ -34,23 +34,23 @@ impl Comment {
deleted.eq(true),
updated.eq(naive_now()),
))
.get_results::<Self>(&mut *conn)
.get_results::<Self>(conn)
.await
}
pub async fn update_removed_for_creator(
mut conn: impl DbConn,
mut conn: impl GetConn,
for_creator_id: PersonId,
new_removed: bool,
) -> Result<Vec<Self>, Error> {
diesel::update(comment.filter(creator_id.eq(for_creator_id)))
.set((removed.eq(new_removed), updated.eq(naive_now())))
.get_results::<Self>(&mut *conn)
.get_results::<Self>(conn)
.await
}
pub async fn create(
mut conn: impl DbConn,
mut conn: impl GetConn,
comment_form: &CommentInsertForm,
parent_path: Option<&Ltree>,
) -> Result<Comment, Error> {
@ -60,7 +60,7 @@ impl Comment {
.on_conflict(ap_id)
.do_update()
.set(comment_form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await;
if let Ok(comment_insert) = inserted_comment {
@ -78,7 +78,7 @@ impl Comment {
let updated_comment = diesel::update(comment.find(comment_id))
.set(path.eq(ltree))
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await;
// Update the child count for the parent comment_aggregates
@ -111,7 +111,7 @@ where ca.comment_id = c.id"
);
sql_query(update_child_count_stmt)
.execute(&mut *conn)
.execute(conn)
.await?;
}
}
@ -121,14 +121,14 @@ where ca.comment_id = c.id"
}
}
pub async fn read_from_apub_id(
mut conn: impl DbConn,
mut conn: impl GetConn,
object_id: Url,
) -> Result<Option<Self>, Error> {
let object_id: DbUrl = object_id.into();
Ok(
comment
.filter(ap_id.eq(object_id))
.first::<Comment>(&mut *conn)
.first::<Comment>(conn)
.await
.ok()
.map(Into::into),
@ -152,29 +152,29 @@ impl Crud for Comment {
type InsertForm = CommentInsertForm;
type UpdateForm = CommentUpdateForm;
type IdType = CommentId;
async fn read(mut conn: impl DbConn, comment_id: CommentId) -> Result<Self, Error> {
comment.find(comment_id).first::<Self>(&mut *conn).await
async fn read(mut conn: impl GetConn, comment_id: CommentId) -> Result<Self, Error> {
comment.find(comment_id).first::<Self>(conn).await
}
async fn delete(mut conn: impl DbConn, comment_id: CommentId) -> Result<usize, Error> {
async fn delete(mut conn: impl GetConn, comment_id: CommentId) -> Result<usize, Error> {
diesel::delete(comment.find(comment_id))
.execute(&mut *conn)
.execute(conn)
.await
}
/// This is unimplemented, use [[Comment::create]]
async fn create(_conn: impl DbConn, _comment_form: &Self::InsertForm) -> Result<Self, Error> {
async fn create(_conn: impl GetConn, _comment_form: &Self::InsertForm) -> Result<Self, Error> {
unimplemented!();
}
async fn update(
mut conn: impl DbConn,
mut conn: impl GetConn,
comment_id: CommentId,
comment_form: &Self::UpdateForm,
) -> Result<Self, Error> {
diesel::update(comment.find(comment_id))
.set(comment_form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
}
@ -183,18 +183,21 @@ impl Crud for Comment {
impl Likeable for CommentLike {
type Form = CommentLikeForm;
type IdType = CommentId;
async fn like(mut conn: impl DbConn, comment_like_form: &CommentLikeForm) -> Result<Self, Error> {
async fn like(
mut conn: impl GetConn,
comment_like_form: &CommentLikeForm,
) -> Result<Self, Error> {
use crate::schema::comment_like::dsl::{comment_id, comment_like, person_id};
insert_into(comment_like)
.values(comment_like_form)
.on_conflict((comment_id, person_id))
.do_update()
.set(comment_like_form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn remove(
mut conn: impl DbConn,
mut conn: impl GetConn,
person_id_: PersonId,
comment_id_: CommentId,
) -> Result<usize, Error> {
@ -204,7 +207,7 @@ impl Likeable for CommentLike {
.filter(comment_id.eq(comment_id_))
.filter(person_id.eq(person_id_)),
)
.execute(&mut *conn)
.execute(conn)
.await
}
}
@ -213,7 +216,7 @@ impl Likeable for CommentLike {
impl Saveable for CommentSaved {
type Form = CommentSavedForm;
async fn save(
mut conn: impl DbConn,
mut conn: impl GetConn,
comment_saved_form: &CommentSavedForm,
) -> Result<Self, Error> {
use crate::schema::comment_saved::dsl::{comment_id, comment_saved, person_id};
@ -222,11 +225,11 @@ impl Saveable for CommentSaved {
.on_conflict((comment_id, person_id))
.do_update()
.set(comment_saved_form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn unsave(
mut conn: impl DbConn,
mut conn: impl GetConn,
comment_saved_form: &CommentSavedForm,
) -> Result<usize, Error> {
use crate::schema::comment_saved::dsl::{comment_id, comment_saved, person_id};
@ -235,7 +238,7 @@ impl Saveable for CommentSaved {
.filter(comment_id.eq(comment_saved_form.comment_id))
.filter(person_id.eq(comment_saved_form.person_id)),
)
.execute(&mut *conn)
.execute(conn)
.await
}
}
@ -270,7 +273,7 @@ mod tests {
async fn test_crud() {
let mut conn = build_db_conn_for_tests().await;
let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string())
let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string())
.await
.unwrap();
@ -280,7 +283,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_person = Person::create(&mut *conn, &new_person).await.unwrap();
let inserted_person = Person::create(conn, &new_person).await.unwrap();
let new_community = CommunityInsertForm::builder()
.name("test community".to_string())
@ -289,7 +292,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_community = Community::create(&mut *conn, &new_community).await.unwrap();
let inserted_community = Community::create(conn, &new_community).await.unwrap();
let new_post = PostInsertForm::builder()
.name("A test post".into())
@ -297,7 +300,7 @@ mod tests {
.community_id(inserted_community.id)
.build();
let inserted_post = Post::create(&mut *conn, &new_post).await.unwrap();
let inserted_post = Post::create(conn, &new_post).await.unwrap();
let comment_form = CommentInsertForm::builder()
.content("A test comment".into())
@ -305,7 +308,7 @@ mod tests {
.post_id(inserted_post.id)
.build();
let inserted_comment = Comment::create(&mut *conn, &comment_form, None)
let inserted_comment = Comment::create(conn, &comment_form, None)
.await
.unwrap();
@ -332,7 +335,7 @@ mod tests {
.build();
let inserted_child_comment = Comment::create(
&mut *conn,
conn,
&child_comment_form,
Some(&inserted_comment.path),
)
@ -347,7 +350,7 @@ mod tests {
score: 1,
};
let inserted_comment_like = CommentLike::like(&mut *conn, &comment_like_form)
let inserted_comment_like = CommentLike::like(conn, &comment_like_form)
.await
.unwrap();
@ -366,7 +369,7 @@ mod tests {
person_id: inserted_person.id,
};
let inserted_comment_saved = CommentSaved::save(&mut *conn, &comment_saved_form)
let inserted_comment_saved = CommentSaved::save(conn, &comment_saved_form)
.await
.unwrap();
@ -381,33 +384,33 @@ mod tests {
.content(Some("A test comment".into()))
.build();
let updated_comment = Comment::update(&mut *conn, inserted_comment.id, &comment_update_form)
let updated_comment = Comment::update(conn, inserted_comment.id, &comment_update_form)
.await
.unwrap();
let read_comment = Comment::read(&mut *conn, inserted_comment.id)
let read_comment = Comment::read(conn, inserted_comment.id)
.await
.unwrap();
let like_removed = CommentLike::remove(&mut *conn, inserted_person.id, inserted_comment.id)
let like_removed = CommentLike::remove(conn, inserted_person.id, inserted_comment.id)
.await
.unwrap();
let saved_removed = CommentSaved::unsave(&mut *conn, &comment_saved_form)
let saved_removed = CommentSaved::unsave(conn, &comment_saved_form)
.await
.unwrap();
let num_deleted = Comment::delete(&mut *conn, inserted_comment.id)
let num_deleted = Comment::delete(conn, inserted_comment.id)
.await
.unwrap();
Comment::delete(&mut *conn, inserted_child_comment.id)
Comment::delete(conn, inserted_child_comment.id)
.await
.unwrap();
Post::delete(&mut *conn, inserted_post.id).await.unwrap();
Community::delete(&mut *conn, inserted_community.id)
Post::delete(conn, inserted_post.id).await.unwrap();
Community::delete(conn, inserted_community.id)
.await
.unwrap();
Person::delete(&mut *conn, inserted_person.id)
Person::delete(conn, inserted_person.id)
.await
.unwrap();
Instance::delete(&mut *conn, inserted_instance.id)
Instance::delete(conn, inserted_instance.id)
.await
.unwrap();

View File

@ -3,25 +3,25 @@ use crate::{
schema::comment_reply::dsl::{comment_id, comment_reply, read, recipient_id},
source::comment_reply::{CommentReply, CommentReplyInsertForm, CommentReplyUpdateForm},
traits::Crud,
utils::DbConn,
utils::GetConn,
};
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::utils::RunQueryDsl;
#[async_trait]
impl Crud for CommentReply {
type InsertForm = CommentReplyInsertForm;
type UpdateForm = CommentReplyUpdateForm;
type IdType = CommentReplyId;
async fn read(mut conn: impl DbConn, comment_reply_id: CommentReplyId) -> Result<Self, Error> {
async fn read(mut conn: impl GetConn, comment_reply_id: CommentReplyId) -> Result<Self, Error> {
comment_reply
.find(comment_reply_id)
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await
}
async fn create(
mut conn: impl DbConn,
mut conn: impl GetConn,
comment_reply_form: &Self::InsertForm,
) -> Result<Self, Error> {
// since the return here isnt utilized, we dont need to do an update
@ -31,25 +31,25 @@ impl Crud for CommentReply {
.on_conflict((recipient_id, comment_id))
.do_update()
.set(comment_reply_form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn update(
mut conn: impl DbConn,
mut conn: impl GetConn,
comment_reply_id: CommentReplyId,
comment_reply_form: &Self::UpdateForm,
) -> Result<Self, Error> {
diesel::update(comment_reply.find(comment_reply_id))
.set(comment_reply_form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
}
impl CommentReply {
pub async fn mark_all_as_read(
mut conn: impl DbConn,
mut conn: impl GetConn,
for_recipient_id: PersonId,
) -> Result<Vec<CommentReply>, Error> {
diesel::update(
@ -58,17 +58,17 @@ impl CommentReply {
.filter(read.eq(false)),
)
.set(read.eq(true))
.get_results::<Self>(&mut *conn)
.get_results::<Self>(conn)
.await
}
pub async fn read_by_comment(
mut conn: impl DbConn,
mut conn: impl GetConn,
for_comment_id: CommentId,
) -> Result<Self, Error> {
comment_reply
.filter(comment_id.eq(for_comment_id))
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await
}
}
@ -94,7 +94,7 @@ mod tests {
async fn test_crud() {
let mut conn = build_db_conn_for_tests().await;
let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string())
let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string())
.await
.unwrap();
@ -104,7 +104,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_person = Person::create(&mut *conn, &new_person).await.unwrap();
let inserted_person = Person::create(conn, &new_person).await.unwrap();
let recipient_form = PersonInsertForm::builder()
.name("terrylakes recipient".into())
@ -112,7 +112,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_recipient = Person::create(&mut *conn, &recipient_form).await.unwrap();
let inserted_recipient = Person::create(conn, &recipient_form).await.unwrap();
let new_community = CommunityInsertForm::builder()
.name("test community lake".to_string())
@ -121,7 +121,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_community = Community::create(&mut *conn, &new_community).await.unwrap();
let inserted_community = Community::create(conn, &new_community).await.unwrap();
let new_post = PostInsertForm::builder()
.name("A test post".into())
@ -129,7 +129,7 @@ mod tests {
.community_id(inserted_community.id)
.build();
let inserted_post = Post::create(&mut *conn, &new_post).await.unwrap();
let inserted_post = Post::create(conn, &new_post).await.unwrap();
let comment_form = CommentInsertForm::builder()
.content("A test comment".into())
@ -137,7 +137,7 @@ mod tests {
.post_id(inserted_post.id)
.build();
let inserted_comment = Comment::create(&mut *conn, &comment_form, None)
let inserted_comment = Comment::create(conn, &comment_form, None)
.await
.unwrap();
@ -147,7 +147,7 @@ mod tests {
read: None,
};
let inserted_reply = CommentReply::create(&mut *conn, &comment_reply_form)
let inserted_reply = CommentReply::create(conn, &comment_reply_form)
.await
.unwrap();
@ -159,30 +159,30 @@ mod tests {
published: inserted_reply.published,
};
let read_reply = CommentReply::read(&mut *conn, inserted_reply.id)
let read_reply = CommentReply::read(conn, inserted_reply.id)
.await
.unwrap();
let comment_reply_update_form = CommentReplyUpdateForm { read: Some(false) };
let updated_reply =
CommentReply::update(&mut *conn, inserted_reply.id, &comment_reply_update_form)
CommentReply::update(conn, inserted_reply.id, &comment_reply_update_form)
.await
.unwrap();
Comment::delete(&mut *conn, inserted_comment.id)
Comment::delete(conn, inserted_comment.id)
.await
.unwrap();
Post::delete(&mut *conn, inserted_post.id).await.unwrap();
Community::delete(&mut *conn, inserted_community.id)
Post::delete(conn, inserted_post.id).await.unwrap();
Community::delete(conn, inserted_community.id)
.await
.unwrap();
Person::delete(&mut *conn, inserted_person.id)
Person::delete(conn, inserted_person.id)
.await
.unwrap();
Person::delete(&mut *conn, inserted_recipient.id)
Person::delete(conn, inserted_recipient.id)
.await
.unwrap();
Instance::delete(&mut *conn, inserted_instance.id)
Instance::delete(conn, inserted_instance.id)
.await
.unwrap();

View File

@ -3,7 +3,7 @@ use crate::{
schema::comment_report::dsl::{comment_report, resolved, resolver_id, updated},
source::comment_report::{CommentReport, CommentReportForm},
traits::Reportable,
utils::{naive_now, DbConn},
utils::{naive_now, GetConn},
};
use diesel::{
dsl::{insert_into, update},
@ -11,7 +11,7 @@ use diesel::{
ExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::utils::RunQueryDsl;
#[async_trait]
impl Reportable for CommentReport {
@ -22,12 +22,12 @@ impl Reportable for CommentReport {
/// * `conn` - the postgres connection
/// * `comment_report_form` - the filled CommentReportForm to insert
async fn report(
mut conn: impl DbConn,
mut conn: impl GetConn,
comment_report_form: &CommentReportForm,
) -> Result<Self, Error> {
insert_into(comment_report)
.values(comment_report_form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
@ -37,7 +37,7 @@ impl Reportable for CommentReport {
/// * `report_id` - the id of the report to resolve
/// * `by_resolver_id` - the id of the user resolving the report
async fn resolve(
mut conn: impl DbConn,
mut conn: impl GetConn,
report_id_: Self::IdType,
by_resolver_id: PersonId,
) -> Result<usize, Error> {
@ -47,7 +47,7 @@ impl Reportable for CommentReport {
resolver_id.eq(by_resolver_id),
updated.eq(naive_now()),
))
.execute(&mut *conn)
.execute(conn)
.await
}
@ -57,7 +57,7 @@ impl Reportable for CommentReport {
/// * `report_id` - the id of the report to unresolve
/// * `by_resolver_id` - the id of the user unresolving the report
async fn unresolve(
mut conn: impl DbConn,
mut conn: impl GetConn,
report_id_: Self::IdType,
by_resolver_id: PersonId,
) -> Result<usize, Error> {
@ -67,7 +67,7 @@ impl Reportable for CommentReport {
resolver_id.eq(by_resolver_id),
updated.eq(naive_now()),
))
.execute(&mut *conn)
.execute(conn)
.await
}
}

View File

@ -16,33 +16,33 @@ use crate::{
},
},
traits::{ApubActor, Bannable, Crud, Followable, Joinable},
utils::{functions::lower, DbConn},
utils::{functions::lower, GetConn},
SubscribedType,
};
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::utils::RunQueryDsl;
#[async_trait]
impl Crud for Community {
type InsertForm = CommunityInsertForm;
type UpdateForm = CommunityUpdateForm;
type IdType = CommunityId;
async fn read(mut conn: impl DbConn, community_id: CommunityId) -> Result<Self, Error> {
async fn read(mut conn: impl GetConn, community_id: CommunityId) -> Result<Self, Error> {
community::table
.find(community_id)
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await
}
async fn delete(mut conn: impl DbConn, community_id: CommunityId) -> Result<usize, Error> {
async fn delete(mut conn: impl GetConn, community_id: CommunityId) -> Result<usize, Error> {
diesel::delete(community::table.find(community_id))
.execute(&mut *conn)
.execute(conn)
.await
}
async fn create(mut conn: impl DbConn, form: &Self::InsertForm) -> Result<Self, Error> {
async fn create(mut conn: impl GetConn, form: &Self::InsertForm) -> Result<Self, Error> {
let is_new_community = match &form.actor_id {
Some(id) => Community::read_from_apub_id(&mut *conn, id)
Some(id) => Community::read_from_apub_id(conn, id)
.await?
.is_none(),
None => true,
@ -54,25 +54,25 @@ impl Crud for Community {
.on_conflict(community::actor_id)
.do_update()
.set(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await?;
// Initialize languages for new community
if is_new_community {
CommunityLanguage::update(&mut *conn, vec![], community_.id).await?;
CommunityLanguage::update(conn, vec![], community_.id).await?;
}
Ok(community_)
}
async fn update(
mut conn: impl DbConn,
mut conn: impl GetConn,
community_id: CommunityId,
form: &Self::UpdateForm,
) -> Result<Self, Error> {
diesel::update(community::table.find(community_id))
.set(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
}
@ -81,18 +81,18 @@ impl Crud for Community {
impl Joinable for CommunityModerator {
type Form = CommunityModeratorForm;
async fn join(
mut conn: impl DbConn,
mut conn: impl GetConn,
community_moderator_form: &CommunityModeratorForm,
) -> Result<Self, Error> {
use crate::schema::community_moderator::dsl::community_moderator;
insert_into(community_moderator)
.values(community_moderator_form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn leave(
mut conn: impl DbConn,
mut conn: impl GetConn,
community_moderator_form: &CommunityModeratorForm,
) -> Result<usize, Error> {
use crate::schema::community_moderator::dsl::{community_id, community_moderator, person_id};
@ -101,7 +101,7 @@ impl Joinable for CommunityModerator {
.filter(community_id.eq(community_moderator_form.community_id))
.filter(person_id.eq(community_moderator_form.person_id)),
)
.execute(&mut *conn)
.execute(conn)
.await
}
}
@ -114,21 +114,21 @@ pub enum CollectionType {
impl Community {
/// Get the community which has a given moderators or featured url, also return the collection type
pub async fn get_by_collection_url(
mut conn: impl DbConn,
mut conn: impl GetConn,
url: &DbUrl,
) -> Result<(Community, CollectionType), Error> {
use crate::schema::community::dsl::{featured_url, moderators_url};
use CollectionType::*;
let res = community::table
.filter(moderators_url.eq(url))
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await;
if let Ok(c) = res {
return Ok((c, Moderators));
}
let res = community::table
.filter(featured_url.eq(url))
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await;
if let Ok(c) = res {
return Ok((c, Featured));
@ -139,35 +139,35 @@ impl Community {
impl CommunityModerator {
pub async fn delete_for_community(
mut conn: impl DbConn,
mut conn: impl GetConn,
for_community_id: CommunityId,
) -> Result<usize, Error> {
use crate::schema::community_moderator::dsl::{community_id, community_moderator};
diesel::delete(community_moderator.filter(community_id.eq(for_community_id)))
.execute(&mut *conn)
.execute(conn)
.await
}
pub async fn leave_all_communities(
mut conn: impl DbConn,
mut conn: impl GetConn,
for_person_id: PersonId,
) -> Result<usize, Error> {
use crate::schema::community_moderator::dsl::{community_moderator, person_id};
diesel::delete(community_moderator.filter(person_id.eq(for_person_id)))
.execute(&mut *conn)
.execute(conn)
.await
}
pub async fn get_person_moderated_communities(
mut conn: impl DbConn,
mut conn: impl GetConn,
for_person_id: PersonId,
) -> Result<Vec<CommunityId>, Error> {
use crate::schema::community_moderator::dsl::{community_id, community_moderator, person_id};
community_moderator
.filter(person_id.eq(for_person_id))
.select(community_id)
.load::<CommunityId>(&mut *conn)
.load::<CommunityId>(conn)
.await
}
}
@ -176,7 +176,7 @@ impl CommunityModerator {
impl Bannable for CommunityPersonBan {
type Form = CommunityPersonBanForm;
async fn ban(
mut conn: impl DbConn,
mut conn: impl GetConn,
community_person_ban_form: &CommunityPersonBanForm,
) -> Result<Self, Error> {
use crate::schema::community_person_ban::dsl::{community_id, community_person_ban, person_id};
@ -185,12 +185,12 @@ impl Bannable for CommunityPersonBan {
.on_conflict((community_id, person_id))
.do_update()
.set(community_person_ban_form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn unban(
mut conn: impl DbConn,
mut conn: impl GetConn,
community_person_ban_form: &CommunityPersonBanForm,
) -> Result<usize, Error> {
use crate::schema::community_person_ban::dsl::{community_id, community_person_ban, person_id};
@ -199,7 +199,7 @@ impl Bannable for CommunityPersonBan {
.filter(community_id.eq(community_person_ban_form.community_id))
.filter(person_id.eq(community_person_ban_form.person_id)),
)
.execute(&mut *conn)
.execute(conn)
.await
}
}
@ -223,18 +223,18 @@ impl CommunityFollower {
#[async_trait]
impl Followable for CommunityFollower {
type Form = CommunityFollowerForm;
async fn follow(mut conn: impl DbConn, form: &CommunityFollowerForm) -> Result<Self, Error> {
async fn follow(mut conn: impl GetConn, form: &CommunityFollowerForm) -> Result<Self, Error> {
use crate::schema::community_follower::dsl::{community_follower, community_id, person_id};
insert_into(community_follower)
.values(form)
.on_conflict((community_id, person_id))
.do_update()
.set(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn follow_accepted(
mut conn: impl DbConn,
mut conn: impl GetConn,
community_id_: CommunityId,
person_id_: PersonId,
) -> Result<Self, Error> {
@ -250,17 +250,17 @@ impl Followable for CommunityFollower {
.filter(person_id.eq(person_id_)),
)
.set(pending.eq(false))
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn unfollow(mut conn: impl DbConn, form: &CommunityFollowerForm) -> Result<usize, Error> {
async fn unfollow(mut conn: impl GetConn, form: &CommunityFollowerForm) -> Result<usize, Error> {
use crate::schema::community_follower::dsl::{community_follower, community_id, person_id};
diesel::delete(
community_follower
.filter(community_id.eq(&form.community_id))
.filter(person_id.eq(&form.person_id)),
)
.execute(&mut *conn)
.execute(conn)
.await
}
}
@ -268,13 +268,13 @@ impl Followable for CommunityFollower {
#[async_trait]
impl ApubActor for Community {
async fn read_from_apub_id(
mut conn: impl DbConn,
mut conn: impl GetConn,
object_id: &DbUrl,
) -> Result<Option<Self>, Error> {
Ok(
community::table
.filter(community::actor_id.eq(object_id))
.first::<Community>(&mut *conn)
.first::<Community>(conn)
.await
.ok()
.map(Into::into),
@ -282,7 +282,7 @@ impl ApubActor for Community {
}
async fn read_from_name(
mut conn: impl DbConn,
mut conn: impl GetConn,
community_name: &str,
include_deleted: bool,
) -> Result<Community, Error> {
@ -295,11 +295,11 @@ impl ApubActor for Community {
.filter(community::deleted.eq(false))
.filter(community::removed.eq(false));
}
q.first::<Self>(&mut *conn).await
q.first::<Self>(conn).await
}
async fn read_from_name_and_domain(
mut conn: impl DbConn,
mut conn: impl GetConn,
community_name: &str,
for_domain: &str,
) -> Result<Community, Error> {
@ -308,7 +308,7 @@ impl ApubActor for Community {
.filter(lower(community::name).eq(community_name.to_lowercase()))
.filter(instance::domain.eq(for_domain))
.select(community::all_columns)
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await
}
}
@ -341,7 +341,7 @@ mod tests {
async fn test_crud() {
let mut conn = build_db_conn_for_tests().await;
let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string())
let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string())
.await
.unwrap();
@ -351,7 +351,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_person = Person::create(&mut *conn, &new_person).await.unwrap();
let inserted_person = Person::create(conn, &new_person).await.unwrap();
let new_community = CommunityInsertForm::builder()
.name("TIL".into())
@ -360,7 +360,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_community = Community::create(&mut *conn, &new_community).await.unwrap();
let inserted_community = Community::create(conn, &new_community).await.unwrap();
let expected_community = Community {
id: inserted_community.id,
@ -396,7 +396,7 @@ mod tests {
};
let inserted_community_follower =
CommunityFollower::follow(&mut *conn, &community_follower_form)
CommunityFollower::follow(conn, &community_follower_form)
.await
.unwrap();
@ -414,7 +414,7 @@ mod tests {
};
let inserted_community_moderator =
CommunityModerator::join(&mut *conn, &community_moderator_form)
CommunityModerator::join(conn, &community_moderator_form)
.await
.unwrap();
@ -432,7 +432,7 @@ mod tests {
};
let inserted_community_person_ban =
CommunityPersonBan::ban(&mut *conn, &community_person_ban_form)
CommunityPersonBan::ban(conn, &community_person_ban_form)
.await
.unwrap();
@ -444,7 +444,7 @@ mod tests {
expires: None,
};
let read_community = Community::read(&mut *conn, inserted_community.id)
let read_community = Community::read(conn, inserted_community.id)
.await
.unwrap();
@ -452,26 +452,26 @@ mod tests {
.title(Some("nada".to_owned()))
.build();
let updated_community =
Community::update(&mut *conn, inserted_community.id, &update_community_form)
Community::update(conn, inserted_community.id, &update_community_form)
.await
.unwrap();
let ignored_community = CommunityFollower::unfollow(&mut *conn, &community_follower_form)
let ignored_community = CommunityFollower::unfollow(conn, &community_follower_form)
.await
.unwrap();
let left_community = CommunityModerator::leave(&mut *conn, &community_moderator_form)
let left_community = CommunityModerator::leave(conn, &community_moderator_form)
.await
.unwrap();
let unban = CommunityPersonBan::unban(&mut *conn, &community_person_ban_form)
let unban = CommunityPersonBan::unban(conn, &community_person_ban_form)
.await
.unwrap();
let num_deleted = Community::delete(&mut *conn, inserted_community.id)
let num_deleted = Community::delete(conn, inserted_community.id)
.await
.unwrap();
Person::delete(&mut *conn, inserted_person.id)
Person::delete(conn, inserted_person.id)
.await
.unwrap();
Instance::delete(&mut *conn, inserted_instance.id)
Instance::delete(conn, inserted_instance.id)
.await
.unwrap();

View File

@ -2,25 +2,25 @@ use crate::{
schema::community_block::dsl::{community_block, community_id, person_id},
source::community_block::{CommunityBlock, CommunityBlockForm},
traits::Blockable,
utils::DbConn,
utils::GetConn,
};
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::utils::RunQueryDsl;
#[async_trait]
impl Blockable for CommunityBlock {
type Form = CommunityBlockForm;
async fn block(mut conn: impl DbConn, community_block_form: &Self::Form) -> Result<Self, Error> {
async fn block(mut conn: impl GetConn, community_block_form: &Self::Form) -> Result<Self, Error> {
insert_into(community_block)
.values(community_block_form)
.on_conflict((person_id, community_id))
.do_update()
.set(community_block_form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn unblock(
mut conn: impl DbConn,
mut conn: impl GetConn,
community_block_form: &Self::Form,
) -> Result<usize, Error> {
diesel::delete(
@ -28,7 +28,7 @@ impl Blockable for CommunityBlock {
.filter(person_id.eq(community_block_form.person_id))
.filter(community_id.eq(community_block_form.community_id)),
)
.execute(&mut *conn)
.execute(conn)
.await
}
}

View File

@ -8,48 +8,48 @@ use crate::{
custom_emoji::{CustomEmoji, CustomEmojiInsertForm, CustomEmojiUpdateForm},
custom_emoji_keyword::{CustomEmojiKeyword, CustomEmojiKeywordInsertForm},
},
utils::DbConn,
utils::GetConn,
};
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::utils::RunQueryDsl;
impl CustomEmoji {
pub async fn create(mut conn: impl DbConn, form: &CustomEmojiInsertForm) -> Result<Self, Error> {
pub async fn create(mut conn: impl GetConn, form: &CustomEmojiInsertForm) -> Result<Self, Error> {
insert_into(custom_emoji)
.values(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
pub async fn update(
mut conn: impl DbConn,
mut conn: impl GetConn,
emoji_id: CustomEmojiId,
form: &CustomEmojiUpdateForm,
) -> Result<Self, Error> {
diesel::update(custom_emoji.find(emoji_id))
.set(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
pub async fn delete(mut conn: impl DbConn, emoji_id: CustomEmojiId) -> Result<usize, Error> {
pub async fn delete(mut conn: impl GetConn, emoji_id: CustomEmojiId) -> Result<usize, Error> {
diesel::delete(custom_emoji.find(emoji_id))
.execute(&mut *conn)
.execute(conn)
.await
}
}
impl CustomEmojiKeyword {
pub async fn create(
mut conn: impl DbConn,
mut conn: impl GetConn,
form: Vec<CustomEmojiKeywordInsertForm>,
) -> Result<Vec<Self>, Error> {
insert_into(custom_emoji_keyword)
.values(form)
.get_results::<Self>(&mut *conn)
.get_results::<Self>(conn)
.await
}
pub async fn delete(mut conn: impl DbConn, emoji_id: CustomEmojiId) -> Result<usize, Error> {
pub async fn delete(mut conn: impl GetConn, emoji_id: CustomEmojiId) -> Result<usize, Error> {
diesel::delete(custom_emoji_keyword.filter(custom_emoji_id.eq(emoji_id)))
.execute(&mut *conn)
.execute(conn)
.await
}
}

View File

@ -7,7 +7,7 @@ use crate::{
verification_token,
},
source::email_verification::{EmailVerification, EmailVerificationForm},
utils::DbConn,
utils::GetConn,
};
use diesel::{
dsl::{now, IntervalDsl},
@ -16,29 +16,29 @@ use diesel::{
ExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::utils::RunQueryDsl;
impl EmailVerification {
pub async fn create(mut conn: impl DbConn, form: &EmailVerificationForm) -> Result<Self, Error> {
pub async fn create(mut conn: impl GetConn, form: &EmailVerificationForm) -> Result<Self, Error> {
insert_into(email_verification)
.values(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
pub async fn read_for_token(mut conn: impl DbConn, token: &str) -> Result<Self, Error> {
pub async fn read_for_token(mut conn: impl GetConn, token: &str) -> Result<Self, Error> {
email_verification
.filter(verification_token.eq(token))
.filter(published.gt(now - 7.days()))
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await
}
pub async fn delete_old_tokens_for_local_user(
mut conn: impl DbConn,
mut conn: impl GetConn,
local_user_id_: LocalUserId,
) -> Result<usize, Error> {
diesel::delete(email_verification.filter(local_user_id.eq(local_user_id_)))
.execute(&mut *conn)
.execute(conn)
.await
}
}

View File

@ -4,23 +4,23 @@ use crate::{
federation_allowlist::{FederationAllowList, FederationAllowListForm},
instance::Instance,
},
utils::DbConn,
utils::GetConn,
};
use diesel::{dsl::insert_into, result::Error};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::utils::RunQueryDsl;
impl FederationAllowList {
pub async fn replace(mut conn: impl DbConn, list_opt: Option<Vec<String>>) -> Result<(), Error> {
pub async fn replace(mut conn: impl GetConn, list_opt: Option<Vec<String>>) -> Result<(), Error> {
conn
.build_transaction()
.run(|conn| {
Box::pin(async move {
if let Some(list) = list_opt {
Self::clear(&mut *conn).await?;
Self::clear(conn).await?;
for domain in list {
// Upsert all of these as instances
let instance = Instance::read_or_create_with_conn(&mut *conn, domain).await?;
let instance = Instance::read_or_create_with_conn(conn, domain).await?;
let form = FederationAllowListForm {
instance_id: instance.id,
@ -28,7 +28,7 @@ impl FederationAllowList {
};
insert_into(federation_allowlist::table)
.values(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await?;
}
Ok(())
@ -40,9 +40,9 @@ impl FederationAllowList {
.await
}
async fn clear(mut conn: impl DbConn) -> Result<usize, Error> {
async fn clear(mut conn: impl GetConn) -> Result<usize, Error> {
diesel::delete(federation_allowlist::table)
.execute(&mut *conn)
.execute(conn)
.await
}
}
@ -66,11 +66,11 @@ mod tests {
let allowed = Some(domains.clone());
FederationAllowList::replace(&mut *conn, allowed)
FederationAllowList::replace(conn, allowed)
.await
.unwrap();
let allows = Instance::allowlist(&mut *conn).await.unwrap();
let allows = Instance::allowlist(conn).await.unwrap();
let allows_domains = allows
.iter()
.map(|i| i.domain.clone())
@ -82,13 +82,13 @@ mod tests {
// Now test clearing them via Some(empty vec)
let clear_allows = Some(Vec::new());
FederationAllowList::replace(&mut *conn, clear_allows)
FederationAllowList::replace(conn, clear_allows)
.await
.unwrap();
let allows = Instance::allowlist(&mut *conn).await.unwrap();
let allows = Instance::allowlist(conn).await.unwrap();
assert_eq!(0, allows.len());
Instance::delete_all(&mut *conn).await.unwrap();
Instance::delete_all(conn).await.unwrap();
}
}

View File

@ -4,23 +4,23 @@ use crate::{
federation_blocklist::{FederationBlockList, FederationBlockListForm},
instance::Instance,
},
utils::DbConn,
utils::GetConn,
};
use diesel::{dsl::insert_into, result::Error};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::utils::RunQueryDsl;
impl FederationBlockList {
pub async fn replace(mut conn: impl DbConn, list_opt: Option<Vec<String>>) -> Result<(), Error> {
pub async fn replace(mut conn: impl GetConn, list_opt: Option<Vec<String>>) -> Result<(), Error> {
conn
.build_transaction()
.run(|conn| {
Box::pin(async move {
if let Some(list) = list_opt {
Self::clear(&mut *conn).await?;
Self::clear(conn).await?;
for domain in list {
// Upsert all of these as instances
let instance = Instance::read_or_create_with_conn(&mut *conn, domain).await?;
let instance = Instance::read_or_create_with_conn(conn, domain).await?;
let form = FederationBlockListForm {
instance_id: instance.id,
@ -28,7 +28,7 @@ impl FederationBlockList {
};
insert_into(federation_blocklist::table)
.values(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await?;
}
Ok(())
@ -40,9 +40,9 @@ impl FederationBlockList {
.await
}
async fn clear(mut conn: impl DbConn) -> Result<usize, Error> {
async fn clear(mut conn: impl GetConn) -> Result<usize, Error> {
diesel::delete(federation_blocklist::table)
.execute(&mut *conn)
.execute(conn)
.await
}
}

View File

@ -2,21 +2,21 @@ use crate::{
newtypes::InstanceId,
schema::{federation_allowlist, federation_blocklist, instance},
source::instance::{Instance, InstanceForm},
utils::{naive_now, DbConn},
utils::{naive_now, GetConn},
};
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::utils::RunQueryDsl;
impl Instance {
pub(crate) async fn read_or_create_with_conn(
mut conn: impl DbConn,
mut conn: impl GetConn,
domain_: String,
) -> Result<Self, Error> {
use crate::schema::instance::domain;
// First try to read the instance row and return directly if found
let instance = instance::table
.filter(domain.eq(&domain_))
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await;
match instance {
Ok(i) => Ok(i),
@ -33,7 +33,7 @@ impl Instance {
.on_conflict(instance::domain)
.do_update()
.set(&form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
e => e,
@ -42,40 +42,40 @@ impl Instance {
/// Attempt to read Instance column for the given domain. If it doesnt exist, insert a new one.
/// There is no need for update as the domain of an existing instance cant change.
pub async fn read_or_create(mut conn: impl DbConn, domain: String) -> Result<Self, Error> {
Self::read_or_create_with_conn(&mut *conn, domain).await
pub async fn read_or_create(mut conn: impl GetConn, domain: String) -> Result<Self, Error> {
Self::read_or_create_with_conn(conn, domain).await
}
pub async fn delete(mut conn: impl DbConn, instance_id: InstanceId) -> Result<usize, Error> {
pub async fn delete(mut conn: impl GetConn, instance_id: InstanceId) -> Result<usize, Error> {
diesel::delete(instance::table.find(instance_id))
.execute(&mut *conn)
.execute(conn)
.await
}
#[cfg(test)]
pub async fn delete_all(mut conn: impl DbConn) -> Result<usize, Error> {
diesel::delete(instance::table).execute(&mut *conn).await
pub async fn delete_all(mut conn: impl GetConn) -> Result<usize, Error> {
diesel::delete(instance::table).execute(conn).await
}
pub async fn allowlist(mut conn: impl DbConn) -> Result<Vec<Self>, Error> {
pub async fn allowlist(mut conn: impl GetConn) -> Result<Vec<Self>, Error> {
instance::table
.inner_join(federation_allowlist::table)
.select(instance::all_columns)
.get_results(&mut *conn)
.get_results(conn)
.await
}
pub async fn blocklist(mut conn: impl DbConn) -> Result<Vec<Self>, Error> {
pub async fn blocklist(mut conn: impl GetConn) -> Result<Vec<Self>, Error> {
instance::table
.inner_join(federation_blocklist::table)
.select(instance::all_columns)
.get_results(&mut *conn)
.get_results(conn)
.await
}
pub async fn linked(mut conn: impl DbConn) -> Result<Vec<Self>, Error> {
pub async fn linked(mut conn: impl GetConn) -> Result<Vec<Self>, Error> {
instance::table
.left_join(federation_blocklist::table)
.filter(federation_blocklist::id.is_null())
.select(instance::all_columns)
.get_results(&mut *conn)
.get_results(conn)
.await
}
}

View File

@ -3,34 +3,34 @@ use crate::{
newtypes::LanguageId,
schema::language::dsl::{code, id, language},
source::language::Language,
utils::DbConn,
utils::GetConn,
};
use diesel::{result::Error, QueryDsl};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::utils::RunQueryDsl;
impl Language {
pub async fn read_all(mut conn: impl DbConn) -> Result<Vec<Language>, Error> {
Self::read_all_conn(&mut *conn).await
pub async fn read_all(mut conn: impl GetConn) -> Result<Vec<Language>, Error> {
Self::read_all_conn(conn).await
}
pub async fn read_all_conn(mut conn: impl DbConn) -> Result<Vec<Language>, Error> {
language.load::<Self>(&mut *conn).await
pub async fn read_all_conn(mut conn: impl GetConn) -> Result<Vec<Language>, Error> {
language.load::<Self>(conn).await
}
pub async fn read_from_id(mut conn: impl DbConn, id_: LanguageId) -> Result<Language, Error> {
language.filter(id.eq(id_)).first::<Self>(&mut *conn).await
pub async fn read_from_id(mut conn: impl GetConn, id_: LanguageId) -> Result<Language, Error> {
language.filter(id.eq(id_)).first::<Self>(conn).await
}
/// Attempts to find the given language code and return its ID. If not found, returns none.
pub async fn read_id_from_code(
mut conn: impl DbConn,
mut conn: impl GetConn,
code_: Option<&str>,
) -> Result<Option<LanguageId>, Error> {
if let Some(code_) = code_ {
Ok(
language
.filter(code.eq(code_))
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await
.map(|l| l.id)
.ok(),
@ -51,7 +51,7 @@ mod tests {
async fn test_languages() {
let mut conn = build_db_conn_for_tests().await;
let all = Language::read_all(&mut *conn).await.unwrap();
let all = Language::read_all(conn).await.unwrap();
assert_eq!(184, all.len());
assert_eq!("ak", all[5].code);

View File

@ -1,28 +1,28 @@
use crate::{
schema::local_site::dsl::local_site,
source::local_site::{LocalSite, LocalSiteInsertForm, LocalSiteUpdateForm},
utils::DbConn,
utils::GetConn,
};
use diesel::{dsl::insert_into, result::Error};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::utils::RunQueryDsl;
impl LocalSite {
pub async fn create(mut conn: impl DbConn, form: &LocalSiteInsertForm) -> Result<Self, Error> {
pub async fn create(mut conn: impl GetConn, form: &LocalSiteInsertForm) -> Result<Self, Error> {
insert_into(local_site)
.values(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
pub async fn read(mut conn: impl DbConn) -> Result<Self, Error> {
local_site.first::<Self>(&mut *conn).await
pub async fn read(mut conn: impl GetConn) -> Result<Self, Error> {
local_site.first::<Self>(conn).await
}
pub async fn update(mut conn: impl DbConn, form: &LocalSiteUpdateForm) -> Result<Self, Error> {
pub async fn update(mut conn: impl GetConn, form: &LocalSiteUpdateForm) -> Result<Self, Error> {
diesel::update(local_site)
.set(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
pub async fn delete(mut conn: impl DbConn) -> Result<usize, Error> {
diesel::delete(local_site).execute(&mut *conn).await
pub async fn delete(mut conn: impl GetConn) -> Result<usize, Error> {
diesel::delete(local_site).execute(conn).await
}
}

View File

@ -5,27 +5,27 @@ use crate::{
LocalSiteRateLimitInsertForm,
LocalSiteRateLimitUpdateForm,
},
utils::DbConn,
utils::GetConn,
};
use diesel::{dsl::insert_into, result::Error};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::utils::RunQueryDsl;
impl LocalSiteRateLimit {
pub async fn read(mut conn: impl DbConn) -> Result<Self, Error> {
local_site_rate_limit::table.first::<Self>(&mut *conn).await
pub async fn read(mut conn: impl GetConn) -> Result<Self, Error> {
local_site_rate_limit::table.first::<Self>(conn).await
}
pub async fn create(
mut conn: impl DbConn,
mut conn: impl GetConn,
form: &LocalSiteRateLimitInsertForm,
) -> Result<Self, Error> {
insert_into(local_site_rate_limit::table)
.values(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
pub async fn update(
mut conn: impl DbConn,
mut conn: impl GetConn,
form: &LocalSiteRateLimitUpdateForm,
) -> Result<(), Error> {
// avoid error "There are no changes to save. This query cannot be built"
@ -34,7 +34,7 @@ impl LocalSiteRateLimit {
}
diesel::update(local_site_rate_limit::table)
.set(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await?;
Ok(())
}

View File

@ -13,15 +13,15 @@ use crate::{
local_user::{LocalUser, LocalUserInsertForm, LocalUserUpdateForm},
},
traits::Crud,
utils::{naive_now, DbConn},
utils::{naive_now, GetConn},
};
use bcrypt::{hash, DEFAULT_COST};
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::utils::RunQueryDsl;
impl LocalUser {
pub async fn update_password(
mut conn: impl DbConn,
mut conn: impl GetConn,
local_user_id: LocalUserId,
new_password: &str,
) -> Result<Self, Error> {
@ -32,30 +32,30 @@ impl LocalUser {
password_encrypted.eq(password_hash),
validator_time.eq(naive_now()),
))
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
pub async fn set_all_users_email_verified(mut conn: impl DbConn) -> Result<Vec<Self>, Error> {
pub async fn set_all_users_email_verified(mut conn: impl GetConn) -> Result<Vec<Self>, Error> {
diesel::update(local_user)
.set(email_verified.eq(true))
.get_results::<Self>(&mut *conn)
.get_results::<Self>(conn)
.await
}
pub async fn set_all_users_registration_applications_accepted(
mut conn: impl DbConn,
mut conn: impl GetConn,
) -> Result<Vec<Self>, Error> {
diesel::update(local_user)
.set(accepted_application.eq(true))
.get_results::<Self>(&mut *conn)
.get_results::<Self>(conn)
.await
}
pub async fn is_email_taken(mut conn: impl DbConn, email_: &str) -> Result<bool, Error> {
pub async fn is_email_taken(mut conn: impl GetConn, email_: &str) -> Result<bool, Error> {
use diesel::dsl::{exists, select};
select(exists(local_user.filter(email.eq(email_))))
.get_result(&mut *conn)
.get_result(conn)
.await
}
}
@ -65,18 +65,18 @@ impl Crud for LocalUser {
type InsertForm = LocalUserInsertForm;
type UpdateForm = LocalUserUpdateForm;
type IdType = LocalUserId;
async fn read(mut conn: impl DbConn, local_user_id: LocalUserId) -> Result<Self, Error> {
async fn read(mut conn: impl GetConn, local_user_id: LocalUserId) -> Result<Self, Error> {
local_user
.find(local_user_id)
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await
}
async fn delete(mut conn: impl DbConn, local_user_id: LocalUserId) -> Result<usize, Error> {
async fn delete(mut conn: impl GetConn, local_user_id: LocalUserId) -> Result<usize, Error> {
diesel::delete(local_user.find(local_user_id))
.execute(&mut *conn)
.execute(conn)
.await
}
async fn create(mut conn: impl DbConn, form: &Self::InsertForm) -> Result<Self, Error> {
async fn create(mut conn: impl GetConn, form: &Self::InsertForm) -> Result<Self, Error> {
let mut form_with_encrypted_password = form.clone();
let password_hash =
hash(&form.password_encrypted, DEFAULT_COST).expect("Couldn't hash password");
@ -84,29 +84,29 @@ impl Crud for LocalUser {
let local_user_ = insert_into(local_user)
.values(form_with_encrypted_password)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await?;
let site_languages = SiteLanguage::read_local_raw(&mut *conn).await;
let site_languages = SiteLanguage::read_local_raw(conn).await;
if let Ok(langs) = site_languages {
// if site exists, init user with site languages
LocalUserLanguage::update(&mut *conn, langs, local_user_.id).await?;
LocalUserLanguage::update(conn, langs, local_user_.id).await?;
} else {
// otherwise, init with all languages (this only happens during tests and
// for first admin user, which is created before site)
LocalUserLanguage::update(&mut *conn, vec![], local_user_.id).await?;
LocalUserLanguage::update(conn, vec![], local_user_.id).await?;
}
Ok(local_user_)
}
async fn update(
mut conn: impl DbConn,
mut conn: impl GetConn,
local_user_id: LocalUserId,
form: &Self::UpdateForm,
) -> Result<Self, Error> {
diesel::update(local_user.find(local_user_id))
.set(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
}

View File

@ -32,41 +32,41 @@ use crate::{
ModTransferCommunityForm,
},
traits::Crud,
utils::DbConn,
utils::GetConn,
};
use diesel::{dsl::insert_into, result::Error, QueryDsl};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::utils::RunQueryDsl;
#[async_trait]
impl Crud for ModRemovePost {
type InsertForm = ModRemovePostForm;
type UpdateForm = ModRemovePostForm;
type IdType = i32;
async fn read(mut conn: impl DbConn, from_id: i32) -> Result<Self, Error> {
async fn read(mut conn: impl GetConn, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_remove_post::dsl::mod_remove_post;
mod_remove_post
.find(from_id)
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await
}
async fn create(mut conn: impl DbConn, form: &ModRemovePostForm) -> Result<Self, Error> {
async fn create(mut conn: impl GetConn, form: &ModRemovePostForm) -> Result<Self, Error> {
use crate::schema::mod_remove_post::dsl::mod_remove_post;
insert_into(mod_remove_post)
.values(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn update(
mut conn: impl DbConn,
mut conn: impl GetConn,
from_id: i32,
form: &ModRemovePostForm,
) -> Result<Self, Error> {
use crate::schema::mod_remove_post::dsl::mod_remove_post;
diesel::update(mod_remove_post.find(from_id))
.set(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
}
@ -76,28 +76,28 @@ impl Crud for ModLockPost {
type InsertForm = ModLockPostForm;
type UpdateForm = ModLockPostForm;
type IdType = i32;
async fn read(mut conn: impl DbConn, from_id: i32) -> Result<Self, Error> {
async fn read(mut conn: impl GetConn, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_lock_post::dsl::mod_lock_post;
mod_lock_post.find(from_id).first::<Self>(&mut *conn).await
mod_lock_post.find(from_id).first::<Self>(conn).await
}
async fn create(mut conn: impl DbConn, form: &ModLockPostForm) -> Result<Self, Error> {
async fn create(mut conn: impl GetConn, form: &ModLockPostForm) -> Result<Self, Error> {
use crate::schema::mod_lock_post::dsl::mod_lock_post;
insert_into(mod_lock_post)
.values(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn update(
mut conn: impl DbConn,
mut conn: impl GetConn,
from_id: i32,
form: &ModLockPostForm,
) -> Result<Self, Error> {
use crate::schema::mod_lock_post::dsl::mod_lock_post;
diesel::update(mod_lock_post.find(from_id))
.set(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
}
@ -107,31 +107,31 @@ impl Crud for ModFeaturePost {
type InsertForm = ModFeaturePostForm;
type UpdateForm = ModFeaturePostForm;
type IdType = i32;
async fn read(mut conn: impl DbConn, from_id: i32) -> Result<Self, Error> {
async fn read(mut conn: impl GetConn, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_feature_post::dsl::mod_feature_post;
mod_feature_post
.find(from_id)
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await
}
async fn create(mut conn: impl DbConn, form: &ModFeaturePostForm) -> Result<Self, Error> {
async fn create(mut conn: impl GetConn, form: &ModFeaturePostForm) -> Result<Self, Error> {
use crate::schema::mod_feature_post::dsl::mod_feature_post;
insert_into(mod_feature_post)
.values(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn update(
mut conn: impl DbConn,
mut conn: impl GetConn,
from_id: i32,
form: &ModFeaturePostForm,
) -> Result<Self, Error> {
use crate::schema::mod_feature_post::dsl::mod_feature_post;
diesel::update(mod_feature_post.find(from_id))
.set(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
}
@ -141,31 +141,31 @@ impl Crud for ModRemoveComment {
type InsertForm = ModRemoveCommentForm;
type UpdateForm = ModRemoveCommentForm;
type IdType = i32;
async fn read(mut conn: impl DbConn, from_id: i32) -> Result<Self, Error> {
async fn read(mut conn: impl GetConn, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_remove_comment::dsl::mod_remove_comment;
mod_remove_comment
.find(from_id)
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await
}
async fn create(mut conn: impl DbConn, form: &ModRemoveCommentForm) -> Result<Self, Error> {
async fn create(mut conn: impl GetConn, form: &ModRemoveCommentForm) -> Result<Self, Error> {
use crate::schema::mod_remove_comment::dsl::mod_remove_comment;
insert_into(mod_remove_comment)
.values(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn update(
mut conn: impl DbConn,
mut conn: impl GetConn,
from_id: i32,
form: &ModRemoveCommentForm,
) -> Result<Self, Error> {
use crate::schema::mod_remove_comment::dsl::mod_remove_comment;
diesel::update(mod_remove_comment.find(from_id))
.set(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
}
@ -175,31 +175,31 @@ impl Crud for ModRemoveCommunity {
type InsertForm = ModRemoveCommunityForm;
type UpdateForm = ModRemoveCommunityForm;
type IdType = i32;
async fn read(mut conn: impl DbConn, from_id: i32) -> Result<Self, Error> {
async fn read(mut conn: impl GetConn, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_remove_community::dsl::mod_remove_community;
mod_remove_community
.find(from_id)
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await
}
async fn create(mut conn: impl DbConn, form: &ModRemoveCommunityForm) -> Result<Self, Error> {
async fn create(mut conn: impl GetConn, form: &ModRemoveCommunityForm) -> Result<Self, Error> {
use crate::schema::mod_remove_community::dsl::mod_remove_community;
insert_into(mod_remove_community)
.values(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn update(
mut conn: impl DbConn,
mut conn: impl GetConn,
from_id: i32,
form: &ModRemoveCommunityForm,
) -> Result<Self, Error> {
use crate::schema::mod_remove_community::dsl::mod_remove_community;
diesel::update(mod_remove_community.find(from_id))
.set(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
}
@ -209,31 +209,31 @@ impl Crud for ModBanFromCommunity {
type InsertForm = ModBanFromCommunityForm;
type UpdateForm = ModBanFromCommunityForm;
type IdType = i32;
async fn read(mut conn: impl DbConn, from_id: i32) -> Result<Self, Error> {
async fn read(mut conn: impl GetConn, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_ban_from_community::dsl::mod_ban_from_community;
mod_ban_from_community
.find(from_id)
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await
}
async fn create(mut conn: impl DbConn, form: &ModBanFromCommunityForm) -> Result<Self, Error> {
async fn create(mut conn: impl GetConn, form: &ModBanFromCommunityForm) -> Result<Self, Error> {
use crate::schema::mod_ban_from_community::dsl::mod_ban_from_community;
insert_into(mod_ban_from_community)
.values(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn update(
mut conn: impl DbConn,
mut conn: impl GetConn,
from_id: i32,
form: &ModBanFromCommunityForm,
) -> Result<Self, Error> {
use crate::schema::mod_ban_from_community::dsl::mod_ban_from_community;
diesel::update(mod_ban_from_community.find(from_id))
.set(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
}
@ -243,24 +243,24 @@ impl Crud for ModBan {
type InsertForm = ModBanForm;
type UpdateForm = ModBanForm;
type IdType = i32;
async fn read(mut conn: impl DbConn, from_id: i32) -> Result<Self, Error> {
async fn read(mut conn: impl GetConn, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_ban::dsl::mod_ban;
mod_ban.find(from_id).first::<Self>(&mut *conn).await
mod_ban.find(from_id).first::<Self>(conn).await
}
async fn create(mut conn: impl DbConn, form: &ModBanForm) -> Result<Self, Error> {
async fn create(mut conn: impl GetConn, form: &ModBanForm) -> Result<Self, Error> {
use crate::schema::mod_ban::dsl::mod_ban;
insert_into(mod_ban)
.values(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn update(mut conn: impl DbConn, from_id: i32, form: &ModBanForm) -> Result<Self, Error> {
async fn update(mut conn: impl GetConn, from_id: i32, form: &ModBanForm) -> Result<Self, Error> {
use crate::schema::mod_ban::dsl::mod_ban;
diesel::update(mod_ban.find(from_id))
.set(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
}
@ -271,31 +271,31 @@ impl Crud for ModHideCommunity {
type UpdateForm = ModHideCommunityForm;
type IdType = i32;
async fn read(mut conn: impl DbConn, from_id: i32) -> Result<Self, Error> {
async fn read(mut conn: impl GetConn, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_hide_community::dsl::mod_hide_community;
mod_hide_community
.find(from_id)
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await
}
async fn create(mut conn: impl DbConn, form: &ModHideCommunityForm) -> Result<Self, Error> {
async fn create(mut conn: impl GetConn, form: &ModHideCommunityForm) -> Result<Self, Error> {
use crate::schema::mod_hide_community::dsl::mod_hide_community;
insert_into(mod_hide_community)
.values(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn update(
mut conn: impl DbConn,
mut conn: impl GetConn,
from_id: i32,
form: &ModHideCommunityForm,
) -> Result<Self, Error> {
use crate::schema::mod_hide_community::dsl::mod_hide_community;
diesel::update(mod_hide_community.find(from_id))
.set(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
}
@ -305,31 +305,31 @@ impl Crud for ModAddCommunity {
type InsertForm = ModAddCommunityForm;
type UpdateForm = ModAddCommunityForm;
type IdType = i32;
async fn read(mut conn: impl DbConn, from_id: i32) -> Result<Self, Error> {
async fn read(mut conn: impl GetConn, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_add_community::dsl::mod_add_community;
mod_add_community
.find(from_id)
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await
}
async fn create(mut conn: impl DbConn, form: &ModAddCommunityForm) -> Result<Self, Error> {
async fn create(mut conn: impl GetConn, form: &ModAddCommunityForm) -> Result<Self, Error> {
use crate::schema::mod_add_community::dsl::mod_add_community;
insert_into(mod_add_community)
.values(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn update(
mut conn: impl DbConn,
mut conn: impl GetConn,
from_id: i32,
form: &ModAddCommunityForm,
) -> Result<Self, Error> {
use crate::schema::mod_add_community::dsl::mod_add_community;
diesel::update(mod_add_community.find(from_id))
.set(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
}
@ -339,31 +339,31 @@ impl Crud for ModTransferCommunity {
type InsertForm = ModTransferCommunityForm;
type UpdateForm = ModTransferCommunityForm;
type IdType = i32;
async fn read(mut conn: impl DbConn, from_id: i32) -> Result<Self, Error> {
async fn read(mut conn: impl GetConn, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_transfer_community::dsl::mod_transfer_community;
mod_transfer_community
.find(from_id)
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await
}
async fn create(mut conn: impl DbConn, form: &ModTransferCommunityForm) -> Result<Self, Error> {
async fn create(mut conn: impl GetConn, form: &ModTransferCommunityForm) -> Result<Self, Error> {
use crate::schema::mod_transfer_community::dsl::mod_transfer_community;
insert_into(mod_transfer_community)
.values(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn update(
mut conn: impl DbConn,
mut conn: impl GetConn,
from_id: i32,
form: &ModTransferCommunityForm,
) -> Result<Self, Error> {
use crate::schema::mod_transfer_community::dsl::mod_transfer_community;
diesel::update(mod_transfer_community.find(from_id))
.set(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
}
@ -373,24 +373,24 @@ impl Crud for ModAdd {
type InsertForm = ModAddForm;
type UpdateForm = ModAddForm;
type IdType = i32;
async fn read(mut conn: impl DbConn, from_id: i32) -> Result<Self, Error> {
async fn read(mut conn: impl GetConn, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_add::dsl::mod_add;
mod_add.find(from_id).first::<Self>(&mut *conn).await
mod_add.find(from_id).first::<Self>(conn).await
}
async fn create(mut conn: impl DbConn, form: &ModAddForm) -> Result<Self, Error> {
async fn create(mut conn: impl GetConn, form: &ModAddForm) -> Result<Self, Error> {
use crate::schema::mod_add::dsl::mod_add;
insert_into(mod_add)
.values(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn update(mut conn: impl DbConn, from_id: i32, form: &ModAddForm) -> Result<Self, Error> {
async fn update(mut conn: impl GetConn, from_id: i32, form: &ModAddForm) -> Result<Self, Error> {
use crate::schema::mod_add::dsl::mod_add;
diesel::update(mod_add.find(from_id))
.set(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
}
@ -400,31 +400,31 @@ impl Crud for AdminPurgePerson {
type InsertForm = AdminPurgePersonForm;
type UpdateForm = AdminPurgePersonForm;
type IdType = i32;
async fn read(mut conn: impl DbConn, from_id: i32) -> Result<Self, Error> {
async fn read(mut conn: impl GetConn, from_id: i32) -> Result<Self, Error> {
use crate::schema::admin_purge_person::dsl::admin_purge_person;
admin_purge_person
.find(from_id)
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await
}
async fn create(mut conn: impl DbConn, form: &Self::InsertForm) -> Result<Self, Error> {
async fn create(mut conn: impl GetConn, form: &Self::InsertForm) -> Result<Self, Error> {
use crate::schema::admin_purge_person::dsl::admin_purge_person;
insert_into(admin_purge_person)
.values(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn update(
mut conn: impl DbConn,
mut conn: impl GetConn,
from_id: i32,
form: &Self::InsertForm,
) -> Result<Self, Error> {
use crate::schema::admin_purge_person::dsl::admin_purge_person;
diesel::update(admin_purge_person.find(from_id))
.set(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
}
@ -434,31 +434,31 @@ impl Crud for AdminPurgeCommunity {
type InsertForm = AdminPurgeCommunityForm;
type UpdateForm = AdminPurgeCommunityForm;
type IdType = i32;
async fn read(mut conn: impl DbConn, from_id: i32) -> Result<Self, Error> {
async fn read(mut conn: impl GetConn, from_id: i32) -> Result<Self, Error> {
use crate::schema::admin_purge_community::dsl::admin_purge_community;
admin_purge_community
.find(from_id)
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await
}
async fn create(mut conn: impl DbConn, form: &Self::InsertForm) -> Result<Self, Error> {
async fn create(mut conn: impl GetConn, form: &Self::InsertForm) -> Result<Self, Error> {
use crate::schema::admin_purge_community::dsl::admin_purge_community;
insert_into(admin_purge_community)
.values(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn update(
mut conn: impl DbConn,
mut conn: impl GetConn,
from_id: i32,
form: &Self::InsertForm,
) -> Result<Self, Error> {
use crate::schema::admin_purge_community::dsl::admin_purge_community;
diesel::update(admin_purge_community.find(from_id))
.set(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
}
@ -468,31 +468,31 @@ impl Crud for AdminPurgePost {
type InsertForm = AdminPurgePostForm;
type UpdateForm = AdminPurgePostForm;
type IdType = i32;
async fn read(mut conn: impl DbConn, from_id: i32) -> Result<Self, Error> {
async fn read(mut conn: impl GetConn, from_id: i32) -> Result<Self, Error> {
use crate::schema::admin_purge_post::dsl::admin_purge_post;
admin_purge_post
.find(from_id)
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await
}
async fn create(mut conn: impl DbConn, form: &Self::InsertForm) -> Result<Self, Error> {
async fn create(mut conn: impl GetConn, form: &Self::InsertForm) -> Result<Self, Error> {
use crate::schema::admin_purge_post::dsl::admin_purge_post;
insert_into(admin_purge_post)
.values(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn update(
mut conn: impl DbConn,
mut conn: impl GetConn,
from_id: i32,
form: &Self::InsertForm,
) -> Result<Self, Error> {
use crate::schema::admin_purge_post::dsl::admin_purge_post;
diesel::update(admin_purge_post.find(from_id))
.set(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
}
@ -502,31 +502,31 @@ impl Crud for AdminPurgeComment {
type InsertForm = AdminPurgeCommentForm;
type UpdateForm = AdminPurgeCommentForm;
type IdType = i32;
async fn read(mut conn: impl DbConn, from_id: i32) -> Result<Self, Error> {
async fn read(mut conn: impl GetConn, from_id: i32) -> Result<Self, Error> {
use crate::schema::admin_purge_comment::dsl::admin_purge_comment;
admin_purge_comment
.find(from_id)
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await
}
async fn create(mut conn: impl DbConn, form: &Self::InsertForm) -> Result<Self, Error> {
async fn create(mut conn: impl GetConn, form: &Self::InsertForm) -> Result<Self, Error> {
use crate::schema::admin_purge_comment::dsl::admin_purge_comment;
insert_into(admin_purge_comment)
.values(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn update(
mut conn: impl DbConn,
mut conn: impl GetConn,
from_id: i32,
form: &Self::InsertForm,
) -> Result<Self, Error> {
use crate::schema::admin_purge_comment::dsl::admin_purge_comment;
diesel::update(admin_purge_comment.find(from_id))
.set(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
}
@ -571,7 +571,7 @@ mod tests {
async fn test_crud() {
let mut conn = build_db_conn_for_tests().await;
let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string())
let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string())
.await
.unwrap();
@ -581,7 +581,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_mod = Person::create(&mut *conn, &new_mod).await.unwrap();
let inserted_mod = Person::create(conn, &new_mod).await.unwrap();
let new_person = PersonInsertForm::builder()
.name("jim2".into())
@ -589,7 +589,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_person = Person::create(&mut *conn, &new_person).await.unwrap();
let inserted_person = Person::create(conn, &new_person).await.unwrap();
let new_community = CommunityInsertForm::builder()
.name("mod_community".to_string())
@ -598,7 +598,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_community = Community::create(&mut *conn, &new_community).await.unwrap();
let inserted_community = Community::create(conn, &new_community).await.unwrap();
let new_post = PostInsertForm::builder()
.name("A test post thweep".into())
@ -606,7 +606,7 @@ mod tests {
.community_id(inserted_community.id)
.build();
let inserted_post = Post::create(&mut *conn, &new_post).await.unwrap();
let inserted_post = Post::create(conn, &new_post).await.unwrap();
let comment_form = CommentInsertForm::builder()
.content("A test comment".into())
@ -614,7 +614,7 @@ mod tests {
.post_id(inserted_post.id)
.build();
let inserted_comment = Comment::create(&mut *conn, &comment_form, None)
let inserted_comment = Comment::create(conn, &comment_form, None)
.await
.unwrap();
@ -627,10 +627,10 @@ mod tests {
reason: None,
removed: None,
};
let inserted_mod_remove_post = ModRemovePost::create(&mut *conn, &mod_remove_post_form)
let inserted_mod_remove_post = ModRemovePost::create(conn, &mod_remove_post_form)
.await
.unwrap();
let read_mod_remove_post = ModRemovePost::read(&mut *conn, inserted_mod_remove_post.id)
let read_mod_remove_post = ModRemovePost::read(conn, inserted_mod_remove_post.id)
.await
.unwrap();
let expected_mod_remove_post = ModRemovePost {
@ -649,10 +649,10 @@ mod tests {
post_id: inserted_post.id,
locked: None,
};
let inserted_mod_lock_post = ModLockPost::create(&mut *conn, &mod_lock_post_form)
let inserted_mod_lock_post = ModLockPost::create(conn, &mod_lock_post_form)
.await
.unwrap();
let read_mod_lock_post = ModLockPost::read(&mut *conn, inserted_mod_lock_post.id)
let read_mod_lock_post = ModLockPost::read(conn, inserted_mod_lock_post.id)
.await
.unwrap();
let expected_mod_lock_post = ModLockPost {
@ -671,10 +671,10 @@ mod tests {
featured: false,
is_featured_community: true,
};
let inserted_mod_feature_post = ModFeaturePost::create(&mut *conn, &mod_feature_post_form)
let inserted_mod_feature_post = ModFeaturePost::create(conn, &mod_feature_post_form)
.await
.unwrap();
let read_mod_feature_post = ModFeaturePost::read(&mut *conn, inserted_mod_feature_post.id)
let read_mod_feature_post = ModFeaturePost::read(conn, inserted_mod_feature_post.id)
.await
.unwrap();
let expected_mod_feature_post = ModFeaturePost {
@ -695,11 +695,11 @@ mod tests {
removed: None,
};
let inserted_mod_remove_comment =
ModRemoveComment::create(&mut *conn, &mod_remove_comment_form)
ModRemoveComment::create(conn, &mod_remove_comment_form)
.await
.unwrap();
let read_mod_remove_comment =
ModRemoveComment::read(&mut *conn, inserted_mod_remove_comment.id)
ModRemoveComment::read(conn, inserted_mod_remove_comment.id)
.await
.unwrap();
let expected_mod_remove_comment = ModRemoveComment {
@ -721,11 +721,11 @@ mod tests {
expires: None,
};
let inserted_mod_remove_community =
ModRemoveCommunity::create(&mut *conn, &mod_remove_community_form)
ModRemoveCommunity::create(conn, &mod_remove_community_form)
.await
.unwrap();
let read_mod_remove_community =
ModRemoveCommunity::read(&mut *conn, inserted_mod_remove_community.id)
ModRemoveCommunity::read(conn, inserted_mod_remove_community.id)
.await
.unwrap();
let expected_mod_remove_community = ModRemoveCommunity {
@ -749,11 +749,11 @@ mod tests {
expires: None,
};
let inserted_mod_ban_from_community =
ModBanFromCommunity::create(&mut *conn, &mod_ban_from_community_form)
ModBanFromCommunity::create(conn, &mod_ban_from_community_form)
.await
.unwrap();
let read_mod_ban_from_community =
ModBanFromCommunity::read(&mut *conn, inserted_mod_ban_from_community.id)
ModBanFromCommunity::read(conn, inserted_mod_ban_from_community.id)
.await
.unwrap();
let expected_mod_ban_from_community = ModBanFromCommunity {
@ -776,8 +776,8 @@ mod tests {
banned: None,
expires: None,
};
let inserted_mod_ban = ModBan::create(&mut *conn, &mod_ban_form).await.unwrap();
let read_mod_ban = ModBan::read(&mut *conn, inserted_mod_ban.id).await.unwrap();
let inserted_mod_ban = ModBan::create(conn, &mod_ban_form).await.unwrap();
let read_mod_ban = ModBan::read(conn, inserted_mod_ban.id).await.unwrap();
let expected_mod_ban = ModBan {
id: inserted_mod_ban.id,
mod_person_id: inserted_mod.id,
@ -796,10 +796,10 @@ mod tests {
community_id: inserted_community.id,
removed: None,
};
let inserted_mod_add_community = ModAddCommunity::create(&mut *conn, &mod_add_community_form)
let inserted_mod_add_community = ModAddCommunity::create(conn, &mod_add_community_form)
.await
.unwrap();
let read_mod_add_community = ModAddCommunity::read(&mut *conn, inserted_mod_add_community.id)
let read_mod_add_community = ModAddCommunity::read(conn, inserted_mod_add_community.id)
.await
.unwrap();
let expected_mod_add_community = ModAddCommunity {
@ -818,8 +818,8 @@ mod tests {
other_person_id: inserted_person.id,
removed: None,
};
let inserted_mod_add = ModAdd::create(&mut *conn, &mod_add_form).await.unwrap();
let read_mod_add = ModAdd::read(&mut *conn, inserted_mod_add.id).await.unwrap();
let inserted_mod_add = ModAdd::create(conn, &mod_add_form).await.unwrap();
let read_mod_add = ModAdd::read(conn, inserted_mod_add.id).await.unwrap();
let expected_mod_add = ModAdd {
id: inserted_mod_add.id,
mod_person_id: inserted_mod.id,
@ -828,18 +828,18 @@ mod tests {
when_: inserted_mod_add.when_,
};
Comment::delete(&mut *conn, inserted_comment.id)
Comment::delete(conn, inserted_comment.id)
.await
.unwrap();
Post::delete(&mut *conn, inserted_post.id).await.unwrap();
Community::delete(&mut *conn, inserted_community.id)
Post::delete(conn, inserted_post.id).await.unwrap();
Community::delete(conn, inserted_community.id)
.await
.unwrap();
Person::delete(&mut *conn, inserted_person.id)
Person::delete(conn, inserted_person.id)
.await
.unwrap();
Person::delete(&mut *conn, inserted_mod.id).await.unwrap();
Instance::delete(&mut *conn, inserted_instance.id)
Person::delete(conn, inserted_mod.id).await.unwrap();
Instance::delete(conn, inserted_instance.id)
.await
.unwrap();

View File

@ -8,7 +8,7 @@ use crate::{
},
source::password_reset_request::{PasswordResetRequest, PasswordResetRequestForm},
traits::Crud,
utils::DbConn,
utils::GetConn,
};
use diesel::{
dsl::{insert_into, now, IntervalDsl},
@ -16,7 +16,7 @@ use diesel::{
ExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::utils::RunQueryDsl;
use sha2::{Digest, Sha256};
#[async_trait]
@ -24,33 +24,33 @@ impl Crud for PasswordResetRequest {
type InsertForm = PasswordResetRequestForm;
type UpdateForm = PasswordResetRequestForm;
type IdType = i32;
async fn read(mut conn: impl DbConn, password_reset_request_id: i32) -> Result<Self, Error> {
async fn read(mut conn: impl GetConn, password_reset_request_id: i32) -> Result<Self, Error> {
password_reset_request
.find(password_reset_request_id)
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await
}
async fn create(mut conn: impl DbConn, form: &PasswordResetRequestForm) -> Result<Self, Error> {
async fn create(mut conn: impl GetConn, form: &PasswordResetRequestForm) -> Result<Self, Error> {
insert_into(password_reset_request)
.values(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn update(
mut conn: impl DbConn,
mut conn: impl GetConn,
password_reset_request_id: i32,
form: &PasswordResetRequestForm,
) -> Result<Self, Error> {
diesel::update(password_reset_request.find(password_reset_request_id))
.set(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
}
impl PasswordResetRequest {
pub async fn create_token(
mut conn: impl DbConn,
mut conn: impl GetConn,
from_local_user_id: LocalUserId,
token: &str,
) -> Result<PasswordResetRequest, Error> {
@ -63,10 +63,10 @@ impl PasswordResetRequest {
token_encrypted: token_hash,
};
Self::create(&mut *conn, &form).await
Self::create(conn, &form).await
}
pub async fn read_from_token(
mut conn: impl DbConn,
mut conn: impl GetConn,
token: &str,
) -> Result<PasswordResetRequest, Error> {
let mut hasher = Sha256::new();
@ -75,19 +75,19 @@ impl PasswordResetRequest {
password_reset_request
.filter(token_encrypted.eq(token_hash))
.filter(published.gt(now - 1.days()))
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await
}
pub async fn get_recent_password_resets_count(
mut conn: impl DbConn,
mut conn: impl GetConn,
user_id: LocalUserId,
) -> Result<i64, Error> {
password_reset_request
.filter(local_user_id.eq(user_id))
.filter(published.gt(now - 1.days()))
.count()
.get_result(&mut *conn)
.get_result(conn)
.await
}
}
@ -119,7 +119,7 @@ mod tests {
async fn test_crud() {
let mut conn = build_db_conn_for_tests().await;
let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string())
let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string())
.await
.unwrap();
@ -129,14 +129,14 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_person = Person::create(&mut *conn, &new_person).await.unwrap();
let inserted_person = Person::create(conn, &new_person).await.unwrap();
let new_local_user = LocalUserInsertForm::builder()
.person_id(inserted_person.id)
.password_encrypted("pass".to_string())
.build();
let inserted_local_user = LocalUser::create(&mut *conn, &new_local_user)
let inserted_local_user = LocalUser::create(conn, &new_local_user)
.await
.unwrap();
@ -144,7 +144,7 @@ mod tests {
let token_encrypted_ = "ca3704aa0b06f5954c79ee837faa152d84d6b2d42838f0637a15eda8337dbdce";
let inserted_password_reset_request =
PasswordResetRequest::create_token(&mut *conn, inserted_local_user.id, token)
PasswordResetRequest::create_token(conn, inserted_local_user.id, token)
.await
.unwrap();
@ -155,13 +155,13 @@ mod tests {
published: inserted_password_reset_request.published,
};
let read_password_reset_request = PasswordResetRequest::read_from_token(&mut *conn, token)
let read_password_reset_request = PasswordResetRequest::read_from_token(conn, token)
.await
.unwrap();
let num_deleted = Person::delete(&mut *conn, inserted_person.id)
let num_deleted = Person::delete(conn, inserted_person.id)
.await
.unwrap();
Instance::delete(&mut *conn, inserted_instance.id)
Instance::delete(conn, inserted_instance.id)
.await
.unwrap();

View File

@ -9,42 +9,42 @@ use crate::{
PersonUpdateForm,
},
traits::{ApubActor, Crud, Followable},
utils::{functions::lower, naive_now, DbConn},
utils::{functions::lower, naive_now, GetConn},
};
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, JoinOnDsl, QueryDsl};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::utils::RunQueryDsl;
#[async_trait]
impl Crud for Person {
type InsertForm = PersonInsertForm;
type UpdateForm = PersonUpdateForm;
type IdType = PersonId;
async fn read(mut conn: impl DbConn, person_id: PersonId) -> Result<Self, Error> {
async fn read(mut conn: impl GetConn, person_id: PersonId) -> Result<Self, Error> {
person::table
.filter(person::deleted.eq(false))
.find(person_id)
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await
}
async fn delete(mut conn: impl DbConn, person_id: PersonId) -> Result<usize, Error> {
async fn delete(mut conn: impl GetConn, person_id: PersonId) -> Result<usize, Error> {
diesel::delete(person::table.find(person_id))
.execute(&mut *conn)
.execute(conn)
.await
}
async fn create(mut conn: impl DbConn, form: &PersonInsertForm) -> Result<Self, Error> {
async fn create(mut conn: impl GetConn, form: &PersonInsertForm) -> Result<Self, Error> {
insert_into(person::table)
.values(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn update(
mut conn: impl DbConn,
mut conn: impl GetConn,
person_id: PersonId,
form: &PersonUpdateForm,
) -> Result<Self, Error> {
diesel::update(person::table.find(person_id))
.set(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
}
@ -53,23 +53,26 @@ impl Person {
/// Update or insert the person.
///
/// This is necessary for federation, because Activitypub doesnt distinguish between these actions.
pub async fn upsert(mut conn: impl DbConn, form: &PersonInsertForm) -> Result<Self, Error> {
pub async fn upsert(mut conn: impl GetConn, form: &PersonInsertForm) -> Result<Self, Error> {
insert_into(person::table)
.values(form)
.on_conflict(person::actor_id)
.do_update()
.set(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
pub async fn delete_account(mut conn: impl DbConn, person_id: PersonId) -> Result<Person, Error> {
pub async fn delete_account(
mut conn: impl GetConn,
person_id: PersonId,
) -> Result<Person, Error> {
// Set the local user info to none
diesel::update(local_user::table.filter(local_user::person_id.eq(person_id)))
.set((
local_user::email.eq::<Option<String>>(None),
local_user::validator_time.eq(naive_now()),
))
.execute(&mut *conn)
.execute(conn)
.await?;
diesel::update(person::table.find(person_id))
@ -82,7 +85,7 @@ impl Person {
person::deleted.eq(true),
person::updated.eq(naive_now()),
))
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
}
@ -98,14 +101,14 @@ pub fn is_banned(banned_: bool, expires: Option<chrono::NaiveDateTime>) -> bool
#[async_trait]
impl ApubActor for Person {
async fn read_from_apub_id(
mut conn: impl DbConn,
mut conn: impl GetConn,
object_id: &DbUrl,
) -> Result<Option<Self>, Error> {
Ok(
person::table
.filter(person::deleted.eq(false))
.filter(person::actor_id.eq(object_id))
.first::<Person>(&mut *conn)
.first::<Person>(conn)
.await
.ok()
.map(Into::into),
@ -113,7 +116,7 @@ impl ApubActor for Person {
}
async fn read_from_name(
mut conn: impl DbConn,
mut conn: impl GetConn,
from_name: &str,
include_deleted: bool,
) -> Result<Person, Error> {
@ -124,11 +127,11 @@ impl ApubActor for Person {
if !include_deleted {
q = q.filter(person::deleted.eq(false))
}
q.first::<Self>(&mut *conn).await
q.first::<Self>(conn).await
}
async fn read_from_name_and_domain(
mut conn: impl DbConn,
mut conn: impl GetConn,
person_name: &str,
for_domain: &str,
) -> Result<Person, Error> {
@ -137,7 +140,7 @@ impl ApubActor for Person {
.filter(lower(person::name).eq(person_name.to_lowercase()))
.filter(instance::domain.eq(for_domain))
.select(person::all_columns)
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await
}
}
@ -145,41 +148,41 @@ impl ApubActor for Person {
#[async_trait]
impl Followable for PersonFollower {
type Form = PersonFollowerForm;
async fn follow(mut conn: impl DbConn, form: &PersonFollowerForm) -> Result<Self, Error> {
async fn follow(mut conn: impl GetConn, form: &PersonFollowerForm) -> Result<Self, Error> {
use crate::schema::person_follower::dsl::{follower_id, person_follower, person_id};
insert_into(person_follower)
.values(form)
.on_conflict((follower_id, person_id))
.do_update()
.set(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn follow_accepted(_: impl DbConn, _: CommunityId, _: PersonId) -> Result<Self, Error> {
async fn follow_accepted(_: impl GetConn, _: CommunityId, _: PersonId) -> Result<Self, Error> {
unimplemented!()
}
async fn unfollow(mut conn: impl DbConn, form: &PersonFollowerForm) -> Result<usize, Error> {
async fn unfollow(mut conn: impl GetConn, form: &PersonFollowerForm) -> Result<usize, Error> {
use crate::schema::person_follower::dsl::{follower_id, person_follower, person_id};
diesel::delete(
person_follower
.filter(follower_id.eq(&form.follower_id))
.filter(person_id.eq(&form.person_id)),
)
.execute(&mut *conn)
.execute(conn)
.await
}
}
impl PersonFollower {
pub async fn list_followers(
mut conn: impl DbConn,
mut conn: impl GetConn,
for_person_id: PersonId,
) -> Result<Vec<Person>, Error> {
person_follower::table
.inner_join(person::table.on(person_follower::follower_id.eq(person::id)))
.filter(person_follower::person_id.eq(for_person_id))
.select(person::all_columns)
.load(&mut *conn)
.load(conn)
.await
}
}
@ -201,7 +204,7 @@ mod tests {
async fn test_crud() {
let mut conn = build_db_conn_for_tests().await;
let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string())
let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string())
.await
.unwrap();
@ -211,7 +214,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_person = Person::create(&mut *conn, &new_person).await.unwrap();
let inserted_person = Person::create(conn, &new_person).await.unwrap();
let expected_person = Person {
id: inserted_person.id,
@ -238,19 +241,19 @@ mod tests {
instance_id: inserted_instance.id,
};
let read_person = Person::read(&mut *conn, inserted_person.id).await.unwrap();
let read_person = Person::read(conn, inserted_person.id).await.unwrap();
let update_person_form = PersonUpdateForm::builder()
.actor_id(Some(inserted_person.actor_id.clone()))
.build();
let updated_person = Person::update(&mut *conn, inserted_person.id, &update_person_form)
let updated_person = Person::update(conn, inserted_person.id, &update_person_form)
.await
.unwrap();
let num_deleted = Person::delete(&mut *conn, inserted_person.id)
let num_deleted = Person::delete(conn, inserted_person.id)
.await
.unwrap();
Instance::delete(&mut *conn, inserted_instance.id)
Instance::delete(conn, inserted_instance.id)
.await
.unwrap();
@ -264,7 +267,7 @@ mod tests {
#[serial]
async fn follow() {
let mut conn = build_db_conn_for_tests().await;
let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string())
let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string())
.await
.unwrap();
@ -273,32 +276,32 @@ mod tests {
.public_key("pubkey".to_string())
.instance_id(inserted_instance.id)
.build();
let person_1 = Person::create(&mut *conn, &person_form_1).await.unwrap();
let person_1 = Person::create(conn, &person_form_1).await.unwrap();
let person_form_2 = PersonInsertForm::builder()
.name("michele".into())
.public_key("pubkey".to_string())
.instance_id(inserted_instance.id)
.build();
let person_2 = Person::create(&mut *conn, &person_form_2).await.unwrap();
let person_2 = Person::create(conn, &person_form_2).await.unwrap();
let follow_form = PersonFollowerForm {
person_id: person_1.id,
follower_id: person_2.id,
pending: false,
};
let person_follower = PersonFollower::follow(&mut *conn, &follow_form)
let person_follower = PersonFollower::follow(conn, &follow_form)
.await
.unwrap();
assert_eq!(person_1.id, person_follower.person_id);
assert_eq!(person_2.id, person_follower.follower_id);
assert!(!person_follower.pending);
let followers = PersonFollower::list_followers(&mut *conn, person_1.id)
let followers = PersonFollower::list_followers(conn, person_1.id)
.await
.unwrap();
assert_eq!(vec![person_2], followers);
let unfollow = PersonFollower::unfollow(&mut *conn, &follow_form)
let unfollow = PersonFollower::unfollow(conn, &follow_form)
.await
.unwrap();
assert_eq!(1, unfollow);

View File

@ -3,21 +3,21 @@ use crate::{
schema::person_block::dsl::{person_block, person_id, target_id},
source::person_block::{PersonBlock, PersonBlockForm},
traits::Blockable,
utils::DbConn,
utils::GetConn,
};
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::utils::RunQueryDsl;
impl PersonBlock {
pub async fn read(
mut conn: impl DbConn,
mut conn: impl GetConn,
for_person_id: PersonId,
for_recipient_id: PersonId,
) -> Result<Self, Error> {
person_block
.filter(person_id.eq(for_person_id))
.filter(target_id.eq(for_recipient_id))
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await
}
}
@ -26,7 +26,7 @@ impl PersonBlock {
impl Blockable for PersonBlock {
type Form = PersonBlockForm;
async fn block(
mut conn: impl DbConn,
mut conn: impl GetConn,
person_block_form: &PersonBlockForm,
) -> Result<Self, Error> {
insert_into(person_block)
@ -34,16 +34,16 @@ impl Blockable for PersonBlock {
.on_conflict((person_id, target_id))
.do_update()
.set(person_block_form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn unblock(mut conn: impl DbConn, person_block_form: &Self::Form) -> Result<usize, Error> {
async fn unblock(mut conn: impl GetConn, person_block_form: &Self::Form) -> Result<usize, Error> {
diesel::delete(
person_block
.filter(person_id.eq(person_block_form.person_id))
.filter(target_id.eq(person_block_form.target_id)),
)
.execute(&mut *conn)
.execute(conn)
.await
}
}

View File

@ -3,25 +3,25 @@ use crate::{
schema::person_mention::dsl::{comment_id, person_mention, read, recipient_id},
source::person_mention::{PersonMention, PersonMentionInsertForm, PersonMentionUpdateForm},
traits::Crud,
utils::DbConn,
utils::GetConn,
};
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::utils::RunQueryDsl;
#[async_trait]
impl Crud for PersonMention {
type InsertForm = PersonMentionInsertForm;
type UpdateForm = PersonMentionUpdateForm;
type IdType = PersonMentionId;
async fn read(mut conn: impl DbConn, person_mention_id: PersonMentionId) -> Result<Self, Error> {
async fn read(mut conn: impl GetConn, person_mention_id: PersonMentionId) -> Result<Self, Error> {
person_mention
.find(person_mention_id)
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await
}
async fn create(
mut conn: impl DbConn,
mut conn: impl GetConn,
person_mention_form: &Self::InsertForm,
) -> Result<Self, Error> {
// since the return here isnt utilized, we dont need to do an update
@ -31,25 +31,25 @@ impl Crud for PersonMention {
.on_conflict((recipient_id, comment_id))
.do_update()
.set(person_mention_form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn update(
mut conn: impl DbConn,
mut conn: impl GetConn,
person_mention_id: PersonMentionId,
person_mention_form: &Self::UpdateForm,
) -> Result<Self, Error> {
diesel::update(person_mention.find(person_mention_id))
.set(person_mention_form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
}
impl PersonMention {
pub async fn mark_all_as_read(
mut conn: impl DbConn,
mut conn: impl GetConn,
for_recipient_id: PersonId,
) -> Result<Vec<PersonMention>, Error> {
diesel::update(
@ -58,19 +58,19 @@ impl PersonMention {
.filter(read.eq(false)),
)
.set(read.eq(true))
.get_results::<Self>(&mut *conn)
.get_results::<Self>(conn)
.await
}
pub async fn read_by_comment_and_person(
mut conn: impl DbConn,
mut conn: impl GetConn,
for_comment_id: CommentId,
for_recipient_id: PersonId,
) -> Result<Self, Error> {
person_mention
.filter(comment_id.eq(for_comment_id))
.filter(recipient_id.eq(for_recipient_id))
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await
}
}
@ -96,7 +96,7 @@ mod tests {
async fn test_crud() {
let mut conn = build_db_conn_for_tests().await;
let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string())
let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string())
.await
.unwrap();
@ -106,7 +106,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_person = Person::create(&mut *conn, &new_person).await.unwrap();
let inserted_person = Person::create(conn, &new_person).await.unwrap();
let recipient_form = PersonInsertForm::builder()
.name("terrylakes recipient".into())
@ -114,7 +114,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_recipient = Person::create(&mut *conn, &recipient_form).await.unwrap();
let inserted_recipient = Person::create(conn, &recipient_form).await.unwrap();
let new_community = CommunityInsertForm::builder()
.name("test community lake".to_string())
@ -123,7 +123,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_community = Community::create(&mut *conn, &new_community).await.unwrap();
let inserted_community = Community::create(conn, &new_community).await.unwrap();
let new_post = PostInsertForm::builder()
.name("A test post".into())
@ -131,7 +131,7 @@ mod tests {
.community_id(inserted_community.id)
.build();
let inserted_post = Post::create(&mut *conn, &new_post).await.unwrap();
let inserted_post = Post::create(conn, &new_post).await.unwrap();
let comment_form = CommentInsertForm::builder()
.content("A test comment".into())
@ -139,7 +139,7 @@ mod tests {
.post_id(inserted_post.id)
.build();
let inserted_comment = Comment::create(&mut *conn, &comment_form, None)
let inserted_comment = Comment::create(conn, &comment_form, None)
.await
.unwrap();
@ -149,7 +149,7 @@ mod tests {
read: None,
};
let inserted_mention = PersonMention::create(&mut *conn, &person_mention_form)
let inserted_mention = PersonMention::create(conn, &person_mention_form)
.await
.unwrap();
@ -161,29 +161,29 @@ mod tests {
published: inserted_mention.published,
};
let read_mention = PersonMention::read(&mut *conn, inserted_mention.id)
let read_mention = PersonMention::read(conn, inserted_mention.id)
.await
.unwrap();
let person_mention_update_form = PersonMentionUpdateForm { read: Some(false) };
let updated_mention =
PersonMention::update(&mut *conn, inserted_mention.id, &person_mention_update_form)
PersonMention::update(conn, inserted_mention.id, &person_mention_update_form)
.await
.unwrap();
Comment::delete(&mut *conn, inserted_comment.id)
Comment::delete(conn, inserted_comment.id)
.await
.unwrap();
Post::delete(&mut *conn, inserted_post.id).await.unwrap();
Community::delete(&mut *conn, inserted_community.id)
Post::delete(conn, inserted_post.id).await.unwrap();
Community::delete(conn, inserted_community.id)
.await
.unwrap();
Person::delete(&mut *conn, inserted_person.id)
Person::delete(conn, inserted_person.id)
.await
.unwrap();
Person::delete(&mut *conn, inserted_recipient.id)
Person::delete(conn, inserted_recipient.id)
.await
.unwrap();
Instance::delete(&mut *conn, inserted_instance.id)
Instance::delete(conn, inserted_instance.id)
.await
.unwrap();

View File

@ -27,50 +27,50 @@ use crate::{
PostUpdateForm,
},
traits::{Crud, Likeable, Readable, Saveable},
utils::{naive_now, DbConn, DELETED_REPLACEMENT_TEXT, FETCH_LIMIT_MAX},
utils::{naive_now, GetConn, DELETED_REPLACEMENT_TEXT, FETCH_LIMIT_MAX},
};
use ::url::Url;
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl, TextExpressionMethods};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::utils::RunQueryDsl;
#[async_trait]
impl Crud for Post {
type InsertForm = PostInsertForm;
type UpdateForm = PostUpdateForm;
type IdType = PostId;
async fn read(mut conn: impl DbConn, post_id: PostId) -> Result<Self, Error> {
post.find(post_id).first::<Self>(&mut *conn).await
async fn read(mut conn: impl GetConn, post_id: PostId) -> Result<Self, Error> {
post.find(post_id).first::<Self>(conn).await
}
async fn delete(mut conn: impl DbConn, post_id: PostId) -> Result<usize, Error> {
diesel::delete(post.find(post_id)).execute(&mut *conn).await
async fn delete(mut conn: impl GetConn, post_id: PostId) -> Result<usize, Error> {
diesel::delete(post.find(post_id)).execute(conn).await
}
async fn create(mut conn: impl DbConn, form: &Self::InsertForm) -> Result<Self, Error> {
async fn create(mut conn: impl GetConn, form: &Self::InsertForm) -> Result<Self, Error> {
insert_into(post)
.values(form)
.on_conflict(ap_id)
.do_update()
.set(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn update(
mut conn: impl DbConn,
mut conn: impl GetConn,
post_id: PostId,
new_post: &Self::UpdateForm,
) -> Result<Self, Error> {
diesel::update(post.find(post_id))
.set(new_post)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
}
impl Post {
pub async fn list_for_community(
mut conn: impl DbConn,
mut conn: impl GetConn,
the_community_id: CommunityId,
) -> Result<Vec<Self>, Error> {
post
@ -80,12 +80,12 @@ impl Post {
.then_order_by(featured_community.desc())
.then_order_by(published.desc())
.limit(FETCH_LIMIT_MAX)
.load::<Self>(&mut *conn)
.load::<Self>(conn)
.await
}
pub async fn list_featured_for_community(
mut conn: impl DbConn,
mut conn: impl GetConn,
the_community_id: CommunityId,
) -> Result<Vec<Self>, Error> {
post
@ -95,12 +95,12 @@ impl Post {
.filter(featured_community.eq(true))
.then_order_by(published.desc())
.limit(FETCH_LIMIT_MAX)
.load::<Self>(&mut *conn)
.load::<Self>(conn)
.await
}
pub async fn permadelete_for_creator(
mut conn: impl DbConn,
mut conn: impl GetConn,
for_creator_id: PersonId,
) -> Result<Vec<Self>, Error> {
diesel::update(post.filter(creator_id.eq(for_creator_id)))
@ -111,12 +111,12 @@ impl Post {
deleted.eq(true),
updated.eq(naive_now()),
))
.get_results::<Self>(&mut *conn)
.get_results::<Self>(conn)
.await
}
pub async fn update_removed_for_creator(
mut conn: impl DbConn,
mut conn: impl GetConn,
for_creator_id: PersonId,
for_community_id: Option<CommunityId>,
new_removed: bool,
@ -130,7 +130,7 @@ impl Post {
update
.set((removed.eq(new_removed), updated.eq(naive_now())))
.get_results::<Self>(&mut *conn)
.get_results::<Self>(conn)
.await
}
@ -139,14 +139,14 @@ impl Post {
}
pub async fn read_from_apub_id(
mut conn: impl DbConn,
mut conn: impl GetConn,
object_id: Url,
) -> Result<Option<Self>, Error> {
let object_id: DbUrl = object_id.into();
Ok(
post
.filter(ap_id.eq(object_id))
.first::<Post>(&mut *conn)
.first::<Post>(conn)
.await
.ok()
.map(Into::into),
@ -154,7 +154,7 @@ impl Post {
}
pub async fn fetch_pictrs_posts_for_creator(
mut conn: impl DbConn,
mut conn: impl GetConn,
for_creator_id: PersonId,
) -> Result<Vec<Self>, Error> {
let pictrs_search = "%pictrs/image%";
@ -162,13 +162,13 @@ impl Post {
post
.filter(creator_id.eq(for_creator_id))
.filter(url.like(pictrs_search))
.load::<Self>(&mut *conn)
.load::<Self>(conn)
.await
}
/// Sets the url and thumbnails fields to None
pub async fn remove_pictrs_post_images_and_thumbnails_for_creator(
mut conn: impl DbConn,
mut conn: impl GetConn,
for_creator_id: PersonId,
) -> Result<Vec<Self>, Error> {
let pictrs_search = "%pictrs/image%";
@ -182,25 +182,25 @@ impl Post {
url.eq::<Option<String>>(None),
thumbnail_url.eq::<Option<String>>(None),
))
.get_results::<Self>(&mut *conn)
.get_results::<Self>(conn)
.await
}
pub async fn fetch_pictrs_posts_for_community(
mut conn: impl DbConn,
mut conn: impl GetConn,
for_community_id: CommunityId,
) -> Result<Vec<Self>, Error> {
let pictrs_search = "%pictrs/image%";
post
.filter(community_id.eq(for_community_id))
.filter(url.like(pictrs_search))
.load::<Self>(&mut *conn)
.load::<Self>(conn)
.await
}
/// Sets the url and thumbnails fields to None
pub async fn remove_pictrs_post_images_and_thumbnails_for_community(
mut conn: impl DbConn,
mut conn: impl GetConn,
for_community_id: CommunityId,
) -> Result<Vec<Self>, Error> {
let pictrs_search = "%pictrs/image%";
@ -214,7 +214,7 @@ impl Post {
url.eq::<Option<String>>(None),
thumbnail_url.eq::<Option<String>>(None),
))
.get_results::<Self>(&mut *conn)
.get_results::<Self>(conn)
.await
}
}
@ -223,18 +223,18 @@ impl Post {
impl Likeable for PostLike {
type Form = PostLikeForm;
type IdType = PostId;
async fn like(mut conn: impl DbConn, post_like_form: &PostLikeForm) -> Result<Self, Error> {
async fn like(mut conn: impl GetConn, post_like_form: &PostLikeForm) -> Result<Self, Error> {
use crate::schema::post_like::dsl::{person_id, post_id, post_like};
insert_into(post_like)
.values(post_like_form)
.on_conflict((post_id, person_id))
.do_update()
.set(post_like_form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn remove(
mut conn: impl DbConn,
mut conn: impl GetConn,
person_id: PersonId,
post_id: PostId,
) -> Result<usize, Error> {
@ -244,7 +244,7 @@ impl Likeable for PostLike {
.filter(dsl::post_id.eq(post_id))
.filter(dsl::person_id.eq(person_id)),
)
.execute(&mut *conn)
.execute(conn)
.await
}
}
@ -252,24 +252,24 @@ impl Likeable for PostLike {
#[async_trait]
impl Saveable for PostSaved {
type Form = PostSavedForm;
async fn save(mut conn: impl DbConn, post_saved_form: &PostSavedForm) -> Result<Self, Error> {
async fn save(mut conn: impl GetConn, post_saved_form: &PostSavedForm) -> Result<Self, Error> {
use crate::schema::post_saved::dsl::{person_id, post_id, post_saved};
insert_into(post_saved)
.values(post_saved_form)
.on_conflict((post_id, person_id))
.do_update()
.set(post_saved_form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn unsave(mut conn: impl DbConn, post_saved_form: &PostSavedForm) -> Result<usize, Error> {
async fn unsave(mut conn: impl GetConn, post_saved_form: &PostSavedForm) -> Result<usize, Error> {
use crate::schema::post_saved::dsl::{person_id, post_id, post_saved};
diesel::delete(
post_saved
.filter(post_id.eq(post_saved_form.post_id))
.filter(person_id.eq(post_saved_form.person_id)),
)
.execute(&mut *conn)
.execute(conn)
.await
}
}
@ -278,7 +278,7 @@ impl Saveable for PostSaved {
impl Readable for PostRead {
type Form = PostReadForm;
async fn mark_as_read(
mut conn: impl DbConn,
mut conn: impl GetConn,
post_read_form: &PostReadForm,
) -> Result<Self, Error> {
use crate::schema::post_read::dsl::{person_id, post_id, post_read};
@ -287,12 +287,12 @@ impl Readable for PostRead {
.on_conflict((post_id, person_id))
.do_update()
.set(post_read_form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn mark_as_unread(
mut conn: impl DbConn,
mut conn: impl GetConn,
post_read_form: &PostReadForm,
) -> Result<usize, Error> {
use crate::schema::post_read::dsl::{person_id, post_id, post_read};
@ -301,7 +301,7 @@ impl Readable for PostRead {
.filter(post_id.eq(post_read_form.post_id))
.filter(person_id.eq(post_read_form.person_id)),
)
.execute(&mut *conn)
.execute(conn)
.await
}
}
@ -335,7 +335,7 @@ mod tests {
async fn test_crud() {
let mut conn = build_db_conn_for_tests().await;
let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string())
let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string())
.await
.unwrap();
@ -345,7 +345,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_person = Person::create(&mut *conn, &new_person).await.unwrap();
let inserted_person = Person::create(conn, &new_person).await.unwrap();
let new_community = CommunityInsertForm::builder()
.name("test community_3".to_string())
@ -354,7 +354,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_community = Community::create(&mut *conn, &new_community).await.unwrap();
let inserted_community = Community::create(conn, &new_community).await.unwrap();
let new_post = PostInsertForm::builder()
.name("A test post".into())
@ -362,7 +362,7 @@ mod tests {
.community_id(inserted_community.id)
.build();
let inserted_post = Post::create(&mut *conn, &new_post).await.unwrap();
let inserted_post = Post::create(conn, &new_post).await.unwrap();
let expected_post = Post {
id: inserted_post.id,
@ -395,7 +395,7 @@ mod tests {
score: 1,
};
let inserted_post_like = PostLike::like(&mut *conn, &post_like_form).await.unwrap();
let inserted_post_like = PostLike::like(conn, &post_like_form).await.unwrap();
let expected_post_like = PostLike {
id: inserted_post_like.id,
@ -411,7 +411,7 @@ mod tests {
person_id: inserted_person.id,
};
let inserted_post_saved = PostSaved::save(&mut *conn, &post_saved_form).await.unwrap();
let inserted_post_saved = PostSaved::save(conn, &post_saved_form).await.unwrap();
let expected_post_saved = PostSaved {
id: inserted_post_saved.id,
@ -426,7 +426,7 @@ mod tests {
person_id: inserted_person.id,
};
let inserted_post_read = PostRead::mark_as_read(&mut *conn, &post_read_form)
let inserted_post_read = PostRead::mark_as_read(conn, &post_read_form)
.await
.unwrap();
@ -437,32 +437,32 @@ mod tests {
published: inserted_post_read.published,
};
let read_post = Post::read(&mut *conn, inserted_post.id).await.unwrap();
let read_post = Post::read(conn, inserted_post.id).await.unwrap();
let new_post_update = PostUpdateForm::builder()
.name(Some("A test post".into()))
.build();
let updated_post = Post::update(&mut *conn, inserted_post.id, &new_post_update)
let updated_post = Post::update(conn, inserted_post.id, &new_post_update)
.await
.unwrap();
let like_removed = PostLike::remove(&mut *conn, inserted_person.id, inserted_post.id)
let like_removed = PostLike::remove(conn, inserted_person.id, inserted_post.id)
.await
.unwrap();
let saved_removed = PostSaved::unsave(&mut *conn, &post_saved_form)
let saved_removed = PostSaved::unsave(conn, &post_saved_form)
.await
.unwrap();
let read_removed = PostRead::mark_as_unread(&mut *conn, &post_read_form)
let read_removed = PostRead::mark_as_unread(conn, &post_read_form)
.await
.unwrap();
let num_deleted = Post::delete(&mut *conn, inserted_post.id).await.unwrap();
Community::delete(&mut *conn, inserted_community.id)
let num_deleted = Post::delete(conn, inserted_post.id).await.unwrap();
Community::delete(conn, inserted_community.id)
.await
.unwrap();
Person::delete(&mut *conn, inserted_person.id)
Person::delete(conn, inserted_person.id)
.await
.unwrap();
Instance::delete(&mut *conn, inserted_instance.id)
Instance::delete(conn, inserted_instance.id)
.await
.unwrap();

View File

@ -3,7 +3,7 @@ use crate::{
schema::post_report::dsl::{post_report, resolved, resolver_id, updated},
source::post_report::{PostReport, PostReportForm},
traits::Reportable,
utils::{naive_now, DbConn},
utils::{naive_now, GetConn},
};
use diesel::{
dsl::{insert_into, update},
@ -11,22 +11,25 @@ use diesel::{
ExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::utils::RunQueryDsl;
#[async_trait]
impl Reportable for PostReport {
type Form = PostReportForm;
type IdType = PostReportId;
async fn report(mut conn: impl DbConn, post_report_form: &PostReportForm) -> Result<Self, Error> {
async fn report(
mut conn: impl GetConn,
post_report_form: &PostReportForm,
) -> Result<Self, Error> {
insert_into(post_report)
.values(post_report_form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn resolve(
mut conn: impl DbConn,
mut conn: impl GetConn,
report_id: Self::IdType,
by_resolver_id: PersonId,
) -> Result<usize, Error> {
@ -36,12 +39,12 @@ impl Reportable for PostReport {
resolver_id.eq(by_resolver_id),
updated.eq(naive_now()),
))
.execute(&mut *conn)
.execute(conn)
.await
}
async fn unresolve(
mut conn: impl DbConn,
mut conn: impl GetConn,
report_id: Self::IdType,
by_resolver_id: PersonId,
) -> Result<usize, Error> {
@ -51,7 +54,7 @@ impl Reportable for PostReport {
resolver_id.eq(by_resolver_id),
updated.eq(naive_now()),
))
.execute(&mut *conn)
.execute(conn)
.await
}
}

View File

@ -3,10 +3,10 @@ use crate::{
schema::private_message::dsl::{ap_id, private_message, read, recipient_id},
source::private_message::{PrivateMessage, PrivateMessageInsertForm, PrivateMessageUpdateForm},
traits::Crud,
utils::DbConn,
utils::GetConn,
};
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::utils::RunQueryDsl;
use lemmy_utils::error::LemmyError;
use url::Url;
@ -16,45 +16,45 @@ impl Crud for PrivateMessage {
type UpdateForm = PrivateMessageUpdateForm;
type IdType = PrivateMessageId;
async fn read(
mut conn: impl DbConn,
mut conn: impl GetConn,
private_message_id: PrivateMessageId,
) -> Result<Self, Error> {
private_message
.find(private_message_id)
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await
}
async fn create(mut conn: impl DbConn, form: &Self::InsertForm) -> Result<Self, Error> {
async fn create(mut conn: impl GetConn, form: &Self::InsertForm) -> Result<Self, Error> {
insert_into(private_message)
.values(form)
.on_conflict(ap_id)
.do_update()
.set(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn update(
mut conn: impl DbConn,
mut conn: impl GetConn,
private_message_id: PrivateMessageId,
form: &Self::UpdateForm,
) -> Result<Self, Error> {
diesel::update(private_message.find(private_message_id))
.set(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn delete(mut conn: impl DbConn, pm_id: Self::IdType) -> Result<usize, Error> {
async fn delete(mut conn: impl GetConn, pm_id: Self::IdType) -> Result<usize, Error> {
diesel::delete(private_message.find(pm_id))
.execute(&mut *conn)
.execute(conn)
.await
}
}
impl PrivateMessage {
pub async fn mark_all_as_read(
mut conn: impl DbConn,
mut conn: impl GetConn,
for_recipient_id: PersonId,
) -> Result<Vec<PrivateMessage>, Error> {
diesel::update(
@ -63,19 +63,19 @@ impl PrivateMessage {
.filter(read.eq(false)),
)
.set(read.eq(true))
.get_results::<Self>(&mut *conn)
.get_results::<Self>(conn)
.await
}
pub async fn read_from_apub_id(
mut conn: impl DbConn,
mut conn: impl GetConn,
object_id: Url,
) -> Result<Option<Self>, LemmyError> {
let object_id: DbUrl = object_id.into();
Ok(
private_message
.filter(ap_id.eq(object_id))
.first::<PrivateMessage>(&mut *conn)
.first::<PrivateMessage>(conn)
.await
.ok()
.map(Into::into),
@ -101,7 +101,7 @@ mod tests {
async fn test_crud() {
let mut conn = build_db_conn_for_tests().await;
let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string())
let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string())
.await
.unwrap();
@ -111,7 +111,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_creator = Person::create(&mut *conn, &creator_form).await.unwrap();
let inserted_creator = Person::create(conn, &creator_form).await.unwrap();
let recipient_form = PersonInsertForm::builder()
.name("recipient_pm".into())
@ -119,7 +119,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_recipient = Person::create(&mut *conn, &recipient_form).await.unwrap();
let inserted_recipient = Person::create(conn, &recipient_form).await.unwrap();
let private_message_form = PrivateMessageInsertForm::builder()
.content("A test private message".into())
@ -127,7 +127,7 @@ mod tests {
.recipient_id(inserted_recipient.id)
.build();
let inserted_private_message = PrivateMessage::create(&mut *conn, &private_message_form)
let inserted_private_message = PrivateMessage::create(conn, &private_message_form)
.await
.unwrap();
@ -144,7 +144,7 @@ mod tests {
local: true,
};
let read_private_message = PrivateMessage::read(&mut *conn, inserted_private_message.id)
let read_private_message = PrivateMessage::read(conn, inserted_private_message.id)
.await
.unwrap();
@ -152,7 +152,7 @@ mod tests {
.content(Some("A test private message".into()))
.build();
let updated_private_message = PrivateMessage::update(
&mut *conn,
conn,
inserted_private_message.id,
&private_message_update_form,
)
@ -160,7 +160,7 @@ mod tests {
.unwrap();
let deleted_private_message = PrivateMessage::update(
&mut *conn,
conn,
inserted_private_message.id,
&PrivateMessageUpdateForm::builder()
.deleted(Some(true))
@ -169,19 +169,19 @@ mod tests {
.await
.unwrap();
let marked_read_private_message = PrivateMessage::update(
&mut *conn,
conn,
inserted_private_message.id,
&PrivateMessageUpdateForm::builder().read(Some(true)).build(),
)
.await
.unwrap();
Person::delete(&mut *conn, inserted_creator.id)
Person::delete(conn, inserted_creator.id)
.await
.unwrap();
Person::delete(&mut *conn, inserted_recipient.id)
Person::delete(conn, inserted_recipient.id)
.await
.unwrap();
Instance::delete(&mut *conn, inserted_instance.id)
Instance::delete(conn, inserted_instance.id)
.await
.unwrap();

View File

@ -3,7 +3,7 @@ use crate::{
schema::private_message_report::dsl::{private_message_report, resolved, resolver_id, updated},
source::private_message_report::{PrivateMessageReport, PrivateMessageReportForm},
traits::Reportable,
utils::{naive_now, DbConn},
utils::{naive_now, GetConn},
};
use diesel::{
dsl::{insert_into, update},
@ -11,7 +11,7 @@ use diesel::{
ExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::utils::RunQueryDsl;
#[async_trait]
impl Reportable for PrivateMessageReport {
@ -19,17 +19,17 @@ impl Reportable for PrivateMessageReport {
type IdType = PrivateMessageReportId;
async fn report(
mut conn: impl DbConn,
mut conn: impl GetConn,
pm_report_form: &PrivateMessageReportForm,
) -> Result<Self, Error> {
insert_into(private_message_report)
.values(pm_report_form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn resolve(
mut conn: impl DbConn,
mut conn: impl GetConn,
report_id: Self::IdType,
by_resolver_id: PersonId,
) -> Result<usize, Error> {
@ -39,12 +39,12 @@ impl Reportable for PrivateMessageReport {
resolver_id.eq(by_resolver_id),
updated.eq(naive_now()),
))
.execute(&mut *conn)
.execute(conn)
.await
}
async fn unresolve(
mut conn: impl DbConn,
mut conn: impl GetConn,
report_id: Self::IdType,
by_resolver_id: PersonId,
) -> Result<usize, Error> {
@ -54,7 +54,7 @@ impl Reportable for PrivateMessageReport {
resolver_id.eq(by_resolver_id),
updated.eq(naive_now()),
))
.execute(&mut *conn)
.execute(conn)
.await
}
}

View File

@ -7,10 +7,10 @@ use crate::{
RegistrationApplicationUpdateForm,
},
traits::Crud,
utils::DbConn,
utils::GetConn,
};
use diesel::{insert_into, result::Error, ExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::utils::RunQueryDsl;
#[async_trait]
impl Crud for RegistrationApplication {
@ -18,46 +18,46 @@ impl Crud for RegistrationApplication {
type UpdateForm = RegistrationApplicationUpdateForm;
type IdType = i32;
async fn create(mut conn: impl DbConn, form: &Self::InsertForm) -> Result<Self, Error> {
async fn create(mut conn: impl GetConn, form: &Self::InsertForm) -> Result<Self, Error> {
insert_into(registration_application)
.values(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn read(mut conn: impl DbConn, id_: Self::IdType) -> Result<Self, Error> {
async fn read(mut conn: impl GetConn, id_: Self::IdType) -> Result<Self, Error> {
registration_application
.find(id_)
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await
}
async fn update(
mut conn: impl DbConn,
mut conn: impl GetConn,
id_: Self::IdType,
form: &Self::UpdateForm,
) -> Result<Self, Error> {
diesel::update(registration_application.find(id_))
.set(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn delete(mut conn: impl DbConn, id_: Self::IdType) -> Result<usize, Error> {
async fn delete(mut conn: impl GetConn, id_: Self::IdType) -> Result<usize, Error> {
diesel::delete(registration_application.find(id_))
.execute(&mut *conn)
.execute(conn)
.await
}
}
impl RegistrationApplication {
pub async fn find_by_local_user_id(
mut conn: impl DbConn,
mut conn: impl GetConn,
local_user_id_: LocalUserId,
) -> Result<Self, Error> {
registration_application
.filter(local_user_id.eq(local_user_id_))
.first::<Self>(&mut *conn)
.first::<Self>(conn)
.await
}
}

View File

@ -1,15 +1,15 @@
use crate::{schema::secret::dsl::secret, source::secret::Secret, utils::DbConn};
use crate::{schema::secret::dsl::secret, source::secret::Secret, utils::GetConn};
use diesel::result::Error;
use diesel_async::RunQueryDsl;
use lemmy_db_schema::utils::RunQueryDsl;
impl Secret {
/// Initialize the Secrets from the DB.
/// Warning: You should only call this once.
pub async fn init(mut conn: impl DbConn) -> Result<Secret, Error> {
Self::read_secrets(&mut *conn).await
pub async fn init(mut conn: impl GetConn) -> Result<Secret, Error> {
Self::read_secrets(conn).await
}
async fn read_secrets(mut conn: impl DbConn) -> Result<Secret, Error> {
secret.first::<Secret>(&mut *conn).await
async fn read_secrets(mut conn: impl GetConn) -> Result<Secret, Error> {
secret.first::<Secret>(conn).await
}
}

View File

@ -6,10 +6,10 @@ use crate::{
site::{Site, SiteInsertForm, SiteUpdateForm},
},
traits::Crud,
utils::DbConn,
utils::GetConn,
};
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::utils::RunQueryDsl;
use url::Url;
#[async_trait]
@ -19,13 +19,13 @@ impl Crud for Site {
type IdType = SiteId;
/// Use SiteView::read_local, or Site::read_from_apub_id instead
async fn read(_conn: impl DbConn, _site_id: SiteId) -> Result<Self, Error> {
async fn read(_conn: impl GetConn, _site_id: SiteId) -> Result<Self, Error> {
unimplemented!()
}
async fn create(mut conn: impl DbConn, form: &Self::InsertForm) -> Result<Self, Error> {
async fn create(mut conn: impl GetConn, form: &Self::InsertForm) -> Result<Self, Error> {
let is_new_site = match &form.actor_id {
Some(id_) => Site::read_from_apub_id(&mut *conn, id_).await?.is_none(),
Some(id_) => Site::read_from_apub_id(conn, id_).await?.is_none(),
None => true,
};
@ -35,42 +35,42 @@ impl Crud for Site {
.on_conflict(actor_id)
.do_update()
.set(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await?;
// initialize languages if site is newly created
if is_new_site {
// initialize with all languages
SiteLanguage::update(&mut *conn, vec![], &site_).await?;
SiteLanguage::update(conn, vec![], &site_).await?;
}
Ok(site_)
}
async fn update(
mut conn: impl DbConn,
mut conn: impl GetConn,
site_id: SiteId,
new_site: &Self::UpdateForm,
) -> Result<Self, Error> {
diesel::update(site.find(site_id))
.set(new_site)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await
}
async fn delete(mut conn: impl DbConn, site_id: SiteId) -> Result<usize, Error> {
diesel::delete(site.find(site_id)).execute(&mut *conn).await
async fn delete(mut conn: impl GetConn, site_id: SiteId) -> Result<usize, Error> {
diesel::delete(site.find(site_id)).execute(conn).await
}
}
impl Site {
pub async fn read_from_apub_id(
mut conn: impl DbConn,
mut conn: impl GetConn,
object_id: &DbUrl,
) -> Result<Option<Self>, Error> {
Ok(
site
.filter(actor_id.eq(object_id))
.first::<Site>(&mut *conn)
.first::<Site>(conn)
.await
.ok()
.map(Into::into),
@ -78,11 +78,11 @@ impl Site {
}
// TODO this needs fixed
pub async fn read_remote_sites(mut conn: impl DbConn) -> Result<Vec<Self>, Error> {
pub async fn read_remote_sites(mut conn: impl GetConn) -> Result<Vec<Self>, Error> {
site
.order_by(id)
.offset(1)
.get_results::<Self>(&mut *conn)
.get_results::<Self>(conn)
.await
}

View File

@ -2,14 +2,14 @@ use crate::{
newtypes::LocalSiteId,
schema::tagline::dsl::{local_site_id, tagline},
source::tagline::{Tagline, TaglineForm},
utils::DbConn,
utils::GetConn,
};
use diesel::{insert_into, result::Error, ExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::utils::RunQueryDsl;
impl Tagline {
pub async fn replace(
mut conn: impl DbConn,
mut conn: impl GetConn,
for_local_site_id: LocalSiteId,
list_content: Option<Vec<String>>,
) -> Result<Vec<Self>, Error> {
@ -18,7 +18,7 @@ impl Tagline {
.build_transaction()
.run(|conn| {
Box::pin(async move {
Self::clear(&mut *conn).await?;
Self::clear(conn).await?;
for item in list {
let form = TaglineForm {
@ -28,35 +28,35 @@ impl Tagline {
};
insert_into(tagline)
.values(form)
.get_result::<Self>(&mut *conn)
.get_result::<Self>(conn)
.await?;
}
Self::get_all_conn(&mut *conn, for_local_site_id).await
Self::get_all_conn(conn, for_local_site_id).await
}) as _
})
.await
} else {
Self::get_all_conn(&mut *conn, for_local_site_id).await
Self::get_all_conn(conn, for_local_site_id).await
}
}
async fn clear(mut conn: impl DbConn) -> Result<usize, Error> {
diesel::delete(tagline).execute(&mut *conn).await
async fn clear(mut conn: impl GetConn) -> Result<usize, Error> {
diesel::delete(tagline).execute(conn).await
}
async fn get_all_conn(
mut conn: impl DbConn,
mut conn: impl GetConn,
for_local_site_id: LocalSiteId,
) -> Result<Vec<Self>, Error> {
tagline
.filter(local_site_id.eq(for_local_site_id))
.get_results::<Self>(&mut *conn)
.get_results::<Self>(conn)
.await
}
pub async fn get_all(
mut conn: impl DbConn,
mut conn: impl GetConn,
for_local_site_id: LocalSiteId,
) -> Result<Vec<Self>, Error> {
Self::get_all_conn(&mut *conn, for_local_site_id).await
Self::get_all_conn(conn, for_local_site_id).await
}
}

View File

@ -1,6 +1,6 @@
use crate::{
newtypes::{CommunityId, DbUrl, PersonId},
utils::DbConn,
utils::GetConn,
};
use diesel::result::Error;
@ -9,21 +9,21 @@ pub trait Crud {
type InsertForm;
type UpdateForm;
type IdType;
async fn create(mut conn: impl DbConn, form: &Self::InsertForm) -> Result<Self, Error>
async fn create(mut conn: impl GetConn, form: &Self::InsertForm) -> Result<Self, Error>
where
Self: Sized;
async fn read(mut conn: impl DbConn, id: Self::IdType) -> Result<Self, Error>
async fn read(mut conn: impl GetConn, id: Self::IdType) -> Result<Self, Error>
where
Self: Sized;
/// when you want to null out a column, you have to send Some(None)), since sending None means you just don't want to update that column.
async fn update(
mut conn: impl DbConn,
mut conn: impl GetConn,
id: Self::IdType,
form: &Self::UpdateForm,
) -> Result<Self, Error>
where
Self: Sized;
async fn delete(_conn: impl DbConn, _id: Self::IdType) -> Result<usize, Error>
async fn delete(_conn: impl GetConn, _id: Self::IdType) -> Result<usize, Error>
where
Self: Sized,
Self::IdType: Send,
@ -35,17 +35,17 @@ pub trait Crud {
#[async_trait]
pub trait Followable {
type Form;
async fn follow(mut conn: impl DbConn, form: &Self::Form) -> Result<Self, Error>
async fn follow(mut conn: impl GetConn, form: &Self::Form) -> Result<Self, Error>
where
Self: Sized;
async fn follow_accepted(
mut conn: impl DbConn,
mut conn: impl GetConn,
community_id: CommunityId,
person_id: PersonId,
) -> Result<Self, Error>
where
Self: Sized;
async fn unfollow(mut conn: impl DbConn, form: &Self::Form) -> Result<usize, Error>
async fn unfollow(mut conn: impl GetConn, form: &Self::Form) -> Result<usize, Error>
where
Self: Sized;
}
@ -53,10 +53,10 @@ pub trait Followable {
#[async_trait]
pub trait Joinable {
type Form;
async fn join(mut conn: impl DbConn, form: &Self::Form) -> Result<Self, Error>
async fn join(mut conn: impl GetConn, form: &Self::Form) -> Result<Self, Error>
where
Self: Sized;
async fn leave(mut conn: impl DbConn, form: &Self::Form) -> Result<usize, Error>
async fn leave(mut conn: impl GetConn, form: &Self::Form) -> Result<usize, Error>
where
Self: Sized;
}
@ -65,11 +65,11 @@ pub trait Joinable {
pub trait Likeable {
type Form;
type IdType;
async fn like(mut conn: impl DbConn, form: &Self::Form) -> Result<Self, Error>
async fn like(mut conn: impl GetConn, form: &Self::Form) -> Result<Self, Error>
where
Self: Sized;
async fn remove(
mut conn: impl DbConn,
mut conn: impl GetConn,
person_id: PersonId,
item_id: Self::IdType,
) -> Result<usize, Error>
@ -80,10 +80,10 @@ pub trait Likeable {
#[async_trait]
pub trait Bannable {
type Form;
async fn ban(mut conn: impl DbConn, form: &Self::Form) -> Result<Self, Error>
async fn ban(mut conn: impl GetConn, form: &Self::Form) -> Result<Self, Error>
where
Self: Sized;
async fn unban(mut conn: impl DbConn, form: &Self::Form) -> Result<usize, Error>
async fn unban(mut conn: impl GetConn, form: &Self::Form) -> Result<usize, Error>
where
Self: Sized;
}
@ -91,10 +91,10 @@ pub trait Bannable {
#[async_trait]
pub trait Saveable {
type Form;
async fn save(mut conn: impl DbConn, form: &Self::Form) -> Result<Self, Error>
async fn save(mut conn: impl GetConn, form: &Self::Form) -> Result<Self, Error>
where
Self: Sized;
async fn unsave(mut conn: impl DbConn, form: &Self::Form) -> Result<usize, Error>
async fn unsave(mut conn: impl GetConn, form: &Self::Form) -> Result<usize, Error>
where
Self: Sized;
}
@ -102,10 +102,10 @@ pub trait Saveable {
#[async_trait]
pub trait Blockable {
type Form;
async fn block(mut conn: impl DbConn, form: &Self::Form) -> Result<Self, Error>
async fn block(mut conn: impl GetConn, form: &Self::Form) -> Result<Self, Error>
where
Self: Sized;
async fn unblock(mut conn: impl DbConn, form: &Self::Form) -> Result<usize, Error>
async fn unblock(mut conn: impl GetConn, form: &Self::Form) -> Result<usize, Error>
where
Self: Sized;
}
@ -113,10 +113,10 @@ pub trait Blockable {
#[async_trait]
pub trait Readable {
type Form;
async fn mark_as_read(mut conn: impl DbConn, form: &Self::Form) -> Result<Self, Error>
async fn mark_as_read(mut conn: impl GetConn, form: &Self::Form) -> Result<Self, Error>
where
Self: Sized;
async fn mark_as_unread(mut conn: impl DbConn, form: &Self::Form) -> Result<usize, Error>
async fn mark_as_unread(mut conn: impl GetConn, form: &Self::Form) -> Result<usize, Error>
where
Self: Sized;
}
@ -125,18 +125,18 @@ pub trait Readable {
pub trait Reportable {
type Form;
type IdType;
async fn report(mut conn: impl DbConn, form: &Self::Form) -> Result<Self, Error>
async fn report(mut conn: impl GetConn, form: &Self::Form) -> Result<Self, Error>
where
Self: Sized;
async fn resolve(
mut conn: impl DbConn,
mut conn: impl GetConn,
report_id: Self::IdType,
resolver_id: PersonId,
) -> Result<usize, Error>
where
Self: Sized;
async fn unresolve(
mut conn: impl DbConn,
mut conn: impl GetConn,
report_id: Self::IdType,
resolver_id: PersonId,
) -> Result<usize, Error>
@ -154,7 +154,7 @@ pub trait JoinView {
#[async_trait]
pub trait ApubActor {
async fn read_from_apub_id(
mut conn: impl DbConn,
mut conn: impl GetConn,
object_id: &DbUrl,
) -> Result<Option<Self>, Error>
where
@ -162,14 +162,14 @@ pub trait ApubActor {
/// - actor_name is the name of the community or user to read.
/// - include_deleted, if true, will return communities or users that were deleted/removed
async fn read_from_name(
mut conn: impl DbConn,
mut conn: impl GetConn,
actor_name: &str,
include_deleted: bool,
) -> Result<Self, Error>
where
Self: Sized;
async fn read_from_name_and_domain(
mut conn: impl DbConn,
mut conn: impl GetConn,
actor_name: &str,
protocol_domain: &str,
) -> Result<Self, Error>

View File

@ -23,6 +23,7 @@ use diesel_async::{
deadpool::{Object as PooledConnection, Pool},
AsyncDieselConnectionManager,
},
scoped_futures::ScopedBoxFuture,
};
use diesel_migrations::EmbeddedMigrations;
use futures_util::{future::BoxFuture, FutureExt};
@ -49,8 +50,93 @@ const POOL_TIMEOUT: Option<Duration> = Some(Duration::from_secs(5));
pub type DbPool = Pool<AsyncPgConnection>;
pub type DbPooledConn = PooledConnection<AsyncPgConnection>;
pub trait DbConn: std::ops::DerefMut<Target = AsyncPgConnection> + Send {}
impl<T: std::ops::DerefMut<Target = AsyncPgConnection> + Send> DbConn for T {}
#[async_trait]
pub trait GetConn {
type Conn: std::ops::DerefMut<Target = AsyncPgConnection> + Send;
async fn conn(self) -> Result<Self::Conn, DieselError>;
async fn transaction<'a, R, E, F>(&mut self, callback: F) -> Result<R, E>
where
F: for<'r> FnOnce(&'r mut Self::Conn) -> ScopedBoxFuture<'a, 'r, Result<R, E>> + Send + 'a,
E: From<diesel::result::Error> + Send + 'a,
R: Send + 'a,
{
let conn = self.conn().await?;
conn.transaction(callback).await?
}
}
#[async_trait]
impl<'a> GetConn for &'a mut AsyncPgConnection {
type Conn = Self;
async fn conn(self) -> Result<Self::Conn, DieselError> {
Ok(self)
}
}
#[async_trait]
impl<'a> GetConn for &'a DbPool {
type Conn = PooledConnection<AsyncPgConnection>;
async fn conn(self) -> Result<Self::Conn, DieselError> {
get_conn(self).await
}
}
#[async_trait]
pub trait RunQueryDsl<Conn: GetConn>: diesel_async::RunQueryDsl<Conn::Conn> {
async fn execute<'conn, 'query>(self, conn: Conn) -> Result<usize, DieselError>
where
Self: diesel_async::methods::ExecuteDsl<Conn::Conn> + 'query,
{
Ok(diesel_async::RunQueryDsl::execute(self, &mut conn.conn().await?).await?)
}
async fn load<'query, 'conn, U>(self, conn: Conn) -> Result<Vec<U>, DieselError>
where
U: Send,
Self: diesel_async::methods::LoadQuery<'query, Conn::Conn, U> + 'query,
{
Ok(diesel_async::RunQueryDsl::load(self, &mut conn.conn().await?).await?)
}
/* unsued
async fn load_stream<'conn, 'query, U>(
self,
conn: Conn,
) -> <Self::LoadFuture<'conn> as Future>::Output
where
U: 'conn,
Self: diesel_async::methods::LoadQuery<'query, Conn::Conn, U> + 'query,
{
Ok(diesel_async::RunQueryDsl::load_stream(self, &mut conn.conn().await?).await?)
}*/
async fn get_result<'query, 'conn, U>(self, conn: Conn) -> Result<U, DieselError>
where
U: Send + 'conn,
Self: diesel_async::methods::LoadQuery<'query, Conn::Conn, U> + 'query,
{
Ok(diesel_async::RunQueryDsl::get_result(self, &mut conn.conn().await?).await?)
}
async fn get_results<'query, 'conn, U>(self, conn: Conn) -> Result<Vec<U>, DieselError>
where
U: Send,
Self: diesel_async::methods::LoadQuery<'query, Conn::Conn, U> + 'query,
{
Ok(diesel_async::RunQueryDsl::get_results(self, &mut conn.conn().await?).await?)
}
async fn first<'query, 'conn, U>(self, conn: Conn) -> Result<U, DieselError>
where
U: Send + 'conn,
Self: diesel::query_dsl::methods::LimitDsl,
diesel::helper_types::Limit<Self>:
diesel_async::methods::LoadQuery<'query, Conn::Conn, U> + Send + 'query,
{
Ok(diesel_async::RunQueryDsl::execute(self, &mut conn.conn().await?).await?)
}
}
impl<Conn: GetConn, T> RunQueryDsl<Conn> for T where T: diesel_async::RunQueryDsl<Conn::Conn> {}
pub async fn get_conn(pool: &DbPool) -> Result<PooledConnection<AsyncPgConnection>, DieselError> {
pool.get().await.map_err(|e| QueryBuilderError(e.into()))
@ -220,7 +306,7 @@ pub fn run_migrations(db_url: &str) {
let mut conn =
PgConnection::establish(db_url).unwrap_or_else(|e| panic!("Error connecting to {db_url}: {e}"));
info!("Running Database migrations (This may take a long time)...");
let _ = &mut *conn
let _ = conn
.run_pending_migrations(MIGRATIONS)
.unwrap_or_else(|e| panic!("Couldn't run DB Migrations: {e}"));
info!("Database migrations complete.");

View File

@ -8,7 +8,6 @@ use diesel::{
NullableExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
aggregates::structs::CommentAggregates,
newtypes::{CommentReportId, CommunityId, PersonId},
@ -31,7 +30,7 @@ use lemmy_db_schema::{
post::Post,
},
traits::JoinView,
utils::{limit_and_offset, DbConn},
utils::{limit_and_offset, GetConn, RunQueryDsl},
};
use typed_builder::TypedBuilder;
@ -40,7 +39,7 @@ impl CommentReportView {
///
/// * `report_id` - the report id to obtain
pub async fn read(
mut conn: impl DbConn,
mut conn: impl GetConn,
report_id: CommentReportId,
my_person_id: PersonId,
) -> Result<Self, Error> {
@ -86,7 +85,7 @@ impl CommentReportView {
comment_like::score.nullable(),
person_alias_2.fields(person::all_columns).nullable(),
))
.first::<<CommentReportView as JoinView>::JoinTuple>(&mut *conn)
.first::<<CommentReportView as JoinView>::JoinTuple>(conn)
.await?;
Ok(Self::from_tuple(res))
@ -94,7 +93,7 @@ impl CommentReportView {
/// Returns the current unresolved post report count for the communities you mod
pub async fn get_report_count(
mut conn: impl DbConn,
mut conn: impl GetConn,
my_person_id: PersonId,
admin: bool,
community_id: Option<CommunityId>,
@ -122,12 +121,12 @@ impl CommentReportView {
),
)
.select(count(comment_report::id))
.first::<i64>(&mut *conn)
.first::<i64>(conn)
.await
} else {
query
.select(count(comment_report::id))
.first::<i64>(&mut *conn)
.first::<i64>(conn)
.await
}
}
@ -148,7 +147,7 @@ pub struct CommentReportQuery<Conn> {
unresolved_only: Option<bool>,
}
impl<Conn: DbConn> CommentReportQuery<Conn> {
impl<Conn: GetConn> CommentReportQuery<Conn> {
pub async fn list(self) -> Result<Vec<CommentReportView>, Error> {
let mut conn = self.conn;
@ -225,11 +224,11 @@ impl<Conn: DbConn> CommentReportQuery<Conn> {
.and(community_moderator::person_id.eq(self.my_person_id)),
),
)
.load::<<CommentReportView as JoinView>::JoinTuple>(&mut *conn)
.load::<<CommentReportView as JoinView>::JoinTuple>(conn)
.await?
} else {
query
.load::<<CommentReportView as JoinView>::JoinTuple>(&mut *conn)
.load::<<CommentReportView as JoinView>::JoinTuple>(conn)
.await?
};
@ -290,7 +289,7 @@ mod tests {
async fn test_crud() {
let mut conn = build_db_conn_for_tests().await;
let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string())
let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string())
.await
.unwrap();
@ -300,7 +299,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_timmy = Person::create(&mut *conn, &new_person).await.unwrap();
let inserted_timmy = Person::create(conn, &new_person).await.unwrap();
let new_person_2 = PersonInsertForm::builder()
.name("sara_crv".into())
@ -308,7 +307,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_sara = Person::create(&mut *conn, &new_person_2).await.unwrap();
let inserted_sara = Person::create(conn, &new_person_2).await.unwrap();
// Add a third person, since new ppl can only report something once.
let new_person_3 = PersonInsertForm::builder()
@ -317,7 +316,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_jessica = Person::create(&mut *conn, &new_person_3).await.unwrap();
let inserted_jessica = Person::create(conn, &new_person_3).await.unwrap();
let new_community = CommunityInsertForm::builder()
.name("test community crv".to_string())
@ -326,7 +325,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_community = Community::create(&mut *conn, &new_community).await.unwrap();
let inserted_community = Community::create(conn, &new_community).await.unwrap();
// Make timmy a mod
let timmy_moderator_form = CommunityModeratorForm {
@ -334,7 +333,7 @@ mod tests {
person_id: inserted_timmy.id,
};
let _inserted_moderator = CommunityModerator::join(&mut *conn, &timmy_moderator_form)
let _inserted_moderator = CommunityModerator::join(conn, &timmy_moderator_form)
.await
.unwrap();
@ -344,7 +343,7 @@ mod tests {
.community_id(inserted_community.id)
.build();
let inserted_post = Post::create(&mut *conn, &new_post).await.unwrap();
let inserted_post = Post::create(conn, &new_post).await.unwrap();
let comment_form = CommentInsertForm::builder()
.content("A test comment 32".into())
@ -352,7 +351,7 @@ mod tests {
.post_id(inserted_post.id)
.build();
let inserted_comment = Comment::create(&mut *conn, &comment_form, None)
let inserted_comment = Comment::create(conn, &comment_form, None)
.await
.unwrap();
@ -364,7 +363,7 @@ mod tests {
reason: "from sara".into(),
};
let inserted_sara_report = CommentReport::report(&mut *conn, &sara_report_form)
let inserted_sara_report = CommentReport::report(conn, &sara_report_form)
.await
.unwrap();
@ -376,16 +375,16 @@ mod tests {
reason: "from jessica".into(),
};
let inserted_jessica_report = CommentReport::report(&mut *conn, &jessica_report_form)
let inserted_jessica_report = CommentReport::report(conn, &jessica_report_form)
.await
.unwrap();
let agg = CommentAggregates::read(&mut *conn, inserted_comment.id)
let agg = CommentAggregates::read(conn, inserted_comment.id)
.await
.unwrap();
let read_jessica_report_view =
CommentReportView::read(&mut *conn, inserted_jessica_report.id, inserted_timmy.id)
CommentReportView::read(conn, inserted_jessica_report.id, inserted_timmy.id)
.await
.unwrap();
let expected_jessica_report_view = CommentReportView {
@ -512,7 +511,7 @@ mod tests {
// Do a batch read of timmys reports
let reports = CommentReportQuery::builder()
.conn(&mut *conn)
.conn(conn)
.my_person_id(inserted_timmy.id)
.admin(false)
.build()
@ -530,17 +529,17 @@ mod tests {
// Make sure the counts are correct
let report_count =
CommentReportView::get_report_count(&mut *conn, inserted_timmy.id, false, None)
CommentReportView::get_report_count(conn, inserted_timmy.id, false, None)
.await
.unwrap();
assert_eq!(2, report_count);
// Try to resolve the report
CommentReport::resolve(&mut *conn, inserted_jessica_report.id, inserted_timmy.id)
CommentReport::resolve(conn, inserted_jessica_report.id, inserted_timmy.id)
.await
.unwrap();
let read_jessica_report_view_after_resolve =
CommentReportView::read(&mut *conn, inserted_jessica_report.id, inserted_timmy.id)
CommentReportView::read(conn, inserted_jessica_report.id, inserted_timmy.id)
.await
.unwrap();
@ -589,7 +588,7 @@ mod tests {
// Do a batch read of timmys reports
// It should only show saras, which is unresolved
let reports_after_resolve = CommentReportQuery::builder()
.conn(&mut *conn)
.conn(conn)
.my_person_id(inserted_timmy.id)
.admin(false)
.unresolved_only(Some(true))
@ -602,20 +601,20 @@ mod tests {
// Make sure the counts are correct
let report_count_after_resolved =
CommentReportView::get_report_count(&mut *conn, inserted_timmy.id, false, None)
CommentReportView::get_report_count(conn, inserted_timmy.id, false, None)
.await
.unwrap();
assert_eq!(1, report_count_after_resolved);
Person::delete(&mut *conn, inserted_timmy.id).await.unwrap();
Person::delete(&mut *conn, inserted_sara.id).await.unwrap();
Person::delete(&mut *conn, inserted_jessica.id)
Person::delete(conn, inserted_timmy.id).await.unwrap();
Person::delete(conn, inserted_sara.id).await.unwrap();
Person::delete(conn, inserted_jessica.id)
.await
.unwrap();
Community::delete(&mut *conn, inserted_community.id)
Community::delete(conn, inserted_community.id)
.await
.unwrap();
Instance::delete(&mut *conn, inserted_instance.id)
Instance::delete(conn, inserted_instance.id)
.await
.unwrap();
}

View File

@ -8,7 +8,6 @@ use diesel::{
PgTextExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl;
use diesel_ltree::{nlevel, subpath, Ltree, LtreeExtensions};
use lemmy_db_schema::{
aggregates::structs::CommentAggregates,
@ -36,7 +35,7 @@ use lemmy_db_schema::{
post::Post,
},
traits::JoinView,
utils::{fuzzy_search, limit_and_offset, DbConn},
utils::{fuzzy_search, limit_and_offset, GetConn, RunQueryDsl},
CommentSortType,
ListingType,
};
@ -57,7 +56,7 @@ type CommentViewTuple = (
impl CommentView {
pub async fn read(
mut conn: impl DbConn,
mut conn: impl GetConn,
comment_id: CommentId,
my_person_id: Option<PersonId>,
) -> Result<Self, Error> {
@ -128,7 +127,7 @@ impl CommentView {
person_block::all_columns.nullable(),
comment_like::score.nullable(),
))
.first::<CommentViewTuple>(&mut *conn)
.first::<CommentViewTuple>(conn)
.await?;
// If a person is given, then my_vote, if None, should be 0, not null
@ -174,7 +173,7 @@ pub struct CommentQuery<'a, Conn> {
max_depth: Option<i32>,
}
impl<'a, Conn: DbConn> CommentQuery<'a, Conn> {
impl<'a, Conn: GetConn> CommentQuery<'a, Conn> {
pub async fn list(self) -> Result<Vec<CommentView>, Error> {
let mut conn = self.conn;
@ -357,7 +356,7 @@ impl<'a, Conn: DbConn> CommentQuery<'a, Conn> {
let res = query
.limit(limit)
.offset(offset)
.load::<CommentViewTuple>(&mut *conn)
.load::<CommentViewTuple>(conn)
.await?;
Ok(res.into_iter().map(CommentView::from_tuple).collect())
@ -411,7 +410,7 @@ mod tests {
post::PostInsertForm,
},
traits::{Blockable, Crud, Likeable},
utils::{build_db_conn_for_tests, DbConn},
utils::{build_db_conn_for_tests, GetConn},
SubscribedType,
};
use serial_test::serial;
@ -428,8 +427,8 @@ mod tests {
inserted_community: Community,
}
async fn init_data(mut conn: impl DbConn) -> Data {
let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string())
async fn init_data(mut conn: impl GetConn) -> Data {
let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string())
.await
.unwrap();
@ -438,12 +437,12 @@ mod tests {
.public_key("pubkey".to_string())
.instance_id(inserted_instance.id)
.build();
let inserted_person = Person::create(&mut *conn, &new_person).await.unwrap();
let inserted_person = Person::create(conn, &new_person).await.unwrap();
let local_user_form = LocalUserInsertForm::builder()
.person_id(inserted_person.id)
.password_encrypted(String::new())
.build();
let inserted_local_user = LocalUser::create(&mut *conn, &local_user_form)
let inserted_local_user = LocalUser::create(conn, &local_user_form)
.await
.unwrap();
@ -452,7 +451,7 @@ mod tests {
.public_key("pubkey".to_string())
.instance_id(inserted_instance.id)
.build();
let inserted_person_2 = Person::create(&mut *conn, &new_person_2).await.unwrap();
let inserted_person_2 = Person::create(conn, &new_person_2).await.unwrap();
let new_community = CommunityInsertForm::builder()
.name("test community 5".to_string())
@ -461,7 +460,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_community = Community::create(&mut *conn, &new_community).await.unwrap();
let inserted_community = Community::create(conn, &new_community).await.unwrap();
let new_post = PostInsertForm::builder()
.name("A test post 2".into())
@ -469,8 +468,8 @@ mod tests {
.community_id(inserted_community.id)
.build();
let inserted_post = Post::create(&mut *conn, &new_post).await.unwrap();
let english_id = Language::read_id_from_code(&mut *conn, Some("en"))
let inserted_post = Post::create(conn, &new_post).await.unwrap();
let english_id = Language::read_id_from_code(conn, Some("en"))
.await
.unwrap();
@ -489,7 +488,7 @@ mod tests {
.language_id(english_id)
.build();
let inserted_comment_0 = Comment::create(&mut *conn, &comment_form_0, None)
let inserted_comment_0 = Comment::create(conn, &comment_form_0, None)
.await
.unwrap();
@ -501,11 +500,11 @@ mod tests {
.build();
let inserted_comment_1 =
Comment::create(&mut *conn, &comment_form_1, Some(&inserted_comment_0.path))
Comment::create(conn, &comment_form_1, Some(&inserted_comment_0.path))
.await
.unwrap();
let finnish_id = Language::read_id_from_code(&mut *conn, Some("fi"))
let finnish_id = Language::read_id_from_code(conn, Some("fi"))
.await
.unwrap();
let comment_form_2 = CommentInsertForm::builder()
@ -516,7 +515,7 @@ mod tests {
.build();
let inserted_comment_2 =
Comment::create(&mut *conn, &comment_form_2, Some(&inserted_comment_0.path))
Comment::create(conn, &comment_form_2, Some(&inserted_comment_0.path))
.await
.unwrap();
@ -528,11 +527,11 @@ mod tests {
.build();
let _inserted_comment_3 =
Comment::create(&mut *conn, &comment_form_3, Some(&inserted_comment_1.path))
Comment::create(conn, &comment_form_3, Some(&inserted_comment_1.path))
.await
.unwrap();
let polish_id = Language::read_id_from_code(&mut *conn, Some("pl"))
let polish_id = Language::read_id_from_code(conn, Some("pl"))
.await
.unwrap()
.unwrap();
@ -544,7 +543,7 @@ mod tests {
.build();
let inserted_comment_4 =
Comment::create(&mut *conn, &comment_form_4, Some(&inserted_comment_1.path))
Comment::create(conn, &comment_form_4, Some(&inserted_comment_1.path))
.await
.unwrap();
@ -555,7 +554,7 @@ mod tests {
.build();
let _inserted_comment_5 =
Comment::create(&mut *conn, &comment_form_5, Some(&inserted_comment_4.path))
Comment::create(conn, &comment_form_5, Some(&inserted_comment_4.path))
.await
.unwrap();
@ -564,7 +563,7 @@ mod tests {
target_id: inserted_person_2.id,
};
let inserted_block = PersonBlock::block(&mut *conn, &timmy_blocks_sara_form)
let inserted_block = PersonBlock::block(conn, &timmy_blocks_sara_form)
.await
.unwrap();
@ -583,7 +582,7 @@ mod tests {
score: 1,
};
let _inserted_comment_like = CommentLike::like(&mut *conn, &comment_like_form)
let _inserted_comment_like = CommentLike::like(conn, &comment_like_form)
.await
.unwrap();
@ -604,15 +603,15 @@ mod tests {
#[serial]
async fn test_crud() {
let mut conn = build_db_conn_for_tests().await;
let data = init_data(&mut *conn).await;
let data = init_data(conn).await;
let expected_comment_view_no_person = expected_comment_view(&data, &mut *conn).await;
let expected_comment_view_no_person = expected_comment_view(&data, conn).await;
let mut expected_comment_view_with_person = expected_comment_view_no_person.clone();
expected_comment_view_with_person.my_vote = Some(1);
let read_comment_views_no_person = CommentQuery::builder()
.conn(&mut *conn)
.conn(conn)
.sort(Some(CommentSortType::Old))
.post_id(Some(data.inserted_post.id))
.build()
@ -626,7 +625,7 @@ mod tests {
);
let read_comment_views_with_person = CommentQuery::builder()
.conn(&mut *conn)
.conn(conn)
.sort(Some(CommentSortType::Old))
.post_id(Some(data.inserted_post.id))
.local_user(Some(&data.inserted_local_user))
@ -644,7 +643,7 @@ mod tests {
assert_eq!(5, read_comment_views_with_person.len());
let read_comment_from_blocked_person = CommentView::read(
&mut *conn,
conn,
data.inserted_comment_1.id,
Some(data.inserted_person.id),
)
@ -654,18 +653,18 @@ mod tests {
// Make sure block set the creator blocked
assert!(read_comment_from_blocked_person.creator_blocked);
cleanup(data, &mut *conn).await;
cleanup(data, conn).await;
}
#[tokio::test]
#[serial]
async fn test_comment_tree() {
let mut conn = build_db_conn_for_tests().await;
let data = init_data(&mut *conn).await;
let data = init_data(conn).await;
let top_path = data.inserted_comment_0.path.clone();
let read_comment_views_top_path = CommentQuery::builder()
.conn(&mut *conn)
.conn(conn)
.post_id(Some(data.inserted_post.id))
.parent_path(Some(top_path))
.build()
@ -675,7 +674,7 @@ mod tests {
let child_path = data.inserted_comment_1.path.clone();
let read_comment_views_child_path = CommentQuery::builder()
.conn(&mut *conn)
.conn(conn)
.post_id(Some(data.inserted_post.id))
.parent_path(Some(child_path))
.build()
@ -696,7 +695,7 @@ mod tests {
assert!(!child_comments.contains(&data.inserted_comment_2));
let read_comment_views_top_max_depth = CommentQuery::builder()
.conn(&mut *conn)
.conn(conn)
.post_id(Some(data.inserted_post.id))
.max_depth(Some(1))
.build()
@ -706,14 +705,14 @@ mod tests {
// Make sure a depth limited one only has the top comment
assert_eq!(
expected_comment_view(&data, &mut *conn).await,
expected_comment_view(&data, conn).await,
read_comment_views_top_max_depth[0]
);
assert_eq!(1, read_comment_views_top_max_depth.len());
let child_path = data.inserted_comment_1.path.clone();
let read_comment_views_parent_max_depth = CommentQuery::builder()
.conn(&mut *conn)
.conn(conn)
.post_id(Some(data.inserted_post.id))
.parent_path(Some(child_path))
.max_depth(Some(1))
@ -730,19 +729,19 @@ mod tests {
.eq("Comment 3"));
assert_eq!(3, read_comment_views_parent_max_depth.len());
cleanup(data, &mut *conn).await;
cleanup(data, conn).await;
}
#[tokio::test]
#[serial]
async fn test_languages() {
let mut conn = build_db_conn_for_tests().await;
let data = init_data(&mut *conn).await;
let data = init_data(conn).await;
// by default, user has all languages enabled and should see all comments
// (except from blocked user)
let all_languages = CommentQuery::builder()
.conn(&mut *conn)
.conn(conn)
.local_user(Some(&data.inserted_local_user))
.build()
.list()
@ -751,15 +750,15 @@ mod tests {
assert_eq!(5, all_languages.len());
// change user lang to finnish, should only show one post in finnish and one undetermined
let finnish_id = Language::read_id_from_code(&mut *conn, Some("fi"))
let finnish_id = Language::read_id_from_code(conn, Some("fi"))
.await
.unwrap()
.unwrap();
LocalUserLanguage::update(&mut *conn, vec![finnish_id], data.inserted_local_user.id)
LocalUserLanguage::update(conn, vec![finnish_id], data.inserted_local_user.id)
.await
.unwrap();
let finnish_comments = CommentQuery::builder()
.conn(&mut *conn)
.conn(conn)
.local_user(Some(&data.inserted_local_user))
.build()
.list()
@ -777,14 +776,14 @@ mod tests {
// now show all comments with undetermined language (which is the default value)
LocalUserLanguage::update(
&mut *conn,
conn,
vec![UNDETERMINED_ID],
data.inserted_local_user.id,
)
.await
.unwrap();
let undetermined_comment = CommentQuery::builder()
.conn(&mut *conn)
.conn(conn)
.local_user(Some(&data.inserted_local_user))
.build()
.list()
@ -792,42 +791,42 @@ mod tests {
.unwrap();
assert_eq!(1, undetermined_comment.len());
cleanup(data, &mut *conn).await;
cleanup(data, conn).await;
}
async fn cleanup(data: Data, mut conn: impl DbConn) {
async fn cleanup(data: Data, mut conn: impl GetConn) {
CommentLike::remove(
&mut *conn,
conn,
data.inserted_person.id,
data.inserted_comment_0.id,
)
.await
.unwrap();
Comment::delete(&mut *conn, data.inserted_comment_0.id)
Comment::delete(conn, data.inserted_comment_0.id)
.await
.unwrap();
Comment::delete(&mut *conn, data.inserted_comment_1.id)
Comment::delete(conn, data.inserted_comment_1.id)
.await
.unwrap();
Post::delete(&mut *conn, data.inserted_post.id)
Post::delete(conn, data.inserted_post.id)
.await
.unwrap();
Community::delete(&mut *conn, data.inserted_community.id)
Community::delete(conn, data.inserted_community.id)
.await
.unwrap();
Person::delete(&mut *conn, data.inserted_person.id)
Person::delete(conn, data.inserted_person.id)
.await
.unwrap();
Person::delete(&mut *conn, data.inserted_person_2.id)
Person::delete(conn, data.inserted_person_2.id)
.await
.unwrap();
Instance::delete(&mut *conn, data.inserted_instance.id)
Instance::delete(conn, data.inserted_instance.id)
.await
.unwrap();
}
async fn expected_comment_view(data: &Data, mut conn: impl DbConn) -> CommentView {
let agg = CommentAggregates::read(&mut *conn, data.inserted_comment_0.id)
async fn expected_comment_view(data: &Data, mut conn: impl GetConn) -> CommentView {
let agg = CommentAggregates::read(conn, data.inserted_comment_0.id)
.await
.unwrap();
CommentView {

View File

@ -1,18 +1,17 @@
use crate::structs::CustomEmojiView;
use diesel::{result::Error, ExpressionMethods, JoinOnDsl, NullableExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
newtypes::{CustomEmojiId, LocalSiteId},
schema::{custom_emoji, custom_emoji_keyword},
source::{custom_emoji::CustomEmoji, custom_emoji_keyword::CustomEmojiKeyword},
utils::DbConn,
utils::{GetConn, RunQueryDsl},
};
use std::collections::HashMap;
type CustomEmojiTuple = (CustomEmoji, Option<CustomEmojiKeyword>);
impl CustomEmojiView {
pub async fn get(mut conn: impl DbConn, emoji_id: CustomEmojiId) -> Result<Self, Error> {
pub async fn get(mut conn: impl GetConn, emoji_id: CustomEmojiId) -> Result<Self, Error> {
let emojis = custom_emoji::table
.find(emoji_id)
.left_join(
@ -22,7 +21,7 @@ impl CustomEmojiView {
custom_emoji::all_columns,
custom_emoji_keyword::all_columns.nullable(), // (or all the columns if you want)
))
.load::<CustomEmojiTuple>(&mut *conn)
.load::<CustomEmojiTuple>(conn)
.await?;
if let Some(emoji) = CustomEmojiView::from_tuple_to_vec(emojis)
.into_iter()
@ -35,7 +34,7 @@ impl CustomEmojiView {
}
pub async fn get_all(
mut conn: impl DbConn,
mut conn: impl GetConn,
for_local_site_id: LocalSiteId,
) -> Result<Vec<Self>, Error> {
let emojis = custom_emoji::table
@ -49,7 +48,7 @@ impl CustomEmojiView {
custom_emoji::all_columns,
custom_emoji_keyword::all_columns.nullable(), // (or all the columns if you want)
))
.load::<CustomEmojiTuple>(&mut *conn)
.load::<CustomEmojiTuple>(conn)
.await?;
Ok(CustomEmojiView::from_tuple_to_vec(emojis))

View File

@ -1,19 +1,18 @@
use crate::structs::LocalUserView;
use diesel::{result::Error, BoolExpressionMethods, ExpressionMethods, JoinOnDsl, QueryDsl};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
aggregates::structs::PersonAggregates,
newtypes::{LocalUserId, PersonId},
schema::{local_user, person, person_aggregates},
source::{local_user::LocalUser, person::Person},
traits::JoinView,
utils::{functions::lower, DbConn},
utils::{functions::lower, GetConn, RunQueryDsl},
};
type LocalUserViewTuple = (LocalUser, Person, PersonAggregates);
impl LocalUserView {
pub async fn read(mut conn: impl DbConn, local_user_id: LocalUserId) -> Result<Self, Error> {
pub async fn read(mut conn: impl GetConn, local_user_id: LocalUserId) -> Result<Self, Error> {
let (local_user, person, counts) = local_user::table
.find(local_user_id)
.inner_join(person::table)
@ -23,7 +22,7 @@ impl LocalUserView {
person::all_columns,
person_aggregates::all_columns,
))
.first::<LocalUserViewTuple>(&mut *conn)
.first::<LocalUserViewTuple>(conn)
.await?;
Ok(Self {
local_user,
@ -32,7 +31,7 @@ impl LocalUserView {
})
}
pub async fn read_person(mut conn: impl DbConn, person_id: PersonId) -> Result<Self, Error> {
pub async fn read_person(mut conn: impl GetConn, person_id: PersonId) -> Result<Self, Error> {
let (local_user, person, counts) = local_user::table
.filter(person::id.eq(person_id))
.inner_join(person::table)
@ -42,7 +41,7 @@ impl LocalUserView {
person::all_columns,
person_aggregates::all_columns,
))
.first::<LocalUserViewTuple>(&mut *conn)
.first::<LocalUserViewTuple>(conn)
.await?;
Ok(Self {
local_user,
@ -51,7 +50,7 @@ impl LocalUserView {
})
}
pub async fn read_from_name(mut conn: impl DbConn, name: &str) -> Result<Self, Error> {
pub async fn read_from_name(mut conn: impl GetConn, name: &str) -> Result<Self, Error> {
let (local_user, person, counts) = local_user::table
.filter(lower(person::name).eq(name.to_lowercase()))
.inner_join(person::table)
@ -61,7 +60,7 @@ impl LocalUserView {
person::all_columns,
person_aggregates::all_columns,
))
.first::<LocalUserViewTuple>(&mut *conn)
.first::<LocalUserViewTuple>(conn)
.await?;
Ok(Self {
local_user,
@ -71,7 +70,7 @@ impl LocalUserView {
}
pub async fn find_by_email_or_name(
mut conn: impl DbConn,
mut conn: impl GetConn,
name_or_email: &str,
) -> Result<Self, Error> {
let (local_user, person, counts) = local_user::table
@ -87,7 +86,7 @@ impl LocalUserView {
person::all_columns,
person_aggregates::all_columns,
))
.first::<LocalUserViewTuple>(&mut *conn)
.first::<LocalUserViewTuple>(conn)
.await?;
Ok(Self {
local_user,
@ -96,7 +95,7 @@ impl LocalUserView {
})
}
pub async fn find_by_email(mut conn: impl DbConn, from_email: &str) -> Result<Self, Error> {
pub async fn find_by_email(mut conn: impl GetConn, from_email: &str) -> Result<Self, Error> {
let (local_user, person, counts) = local_user::table
.inner_join(person::table)
.inner_join(person_aggregates::table.on(person::id.eq(person_aggregates::person_id)))
@ -106,7 +105,7 @@ impl LocalUserView {
person::all_columns,
person_aggregates::all_columns,
))
.first::<LocalUserViewTuple>(&mut *conn)
.first::<LocalUserViewTuple>(conn)
.await?;
Ok(Self {
local_user,
@ -115,7 +114,7 @@ impl LocalUserView {
})
}
pub async fn list_admins_with_emails(mut conn: impl DbConn) -> Result<Vec<Self>, Error> {
pub async fn list_admins_with_emails(mut conn: impl GetConn) -> Result<Vec<Self>, Error> {
let res = local_user::table
.filter(person::admin.eq(true))
.filter(local_user::email.is_not_null())
@ -126,7 +125,7 @@ impl LocalUserView {
person::all_columns,
person_aggregates::all_columns,
))
.load::<LocalUserViewTuple>(&mut *conn)
.load::<LocalUserViewTuple>(conn)
.await?;
Ok(res.into_iter().map(LocalUserView::from_tuple).collect())

View File

@ -7,7 +7,6 @@ use diesel::{
NullableExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
aggregates::structs::PostAggregates,
newtypes::{CommunityId, PersonId, PostReportId},
@ -28,7 +27,7 @@ use lemmy_db_schema::{
post_report::PostReport,
},
traits::JoinView,
utils::{limit_and_offset, DbConn},
utils::{limit_and_offset, GetConn, RunQueryDsl},
};
use typed_builder::TypedBuilder;
@ -49,7 +48,7 @@ impl PostReportView {
///
/// * `report_id` - the report id to obtain
pub async fn read(
mut conn: impl DbConn,
mut conn: impl GetConn,
report_id: PostReportId,
my_person_id: PersonId,
) -> Result<Self, Error> {
@ -100,7 +99,7 @@ impl PostReportView {
post_aggregates::all_columns,
person_alias_2.fields(person::all_columns.nullable()),
))
.first::<PostReportViewTuple>(&mut *conn)
.first::<PostReportViewTuple>(conn)
.await?;
let my_vote = post_like;
@ -120,7 +119,7 @@ impl PostReportView {
/// returns the current unresolved post report count for the communities you mod
pub async fn get_report_count(
mut conn: impl DbConn,
mut conn: impl GetConn,
my_person_id: PersonId,
admin: bool,
community_id: Option<CommunityId>,
@ -146,12 +145,12 @@ impl PostReportView {
),
)
.select(count(post_report::id))
.first::<i64>(&mut *conn)
.first::<i64>(conn)
.await
} else {
query
.select(count(post_report::id))
.first::<i64>(&mut *conn)
.first::<i64>(conn)
.await
}
}
@ -172,7 +171,7 @@ pub struct PostReportQuery<Conn> {
unresolved_only: Option<bool>,
}
impl<Conn: DbConn> PostReportQuery<Conn> {
impl<Conn: GetConn> PostReportQuery<Conn> {
pub async fn list(self) -> Result<Vec<PostReportView>, Error> {
let mut conn = self.conn;
let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2);
@ -238,10 +237,10 @@ impl<Conn: DbConn> PostReportQuery<Conn> {
.and(community_moderator::person_id.eq(self.my_person_id)),
),
)
.load::<PostReportViewTuple>(&mut *conn)
.load::<PostReportViewTuple>(conn)
.await?
} else {
query.load::<PostReportViewTuple>(&mut *conn).await?
query.load::<PostReportViewTuple>(conn).await?
};
Ok(res.into_iter().map(PostReportView::from_tuple).collect())
@ -287,7 +286,7 @@ mod tests {
async fn test_crud() {
let mut conn = build_db_conn_for_tests().await;
let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string())
let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string())
.await
.unwrap();
@ -297,7 +296,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_timmy = Person::create(&mut *conn, &new_person).await.unwrap();
let inserted_timmy = Person::create(conn, &new_person).await.unwrap();
let new_person_2 = PersonInsertForm::builder()
.name("sara_prv".into())
@ -305,7 +304,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_sara = Person::create(&mut *conn, &new_person_2).await.unwrap();
let inserted_sara = Person::create(conn, &new_person_2).await.unwrap();
// Add a third person, since new ppl can only report something once.
let new_person_3 = PersonInsertForm::builder()
@ -314,7 +313,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_jessica = Person::create(&mut *conn, &new_person_3).await.unwrap();
let inserted_jessica = Person::create(conn, &new_person_3).await.unwrap();
let new_community = CommunityInsertForm::builder()
.name("test community prv".to_string())
@ -323,7 +322,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_community = Community::create(&mut *conn, &new_community).await.unwrap();
let inserted_community = Community::create(conn, &new_community).await.unwrap();
// Make timmy a mod
let timmy_moderator_form = CommunityModeratorForm {
@ -331,7 +330,7 @@ mod tests {
person_id: inserted_timmy.id,
};
let _inserted_moderator = CommunityModerator::join(&mut *conn, &timmy_moderator_form)
let _inserted_moderator = CommunityModerator::join(conn, &timmy_moderator_form)
.await
.unwrap();
@ -341,7 +340,7 @@ mod tests {
.community_id(inserted_community.id)
.build();
let inserted_post = Post::create(&mut *conn, &new_post).await.unwrap();
let inserted_post = Post::create(conn, &new_post).await.unwrap();
// sara reports
let sara_report_form = PostReportForm {
@ -353,7 +352,7 @@ mod tests {
reason: "from sara".into(),
};
let inserted_sara_report = PostReport::report(&mut *conn, &sara_report_form)
let inserted_sara_report = PostReport::report(conn, &sara_report_form)
.await
.unwrap();
@ -367,16 +366,16 @@ mod tests {
reason: "from jessica".into(),
};
let inserted_jessica_report = PostReport::report(&mut *conn, &jessica_report_form)
let inserted_jessica_report = PostReport::report(conn, &jessica_report_form)
.await
.unwrap();
let agg = PostAggregates::read(&mut *conn, inserted_post.id)
let agg = PostAggregates::read(conn, inserted_post.id)
.await
.unwrap();
let read_jessica_report_view =
PostReportView::read(&mut *conn, inserted_jessica_report.id, inserted_timmy.id)
PostReportView::read(conn, inserted_jessica_report.id, inserted_timmy.id)
.await
.unwrap();
let expected_jessica_report_view = PostReportView {
@ -508,7 +507,7 @@ mod tests {
// Do a batch read of timmys reports
let reports = PostReportQuery::builder()
.conn(&mut *conn)
.conn(conn)
.my_person_id(inserted_timmy.id)
.admin(false)
.build()
@ -525,17 +524,17 @@ mod tests {
);
// Make sure the counts are correct
let report_count = PostReportView::get_report_count(&mut *conn, inserted_timmy.id, false, None)
let report_count = PostReportView::get_report_count(conn, inserted_timmy.id, false, None)
.await
.unwrap();
assert_eq!(2, report_count);
// Try to resolve the report
PostReport::resolve(&mut *conn, inserted_jessica_report.id, inserted_timmy.id)
PostReport::resolve(conn, inserted_jessica_report.id, inserted_timmy.id)
.await
.unwrap();
let read_jessica_report_view_after_resolve =
PostReportView::read(&mut *conn, inserted_jessica_report.id, inserted_timmy.id)
PostReportView::read(conn, inserted_jessica_report.id, inserted_timmy.id)
.await
.unwrap();
@ -582,7 +581,7 @@ mod tests {
// Do a batch read of timmys reports
// It should only show saras, which is unresolved
let reports_after_resolve = PostReportQuery::builder()
.conn(&mut *conn)
.conn(conn)
.my_person_id(inserted_timmy.id)
.admin(false)
.unresolved_only(Some(true))
@ -594,20 +593,20 @@ mod tests {
// Make sure the counts are correct
let report_count_after_resolved =
PostReportView::get_report_count(&mut *conn, inserted_timmy.id, false, None)
PostReportView::get_report_count(conn, inserted_timmy.id, false, None)
.await
.unwrap();
assert_eq!(1, report_count_after_resolved);
Person::delete(&mut *conn, inserted_timmy.id).await.unwrap();
Person::delete(&mut *conn, inserted_sara.id).await.unwrap();
Person::delete(&mut *conn, inserted_jessica.id)
Person::delete(conn, inserted_timmy.id).await.unwrap();
Person::delete(conn, inserted_sara.id).await.unwrap();
Person::delete(conn, inserted_jessica.id)
.await
.unwrap();
Community::delete(&mut *conn, inserted_community.id)
Community::delete(conn, inserted_community.id)
.await
.unwrap();
Instance::delete(&mut *conn, inserted_instance.id)
Instance::delete(conn, inserted_instance.id)
.await
.unwrap();
}

View File

@ -13,7 +13,6 @@ use diesel::{
PgTextExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
aggregates::structs::PostAggregates,
newtypes::{CommunityId, LocalUserId, PersonId, PostId},
@ -40,7 +39,7 @@ use lemmy_db_schema::{
post::{Post, PostRead, PostSaved},
},
traits::JoinView,
utils::{fuzzy_search, limit_and_offset, DbConn},
utils::{fuzzy_search, limit_and_offset, GetConn, RunQueryDsl},
ListingType,
SortType,
};
@ -65,7 +64,7 @@ sql_function!(fn coalesce(x: sql_types::Nullable<sql_types::BigInt>, y: sql_type
impl PostView {
pub async fn read(
mut conn: impl DbConn,
mut conn: impl GetConn,
post_id: PostId,
my_person_id: Option<PersonId>,
is_mod_or_admin: Option<bool>,
@ -165,7 +164,7 @@ impl PostView {
creator_blocked,
post_like,
unread_comments,
) = query.first::<PostViewTuple>(&mut *conn).await?;
) = query.first::<PostViewTuple>(conn).await?;
// If a person is given, then my_vote, if None, should be 0, not null
// Necessary to differentiate between other person's votes
@ -210,7 +209,7 @@ pub struct PostQuery<'a, Conn> {
limit: Option<i64>,
}
impl<'a, Conn: DbConn> PostQuery<'a, Conn> {
impl<'a, Conn: GetConn> PostQuery<'a, Conn> {
pub async fn list(self) -> Result<Vec<PostView>, Error> {
let mut conn = self.conn;
@ -448,7 +447,7 @@ impl<'a, Conn: DbConn> PostQuery<'a, Conn> {
debug!("Post View Query: {:?}", debug_query::<Pg, _>(&query));
let res = query.load::<PostViewTuple>(&mut *conn).await?;
let res = query.load::<PostViewTuple>(conn).await?;
Ok(res.into_iter().map(PostView::from_tuple).collect())
}
@ -492,7 +491,7 @@ mod tests {
post::{Post, PostInsertForm, PostLike, PostLikeForm, PostUpdateForm},
},
traits::{Blockable, Crud, Likeable},
utils::{build_db_conn_for_tests, DbConn},
utils::{build_db_conn_for_tests, GetConn},
SortType,
SubscribedType,
};
@ -508,8 +507,8 @@ mod tests {
inserted_post: Post,
}
async fn init_data(mut conn: impl DbConn) -> Data {
let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string())
async fn init_data(mut conn: impl GetConn) -> Data {
let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string())
.await
.unwrap();
@ -521,13 +520,13 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_person = Person::create(&mut *conn, &new_person).await.unwrap();
let inserted_person = Person::create(conn, &new_person).await.unwrap();
let local_user_form = LocalUserInsertForm::builder()
.person_id(inserted_person.id)
.password_encrypted(String::new())
.build();
let inserted_local_user = LocalUser::create(&mut *conn, &local_user_form)
let inserted_local_user = LocalUser::create(conn, &local_user_form)
.await
.unwrap();
@ -538,7 +537,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_bot = Person::create(&mut *conn, &new_bot).await.unwrap();
let inserted_bot = Person::create(conn, &new_bot).await.unwrap();
let new_community = CommunityInsertForm::builder()
.name("test_community_3".to_string())
@ -547,7 +546,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_community = Community::create(&mut *conn, &new_community).await.unwrap();
let inserted_community = Community::create(conn, &new_community).await.unwrap();
// Test a person block, make sure the post query doesn't include their post
let blocked_person = PersonInsertForm::builder()
@ -556,7 +555,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_blocked_person = Person::create(&mut *conn, &blocked_person).await.unwrap();
let inserted_blocked_person = Person::create(conn, &blocked_person).await.unwrap();
let post_from_blocked_person = PostInsertForm::builder()
.name("blocked_person_post".to_string())
@ -565,7 +564,7 @@ mod tests {
.language_id(Some(LanguageId(1)))
.build();
Post::create(&mut *conn, &post_from_blocked_person)
Post::create(conn, &post_from_blocked_person)
.await
.unwrap();
@ -575,7 +574,7 @@ mod tests {
target_id: inserted_blocked_person.id,
};
PersonBlock::block(&mut *conn, &person_block).await.unwrap();
PersonBlock::block(conn, &person_block).await.unwrap();
// A sample post
let new_post = PostInsertForm::builder()
@ -585,7 +584,7 @@ mod tests {
.language_id(Some(LanguageId(47)))
.build();
let inserted_post = Post::create(&mut *conn, &new_post).await.unwrap();
let inserted_post = Post::create(conn, &new_post).await.unwrap();
let new_bot_post = PostInsertForm::builder()
.name("test bot post".to_string())
@ -593,7 +592,7 @@ mod tests {
.community_id(inserted_community.id)
.build();
let _inserted_bot_post = Post::create(&mut *conn, &new_bot_post).await.unwrap();
let _inserted_bot_post = Post::create(conn, &new_bot_post).await.unwrap();
Data {
inserted_instance,
@ -610,18 +609,18 @@ mod tests {
#[serial]
async fn post_listing_with_person() {
let mut conn = build_db_conn_for_tests().await;
let data = init_data(&mut *conn).await;
let data = init_data(conn).await;
let local_user_form = LocalUserUpdateForm::builder()
.show_bot_accounts(Some(false))
.build();
let inserted_local_user =
LocalUser::update(&mut *conn, data.inserted_local_user.id, &local_user_form)
LocalUser::update(conn, data.inserted_local_user.id, &local_user_form)
.await
.unwrap();
let read_post_listing = PostQuery::builder()
.conn(&mut *conn)
.conn(conn)
.sort(Some(SortType::New))
.community_id(Some(data.inserted_community.id))
.local_user(Some(&inserted_local_user))
@ -631,7 +630,7 @@ mod tests {
.unwrap();
let post_listing_single_with_person = PostView::read(
&mut *conn,
conn,
data.inserted_post.id,
Some(data.inserted_person.id),
None,
@ -639,7 +638,7 @@ mod tests {
.await
.unwrap();
let mut expected_post_listing_with_user = expected_post_view(&data, &mut *conn).await;
let mut expected_post_listing_with_user = expected_post_view(&data, conn).await;
// Should be only one person, IE the bot post, and blocked should be missing
assert_eq!(1, read_post_listing.len());
@ -655,12 +654,12 @@ mod tests {
.show_bot_accounts(Some(true))
.build();
let inserted_local_user =
LocalUser::update(&mut *conn, data.inserted_local_user.id, &local_user_form)
LocalUser::update(conn, data.inserted_local_user.id, &local_user_form)
.await
.unwrap();
let post_listings_with_bots = PostQuery::builder()
.conn(&mut *conn)
.conn(conn)
.sort(Some(SortType::New))
.community_id(Some(data.inserted_community.id))
.local_user(Some(&inserted_local_user))
@ -671,17 +670,17 @@ mod tests {
// should include bot post which has "undetermined" language
assert_eq!(2, post_listings_with_bots.len());
cleanup(data, &mut *conn).await;
cleanup(data, conn).await;
}
#[tokio::test]
#[serial]
async fn post_listing_no_person() {
let mut conn = build_db_conn_for_tests().await;
let data = init_data(&mut *conn).await;
let data = init_data(conn).await;
let read_post_listing_multiple_no_person = PostQuery::builder()
.conn(&mut *conn)
.conn(conn)
.sort(Some(SortType::New))
.community_id(Some(data.inserted_community.id))
.build()
@ -690,11 +689,11 @@ mod tests {
.unwrap();
let read_post_listing_single_no_person =
PostView::read(&mut *conn, data.inserted_post.id, None, None)
PostView::read(conn, data.inserted_post.id, None, None)
.await
.unwrap();
let expected_post_listing_no_person = expected_post_view(&data, &mut *conn).await;
let expected_post_listing_no_person = expected_post_view(&data, conn).await;
// Should be 2 posts, with the bot post, and the blocked
assert_eq!(3, read_post_listing_multiple_no_person.len());
@ -708,25 +707,25 @@ mod tests {
read_post_listing_single_no_person
);
cleanup(data, &mut *conn).await;
cleanup(data, conn).await;
}
#[tokio::test]
#[serial]
async fn post_listing_block_community() {
let mut conn = build_db_conn_for_tests().await;
let data = init_data(&mut *conn).await;
let data = init_data(conn).await;
let community_block = CommunityBlockForm {
person_id: data.inserted_person.id,
community_id: data.inserted_community.id,
};
CommunityBlock::block(&mut *conn, &community_block)
CommunityBlock::block(conn, &community_block)
.await
.unwrap();
let read_post_listings_with_person_after_block = PostQuery::builder()
.conn(&mut *conn)
.conn(conn)
.sort(Some(SortType::New))
.community_id(Some(data.inserted_community.id))
.local_user(Some(&data.inserted_local_user))
@ -737,17 +736,17 @@ mod tests {
// Should be 0 posts after the community block
assert_eq!(0, read_post_listings_with_person_after_block.len());
CommunityBlock::unblock(&mut *conn, &community_block)
CommunityBlock::unblock(conn, &community_block)
.await
.unwrap();
cleanup(data, &mut *conn).await;
cleanup(data, conn).await;
}
#[tokio::test]
#[serial]
async fn post_listing_like() {
let mut conn = build_db_conn_for_tests().await;
let data = init_data(&mut *conn).await;
let data = init_data(conn).await;
let post_like_form = PostLikeForm {
post_id: data.inserted_post.id,
@ -755,7 +754,7 @@ mod tests {
score: 1,
};
let inserted_post_like = PostLike::like(&mut *conn, &post_like_form).await.unwrap();
let inserted_post_like = PostLike::like(conn, &post_like_form).await.unwrap();
let expected_post_like = PostLike {
id: inserted_post_like.id,
@ -767,7 +766,7 @@ mod tests {
assert_eq!(expected_post_like, inserted_post_like);
let post_listing_single_with_person = PostView::read(
&mut *conn,
conn,
data.inserted_post.id,
Some(data.inserted_person.id),
None,
@ -775,7 +774,7 @@ mod tests {
.await
.unwrap();
let mut expected_post_with_upvote = expected_post_view(&data, &mut *conn).await;
let mut expected_post_with_upvote = expected_post_view(&data, conn).await;
expected_post_with_upvote.my_vote = Some(1);
expected_post_with_upvote.counts.score = 1;
expected_post_with_upvote.counts.upvotes = 1;
@ -785,12 +784,12 @@ mod tests {
.show_bot_accounts(Some(false))
.build();
let inserted_local_user =
LocalUser::update(&mut *conn, data.inserted_local_user.id, &local_user_form)
LocalUser::update(conn, data.inserted_local_user.id, &local_user_form)
.await
.unwrap();
let read_post_listing = PostQuery::builder()
.conn(&mut *conn)
.conn(conn)
.sort(Some(SortType::New))
.community_id(Some(data.inserted_community.id))
.local_user(Some(&inserted_local_user))
@ -802,20 +801,20 @@ mod tests {
assert_eq!(expected_post_with_upvote, read_post_listing[0]);
let like_removed = PostLike::remove(&mut *conn, data.inserted_person.id, data.inserted_post.id)
let like_removed = PostLike::remove(conn, data.inserted_person.id, data.inserted_post.id)
.await
.unwrap();
assert_eq!(1, like_removed);
cleanup(data, &mut *conn).await;
cleanup(data, conn).await;
}
#[tokio::test]
#[serial]
async fn post_listing_person_language() {
let mut conn = build_db_conn_for_tests().await;
let data = init_data(&mut *conn).await;
let data = init_data(conn).await;
let spanish_id = Language::read_id_from_code(&mut *conn, Some("es"))
let spanish_id = Language::read_id_from_code(conn, Some("es"))
.await
.unwrap()
.unwrap();
@ -826,10 +825,10 @@ mod tests {
.language_id(Some(spanish_id))
.build();
Post::create(&mut *conn, &post_spanish).await.unwrap();
Post::create(conn, &post_spanish).await.unwrap();
let post_listings_all = PostQuery::builder()
.conn(&mut *conn)
.conn(conn)
.sort(Some(SortType::New))
.local_user(Some(&data.inserted_local_user))
.build()
@ -840,16 +839,16 @@ mod tests {
// no language filters specified, all posts should be returned
assert_eq!(3, post_listings_all.len());
let french_id = Language::read_id_from_code(&mut *conn, Some("fr"))
let french_id = Language::read_id_from_code(conn, Some("fr"))
.await
.unwrap()
.unwrap();
LocalUserLanguage::update(&mut *conn, vec![french_id], data.inserted_local_user.id)
LocalUserLanguage::update(conn, vec![french_id], data.inserted_local_user.id)
.await
.unwrap();
let post_listing_french = PostQuery::builder()
.conn(&mut *conn)
.conn(conn)
.sort(Some(SortType::New))
.local_user(Some(&data.inserted_local_user))
.build()
@ -864,14 +863,14 @@ mod tests {
.any(|p| p.post.language_id == french_id));
LocalUserLanguage::update(
&mut *conn,
conn,
vec![french_id, UNDETERMINED_ID],
data.inserted_local_user.id,
)
.await
.unwrap();
let post_listings_french_und = PostQuery::builder()
.conn(&mut *conn)
.conn(conn)
.sort(Some(SortType::New))
.local_user(Some(&data.inserted_local_user))
.build()
@ -887,18 +886,18 @@ mod tests {
);
assert_eq!(french_id, post_listings_french_und[1].post.language_id);
cleanup(data, &mut *conn).await;
cleanup(data, conn).await;
}
#[tokio::test]
#[serial]
async fn post_listings_deleted() {
let mut conn = build_db_conn_for_tests().await;
let data = init_data(&mut *conn).await;
let data = init_data(conn).await;
// Delete the post
Post::update(
&mut *conn,
conn,
data.inserted_post.id,
&PostUpdateForm::builder().deleted(Some(true)).build(),
)
@ -907,7 +906,7 @@ mod tests {
// Make sure you don't see the deleted post in the results
let post_listings_no_admin = PostQuery::builder()
.conn(&mut *conn)
.conn(conn)
.sort(Some(SortType::New))
.local_user(Some(&data.inserted_local_user))
.is_mod_or_admin(Some(false))
@ -920,7 +919,7 @@ mod tests {
// Make sure they see both
let post_listings_is_admin = PostQuery::builder()
.conn(&mut *conn)
.conn(conn)
.sort(Some(SortType::New))
.local_user(Some(&data.inserted_local_user))
.is_mod_or_admin(Some(true))
@ -931,38 +930,38 @@ mod tests {
assert_eq!(2, post_listings_is_admin.len());
cleanup(data, &mut *conn).await;
cleanup(data, conn).await;
}
async fn cleanup(data: Data, mut conn: impl DbConn) {
let num_deleted = Post::delete(&mut *conn, data.inserted_post.id)
async fn cleanup(data: Data, mut conn: impl GetConn) {
let num_deleted = Post::delete(conn, data.inserted_post.id)
.await
.unwrap();
Community::delete(&mut *conn, data.inserted_community.id)
Community::delete(conn, data.inserted_community.id)
.await
.unwrap();
Person::delete(&mut *conn, data.inserted_person.id)
Person::delete(conn, data.inserted_person.id)
.await
.unwrap();
Person::delete(&mut *conn, data.inserted_bot.id)
Person::delete(conn, data.inserted_bot.id)
.await
.unwrap();
Person::delete(&mut *conn, data.inserted_blocked_person.id)
Person::delete(conn, data.inserted_blocked_person.id)
.await
.unwrap();
Instance::delete(&mut *conn, data.inserted_instance.id)
Instance::delete(conn, data.inserted_instance.id)
.await
.unwrap();
assert_eq!(1, num_deleted);
}
async fn expected_post_view(data: &Data, mut conn: impl DbConn) -> PostView {
async fn expected_post_view(data: &Data, mut conn: impl GetConn) -> PostView {
let (inserted_person, inserted_community, inserted_post) = (
&data.inserted_person,
&data.inserted_community,
&data.inserted_post,
);
let agg = PostAggregates::read(&mut *conn, inserted_post.id)
let agg = PostAggregates::read(conn, inserted_post.id)
.await
.unwrap();

View File

@ -1,6 +1,5 @@
use crate::structs::PrivateMessageReportView;
use diesel::{result::Error, ExpressionMethods, JoinOnDsl, NullableExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
newtypes::PrivateMessageReportId,
schema::{person, private_message, private_message_report},
@ -10,7 +9,7 @@ use lemmy_db_schema::{
private_message_report::PrivateMessageReport,
},
traits::JoinView,
utils::{limit_and_offset, DbConn},
utils::{limit_and_offset, GetConn, RunQueryDsl},
};
use typed_builder::TypedBuilder;
@ -27,7 +26,7 @@ impl PrivateMessageReportView {
///
/// * `report_id` - the report id to obtain
pub async fn read(
mut conn: impl DbConn,
mut conn: impl GetConn,
report_id: PrivateMessageReportId,
) -> Result<Self, Error> {
let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2);
@ -53,7 +52,7 @@ impl PrivateMessageReportView {
person_alias_1.fields(person::all_columns),
person_alias_2.fields(person::all_columns).nullable(),
))
.first::<PrivateMessageReportViewTuple>(&mut *conn)
.first::<PrivateMessageReportViewTuple>(conn)
.await?;
Ok(Self {
@ -66,7 +65,7 @@ impl PrivateMessageReportView {
}
/// Returns the current unresolved post report count for the communities you mod
pub async fn get_report_count(mut conn: impl DbConn) -> Result<i64, Error> {
pub async fn get_report_count(mut conn: impl GetConn) -> Result<i64, Error> {
use diesel::dsl::count;
private_message_report::table
@ -74,7 +73,7 @@ impl PrivateMessageReportView {
.filter(private_message_report::resolved.eq(false))
.into_boxed()
.select(count(private_message_report::id))
.first::<i64>(&mut *conn)
.first::<i64>(conn)
.await
}
}
@ -89,7 +88,7 @@ pub struct PrivateMessageReportQuery<Conn> {
unresolved_only: Option<bool>,
}
impl<Conn: DbConn> PrivateMessageReportQuery<Conn> {
impl<Conn: GetConn> PrivateMessageReportQuery<Conn> {
pub async fn list(self) -> Result<Vec<PrivateMessageReportView>, Error> {
let mut conn = self.conn;
let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2);
@ -125,7 +124,7 @@ impl<Conn: DbConn> PrivateMessageReportQuery<Conn> {
.offset(offset);
let res = query
.load::<PrivateMessageReportViewTuple>(&mut *conn)
.load::<PrivateMessageReportViewTuple>(conn)
.await?;
Ok(
@ -170,7 +169,7 @@ mod tests {
async fn test_crud() {
let mut conn = build_db_conn_for_tests().await;
let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string())
let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string())
.await
.unwrap();
@ -179,14 +178,14 @@ mod tests {
.public_key("pubkey".to_string())
.instance_id(inserted_instance.id)
.build();
let inserted_timmy = Person::create(&mut *conn, &new_person_1).await.unwrap();
let inserted_timmy = Person::create(conn, &new_person_1).await.unwrap();
let new_person_2 = PersonInsertForm::builder()
.name("jessica_mrv".into())
.public_key("pubkey".to_string())
.instance_id(inserted_instance.id)
.build();
let inserted_jessica = Person::create(&mut *conn, &new_person_2).await.unwrap();
let inserted_jessica = Person::create(conn, &new_person_2).await.unwrap();
// timmy sends private message to jessica
let pm_form = PrivateMessageInsertForm::builder()
@ -194,7 +193,7 @@ mod tests {
.recipient_id(inserted_jessica.id)
.content("something offensive".to_string())
.build();
let pm = PrivateMessage::create(&mut *conn, &pm_form).await.unwrap();
let pm = PrivateMessage::create(conn, &pm_form).await.unwrap();
// jessica reports private message
let pm_report_form = PrivateMessageReportForm {
@ -203,12 +202,12 @@ mod tests {
private_message_id: pm.id,
reason: "its offensive".to_string(),
};
let pm_report = PrivateMessageReport::report(&mut *conn, &pm_report_form)
let pm_report = PrivateMessageReport::report(conn, &pm_report_form)
.await
.unwrap();
let reports = PrivateMessageReportQuery::builder()
.conn(&mut *conn)
.conn(conn)
.build()
.list()
.await
@ -225,15 +224,15 @@ mod tests {
.public_key("pubkey".to_string())
.instance_id(inserted_instance.id)
.build();
let inserted_admin = Person::create(&mut *conn, &new_person_3).await.unwrap();
let inserted_admin = Person::create(conn, &new_person_3).await.unwrap();
// admin resolves the report (after taking appropriate action)
PrivateMessageReport::resolve(&mut *conn, pm_report.id, inserted_admin.id)
PrivateMessageReport::resolve(conn, pm_report.id, inserted_admin.id)
.await
.unwrap();
let reports = PrivateMessageReportQuery::builder()
.conn(&mut *conn)
.conn(conn)
.unresolved_only(Some(false))
.build()
.list()
@ -247,7 +246,7 @@ mod tests {
reports[0].resolver.as_ref().unwrap().name
);
Instance::delete(&mut *conn, inserted_instance.id)
Instance::delete(conn, inserted_instance.id)
.await
.unwrap();
}

View File

@ -8,13 +8,12 @@ use diesel::{
JoinOnDsl,
QueryDsl,
};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
newtypes::{PersonId, PrivateMessageId},
schema::{person, private_message},
source::{person::Person, private_message::PrivateMessage},
traits::JoinView,
utils::{limit_and_offset, DbConn},
utils::{limit_and_offset, GetConn, RunQueryDsl},
};
use tracing::debug;
use typed_builder::TypedBuilder;
@ -23,7 +22,7 @@ type PrivateMessageViewTuple = (PrivateMessage, Person, Person);
impl PrivateMessageView {
pub async fn read(
mut conn: impl DbConn,
mut conn: impl GetConn,
private_message_id: PrivateMessageId,
) -> Result<Self, Error> {
let person_alias_1 = diesel::alias!(person as person1);
@ -40,7 +39,7 @@ impl PrivateMessageView {
person::all_columns,
person_alias_1.fields(person::all_columns),
))
.first::<PrivateMessageViewTuple>(&mut *conn)
.first::<PrivateMessageViewTuple>(conn)
.await?;
Ok(PrivateMessageView {
@ -52,7 +51,7 @@ impl PrivateMessageView {
/// Gets the number of unread messages
pub async fn get_unread_messages(
mut conn: impl DbConn,
mut conn: impl GetConn,
my_person_id: PersonId,
) -> Result<i64, Error> {
use diesel::dsl::count;
@ -61,7 +60,7 @@ impl PrivateMessageView {
.filter(private_message::recipient_id.eq(my_person_id))
.filter(private_message::deleted.eq(false))
.select(count(private_message::id))
.first::<i64>(&mut *conn)
.first::<i64>(conn)
.await
}
}
@ -78,7 +77,7 @@ pub struct PrivateMessageQuery<Conn> {
limit: Option<i64>,
}
impl<Conn: DbConn> PrivateMessageQuery<Conn> {
impl<Conn: GetConn> PrivateMessageQuery<Conn> {
pub async fn list(self) -> Result<Vec<PrivateMessageView>, Error> {
let mut conn = self.conn;
let person_alias_1 = diesel::alias!(person as person1);
@ -123,7 +122,7 @@ impl<Conn: DbConn> PrivateMessageQuery<Conn> {
debug_query::<Pg, _>(&query)
);
let res = query.load::<PrivateMessageViewTuple>(&mut *conn).await?;
let res = query.load::<PrivateMessageViewTuple>(conn).await?;
Ok(
res

View File

@ -7,7 +7,6 @@ use diesel::{
NullableExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
schema::{local_user, person, registration_application},
source::{
@ -16,7 +15,7 @@ use lemmy_db_schema::{
registration_application::RegistrationApplication,
},
traits::JoinView,
utils::{limit_and_offset, DbConn},
utils::{limit_and_offset, GetConn, RunQueryDsl},
};
use typed_builder::TypedBuilder;
@ -25,7 +24,7 @@ type RegistrationApplicationViewTuple =
impl RegistrationApplicationView {
pub async fn read(
mut conn: impl DbConn,
mut conn: impl GetConn,
registration_application_id: i32,
) -> Result<Self, Error> {
let person_alias_1 = diesel::alias!(person as person1);
@ -48,7 +47,7 @@ impl RegistrationApplicationView {
person::all_columns,
person_alias_1.fields(person::all_columns).nullable(),
))
.first::<RegistrationApplicationViewTuple>(&mut *conn)
.first::<RegistrationApplicationViewTuple>(conn)
.await?;
Ok(RegistrationApplicationView {
@ -61,7 +60,7 @@ impl RegistrationApplicationView {
/// Returns the current unread registration_application count
pub async fn get_unread_count(
mut conn: impl DbConn,
mut conn: impl GetConn,
verified_email_only: bool,
) -> Result<i64, Error> {
let person_alias_1 = diesel::alias!(person as person1);
@ -82,7 +81,7 @@ impl RegistrationApplicationView {
query
.select(count(registration_application::id))
.first::<i64>(&mut *conn)
.first::<i64>(conn)
.await
}
}
@ -98,7 +97,7 @@ pub struct RegistrationApplicationQuery<Conn> {
limit: Option<i64>,
}
impl<Conn: DbConn> RegistrationApplicationQuery<Conn> {
impl<Conn: GetConn> RegistrationApplicationQuery<Conn> {
pub async fn list(self) -> Result<Vec<RegistrationApplicationView>, Error> {
let mut conn = self.conn;
let person_alias_1 = diesel::alias!(person as person1);
@ -135,7 +134,7 @@ impl<Conn: DbConn> RegistrationApplicationQuery<Conn> {
.order_by(registration_application::published.desc());
let res = query
.load::<RegistrationApplicationViewTuple>(&mut *conn)
.load::<RegistrationApplicationViewTuple>(conn)
.await?;
Ok(
@ -186,7 +185,7 @@ mod tests {
async fn test_crud() {
let mut conn = build_db_conn_for_tests().await;
let inserted_instance = Instance::read_or_create(&mut *conn, "my_domain.tld".to_string())
let inserted_instance = Instance::read_or_create(conn, "my_domain.tld".to_string())
.await
.unwrap();
@ -197,7 +196,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_timmy_person = Person::create(&mut *conn, &timmy_person_form)
let inserted_timmy_person = Person::create(conn, &timmy_person_form)
.await
.unwrap();
@ -206,7 +205,7 @@ mod tests {
.password_encrypted("nada".to_string())
.build();
let _inserted_timmy_local_user = LocalUser::create(&mut *conn, &timmy_local_user_form)
let _inserted_timmy_local_user = LocalUser::create(conn, &timmy_local_user_form)
.await
.unwrap();
@ -216,14 +215,14 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_sara_person = Person::create(&mut *conn, &sara_person_form).await.unwrap();
let inserted_sara_person = Person::create(conn, &sara_person_form).await.unwrap();
let sara_local_user_form = LocalUserInsertForm::builder()
.person_id(inserted_sara_person.id)
.password_encrypted("nada".to_string())
.build();
let inserted_sara_local_user = LocalUser::create(&mut *conn, &sara_local_user_form)
let inserted_sara_local_user = LocalUser::create(conn, &sara_local_user_form)
.await
.unwrap();
@ -233,11 +232,11 @@ mod tests {
answer: "LET ME IIIIINN".to_string(),
};
let sara_app = RegistrationApplication::create(&mut *conn, &sara_app_form)
let sara_app = RegistrationApplication::create(conn, &sara_app_form)
.await
.unwrap();
let read_sara_app_view = RegistrationApplicationView::read(&mut *conn, sara_app.id)
let read_sara_app_view = RegistrationApplicationView::read(conn, sara_app.id)
.await
.unwrap();
@ -247,14 +246,14 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_jess_person = Person::create(&mut *conn, &jess_person_form).await.unwrap();
let inserted_jess_person = Person::create(conn, &jess_person_form).await.unwrap();
let jess_local_user_form = LocalUserInsertForm::builder()
.person_id(inserted_jess_person.id)
.password_encrypted("nada".to_string())
.build();
let inserted_jess_local_user = LocalUser::create(&mut *conn, &jess_local_user_form)
let inserted_jess_local_user = LocalUser::create(conn, &jess_local_user_form)
.await
.unwrap();
@ -264,11 +263,11 @@ mod tests {
answer: "LET ME IIIIINN".to_string(),
};
let jess_app = RegistrationApplication::create(&mut *conn, &jess_app_form)
let jess_app = RegistrationApplication::create(conn, &jess_app_form)
.await
.unwrap();
let read_jess_app_view = RegistrationApplicationView::read(&mut *conn, jess_app.id)
let read_jess_app_view = RegistrationApplicationView::read(conn, jess_app.id)
.await
.unwrap();
@ -328,7 +327,7 @@ mod tests {
// Do a batch read of the applications
let apps = RegistrationApplicationQuery::builder()
.conn(&mut *conn)
.conn(conn)
.unread_only(Some(true))
.build()
.list()
@ -341,7 +340,7 @@ mod tests {
);
// Make sure the counts are correct
let unread_count = RegistrationApplicationView::get_unread_count(&mut *conn, false)
let unread_count = RegistrationApplicationView::get_unread_count(conn, false)
.await
.unwrap();
assert_eq!(unread_count, 2);
@ -352,7 +351,7 @@ mod tests {
deny_reason: None,
};
RegistrationApplication::update(&mut *conn, sara_app.id, &approve_form)
RegistrationApplication::update(conn, sara_app.id, &approve_form)
.await
.unwrap();
@ -362,7 +361,7 @@ mod tests {
.build();
LocalUser::update(
&mut *conn,
conn,
inserted_sara_local_user.id,
&approve_local_user_form,
)
@ -370,7 +369,7 @@ mod tests {
.unwrap();
let read_sara_app_view_after_approve =
RegistrationApplicationView::read(&mut *conn, sara_app.id)
RegistrationApplicationView::read(conn, sara_app.id)
.await
.unwrap();
@ -409,7 +408,7 @@ mod tests {
// Do a batch read of apps again
// It should show only jessicas which is unresolved
let apps_after_resolve = RegistrationApplicationQuery::builder()
.conn(&mut *conn)
.conn(conn)
.unread_only(Some(true))
.build()
.list()
@ -419,30 +418,30 @@ mod tests {
// Make sure the counts are correct
let unread_count_after_approve =
RegistrationApplicationView::get_unread_count(&mut *conn, false)
RegistrationApplicationView::get_unread_count(conn, false)
.await
.unwrap();
assert_eq!(unread_count_after_approve, 1);
// Make sure the not undenied_only has all the apps
let all_apps = RegistrationApplicationQuery::builder()
.conn(&mut *conn)
.conn(conn)
.build()
.list()
.await
.unwrap();
assert_eq!(all_apps.len(), 2);
Person::delete(&mut *conn, inserted_timmy_person.id)
Person::delete(conn, inserted_timmy_person.id)
.await
.unwrap();
Person::delete(&mut *conn, inserted_sara_person.id)
Person::delete(conn, inserted_sara_person.id)
.await
.unwrap();
Person::delete(&mut *conn, inserted_jess_person.id)
Person::delete(conn, inserted_jess_person.id)
.await
.unwrap();
Instance::delete(&mut *conn, inserted_instance.id)
Instance::delete(conn, inserted_instance.id)
.await
.unwrap();
}

View File

@ -1,15 +1,14 @@
use crate::structs::SiteView;
use diesel::{result::Error, ExpressionMethods, JoinOnDsl, QueryDsl};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
aggregates::structs::SiteAggregates,
schema::{local_site, local_site_rate_limit, site, site_aggregates},
source::{local_site::LocalSite, local_site_rate_limit::LocalSiteRateLimit, site::Site},
utils::DbConn,
utils::{GetConn, RunQueryDsl},
};
impl SiteView {
pub async fn read_local(mut conn: impl DbConn) -> Result<Self, Error> {
pub async fn read_local(mut conn: impl GetConn) -> Result<Self, Error> {
let (mut site, local_site, local_site_rate_limit, counts) = site::table
.inner_join(local_site::table)
.inner_join(
@ -22,7 +21,7 @@ impl SiteView {
local_site_rate_limit::all_columns,
site_aggregates::all_columns,
))
.first::<(Site, LocalSite, LocalSiteRateLimit, SiteAggregates)>(&mut *conn)
.first::<(Site, LocalSite, LocalSiteRateLimit, SiteAggregates)>(conn)
.await?;
site.private_key = None;

View File

@ -7,7 +7,6 @@ use diesel::{
NullableExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
aggregates::structs::CommentAggregates,
newtypes::{CommentReplyId, PersonId},
@ -33,7 +32,7 @@ use lemmy_db_schema::{
post::Post,
},
traits::JoinView,
utils::{limit_and_offset, DbConn},
utils::{limit_and_offset, GetConn, RunQueryDsl},
CommentSortType,
};
use typed_builder::TypedBuilder;
@ -55,7 +54,7 @@ type CommentReplyViewTuple = (
impl CommentReplyView {
pub async fn read(
mut conn: impl DbConn,
mut conn: impl GetConn,
comment_reply_id: CommentReplyId,
my_person_id: Option<PersonId>,
) -> Result<Self, Error> {
@ -134,7 +133,7 @@ impl CommentReplyView {
person_block::all_columns.nullable(),
comment_like::score.nullable(),
))
.first::<CommentReplyViewTuple>(&mut *conn)
.first::<CommentReplyViewTuple>(conn)
.await?;
Ok(CommentReplyView {
@ -155,7 +154,7 @@ impl CommentReplyView {
/// Gets the number of unread replies
pub async fn get_unread_replies(
mut conn: impl DbConn,
mut conn: impl GetConn,
my_person_id: PersonId,
) -> Result<i64, Error> {
use diesel::dsl::count;
@ -167,7 +166,7 @@ impl CommentReplyView {
.filter(comment::deleted.eq(false))
.filter(comment::removed.eq(false))
.select(count(comment_reply::id))
.first::<i64>(&mut *conn)
.first::<i64>(conn)
.await
}
}
@ -186,7 +185,7 @@ pub struct CommentReplyQuery<Conn> {
limit: Option<i64>,
}
impl<Conn: DbConn> CommentReplyQuery<Conn> {
impl<Conn: GetConn> CommentReplyQuery<Conn> {
pub async fn list(self) -> Result<Vec<CommentReplyView>, Error> {
let mut conn = self.conn;
@ -277,7 +276,7 @@ impl<Conn: DbConn> CommentReplyQuery<Conn> {
let res = query
.limit(limit)
.offset(offset)
.load::<CommentReplyViewTuple>(&mut *conn)
.load::<CommentReplyViewTuple>(conn)
.await?;
Ok(res.into_iter().map(CommentReplyView::from_tuple).collect())

View File

@ -1,18 +1,17 @@
use crate::structs::CommunityBlockView;
use diesel::{result::Error, ExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
newtypes::PersonId,
schema::{community, community_block, person},
source::{community::Community, person::Person},
traits::JoinView,
utils::DbConn,
utils::{GetConn, RunQueryDsl},
};
type CommunityBlockViewTuple = (Person, Community);
impl CommunityBlockView {
pub async fn for_person(mut conn: impl DbConn, person_id: PersonId) -> Result<Vec<Self>, Error> {
pub async fn for_person(mut conn: impl GetConn, person_id: PersonId) -> Result<Vec<Self>, Error> {
let res = community_block::table
.inner_join(person::table)
.inner_join(community::table)
@ -21,7 +20,7 @@ impl CommunityBlockView {
.filter(community::deleted.eq(false))
.filter(community::removed.eq(false))
.order_by(community_block::published)
.load::<CommunityBlockViewTuple>(&mut *conn)
.load::<CommunityBlockViewTuple>(conn)
.await?;
Ok(res.into_iter().map(Self::from_tuple).collect())

View File

@ -1,19 +1,18 @@
use crate::structs::CommunityFollowerView;
use diesel::{result::Error, ExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
newtypes::{CommunityId, PersonId},
schema::{community, community_follower, person},
source::{community::Community, person::Person},
traits::JoinView,
utils::DbConn,
utils::{GetConn, RunQueryDsl},
};
type CommunityFollowerViewTuple = (Community, Person);
impl CommunityFollowerView {
pub async fn for_community(
mut conn: impl DbConn,
mut conn: impl GetConn,
community_id: CommunityId,
) -> Result<Vec<Self>, Error> {
let res = community_follower::table
@ -22,13 +21,13 @@ impl CommunityFollowerView {
.select((community::all_columns, person::all_columns))
.filter(community_follower::community_id.eq(community_id))
.order_by(community::title)
.load::<CommunityFollowerViewTuple>(&mut *conn)
.load::<CommunityFollowerViewTuple>(conn)
.await?;
Ok(res.into_iter().map(Self::from_tuple).collect())
}
pub async fn for_person(mut conn: impl DbConn, person_id: PersonId) -> Result<Vec<Self>, Error> {
pub async fn for_person(mut conn: impl GetConn, person_id: PersonId) -> Result<Vec<Self>, Error> {
let res = community_follower::table
.inner_join(community::table)
.inner_join(person::table)
@ -37,7 +36,7 @@ impl CommunityFollowerView {
.filter(community::deleted.eq(false))
.filter(community::removed.eq(false))
.order_by(community::title)
.load::<CommunityFollowerViewTuple>(&mut *conn)
.load::<CommunityFollowerViewTuple>(conn)
.await?;
Ok(res.into_iter().map(Self::from_tuple).collect())

View File

@ -1,19 +1,18 @@
use crate::structs::CommunityModeratorView;
use diesel::{result::Error, ExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
newtypes::{CommunityId, PersonId},
schema::{community, community_moderator, person},
source::{community::Community, person::Person},
traits::JoinView,
utils::DbConn,
utils::{GetConn, RunQueryDsl},
};
type CommunityModeratorViewTuple = (Community, Person);
impl CommunityModeratorView {
pub async fn for_community(
mut conn: impl DbConn,
mut conn: impl GetConn,
community_id: CommunityId,
) -> Result<Vec<Self>, Error> {
let res = community_moderator::table
@ -21,13 +20,13 @@ impl CommunityModeratorView {
.inner_join(person::table)
.select((community::all_columns, person::all_columns))
.filter(community_moderator::community_id.eq(community_id))
.load::<CommunityModeratorViewTuple>(&mut *conn)
.load::<CommunityModeratorViewTuple>(conn)
.await?;
Ok(res.into_iter().map(Self::from_tuple).collect())
}
pub async fn for_person(mut conn: impl DbConn, person_id: PersonId) -> Result<Vec<Self>, Error> {
pub async fn for_person(mut conn: impl GetConn, person_id: PersonId) -> Result<Vec<Self>, Error> {
let res = community_moderator::table
.inner_join(community::table)
.inner_join(person::table)
@ -35,7 +34,7 @@ impl CommunityModeratorView {
.filter(community_moderator::person_id.eq(person_id))
.filter(community::deleted.eq(false))
.filter(community::removed.eq(false))
.load::<CommunityModeratorViewTuple>(&mut *conn)
.load::<CommunityModeratorViewTuple>(conn)
.await?;
Ok(res.into_iter().map(Self::from_tuple).collect())
@ -43,7 +42,7 @@ impl CommunityModeratorView {
/// Finds all communities first mods / creators
/// Ideally this should be a group by, but diesel doesn't support it yet
pub async fn get_community_first_mods(mut conn: impl DbConn) -> Result<Vec<Self>, Error> {
pub async fn get_community_first_mods(mut conn: impl GetConn) -> Result<Vec<Self>, Error> {
let res = community_moderator::table
.inner_join(community::table)
.inner_join(person::table)
@ -55,7 +54,7 @@ impl CommunityModeratorView {
community_moderator::community_id,
community_moderator::person_id,
))
.load::<CommunityModeratorViewTuple>(&mut *conn)
.load::<CommunityModeratorViewTuple>(conn)
.await?;
Ok(res.into_iter().map(Self::from_tuple).collect())

View File

@ -1,16 +1,15 @@
use crate::structs::CommunityPersonBanView;
use diesel::{result::Error, ExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
newtypes::{CommunityId, PersonId},
schema::{community, community_person_ban, person},
source::{community::Community, person::Person},
utils::DbConn,
utils::{GetConn, RunQueryDsl},
};
impl CommunityPersonBanView {
pub async fn get(
mut conn: impl DbConn,
mut conn: impl GetConn,
from_person_id: PersonId,
from_community_id: CommunityId,
) -> Result<Self, Error> {
@ -21,7 +20,7 @@ impl CommunityPersonBanView {
.filter(community_person_ban::community_id.eq(from_community_id))
.filter(community_person_ban::person_id.eq(from_person_id))
.order_by(community_person_ban::published)
.first::<(Community, Person)>(&mut *conn)
.first::<(Community, Person)>(conn)
.await?;
Ok(CommunityPersonBanView { community, person })

View File

@ -8,7 +8,6 @@ use diesel::{
PgTextExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
aggregates::structs::CommunityAggregates,
newtypes::{CommunityId, PersonId},
@ -19,7 +18,7 @@ use lemmy_db_schema::{
local_user::LocalUser,
},
traits::JoinView,
utils::{fuzzy_search, limit_and_offset, DbConn},
utils::{fuzzy_search, limit_and_offset, GetConn, RunQueryDsl},
ListingType,
SortType,
};
@ -34,7 +33,7 @@ type CommunityViewTuple = (
impl CommunityView {
pub async fn read(
mut conn: impl DbConn,
mut conn: impl GetConn,
community_id: CommunityId,
my_person_id: Option<PersonId>,
is_mod_or_admin: Option<bool>,
@ -75,7 +74,7 @@ impl CommunityView {
}
let (community, counts, follower, blocked) =
query.first::<CommunityViewTuple>(&mut *conn).await?;
query.first::<CommunityViewTuple>(conn).await?;
Ok(CommunityView {
community,
@ -86,11 +85,11 @@ impl CommunityView {
}
pub async fn is_mod_or_admin(
mut conn: impl DbConn,
mut conn: impl GetConn,
person_id: PersonId,
community_id: CommunityId,
) -> Result<bool, Error> {
let is_mod = CommunityModeratorView::for_community(&mut *conn, community_id)
let is_mod = CommunityModeratorView::for_community(conn, community_id)
.await
.map(|v| {
v.into_iter()
@ -103,7 +102,7 @@ impl CommunityView {
return Ok(true);
}
let is_admin = PersonView::admins(&mut *conn)
let is_admin = PersonView::admins(conn)
.await
.map(|v| {
v.into_iter()
@ -131,7 +130,7 @@ pub struct CommunityQuery<'a, Conn> {
limit: Option<i64>,
}
impl<'a, Conn: DbConn> CommunityQuery<'a, Conn> {
impl<'a, Conn: GetConn> CommunityQuery<'a, Conn> {
pub async fn list(self) -> Result<Vec<CommunityView>, Error> {
use SortType::*;
@ -224,7 +223,7 @@ impl<'a, Conn: DbConn> CommunityQuery<'a, Conn> {
let res = query
.limit(limit)
.offset(offset)
.load::<CommunityViewTuple>(&mut *conn)
.load::<CommunityViewTuple>(conn)
.await?;
Ok(res.into_iter().map(CommunityView::from_tuple).collect())

View File

@ -1,18 +1,17 @@
use crate::structs::PersonBlockView;
use diesel::{result::Error, ExpressionMethods, JoinOnDsl, QueryDsl};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
newtypes::PersonId,
schema::{person, person_block},
source::person::Person,
traits::JoinView,
utils::DbConn,
utils::{GetConn, RunQueryDsl},
};
type PersonBlockViewTuple = (Person, Person);
impl PersonBlockView {
pub async fn for_person(mut conn: impl DbConn, person_id: PersonId) -> Result<Vec<Self>, Error> {
pub async fn for_person(mut conn: impl GetConn, person_id: PersonId) -> Result<Vec<Self>, Error> {
let target_person_alias = diesel::alias!(person as person1);
let res = person_block::table
@ -27,7 +26,7 @@ impl PersonBlockView {
.filter(person_block::person_id.eq(person_id))
.filter(target_person_alias.field(person::deleted).eq(false))
.order_by(person_block::published)
.load::<PersonBlockViewTuple>(&mut *conn)
.load::<PersonBlockViewTuple>(conn)
.await?;
Ok(res.into_iter().map(Self::from_tuple).collect())

View File

@ -8,7 +8,6 @@ use diesel::{
NullableExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
aggregates::structs::CommentAggregates,
newtypes::{PersonId, PersonMentionId},
@ -34,7 +33,7 @@ use lemmy_db_schema::{
post::Post,
},
traits::JoinView,
utils::{limit_and_offset, DbConn},
utils::{limit_and_offset, GetConn, RunQueryDsl},
CommentSortType,
};
use typed_builder::TypedBuilder;
@ -56,7 +55,7 @@ type PersonMentionViewTuple = (
impl PersonMentionView {
pub async fn read(
mut conn: impl DbConn,
mut conn: impl GetConn,
person_mention_id: PersonMentionId,
my_person_id: Option<PersonId>,
) -> Result<Self, Error> {
@ -135,7 +134,7 @@ impl PersonMentionView {
person_block::all_columns.nullable(),
comment_like::score.nullable(),
))
.first::<PersonMentionViewTuple>(&mut *conn)
.first::<PersonMentionViewTuple>(conn)
.await?;
Ok(PersonMentionView {
@ -156,7 +155,7 @@ impl PersonMentionView {
/// Gets the number of unread mentions
pub async fn get_unread_mentions(
mut conn: impl DbConn,
mut conn: impl GetConn,
my_person_id: PersonId,
) -> Result<i64, Error> {
use diesel::dsl::count;
@ -168,7 +167,7 @@ impl PersonMentionView {
.filter(comment::deleted.eq(false))
.filter(comment::removed.eq(false))
.select(count(person_mention::id))
.first::<i64>(&mut *conn)
.first::<i64>(conn)
.await
}
}
@ -187,7 +186,7 @@ pub struct PersonMentionQuery<Conn> {
limit: Option<i64>,
}
impl<Conn: DbConn> PersonMentionQuery<Conn> {
impl<Conn: GetConn> PersonMentionQuery<Conn> {
pub async fn list(self) -> Result<Vec<PersonMentionView>, Error> {
let mut conn = self.conn;
@ -283,7 +282,7 @@ impl<Conn: DbConn> PersonMentionQuery<Conn> {
let res = query
.limit(limit)
.offset(offset)
.load::<PersonMentionViewTuple>(&mut *conn)
.load::<PersonMentionViewTuple>(conn)
.await?;
Ok(res.into_iter().map(PersonMentionView::from_tuple).collect())

View File

@ -7,14 +7,13 @@ use diesel::{
PgTextExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
aggregates::structs::PersonAggregates,
newtypes::PersonId,
schema::{person, person_aggregates},
source::person::Person,
traits::JoinView,
utils::{fuzzy_search, limit_and_offset, DbConn},
utils::{fuzzy_search, limit_and_offset, GetConn, RunQueryDsl},
SortType,
};
use std::iter::Iterator;
@ -23,30 +22,30 @@ use typed_builder::TypedBuilder;
type PersonViewTuple = (Person, PersonAggregates);
impl PersonView {
pub async fn read(mut conn: impl DbConn, person_id: PersonId) -> Result<Self, Error> {
pub async fn read(mut conn: impl GetConn, person_id: PersonId) -> Result<Self, Error> {
let res = person::table
.find(person_id)
.inner_join(person_aggregates::table)
.select((person::all_columns, person_aggregates::all_columns))
.first::<PersonViewTuple>(&mut *conn)
.first::<PersonViewTuple>(conn)
.await?;
Ok(Self::from_tuple(res))
}
pub async fn admins(mut conn: impl DbConn) -> Result<Vec<Self>, Error> {
pub async fn admins(mut conn: impl GetConn) -> Result<Vec<Self>, Error> {
let admins = person::table
.inner_join(person_aggregates::table)
.select((person::all_columns, person_aggregates::all_columns))
.filter(person::admin.eq(true))
.filter(person::deleted.eq(false))
.order_by(person::published)
.load::<PersonViewTuple>(&mut *conn)
.load::<PersonViewTuple>(conn)
.await?;
Ok(admins.into_iter().map(Self::from_tuple).collect())
}
pub async fn banned(mut conn: impl DbConn) -> Result<Vec<Self>, Error> {
pub async fn banned(mut conn: impl GetConn) -> Result<Vec<Self>, Error> {
let banned = person::table
.inner_join(person_aggregates::table)
.select((person::all_columns, person_aggregates::all_columns))
@ -58,7 +57,7 @@ impl PersonView {
),
)
.filter(person::deleted.eq(false))
.load::<PersonViewTuple>(&mut *conn)
.load::<PersonViewTuple>(conn)
.await?;
Ok(banned.into_iter().map(Self::from_tuple).collect())
@ -76,7 +75,7 @@ pub struct PersonQuery<Conn> {
limit: Option<i64>,
}
impl<Conn: DbConn> PersonQuery<Conn> {
impl<Conn: GetConn> PersonQuery<Conn> {
pub async fn list(self) -> Result<Vec<PersonView>, Error> {
let mut conn = self.conn;
let mut query = person::table
@ -133,7 +132,7 @@ impl<Conn: DbConn> PersonQuery<Conn> {
let (limit, offset) = limit_and_offset(self.page, self.limit)?;
query = query.limit(limit).offset(offset);
let res = query.load::<PersonViewTuple>(&mut *conn).await?;
let res = query.load::<PersonViewTuple>(conn).await?;
Ok(res.into_iter().map(PersonView::from_tuple).collect())
}

View File

@ -8,19 +8,18 @@ use diesel::{
NullableExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
newtypes::PersonId,
schema::{admin_purge_comment, person, post},
source::{moderator::AdminPurgeComment, person::Person, post::Post},
traits::JoinView,
utils::{limit_and_offset, DbConn},
utils::{limit_and_offset, GetConn, RunQueryDsl},
};
type AdminPurgeCommentViewTuple = (AdminPurgeComment, Option<Person>, Post);
impl AdminPurgeCommentView {
pub async fn list(mut conn: impl DbConn, params: ModlogListParams) -> Result<Vec<Self>, Error> {
pub async fn list(mut conn: impl GetConn, params: ModlogListParams) -> Result<Vec<Self>, Error> {
let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1));
let show_mod_names = !params.hide_modlog_names;
let show_mod_names_expr = show_mod_names.as_sql::<diesel::sql_types::Bool>();
@ -49,7 +48,7 @@ impl AdminPurgeCommentView {
.limit(limit)
.offset(offset)
.order_by(admin_purge_comment::when_.desc())
.load::<AdminPurgeCommentViewTuple>(&mut *conn)
.load::<AdminPurgeCommentViewTuple>(conn)
.await?;
let results = res.into_iter().map(Self::from_tuple).collect();

View File

@ -8,19 +8,18 @@ use diesel::{
NullableExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
newtypes::PersonId,
schema::{admin_purge_community, person},
source::{moderator::AdminPurgeCommunity, person::Person},
traits::JoinView,
utils::{limit_and_offset, DbConn},
utils::{limit_and_offset, GetConn, RunQueryDsl},
};
type AdminPurgeCommunityViewTuple = (AdminPurgeCommunity, Option<Person>);
impl AdminPurgeCommunityView {
pub async fn list(mut conn: impl DbConn, params: ModlogListParams) -> Result<Vec<Self>, Error> {
pub async fn list(mut conn: impl GetConn, params: ModlogListParams) -> Result<Vec<Self>, Error> {
let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1));
let show_mod_names = !params.hide_modlog_names;
let show_mod_names_expr = show_mod_names.as_sql::<diesel::sql_types::Bool>();
@ -47,7 +46,7 @@ impl AdminPurgeCommunityView {
.limit(limit)
.offset(offset)
.order_by(admin_purge_community::when_.desc())
.load::<AdminPurgeCommunityViewTuple>(&mut *conn)
.load::<AdminPurgeCommunityViewTuple>(conn)
.await?;
let results = res.into_iter().map(Self::from_tuple).collect();

View File

@ -8,19 +8,18 @@ use diesel::{
NullableExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
newtypes::PersonId,
schema::{admin_purge_person, person},
source::{moderator::AdminPurgePerson, person::Person},
traits::JoinView,
utils::{limit_and_offset, DbConn},
utils::{limit_and_offset, GetConn, RunQueryDsl},
};
type AdminPurgePersonViewTuple = (AdminPurgePerson, Option<Person>);
impl AdminPurgePersonView {
pub async fn list(mut conn: impl DbConn, params: ModlogListParams) -> Result<Vec<Self>, Error> {
pub async fn list(mut conn: impl GetConn, params: ModlogListParams) -> Result<Vec<Self>, Error> {
let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1));
let show_mod_names = !params.hide_modlog_names;
let show_mod_names_expr = show_mod_names.as_sql::<diesel::sql_types::Bool>();
@ -46,7 +45,7 @@ impl AdminPurgePersonView {
.limit(limit)
.offset(offset)
.order_by(admin_purge_person::when_.desc())
.load::<AdminPurgePersonViewTuple>(&mut *conn)
.load::<AdminPurgePersonViewTuple>(conn)
.await?;
let results = res.into_iter().map(Self::from_tuple).collect();

View File

@ -8,19 +8,18 @@ use diesel::{
NullableExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
newtypes::PersonId,
schema::{admin_purge_post, community, person},
source::{community::Community, moderator::AdminPurgePost, person::Person},
traits::JoinView,
utils::{limit_and_offset, DbConn},
utils::{limit_and_offset, GetConn, RunQueryDsl},
};
type AdminPurgePostViewTuple = (AdminPurgePost, Option<Person>, Community);
impl AdminPurgePostView {
pub async fn list(mut conn: impl DbConn, params: ModlogListParams) -> Result<Vec<Self>, Error> {
pub async fn list(mut conn: impl GetConn, params: ModlogListParams) -> Result<Vec<Self>, Error> {
let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1));
let show_mod_names = !params.hide_modlog_names;
let show_mod_names_expr = show_mod_names.as_sql::<diesel::sql_types::Bool>();
@ -48,7 +47,7 @@ impl AdminPurgePostView {
.limit(limit)
.offset(offset)
.order_by(admin_purge_post::when_.desc())
.load::<AdminPurgePostViewTuple>(&mut *conn)
.load::<AdminPurgePostViewTuple>(conn)
.await?;
let results = res.into_iter().map(Self::from_tuple).collect();

View File

@ -8,19 +8,18 @@ use diesel::{
NullableExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
newtypes::PersonId,
schema::{community, mod_add_community, person},
source::{community::Community, moderator::ModAddCommunity, person::Person},
traits::JoinView,
utils::{limit_and_offset, DbConn},
utils::{limit_and_offset, GetConn, RunQueryDsl},
};
type ModAddCommunityViewTuple = (ModAddCommunity, Option<Person>, Community, Person);
impl ModAddCommunityView {
pub async fn list(mut conn: impl DbConn, params: ModlogListParams) -> Result<Vec<Self>, Error> {
pub async fn list(mut conn: impl GetConn, params: ModlogListParams) -> Result<Vec<Self>, Error> {
let person_alias_1 = diesel::alias!(person as person1);
let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1));
let show_mod_names = !params.hide_modlog_names;
@ -61,7 +60,7 @@ impl ModAddCommunityView {
.limit(limit)
.offset(offset)
.order_by(mod_add_community::when_.desc())
.load::<ModAddCommunityViewTuple>(&mut *conn)
.load::<ModAddCommunityViewTuple>(conn)
.await?;
let results = res.into_iter().map(Self::from_tuple).collect();

View File

@ -8,19 +8,18 @@ use diesel::{
NullableExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
newtypes::PersonId,
schema::{mod_add, person},
source::{moderator::ModAdd, person::Person},
traits::JoinView,
utils::{limit_and_offset, DbConn},
utils::{limit_and_offset, GetConn, RunQueryDsl},
};
type ModAddViewTuple = (ModAdd, Option<Person>, Person);
impl ModAddView {
pub async fn list(mut conn: impl DbConn, params: ModlogListParams) -> Result<Vec<Self>, Error> {
pub async fn list(mut conn: impl GetConn, params: ModlogListParams) -> Result<Vec<Self>, Error> {
let person_alias_1 = diesel::alias!(person as person1);
let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1));
let show_mod_names = !params.hide_modlog_names;
@ -53,7 +52,7 @@ impl ModAddView {
.limit(limit)
.offset(offset)
.order_by(mod_add::when_.desc())
.load::<ModAddViewTuple>(&mut *conn)
.load::<ModAddViewTuple>(conn)
.await?;
let results = res.into_iter().map(Self::from_tuple).collect();

View File

@ -8,19 +8,18 @@ use diesel::{
NullableExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
newtypes::PersonId,
schema::{community, mod_ban_from_community, person},
source::{community::Community, moderator::ModBanFromCommunity, person::Person},
traits::JoinView,
utils::{limit_and_offset, DbConn},
utils::{limit_and_offset, GetConn, RunQueryDsl},
};
type ModBanFromCommunityViewTuple = (ModBanFromCommunity, Option<Person>, Community, Person);
impl ModBanFromCommunityView {
pub async fn list(mut conn: impl DbConn, params: ModlogListParams) -> Result<Vec<Self>, Error> {
pub async fn list(mut conn: impl GetConn, params: ModlogListParams) -> Result<Vec<Self>, Error> {
let person_alias_1 = diesel::alias!(person as person1);
let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1));
let show_mod_names = !params.hide_modlog_names;
@ -62,7 +61,7 @@ impl ModBanFromCommunityView {
.limit(limit)
.offset(offset)
.order_by(mod_ban_from_community::when_.desc())
.load::<ModBanFromCommunityViewTuple>(&mut *conn)
.load::<ModBanFromCommunityViewTuple>(conn)
.await?;
let results = res.into_iter().map(Self::from_tuple).collect();

View File

@ -8,19 +8,18 @@ use diesel::{
NullableExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
newtypes::PersonId,
schema::{mod_ban, person},
source::{moderator::ModBan, person::Person},
traits::JoinView,
utils::{limit_and_offset, DbConn},
utils::{limit_and_offset, GetConn, RunQueryDsl},
};
type ModBanViewTuple = (ModBan, Option<Person>, Person);
impl ModBanView {
pub async fn list(mut conn: impl DbConn, params: ModlogListParams) -> Result<Vec<Self>, Error> {
pub async fn list(mut conn: impl GetConn, params: ModlogListParams) -> Result<Vec<Self>, Error> {
let person_alias_1 = diesel::alias!(person as person1);
let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1));
let show_mod_names = !params.hide_modlog_names;
@ -53,7 +52,7 @@ impl ModBanView {
.limit(limit)
.offset(offset)
.order_by(mod_ban::when_.desc())
.load::<ModBanViewTuple>(&mut *conn)
.load::<ModBanViewTuple>(conn)
.await?;
let results = res.into_iter().map(Self::from_tuple).collect();

View File

@ -8,19 +8,18 @@ use diesel::{
NullableExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
newtypes::PersonId,
schema::{community, mod_feature_post, person, post},
source::{community::Community, moderator::ModFeaturePost, person::Person, post::Post},
traits::JoinView,
utils::{limit_and_offset, DbConn},
utils::{limit_and_offset, GetConn, RunQueryDsl},
};
type ModFeaturePostViewTuple = (ModFeaturePost, Option<Person>, Post, Community);
impl ModFeaturePostView {
pub async fn list(mut conn: impl DbConn, params: ModlogListParams) -> Result<Vec<Self>, Error> {
pub async fn list(mut conn: impl GetConn, params: ModlogListParams) -> Result<Vec<Self>, Error> {
let person_alias_1 = diesel::alias!(person as person1);
let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1));
let show_mod_names = !params.hide_modlog_names;
@ -60,7 +59,7 @@ impl ModFeaturePostView {
.limit(limit)
.offset(offset)
.order_by(mod_feature_post::when_.desc())
.load::<ModFeaturePostViewTuple>(&mut *conn)
.load::<ModFeaturePostViewTuple>(conn)
.await?;
let results = res.into_iter().map(Self::from_tuple).collect();

View File

@ -8,20 +8,19 @@ use diesel::{
NullableExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
newtypes::PersonId,
schema::{community, mod_hide_community, person},
source::{community::Community, moderator::ModHideCommunity, person::Person},
traits::JoinView,
utils::{limit_and_offset, DbConn},
utils::{limit_and_offset, GetConn, RunQueryDsl},
};
type ModHideCommunityViewTuple = (ModHideCommunity, Option<Person>, Community);
impl ModHideCommunityView {
// Pass in mod_id as admin_id because only admins can do this action
pub async fn list(mut conn: impl DbConn, params: ModlogListParams) -> Result<Vec<Self>, Error> {
pub async fn list(mut conn: impl GetConn, params: ModlogListParams) -> Result<Vec<Self>, Error> {
let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1));
let show_mod_names = !params.hide_modlog_names;
let show_mod_names_expr = show_mod_names.as_sql::<diesel::sql_types::Bool>();
@ -53,7 +52,7 @@ impl ModHideCommunityView {
.limit(limit)
.offset(offset)
.order_by(mod_hide_community::when_.desc())
.load::<ModHideCommunityViewTuple>(&mut *conn)
.load::<ModHideCommunityViewTuple>(conn)
.await?;
let results = res.into_iter().map(Self::from_tuple).collect();

View File

@ -8,19 +8,18 @@ use diesel::{
NullableExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
newtypes::PersonId,
schema::{community, mod_lock_post, person, post},
source::{community::Community, moderator::ModLockPost, person::Person, post::Post},
traits::JoinView,
utils::{limit_and_offset, DbConn},
utils::{limit_and_offset, GetConn, RunQueryDsl},
};
type ModLockPostViewTuple = (ModLockPost, Option<Person>, Post, Community);
impl ModLockPostView {
pub async fn list(mut conn: impl DbConn, params: ModlogListParams) -> Result<Vec<Self>, Error> {
pub async fn list(mut conn: impl GetConn, params: ModlogListParams) -> Result<Vec<Self>, Error> {
let person_alias_1 = diesel::alias!(person as person1);
let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1));
let show_mod_names = !params.hide_modlog_names;
@ -60,7 +59,7 @@ impl ModLockPostView {
.limit(limit)
.offset(offset)
.order_by(mod_lock_post::when_.desc())
.load::<ModLockPostViewTuple>(&mut *conn)
.load::<ModLockPostViewTuple>(conn)
.await?;
let results = res.into_iter().map(Self::from_tuple).collect();

View File

@ -8,7 +8,6 @@ use diesel::{
NullableExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
newtypes::PersonId,
schema::{comment, community, mod_remove_comment, person, post},
@ -20,7 +19,7 @@ use lemmy_db_schema::{
post::Post,
},
traits::JoinView,
utils::{limit_and_offset, DbConn},
utils::{limit_and_offset, GetConn, RunQueryDsl},
};
type ModRemoveCommentViewTuple = (
@ -33,7 +32,7 @@ type ModRemoveCommentViewTuple = (
);
impl ModRemoveCommentView {
pub async fn list(mut conn: impl DbConn, params: ModlogListParams) -> Result<Vec<Self>, Error> {
pub async fn list(mut conn: impl GetConn, params: ModlogListParams) -> Result<Vec<Self>, Error> {
let person_alias_1 = diesel::alias!(lemmy_db_schema::schema::person as person1);
let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1));
let show_mod_names = !params.hide_modlog_names;
@ -76,7 +75,7 @@ impl ModRemoveCommentView {
.limit(limit)
.offset(offset)
.order_by(mod_remove_comment::when_.desc())
.load::<ModRemoveCommentViewTuple>(&mut *conn)
.load::<ModRemoveCommentViewTuple>(conn)
.await?;
let results = res.into_iter().map(Self::from_tuple).collect();

View File

@ -8,19 +8,18 @@ use diesel::{
NullableExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
newtypes::PersonId,
schema::{community, mod_remove_community, person},
source::{community::Community, moderator::ModRemoveCommunity, person::Person},
traits::JoinView,
utils::{limit_and_offset, DbConn},
utils::{limit_and_offset, GetConn, RunQueryDsl},
};
type ModRemoveCommunityTuple = (ModRemoveCommunity, Option<Person>, Community);
impl ModRemoveCommunityView {
pub async fn list(mut conn: impl DbConn, params: ModlogListParams) -> Result<Vec<Self>, Error> {
pub async fn list(mut conn: impl GetConn, params: ModlogListParams) -> Result<Vec<Self>, Error> {
let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1));
let show_mod_names = !params.hide_modlog_names;
let show_mod_names_expr = show_mod_names.as_sql::<diesel::sql_types::Bool>();
@ -48,7 +47,7 @@ impl ModRemoveCommunityView {
.limit(limit)
.offset(offset)
.order_by(mod_remove_community::when_.desc())
.load::<ModRemoveCommunityTuple>(&mut *conn)
.load::<ModRemoveCommunityTuple>(conn)
.await?;
let results = res.into_iter().map(Self::from_tuple).collect();

View File

@ -8,19 +8,18 @@ use diesel::{
NullableExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
newtypes::PersonId,
schema::{community, mod_remove_post, person, post},
source::{community::Community, moderator::ModRemovePost, person::Person, post::Post},
traits::JoinView,
utils::{limit_and_offset, DbConn},
utils::{limit_and_offset, GetConn, RunQueryDsl},
};
type ModRemovePostViewTuple = (ModRemovePost, Option<Person>, Post, Community);
impl ModRemovePostView {
pub async fn list(mut conn: impl DbConn, params: ModlogListParams) -> Result<Vec<Self>, Error> {
pub async fn list(mut conn: impl GetConn, params: ModlogListParams) -> Result<Vec<Self>, Error> {
let person_alias_1 = diesel::alias!(person as person1);
let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1));
let show_mod_names = !params.hide_modlog_names;
@ -60,7 +59,7 @@ impl ModRemovePostView {
.limit(limit)
.offset(offset)
.order_by(mod_remove_post::when_.desc())
.load::<ModRemovePostViewTuple>(&mut *conn)
.load::<ModRemovePostViewTuple>(conn)
.await?;
let results = res.into_iter().map(Self::from_tuple).collect();

View File

@ -8,19 +8,18 @@ use diesel::{
NullableExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
newtypes::PersonId,
schema::{community, mod_transfer_community, person},
source::{community::Community, moderator::ModTransferCommunity, person::Person},
traits::JoinView,
utils::{limit_and_offset, DbConn},
utils::{limit_and_offset, GetConn, RunQueryDsl},
};
type ModTransferCommunityViewTuple = (ModTransferCommunity, Option<Person>, Community, Person);
impl ModTransferCommunityView {
pub async fn list(mut conn: impl DbConn, params: ModlogListParams) -> Result<Vec<Self>, Error> {
pub async fn list(mut conn: impl GetConn, params: ModlogListParams) -> Result<Vec<Self>, Error> {
let person_alias_1 = diesel::alias!(person as person1);
let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1));
let show_mod_names = !params.hide_modlog_names;
@ -62,7 +61,7 @@ impl ModTransferCommunityView {
.limit(limit)
.offset(offset)
.order_by(mod_transfer_community::when_.desc())
.load::<ModTransferCommunityViewTuple>(&mut *conn)
.load::<ModTransferCommunityViewTuple>(conn)
.await?;
let results = res.into_iter().map(Self::from_tuple).collect();

View File

@ -7,7 +7,6 @@ use diesel::{
QueryDsl,
TextExpressionMethods,
};
use diesel_async::RunQueryDsl;
use lemmy_api_common::{
lemmy_db_views::structs::SiteView,
utils::{
@ -33,33 +32,33 @@ use lemmy_db_schema::{
site::{Site, SiteInsertForm, SiteUpdateForm},
},
traits::Crud,
utils::{naive_now, DbConn},
utils::{naive_now, GetConn, RunQueryDsl},
};
use lemmy_utils::{error::LemmyError, settings::structs::Settings};
use tracing::info;
use url::Url;
pub async fn run_advanced_migrations(
mut conn: impl DbConn,
mut conn: impl GetConn,
settings: &Settings,
) -> Result<(), LemmyError> {
let protocol_and_hostname = &settings.get_protocol_and_hostname();
user_updates_2020_04_02(&mut *conn, protocol_and_hostname).await?;
community_updates_2020_04_02(&mut *conn, protocol_and_hostname).await?;
post_updates_2020_04_03(&mut *conn, protocol_and_hostname).await?;
comment_updates_2020_04_03(&mut *conn, protocol_and_hostname).await?;
private_message_updates_2020_05_05(&mut *conn, protocol_and_hostname).await?;
post_thumbnail_url_updates_2020_07_27(&mut *conn, protocol_and_hostname).await?;
apub_columns_2021_02_02(&mut *conn).await?;
instance_actor_2022_01_28(&mut *conn, protocol_and_hostname).await?;
regenerate_public_keys_2022_07_05(&mut *conn).await?;
initialize_local_site_2022_10_10(&mut *conn, settings).await?;
user_updates_2020_04_02(conn, protocol_and_hostname).await?;
community_updates_2020_04_02(conn, protocol_and_hostname).await?;
post_updates_2020_04_03(conn, protocol_and_hostname).await?;
comment_updates_2020_04_03(conn, protocol_and_hostname).await?;
private_message_updates_2020_05_05(conn, protocol_and_hostname).await?;
post_thumbnail_url_updates_2020_07_27(conn, protocol_and_hostname).await?;
apub_columns_2021_02_02(conn).await?;
instance_actor_2022_01_28(conn, protocol_and_hostname).await?;
regenerate_public_keys_2022_07_05(conn).await?;
initialize_local_site_2022_10_10(conn, settings).await?;
Ok(())
}
async fn user_updates_2020_04_02(
mut conn: impl DbConn,
mut conn: impl GetConn,
protocol_and_hostname: &str,
) -> Result<(), LemmyError> {
use lemmy_db_schema::schema::person::dsl::{actor_id, local, person};
@ -70,7 +69,7 @@ async fn user_updates_2020_04_02(
let incorrect_persons = person
.filter(actor_id.like("http://changeme%"))
.filter(local.eq(true))
.load::<Person>(&mut *conn)
.load::<Person>(conn)
.await?;
for cperson in &incorrect_persons {
@ -87,7 +86,7 @@ async fn user_updates_2020_04_02(
.last_refreshed_at(Some(naive_now()))
.build();
Person::update(&mut *conn, cperson.id, &form).await?;
Person::update(conn, cperson.id, &form).await?;
}
info!("{} person rows updated.", incorrect_persons.len());
@ -96,7 +95,7 @@ async fn user_updates_2020_04_02(
}
async fn community_updates_2020_04_02(
mut conn: impl DbConn,
mut conn: impl GetConn,
protocol_and_hostname: &str,
) -> Result<(), LemmyError> {
use lemmy_db_schema::schema::community::dsl::{actor_id, community, local};
@ -107,7 +106,7 @@ async fn community_updates_2020_04_02(
let incorrect_communities = community
.filter(actor_id.like("http://changeme%"))
.filter(local.eq(true))
.load::<Community>(&mut *conn)
.load::<Community>(conn)
.await?;
for ccommunity in &incorrect_communities {
@ -125,7 +124,7 @@ async fn community_updates_2020_04_02(
.last_refreshed_at(Some(naive_now()))
.build();
Community::update(&mut *conn, ccommunity.id, &form).await?;
Community::update(conn, ccommunity.id, &form).await?;
}
info!("{} community rows updated.", incorrect_communities.len());
@ -134,7 +133,7 @@ async fn community_updates_2020_04_02(
}
async fn post_updates_2020_04_03(
mut conn: impl DbConn,
mut conn: impl GetConn,
protocol_and_hostname: &str,
) -> Result<(), LemmyError> {
use lemmy_db_schema::schema::post::dsl::{ap_id, local, post};
@ -145,7 +144,7 @@ async fn post_updates_2020_04_03(
let incorrect_posts = post
.filter(ap_id.like("http://changeme%"))
.filter(local.eq(true))
.load::<Post>(&mut *conn)
.load::<Post>(conn)
.await?;
for cpost in &incorrect_posts {
@ -155,7 +154,7 @@ async fn post_updates_2020_04_03(
protocol_and_hostname,
)?;
Post::update(
&mut *conn,
conn,
cpost.id,
&PostUpdateForm::builder().ap_id(Some(apub_id)).build(),
)
@ -168,7 +167,7 @@ async fn post_updates_2020_04_03(
}
async fn comment_updates_2020_04_03(
mut conn: impl DbConn,
mut conn: impl GetConn,
protocol_and_hostname: &str,
) -> Result<(), LemmyError> {
use lemmy_db_schema::schema::comment::dsl::{ap_id, comment, local};
@ -179,7 +178,7 @@ async fn comment_updates_2020_04_03(
let incorrect_comments = comment
.filter(ap_id.like("http://changeme%"))
.filter(local.eq(true))
.load::<Comment>(&mut *conn)
.load::<Comment>(conn)
.await?;
for ccomment in &incorrect_comments {
@ -189,7 +188,7 @@ async fn comment_updates_2020_04_03(
protocol_and_hostname,
)?;
Comment::update(
&mut *conn,
conn,
ccomment.id,
&CommentUpdateForm::builder().ap_id(Some(apub_id)).build(),
)
@ -202,7 +201,7 @@ async fn comment_updates_2020_04_03(
}
async fn private_message_updates_2020_05_05(
mut conn: impl DbConn,
mut conn: impl GetConn,
protocol_and_hostname: &str,
) -> Result<(), LemmyError> {
use lemmy_db_schema::schema::private_message::dsl::{ap_id, local, private_message};
@ -213,7 +212,7 @@ async fn private_message_updates_2020_05_05(
let incorrect_pms = private_message
.filter(ap_id.like("http://changeme%"))
.filter(local.eq(true))
.load::<PrivateMessage>(&mut *conn)
.load::<PrivateMessage>(conn)
.await?;
for cpm in &incorrect_pms {
@ -223,7 +222,7 @@ async fn private_message_updates_2020_05_05(
protocol_and_hostname,
)?;
PrivateMessage::update(
&mut *conn,
conn,
cpm.id,
&PrivateMessageUpdateForm::builder()
.ap_id(Some(apub_id))
@ -238,7 +237,7 @@ async fn private_message_updates_2020_05_05(
}
async fn post_thumbnail_url_updates_2020_07_27(
mut conn: impl DbConn,
mut conn: impl GetConn,
protocol_and_hostname: &str,
) -> Result<(), LemmyError> {
use lemmy_db_schema::schema::post::dsl::{post, thumbnail_url};
@ -258,7 +257,7 @@ async fn post_thumbnail_url_updates_2020_07_27(
.concat(thumbnail_url),
),
)
.get_results::<Post>(&mut *conn)
.get_results::<Post>(conn)
.await?;
info!("{} Post thumbnail_url rows updated.", res.len());
@ -268,13 +267,13 @@ async fn post_thumbnail_url_updates_2020_07_27(
/// We are setting inbox and follower URLs for local and remote actors alike, because for now
/// all federated instances are also Lemmy and use the same URL scheme.
async fn apub_columns_2021_02_02(mut conn: impl DbConn) -> Result<(), LemmyError> {
async fn apub_columns_2021_02_02(mut conn: impl GetConn) -> Result<(), LemmyError> {
info!("Running apub_columns_2021_02_02");
{
use lemmy_db_schema::schema::person::dsl::{inbox_url, person, shared_inbox_url};
let persons = person
.filter(inbox_url.like("http://changeme%"))
.load::<Person>(&mut *conn)
.load::<Person>(conn)
.await?;
for p in &persons {
@ -285,7 +284,7 @@ async fn apub_columns_2021_02_02(mut conn: impl DbConn) -> Result<(), LemmyError
inbox_url.eq(inbox_url_),
shared_inbox_url.eq(shared_inbox_url_),
))
.get_result::<Person>(&mut *conn)
.get_result::<Person>(conn)
.await?;
}
}
@ -299,7 +298,7 @@ async fn apub_columns_2021_02_02(mut conn: impl DbConn) -> Result<(), LemmyError
};
let communities = community
.filter(inbox_url.like("http://changeme%"))
.load::<Community>(&mut *conn)
.load::<Community>(conn)
.await?;
for c in &communities {
@ -312,7 +311,7 @@ async fn apub_columns_2021_02_02(mut conn: impl DbConn) -> Result<(), LemmyError
inbox_url.eq(inbox_url_),
shared_inbox_url.eq(shared_inbox_url_),
))
.get_result::<Community>(&mut *conn)
.get_result::<Community>(conn)
.await?;
}
}
@ -325,11 +324,11 @@ async fn apub_columns_2021_02_02(mut conn: impl DbConn) -> Result<(), LemmyError
/// Before this point, there is only a single value in the site table which refers to the local
/// Lemmy instance, so thats all we need to update.
async fn instance_actor_2022_01_28(
mut conn: impl DbConn,
mut conn: impl GetConn,
protocol_and_hostname: &str,
) -> Result<(), LemmyError> {
info!("Running instance_actor_2021_09_29");
if let Ok(site_view) = SiteView::read_local(&mut *conn).await {
if let Ok(site_view) = SiteView::read_local(conn).await {
let site = site_view.site;
// if site already has public key, we dont need to do anything here
if !site.public_key.is_empty() {
@ -344,7 +343,7 @@ async fn instance_actor_2022_01_28(
.private_key(Some(Some(key_pair.private_key)))
.public_key(Some(key_pair.public_key))
.build();
Site::update(&mut *conn, site.id, &site_form).await?;
Site::update(conn, site.id, &site_form).await?;
}
Ok(())
}
@ -354,7 +353,7 @@ async fn instance_actor_2022_01_28(
/// key field is empty, generate a new keypair. It would be possible to regenerate only the pubkey,
/// but thats more complicated and has no benefit, as federation is already broken for these actors.
/// https://github.com/LemmyNet/lemmy/issues/2347
async fn regenerate_public_keys_2022_07_05(mut conn: impl DbConn) -> Result<(), LemmyError> {
async fn regenerate_public_keys_2022_07_05(mut conn: impl GetConn) -> Result<(), LemmyError> {
info!("Running regenerate_public_keys_2022_07_05");
{
@ -363,7 +362,7 @@ async fn regenerate_public_keys_2022_07_05(mut conn: impl DbConn) -> Result<(),
let communities: Vec<Community> = community
.filter(local.eq(true))
.filter(public_key.eq(""))
.load::<Community>(&mut *conn)
.load::<Community>(conn)
.await?;
for community_ in communities {
info!(
@ -375,7 +374,7 @@ async fn regenerate_public_keys_2022_07_05(mut conn: impl DbConn) -> Result<(),
.public_key(Some(key_pair.public_key))
.private_key(Some(Some(key_pair.private_key)))
.build();
Community::update(&mut *conn, community_.id, &form).await?;
Community::update(conn, community_.id, &form).await?;
}
}
@ -385,7 +384,7 @@ async fn regenerate_public_keys_2022_07_05(mut conn: impl DbConn) -> Result<(),
let persons = person
.filter(local.eq(true))
.filter(public_key.eq(""))
.load::<Person>(&mut *conn)
.load::<Person>(conn)
.await?;
for person_ in persons {
info!(
@ -397,7 +396,7 @@ async fn regenerate_public_keys_2022_07_05(mut conn: impl DbConn) -> Result<(),
.public_key(Some(key_pair.public_key))
.private_key(Some(Some(key_pair.private_key)))
.build();
Person::update(&mut *conn, person_.id, &form).await?;
Person::update(conn, person_.id, &form).await?;
}
}
Ok(())
@ -408,13 +407,13 @@ async fn regenerate_public_keys_2022_07_05(mut conn: impl DbConn) -> Result<(),
/// If a site already exists, the DB migration should generate a local_site row.
/// This will only be run for brand new sites.
async fn initialize_local_site_2022_10_10(
mut conn: impl DbConn,
mut conn: impl GetConn,
settings: &Settings,
) -> Result<(), LemmyError> {
info!("Running initialize_local_site_2022_10_10");
// Check to see if local_site exists
if LocalSite::read(&mut *conn).await.is_ok() {
if LocalSite::read(conn).await.is_ok() {
return Ok(());
}
info!("No Local Site found, creating it.");
@ -424,7 +423,7 @@ async fn initialize_local_site_2022_10_10(
.expect("must have domain");
// Upsert this to the instance table
let instance = Instance::read_or_create(&mut *conn, domain).await?;
let instance = Instance::read_or_create(conn, domain).await?;
if let Some(setup) = &settings.setup {
let person_keypair = generate_actor_keypair()?;
@ -445,14 +444,14 @@ async fn initialize_local_site_2022_10_10(
.inbox_url(Some(generate_inbox_url(&person_actor_id)?))
.shared_inbox_url(Some(generate_shared_inbox_url(&person_actor_id)?))
.build();
let person_inserted = Person::create(&mut *conn, &person_form).await?;
let person_inserted = Person::create(conn, &person_form).await?;
let local_user_form = LocalUserInsertForm::builder()
.person_id(person_inserted.id)
.password_encrypted(setup.admin_password.clone())
.email(setup.admin_email.clone())
.build();
LocalUser::create(&mut *conn, &local_user_form).await?;
LocalUser::create(conn, &local_user_form).await?;
};
// Add an entry for the site table
@ -474,14 +473,14 @@ async fn initialize_local_site_2022_10_10(
.private_key(Some(site_key_pair.private_key))
.public_key(Some(site_key_pair.public_key))
.build();
let site = Site::create(&mut *conn, &site_form).await?;
let site = Site::create(conn, &site_form).await?;
// Finally create the local_site row
let local_site_form = LocalSiteInsertForm::builder()
.site_id(site.id)
.site_setup(Some(settings.setup.is_some()))
.build();
let local_site = LocalSite::create(&mut *conn, &local_site_form).await?;
let local_site = LocalSite::create(conn, &local_site_form).await?;
// Create the rate limit table
let local_site_rate_limit_form = LocalSiteRateLimitInsertForm::builder()
@ -497,7 +496,7 @@ async fn initialize_local_site_2022_10_10(
.search(Some(999))
.local_site_id(local_site.id)
.build();
LocalSiteRateLimit::create(&mut *conn, &local_site_rate_limit_form).await?;
LocalSiteRateLimit::create(conn, &local_site_rate_limit_form).await?;
Ok(())
}