use crate::Perform;
use actix_web::web::Data;
use lemmy_api_common::{
- blocking,
- get_local_user_view_from_jwt,
+ context::LemmyContext,
person::{GetRepliesResponse, MarkAllAsRead},
+ 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::{ConnectionId, LemmyError};
-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![] })
}