Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make functions work with both connection and pool #3420

Merged
merged 67 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
a8adf2d
a lot
dullbananas Jun 30, 2023
2c47931
merge
dullbananas Jun 30, 2023
a6cbbee
Merge branch 'db-traits-refactor' of https://github.com/dullbananas/l…
dullbananas Jun 30, 2023
9d54354
Fix stuff broken by merge
dullbananas Jun 30, 2023
d2c6263
Get rid of repetitive `&mut *context.conn().await?`
dullbananas Jun 30, 2023
773a6d3
Add blank lines under each line with `conn =`
dullbananas Jun 30, 2023
48a033b
Fix style mistakes (partial)
dullbananas Jun 30, 2023
3021e2c
Revert "Fix style mistakes (partial)"
dullbananas Jun 30, 2023
c1c17ab
Revert "Add blank lines under each line with `conn =`"
dullbananas Jun 30, 2023
cc1a725
Revert "Get rid of repetitive `&mut *context.conn().await?`"
dullbananas Jun 30, 2023
106c315
Use DbConn for CaptchaAnswer methods
dullbananas Jun 30, 2023
6846210
Merge remote-tracking branch 'upstream/main' into db-traits-refactor
dullbananas Jun 30, 2023
5db6b55
DbConn trait
dullbananas Jul 1, 2023
0d9db43
Remove more `&mut *`
dullbananas Jul 1, 2023
0957e2b
Fix stuff
dullbananas Jul 1, 2023
41d7c72
Re-run CI
dullbananas Jul 1, 2023
37bf184
Merge branch 'main' into db-traits-refactor
dullbananas Jul 3, 2023
243b777
Merge branch 'main' into db-traits-refactor
dullbananas Jul 3, 2023
cf827b4
try to make ci start
dullbananas Jul 3, 2023
8f46003
Merge remote-tracking branch 'upstream/main' into db-traits-refactor
dullbananas Jul 3, 2023
2c47ac3
fix
dullbananas Jul 3, 2023
50ab303
Merge remote-tracking branch 'upstream/main' into db-traits-refactor
dullbananas Jul 3, 2023
350fba2
fix
dullbananas Jul 3, 2023
29b00d7
Merge remote-tracking branch 'upstream/main' into db-traits-refactor
dullbananas Jul 3, 2023
7dc73de
Fix api_common::utils
dullbananas Jul 4, 2023
3b02389
Fix apub::activities::block
dullbananas Jul 4, 2023
3e4059a
Fix apub::api::resolve_object
dullbananas Jul 4, 2023
2bf8574
Fix some things
dullbananas Jul 4, 2023
0a8d1f8
Revert "Fix some things"
dullbananas Jul 4, 2023
96ed8bf
Revert "Fix apub::api::resolve_object"
dullbananas Jul 4, 2023
2ee206a
Revert "Fix apub::activities::block"
dullbananas Jul 4, 2023
f740f11
Revert "Fix api_common::utils"
dullbananas Jul 4, 2023
d74374b
Revert "Revert "Fix api_common::utils""
dullbananas Jul 4, 2023
017ffd7
Revert "Revert "Fix apub::activities::block""
dullbananas Jul 4, 2023
53a1837
Revert "Revert "Fix apub::api::resolve_object""
dullbananas Jul 4, 2023
b375b69
Fix fetch_local_site_data
dullbananas Jul 4, 2023
07bad4c
Fix get_comment_parent_creator
dullbananas Jul 4, 2023
0613cf8
Remove unused perma deleted text
dullbananas Jul 4, 2023
9ec77d0
Fix routes::feeds
dullbananas Jul 4, 2023
28116f8
Fix lib.rs
dullbananas Jul 4, 2023
d9e0269
Merge branch 'main' into db-traits-refactor
dullbananas Jul 4, 2023
2d322dc
Update lib.rs
dullbananas Jul 4, 2023
48e12de
rerun ci
dullbananas Jul 4, 2023
7aeeec6
Attempt to create custom GetConn and RunQueryDsl traits
dullbananas Jul 5, 2023
fb147b7
Start over
dullbananas Jul 5, 2023
ab4e94a
Add GetConn trait
dullbananas Jul 5, 2023
acc9ca1
aaaa
dullbananas Jul 5, 2023
443a2a0
Revert "aaaa"
dullbananas Jul 5, 2023
94e83ed
Revert "Revert "aaaa""
dullbananas Jul 5, 2023
a5a1478
still aaaaaaaaaaaaa
dullbananas Jul 5, 2023
23accf7
Return to earlier thing
dullbananas Jul 5, 2023
e4d1712
Try to use DbPool enum
dullbananas Jul 5, 2023
47c1c55
Revert "Try to use DbPool enum"
dullbananas Jul 5, 2023
96ec92c
DbConn and DbPool enums (db_schema only fails to compile for tests)
dullbananas Jul 6, 2023
3bc61dc
fmt
dullbananas Jul 6, 2023
7902f73
Make functions take `&mut DbPool<'_>` and make db_schema tests compile
dullbananas Jul 6, 2023
059045b
Add try_join_with_pool macro and run fix-clippy on more crates
dullbananas Jul 6, 2023
5611e87
Fix some errors
dullbananas Jul 6, 2023
f7951ca
Merge remote-tracking branch 'upstream/main' into db-traits-refactor
dullbananas Jul 6, 2023
989f7cd
I did it
dullbananas Jul 7, 2023
d4f20d7
Remove function variants that take connection
dullbananas Jul 7, 2023
1dd55f7
Merge branch 'main' into db-traits-refactor
dullbananas Jul 7, 2023
f297e04
rerun ci
dullbananas Jul 7, 2023
6f83d53
Merge remote-tracking branch 'upstream/main' into db-traits-refactor
dullbananas Jul 10, 2023
ef46749
rerun ci
dullbananas Jul 10, 2023
735bcac
Merge remote-tracking branch 'upstream/main' into db-traits-refactor
dullbananas Jul 10, 2023
26c9ce0
rerun ci
dullbananas Jul 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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