-
-
Notifications
You must be signed in to change notification settings - Fork 859
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…#4048) * Allow marking multiple posts as read in single api call (fixes #3963) * cleanup * limit array length * fix test * review --------- Co-authored-by: Dessalines <dessalines@users.noreply.github.com>
- Loading branch information
1 parent
3f62135
commit 3a19af5
Showing
8 changed files
with
91 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,34 @@ | ||
use actix_web::web::{Data, Json}; | ||
use lemmy_api_common::{ | ||
context::LemmyContext, | ||
post::{MarkPostAsRead, PostResponse}, | ||
utils, | ||
}; | ||
use lemmy_db_views::structs::{LocalUserView, PostView}; | ||
use lemmy_utils::error::LemmyError; | ||
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; | ||
|
||
#[tracing::instrument(skip(context))] | ||
pub async fn mark_post_as_read( | ||
data: Json<MarkPostAsRead>, | ||
context: Data<LemmyContext>, | ||
local_user_view: LocalUserView, | ||
) -> Result<Json<PostResponse>, LemmyError> { | ||
let post_id = data.post_id; | ||
) -> Result<Json<SuccessResponse>, LemmyError> { | ||
let mut post_ids = data.post_ids.iter().cloned().collect::<HashSet<_>>(); | ||
post_ids.insert(data.post_id); | ||
let person_id = local_user_view.person.id; | ||
|
||
if post_ids.len() > MAX_API_PARAM_ELEMENTS { | ||
Err(LemmyErrorType::TooManyItems)?; | ||
} | ||
|
||
// Mark the post as read / unread | ||
if data.read { | ||
utils::mark_post_as_read(person_id, post_id, &mut context.pool()).await?; | ||
PostRead::mark_as_read(&mut context.pool(), post_ids, person_id) | ||
.await | ||
.with_lemmy_type(LemmyErrorType::CouldntMarkPostAsRead)?; | ||
} else { | ||
utils::mark_post_as_unread(person_id, post_id, &mut context.pool()).await?; | ||
PostRead::mark_as_unread(&mut context.pool(), post_ids, person_id) | ||
.await | ||
.with_lemmy_type(LemmyErrorType::CouldntMarkPostAsRead)?; | ||
} | ||
|
||
// Fetch it | ||
let post_view = PostView::read(&mut context.pool(), post_id, Some(person_id), false).await?; | ||
|
||
Ok(Json(PostResponse { post_view })) | ||
Ok(Json(SuccessResponse::default())) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters