]> Untitled Git - lemmy.git/blobdiff - crates/api_common/src/build_response.rs
Make functions work with both connection and pool (#3420)
[lemmy.git] / crates / api_common / src / build_response.rs
index 217e05de6b96211f6f6e9ee7c65806c96646a46d..8b96206cb214a1489fa5e690f98ea365cf6dfaae 100644 (file)
@@ -30,7 +30,7 @@ pub async fn build_comment_response(
   recipient_ids: Vec<LocalUserId>,
 ) -> Result<CommentResponse, LemmyError> {
   let person_id = local_user_view.map(|l| l.person.id);
-  let comment_view = CommentView::read(context.pool(), comment_id, person_id).await?;
+  let comment_view = CommentView::read(&mut context.pool(), comment_id, person_id).await?;
   Ok(CommentResponse {
     comment_view,
     recipient_ids,
@@ -43,18 +43,19 @@ pub async fn build_community_response(
   local_user_view: LocalUserView,
   community_id: CommunityId,
 ) -> Result<CommunityResponse, LemmyError> {
-  let is_mod_or_admin = is_mod_or_admin(context.pool(), local_user_view.person.id, community_id)
-    .await
-    .is_ok();
+  let is_mod_or_admin =
+    is_mod_or_admin(&mut context.pool(), local_user_view.person.id, community_id)
+      .await
+      .is_ok();
   let person_id = local_user_view.person.id;
   let community_view = CommunityView::read(
-    context.pool(),
+    &mut context.pool(),
     community_id,
     Some(person_id),
     Some(is_mod_or_admin),
   )
   .await?;
-  let discussion_languages = CommunityLanguage::read(context.pool(), community_id).await?;
+  let discussion_languages = CommunityLanguage::read(&mut context.pool(), community_id).await?;
 
   Ok(CommunityResponse {
     community_view,
@@ -68,11 +69,11 @@ pub async fn build_post_response(
   person_id: PersonId,
   post_id: PostId,
 ) -> Result<PostResponse, LemmyError> {
-  let is_mod_or_admin = is_mod_or_admin(context.pool(), person_id, community_id)
+  let is_mod_or_admin = is_mod_or_admin(&mut context.pool(), person_id, community_id)
     .await
     .is_ok();
   let post_view = PostView::read(
-    context.pool(),
+    &mut context.pool(),
     post_id,
     Some(person_id),
     Some(is_mod_or_admin),
@@ -100,7 +101,7 @@ pub async fn send_local_notifs(
     .filter(|m| m.is_local(&context.settings().hostname) && m.name.ne(&person.name))
   {
     let mention_name = mention.name.clone();
-    let user_view = LocalUserView::read_from_name(context.pool(), &mention_name).await;
+    let user_view = LocalUserView::read_from_name(&mut context.pool(), &mention_name).await;
     if let Ok(mention_user_view) = user_view {
       // TODO
       // At some point, make it so you can't tag the parent creator either
@@ -115,7 +116,7 @@ pub async fn send_local_notifs(
 
       // Allow this to fail softly, since comment edits might re-update or replace it
       // Let the uniqueness handle this fail
-      PersonMention::create(context.pool(), &user_mention_form)
+      PersonMention::create(&mut context.pool(), &user_mention_form)
         .await
         .ok();
 
@@ -135,19 +136,19 @@ pub async fn send_local_notifs(
 
   // Send comment_reply to the parent commenter / poster
   if let Some(parent_comment_id) = comment.parent_comment_id() {
-    let parent_comment = Comment::read(context.pool(), parent_comment_id).await?;
+    let parent_comment = Comment::read(&mut context.pool(), parent_comment_id).await?;
 
     // Get the parent commenter local_user
     let parent_creator_id = parent_comment.creator_id;
 
     // Only add to recipients if that person isn't blocked
-    let creator_blocked = check_person_block(person.id, parent_creator_id, context.pool())
+    let creator_blocked = check_person_block(person.id, parent_creator_id, &mut context.pool())
       .await
       .is_err();
 
     // Don't send a notif to yourself
     if parent_comment.creator_id != person.id && !creator_blocked {
-      let user_view = LocalUserView::read_person(context.pool(), parent_creator_id).await;
+      let user_view = LocalUserView::read_person(&mut context.pool(), parent_creator_id).await;
       if let Ok(parent_user_view) = user_view {
         recipient_ids.push(parent_user_view.local_user.id);
 
@@ -159,7 +160,7 @@ pub async fn send_local_notifs(
 
         // Allow this to fail softly, since comment edits might re-update or replace it
         // Let the uniqueness handle this fail
-        CommentReply::create(context.pool(), &comment_reply_form)
+        CommentReply::create(&mut context.pool(), &comment_reply_form)
           .await
           .ok();
 
@@ -178,13 +179,13 @@ pub async fn send_local_notifs(
   } else {
     // If there's no parent, its the post creator
     // Only add to recipients if that person isn't blocked
-    let creator_blocked = check_person_block(person.id, post.creator_id, context.pool())
+    let creator_blocked = check_person_block(person.id, post.creator_id, &mut context.pool())
       .await
       .is_err();
 
     if post.creator_id != person.id && !creator_blocked {
       let creator_id = post.creator_id;
-      let parent_user = LocalUserView::read_person(context.pool(), creator_id).await;
+      let parent_user = LocalUserView::read_person(&mut context.pool(), creator_id).await;
       if let Ok(parent_user_view) = parent_user {
         recipient_ids.push(parent_user_view.local_user.id);
 
@@ -196,7 +197,7 @@ pub async fn send_local_notifs(
 
         // Allow this to fail softly, since comment edits might re-update or replace it
         // Let the uniqueness handle this fail
-        CommentReply::create(context.pool(), &comment_reply_form)
+        CommentReply::create(&mut context.pool(), &comment_reply_form)
           .await
           .ok();