Skip to content

Commit

Permalink
Make functions work with both connection and pool (#3420)
Browse files Browse the repository at this point in the history
* a lot

* merge

* Fix stuff broken by merge

* Get rid of repetitive `&mut *context.conn().await?`

* Add blank lines under each line with `conn =`

* Fix style mistakes (partial)

* Revert "Fix style mistakes (partial)"

This reverts commit 48a033b.

* Revert "Add blank lines under each line with `conn =`"

This reverts commit 773a6d3.

* Revert "Get rid of repetitive `&mut *context.conn().await?`"

This reverts commit d2c6263.

* Use DbConn for CaptchaAnswer methods

* DbConn trait

* Remove more `&mut *`

* Fix stuff

* Re-run CI

* try to make ci start

* fix

* fix

* Fix api_common::utils

* Fix apub::activities::block

* Fix apub::api::resolve_object

* Fix some things

* Revert "Fix some things"

This reverts commit 2bf8574.

* Revert "Fix apub::api::resolve_object"

This reverts commit 3e4059a.

* Revert "Fix apub::activities::block"

This reverts commit 3b02389.

* Revert "Fix api_common::utils"

This reverts commit 7dc73de.

* Revert "Revert "Fix api_common::utils""

This reverts commit f740f11.

* Revert "Revert "Fix apub::activities::block""

This reverts commit 2ee206a.

* Revert "Revert "Fix apub::api::resolve_object""

This reverts commit 96ed8bf.

* Fix fetch_local_site_data

* Fix get_comment_parent_creator

* Remove unused perma deleted text

* Fix routes::feeds

* Fix lib.rs

* Update lib.rs

* rerun ci

* Attempt to create custom GetConn and RunQueryDsl traits

* Start over

* Add GetConn trait

* aaaa

* Revert "aaaa"

This reverts commit acc9ca1.

* Revert "Revert "aaaa""

This reverts commit 443a2a0.

* still aaaaaaaaaaaaa

* Return to earlier thing

Revert "Add GetConn trait"

This reverts commit ab4e94a.

* Try to use DbPool enum

* Revert "Try to use DbPool enum"

This reverts commit e4d1712.

* DbConn and DbPool enums (db_schema only fails to compile for tests)

* fmt

* Make functions take `&mut DbPool<'_>` and make db_schema tests compile

* Add try_join_with_pool macro and run fix-clippy on more crates

* Fix some errors

* I did it

* Remove function variants that take connection

* rerun ci

* rerun ci

* rerun ci
  • Loading branch information
dullbananas authored Jul 11, 2023
1 parent 73492af commit 1d38aad
Show file tree
Hide file tree
Showing 218 changed files with 1,567 additions and 1,159 deletions.
10 changes: 5 additions & 5 deletions crates/api/src/comment/distinguish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ impl Perform for DistinguishComment {
let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;

let comment_id = data.comment_id;
let orig_comment = CommentView::read(context.pool(), comment_id, None).await?;
let orig_comment = CommentView::read(&mut context.pool(), comment_id, None).await?;

check_community_ban(
local_user_view.person.id,
orig_comment.community.id,
context.pool(),
&mut context.pool(),
)
.await?;

// Verify that only a mod or admin can distinguish a comment
is_mod_or_admin(
context.pool(),
&mut context.pool(),
local_user_view.person.id,
orig_comment.community.id,
)
Expand All @@ -44,13 +44,13 @@ impl Perform for DistinguishComment {
let form = CommentUpdateForm::builder()
.distinguished(Some(data.distinguished))
.build();
Comment::update(context.pool(), comment_id, &form)
Comment::update(&mut context.pool(), comment_id, &form)
.await
.with_lemmy_type(LemmyErrorType::CouldntUpdateComment)?;

let comment_id = data.comment_id;
let person_id = local_user_view.person.id;
let comment_view = CommentView::read(context.pool(), comment_id, Some(person_id)).await?;
let comment_view = CommentView::read(&mut context.pool(), comment_id, Some(person_id)).await?;

Ok(CommentResponse {
comment_view,
Expand Down
16 changes: 9 additions & 7 deletions crates/api/src/comment/like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl Perform for CreateCommentLike {
#[tracing::instrument(skip(context))]
async fn perform(&self, context: &Data<LemmyContext>) -> Result<CommentResponse, LemmyError> {
let data: &CreateCommentLike = self;
let local_site = LocalSite::read(context.pool()).await?;
let local_site = LocalSite::read(&mut context.pool()).await?;
let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;

let mut recipient_ids = Vec::<LocalUserId>::new();
Expand All @@ -34,20 +34,22 @@ impl Perform for CreateCommentLike {
check_downvotes_enabled(data.score, &local_site)?;

let comment_id = data.comment_id;
let orig_comment = CommentView::read(context.pool(), comment_id, None).await?;
let orig_comment = CommentView::read(&mut context.pool(), comment_id, None).await?;

check_community_ban(
local_user_view.person.id,
orig_comment.community.id,
context.pool(),
&mut context.pool(),
)
.await?;

// Add parent poster or commenter to recipients
let comment_reply = CommentReply::read_by_comment(context.pool(), comment_id).await;
let comment_reply = CommentReply::read_by_comment(&mut context.pool(), comment_id).await;
if let Ok(reply) = comment_reply {
let recipient_id = reply.recipient_id;
if let Ok(local_recipient) = LocalUserView::read_person(context.pool(), recipient_id).await {
if let Ok(local_recipient) =
LocalUserView::read_person(&mut context.pool(), recipient_id).await
{
recipient_ids.push(local_recipient.local_user.id);
}
}
Expand All @@ -62,12 +64,12 @@ impl Perform for CreateCommentLike {
// Remove any likes first
let person_id = local_user_view.person.id;

CommentLike::remove(context.pool(), person_id, comment_id).await?;
CommentLike::remove(&mut context.pool(), person_id, comment_id).await?;

// Only add the like if the score isnt 0
let do_add = like_form.score != 0 && (like_form.score == 1 || like_form.score == -1);
if do_add {
CommentLike::like(context.pool(), &like_form)
CommentLike::like(&mut context.pool(), &like_form)
.await
.with_lemmy_type(LemmyErrorType::CouldntLikeComment)?;
}
Expand Down
6 changes: 3 additions & 3 deletions crates/api/src/comment/save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ impl Perform for SaveComment {
};

if data.save {
CommentSaved::save(context.pool(), &comment_saved_form)
CommentSaved::save(&mut context.pool(), &comment_saved_form)
.await
.with_lemmy_type(LemmyErrorType::CouldntSaveComment)?;
} else {
CommentSaved::unsave(context.pool(), &comment_saved_form)
CommentSaved::unsave(&mut context.pool(), &comment_saved_form)
.await
.with_lemmy_type(LemmyErrorType::CouldntSaveComment)?;
}

let comment_id = data.comment_id;
let person_id = local_user_view.person.id;
let comment_view = CommentView::read(context.pool(), comment_id, Some(person_id)).await?;
let comment_view = CommentView::read(&mut context.pool(), comment_id, Some(person_id)).await?;

Ok(CommentResponse {
comment_view,
Expand Down
13 changes: 7 additions & 6 deletions crates/api/src/comment_report/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ impl Perform for CreateCommentReport {
) -> Result<CommentReportResponse, LemmyError> {
let data: &CreateCommentReport = self;
let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
let local_site = LocalSite::read(context.pool()).await?;
let local_site = LocalSite::read(&mut context.pool()).await?;

let reason = self.reason.trim();
check_report_reason(reason, &local_site)?;

let person_id = local_user_view.person.id;
let comment_id = data.comment_id;
let comment_view = CommentView::read(context.pool(), comment_id, None).await?;
let comment_view = CommentView::read(&mut context.pool(), comment_id, None).await?;

check_community_ban(person_id, comment_view.community.id, context.pool()).await?;
check_community_ban(person_id, comment_view.community.id, &mut context.pool()).await?;

let report_form = CommentReportForm {
creator_id: person_id,
Expand All @@ -45,18 +45,19 @@ impl Perform for CreateCommentReport {
reason: reason.to_owned(),
};

let report = CommentReport::report(context.pool(), &report_form)
let report = CommentReport::report(&mut context.pool(), &report_form)
.await
.with_lemmy_type(LemmyErrorType::CouldntCreateReport)?;

let comment_report_view = CommentReportView::read(context.pool(), report.id, person_id).await?;
let comment_report_view =
CommentReportView::read(&mut context.pool(), report.id, person_id).await?;

// Email the admins
if local_site.reports_email_admins {
send_new_report_email_to_admins(
&comment_report_view.creator.name,
&comment_report_view.comment_creator.name,
context.pool(),
&mut context.pool(),
context.settings(),
)
.await?;
Expand Down
2 changes: 1 addition & 1 deletion crates/api/src/comment_report/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Perform for ListCommentReports {
let page = data.page;
let limit = data.limit;
let comment_reports = CommentReportQuery::builder()
.pool(context.pool())
.pool(&mut context.pool())
.my_person_id(person_id)
.admin(admin)
.community_id(community_id)
Expand Down
11 changes: 6 additions & 5 deletions crates/api/src/comment_report/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,24 @@ impl Perform for ResolveCommentReport {

let report_id = data.report_id;
let person_id = local_user_view.person.id;
let report = CommentReportView::read(context.pool(), report_id, person_id).await?;
let report = CommentReportView::read(&mut context.pool(), report_id, person_id).await?;

let person_id = local_user_view.person.id;
is_mod_or_admin(context.pool(), person_id, report.community.id).await?;
is_mod_or_admin(&mut context.pool(), person_id, report.community.id).await?;

if data.resolved {
CommentReport::resolve(context.pool(), report_id, person_id)
CommentReport::resolve(&mut context.pool(), report_id, person_id)
.await
.with_lemmy_type(LemmyErrorType::CouldntResolveReport)?;
} else {
CommentReport::unresolve(context.pool(), report_id, person_id)
CommentReport::unresolve(&mut context.pool(), report_id, person_id)
.await
.with_lemmy_type(LemmyErrorType::CouldntResolveReport)?;
}

let report_id = data.report_id;
let comment_report_view = CommentReportView::read(context.pool(), report_id, person_id).await?;
let comment_report_view =
CommentReportView::read(&mut context.pool(), report_id, person_id).await?;

Ok(CommentReportResponse {
comment_report_view,
Expand Down
13 changes: 7 additions & 6 deletions crates/api/src/community/add_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ impl Perform for AddModToCommunity {
let community_id = data.community_id;

// Verify that only mods or admins can add mod
is_mod_or_admin(context.pool(), local_user_view.person.id, community_id).await?;
let community = Community::read(context.pool(), community_id).await?;
is_mod_or_admin(&mut context.pool(), local_user_view.person.id, community_id).await?;
let community = Community::read(&mut context.pool(), community_id).await?;
if local_user_view.person.admin && !community.local {
return Err(LemmyErrorType::NotAModerator)?;
}
Expand All @@ -42,11 +42,11 @@ impl Perform for AddModToCommunity {
person_id: data.person_id,
};
if data.added {
CommunityModerator::join(context.pool(), &community_moderator_form)
CommunityModerator::join(&mut context.pool(), &community_moderator_form)
.await
.with_lemmy_type(LemmyErrorType::CommunityModeratorAlreadyExists)?;
} else {
CommunityModerator::leave(context.pool(), &community_moderator_form)
CommunityModerator::leave(&mut context.pool(), &community_moderator_form)
.await
.with_lemmy_type(LemmyErrorType::CommunityModeratorAlreadyExists)?;
}
Expand All @@ -59,12 +59,13 @@ impl Perform for AddModToCommunity {
removed: Some(!data.added),
};

ModAddCommunity::create(context.pool(), &form).await?;
ModAddCommunity::create(&mut context.pool(), &form).await?;

// Note: in case a remote mod is added, this returns the old moderators list, it will only get
// updated once we receive an activity from the community (like `Announce/Add/Moderator`)
let community_id = data.community_id;
let moderators = CommunityModeratorView::for_community(context.pool(), community_id).await?;
let moderators =
CommunityModeratorView::for_community(&mut context.pool(), community_id).await?;

Ok(AddModToCommunityResponse { moderators })
}
Expand Down
14 changes: 7 additions & 7 deletions crates/api/src/community/ban.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Perform for BanFromCommunity {
let expires = data.expires.map(naive_from_unix);

// Verify that only mods or admins can ban
is_mod_or_admin(context.pool(), local_user_view.person.id, community_id).await?;
is_mod_or_admin(&mut context.pool(), local_user_view.person.id, community_id).await?;
is_valid_body_field(&data.reason, false)?;

let community_user_ban_form = CommunityPersonBanForm {
Expand All @@ -51,7 +51,7 @@ impl Perform for BanFromCommunity {
};

if data.ban {
CommunityPersonBan::ban(context.pool(), &community_user_ban_form)
CommunityPersonBan::ban(&mut context.pool(), &community_user_ban_form)
.await
.with_lemmy_type(LemmyErrorType::CommunityUserAlreadyBanned)?;

Expand All @@ -62,18 +62,18 @@ impl Perform for BanFromCommunity {
pending: false,
};

CommunityFollower::unfollow(context.pool(), &community_follower_form)
CommunityFollower::unfollow(&mut context.pool(), &community_follower_form)
.await
.ok();
} else {
CommunityPersonBan::unban(context.pool(), &community_user_ban_form)
CommunityPersonBan::unban(&mut context.pool(), &community_user_ban_form)
.await
.with_lemmy_type(LemmyErrorType::CommunityUserAlreadyBanned)?;
}

// Remove/Restore their data if that's desired
if remove_data {
remove_user_data_in_community(community_id, banned_person_id, context.pool()).await?;
remove_user_data_in_community(community_id, banned_person_id, &mut context.pool()).await?;
}

// Mod tables
Expand All @@ -86,10 +86,10 @@ impl Perform for BanFromCommunity {
expires,
};

ModBanFromCommunity::create(context.pool(), &form).await?;
ModBanFromCommunity::create(&mut context.pool(), &form).await?;

let person_id = data.person_id;
let person_view = PersonView::read(context.pool(), person_id).await?;
let person_view = PersonView::read(&mut context.pool(), person_id).await?;

Ok(BanFromCommunityResponse {
person_view,
Expand Down
8 changes: 4 additions & 4 deletions crates/api/src/community/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl Perform for BlockCommunity {
};

if data.block {
CommunityBlock::block(context.pool(), &community_block_form)
CommunityBlock::block(&mut context.pool(), &community_block_form)
.await
.with_lemmy_type(LemmyErrorType::CommunityBlockAlreadyExists)?;

Expand All @@ -46,17 +46,17 @@ impl Perform for BlockCommunity {
pending: false,
};

CommunityFollower::unfollow(context.pool(), &community_follower_form)
CommunityFollower::unfollow(&mut context.pool(), &community_follower_form)
.await
.ok();
} else {
CommunityBlock::unblock(context.pool(), &community_block_form)
CommunityBlock::unblock(&mut context.pool(), &community_block_form)
.await
.with_lemmy_type(LemmyErrorType::CommunityBlockAlreadyExists)?;
}

let community_view =
CommunityView::read(context.pool(), community_id, Some(person_id), None).await?;
CommunityView::read(&mut context.pool(), community_id, Some(person_id), None).await?;

Ok(BlockCommunityResponse {
blocked: data.block,
Expand Down
16 changes: 8 additions & 8 deletions crates/api/src/community/follow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl Perform for FollowCommunity {
let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;

let community_id = data.community_id;
let community = Community::read(context.pool(), community_id).await?;
let community = Community::read(&mut context.pool(), community_id).await?;
let mut community_follower_form = CommunityFollowerForm {
community_id: data.community_id,
person_id: local_user_view.person.id,
Expand All @@ -34,31 +34,31 @@ impl Perform for FollowCommunity {

if data.follow {
if community.local {
check_community_ban(local_user_view.person.id, community_id, context.pool()).await?;
check_community_deleted_or_removed(community_id, context.pool()).await?;
check_community_ban(local_user_view.person.id, community_id, &mut context.pool()).await?;
check_community_deleted_or_removed(community_id, &mut context.pool()).await?;

CommunityFollower::follow(context.pool(), &community_follower_form)
CommunityFollower::follow(&mut context.pool(), &community_follower_form)
.await
.with_lemmy_type(LemmyErrorType::CommunityFollowerAlreadyExists)?;
} else {
// Mark as pending, the actual federation activity is sent via `SendActivity` handler
community_follower_form.pending = true;
CommunityFollower::follow(context.pool(), &community_follower_form)
CommunityFollower::follow(&mut context.pool(), &community_follower_form)
.await
.with_lemmy_type(LemmyErrorType::CommunityFollowerAlreadyExists)?;
}
}
if !data.follow {
CommunityFollower::unfollow(context.pool(), &community_follower_form)
CommunityFollower::unfollow(&mut context.pool(), &community_follower_form)
.await
.with_lemmy_type(LemmyErrorType::CommunityFollowerAlreadyExists)?;
}

let community_id = data.community_id;
let person_id = local_user_view.person.id;
let community_view =
CommunityView::read(context.pool(), community_id, Some(person_id), None).await?;
let discussion_languages = CommunityLanguage::read(context.pool(), community_id).await?;
CommunityView::read(&mut context.pool(), community_id, Some(person_id), None).await?;
let discussion_languages = CommunityLanguage::read(&mut context.pool(), community_id).await?;

Ok(Self::Response {
community_view,
Expand Down
4 changes: 2 additions & 2 deletions crates/api/src/community/hide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ impl Perform for HideCommunity {
};

let community_id = data.community_id;
Community::update(context.pool(), community_id, &community_form)
Community::update(&mut context.pool(), community_id, &community_form)
.await
.with_lemmy_type(LemmyErrorType::CouldntUpdateCommunityHiddenStatus)?;

ModHideCommunity::create(context.pool(), &mod_hide_community_form).await?;
ModHideCommunity::create(&mut context.pool(), &mod_hide_community_form).await?;

build_community_response(context, local_user_view, community_id).await
}
Expand Down
Loading

0 comments on commit 1d38aad

Please sign in to comment.