2023-09-05 09:33:46 +00:00
|
|
|
use actix_web::web::{Data, Json};
|
2023-10-17 16:35:51 +00:00
|
|
|
use lemmy_api_common::{context::LemmyContext, post::MarkPostAsRead, SuccessResponse};
|
|
|
|
use lemmy_db_schema::source::post::PostRead;
|
|
|
|
use lemmy_db_views::structs::LocalUserView;
|
|
|
|
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType, MAX_API_PARAM_ELEMENTS};
|
|
|
|
use std::collections::HashSet;
|
2022-04-13 18:12:25 +00:00
|
|
|
|
2023-09-05 09:33:46 +00:00
|
|
|
#[tracing::instrument(skip(context))]
|
|
|
|
pub async fn mark_post_as_read(
|
|
|
|
data: Json<MarkPostAsRead>,
|
|
|
|
context: Data<LemmyContext>,
|
2023-09-21 10:42:28 +00:00
|
|
|
local_user_view: LocalUserView,
|
2023-10-17 16:35:51 +00:00
|
|
|
) -> Result<Json<SuccessResponse>, LemmyError> {
|
|
|
|
let mut post_ids = data.post_ids.iter().cloned().collect::<HashSet<_>>();
|
|
|
|
post_ids.insert(data.post_id);
|
2023-09-05 09:33:46 +00:00
|
|
|
let person_id = local_user_view.person.id;
|
2022-04-13 18:12:25 +00:00
|
|
|
|
2023-10-17 16:35:51 +00:00
|
|
|
if post_ids.len() > MAX_API_PARAM_ELEMENTS {
|
|
|
|
Err(LemmyErrorType::TooManyItems)?;
|
|
|
|
}
|
|
|
|
|
2023-09-05 09:33:46 +00:00
|
|
|
// Mark the post as read / unread
|
|
|
|
if data.read {
|
2023-10-17 16:35:51 +00:00
|
|
|
PostRead::mark_as_read(&mut context.pool(), post_ids, person_id)
|
|
|
|
.await
|
|
|
|
.with_lemmy_type(LemmyErrorType::CouldntMarkPostAsRead)?;
|
2023-09-05 09:33:46 +00:00
|
|
|
} else {
|
2023-10-17 16:35:51 +00:00
|
|
|
PostRead::mark_as_unread(&mut context.pool(), post_ids, person_id)
|
|
|
|
.await
|
|
|
|
.with_lemmy_type(LemmyErrorType::CouldntMarkPostAsRead)?;
|
2023-09-05 09:33:46 +00:00
|
|
|
}
|
2022-04-13 18:12:25 +00:00
|
|
|
|
2023-10-17 16:35:51 +00:00
|
|
|
Ok(Json(SuccessResponse::default()))
|
2022-04-13 18:12:25 +00:00
|
|
|
}
|