]> Untitled Git - lemmy.git/blobdiff - crates/api/src/local_user/notifications/mark_all_read.rs
Make functions work with both connection and pool (#3420)
[lemmy.git] / crates / api / src / local_user / notifications / mark_all_read.rs
index 3925e7913f45076445633381bb6df4e56ceb599e..d852263ef3a7b8dba33370c17bb95f1f3e95d6b5 100644 (file)
@@ -1,67 +1,41 @@
 use crate::Perform;
 use actix_web::web::Data;
 use lemmy_api_common::{
+  context::LemmyContext,
   person::{GetRepliesResponse, MarkAllAsRead},
-  utils::{blocking, get_local_user_view_from_jwt},
+  utils::local_user_view_from_jwt,
 };
 use lemmy_db_schema::source::{
-  comment::Comment,
+  comment_reply::CommentReply,
   person_mention::PersonMention,
   private_message::PrivateMessage,
 };
-use lemmy_db_views::comment_view::CommentQueryBuilder;
-use lemmy_utils::{error::LemmyError, ConnectionId};
-use lemmy_websocket::LemmyContext;
+use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
 
 #[async_trait::async_trait(?Send)]
 impl Perform for MarkAllAsRead {
   type Response = GetRepliesResponse;
 
-  #[tracing::instrument(skip(context, _websocket_id))]
-  async fn perform(
-    &self,
-    context: &Data<LemmyContext>,
-    _websocket_id: Option<ConnectionId>,
-  ) -> Result<GetRepliesResponse, LemmyError> {
+  #[tracing::instrument(skip(context))]
+  async fn perform(&self, context: &Data<LemmyContext>) -> Result<GetRepliesResponse, LemmyError> {
     let data: &MarkAllAsRead = self;
-    let local_user_view =
-      get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
-
+    let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
     let person_id = local_user_view.person.id;
-    let replies = blocking(context.pool(), move |conn| {
-      CommentQueryBuilder::create(conn)
-        .my_person_id(person_id)
-        .recipient_id(person_id)
-        .unread_only(true)
-        .page(1)
-        .limit(999)
-        .list()
-    })
-    .await??;
 
-    // TODO: this should probably be a bulk operation
-    // Not easy to do as a bulk operation,
-    // because recipient_id isn't in the comment table
-    for comment_view in &replies {
-      let reply_id = comment_view.comment.id;
-      let mark_as_read = move |conn: &'_ _| Comment::update_read(conn, reply_id, true);
-      blocking(context.pool(), mark_as_read)
-        .await?
-        .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
-    }
+    // Mark all comment_replies as read
+    CommentReply::mark_all_as_read(&mut context.pool(), person_id)
+      .await
+      .with_lemmy_type(LemmyErrorType::CouldntUpdateComment)?;
 
     // Mark all user mentions as read
-    let update_person_mentions =
-      move |conn: &'_ _| PersonMention::mark_all_as_read(conn, person_id);
-    blocking(context.pool(), update_person_mentions)
-      .await?
-      .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
+    PersonMention::mark_all_as_read(&mut context.pool(), person_id)
+      .await
+      .with_lemmy_type(LemmyErrorType::CouldntUpdateComment)?;
 
     // Mark all private_messages as read
-    let update_pm = move |conn: &'_ _| PrivateMessage::mark_all_as_read(conn, person_id);
-    blocking(context.pool(), update_pm)
-      .await?
-      .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_private_message"))?;
+    PrivateMessage::mark_all_as_read(&mut context.pool(), person_id)
+      .await
+      .with_lemmy_type(LemmyErrorType::CouldntUpdatePrivateMessage)?;
 
     Ok(GetRepliesResponse { replies: vec![] })
   }