Allow marking multiple posts as read in single api call (fixes #3963) (#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>
This commit is contained in:
Nutomic 2023-10-17 18:35:51 +02:00 committed by GitHub
parent 3f62135083
commit 3a19af5215
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 91 additions and 99 deletions

View file

@ -1,30 +1,34 @@
use actix_web::web::{Data, Json}; use actix_web::web::{Data, Json};
use lemmy_api_common::{ use lemmy_api_common::{context::LemmyContext, post::MarkPostAsRead, SuccessResponse};
context::LemmyContext, use lemmy_db_schema::source::post::PostRead;
post::{MarkPostAsRead, PostResponse}, use lemmy_db_views::structs::LocalUserView;
utils, use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType, MAX_API_PARAM_ELEMENTS};
}; use std::collections::HashSet;
use lemmy_db_views::structs::{LocalUserView, PostView};
use lemmy_utils::error::LemmyError;
#[tracing::instrument(skip(context))] #[tracing::instrument(skip(context))]
pub async fn mark_post_as_read( pub async fn mark_post_as_read(
data: Json<MarkPostAsRead>, data: Json<MarkPostAsRead>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: LocalUserView, local_user_view: LocalUserView,
) -> Result<Json<PostResponse>, LemmyError> { ) -> Result<Json<SuccessResponse>, LemmyError> {
let post_id = data.post_id; 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; 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 // Mark the post as read / unread
if data.read { 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 { } 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 Ok(Json(SuccessResponse::default()))
let post_view = PostView::read(&mut context.pool(), post_id, Some(person_id), false).await?;
Ok(Json(PostResponse { post_view }))
} }

View file

@ -140,7 +140,9 @@ pub struct RemovePost {
#[cfg_attr(feature = "full", ts(export))] #[cfg_attr(feature = "full", ts(export))]
/// Mark a post as read. /// Mark a post as read.
pub struct MarkPostAsRead { pub struct MarkPostAsRead {
/// TODO: deprecated, send `post_ids` instead
pub post_id: PostId, pub post_id: PostId,
pub post_ids: Vec<PostId>,
pub read: bool, pub read: bool,
} }

View file

@ -18,9 +18,9 @@ use lemmy_db_schema::{
password_reset_request::PasswordResetRequest, password_reset_request::PasswordResetRequest,
person::{Person, PersonUpdateForm}, person::{Person, PersonUpdateForm},
person_block::PersonBlock, person_block::PersonBlock,
post::{Post, PostRead, PostReadForm}, post::{Post, PostRead},
}, },
traits::{Crud, Readable}, traits::Crud,
utils::DbPool, utils::DbPool,
}; };
use lemmy_db_views::{comment_view::CommentQuery, structs::LocalUserView}; use lemmy_db_views::{comment_view::CommentQuery, structs::LocalUserView};
@ -39,6 +39,7 @@ use lemmy_utils::{
}; };
use regex::Regex; use regex::Regex;
use rosetta_i18n::{Language, LanguageId}; use rosetta_i18n::{Language, LanguageId};
use std::collections::HashSet;
use tracing::warn; use tracing::warn;
use url::{ParseError, Url}; use url::{ParseError, Url};
@ -117,25 +118,11 @@ pub async fn mark_post_as_read(
person_id: PersonId, person_id: PersonId,
post_id: PostId, post_id: PostId,
pool: &mut DbPool<'_>, pool: &mut DbPool<'_>,
) -> Result<PostRead, LemmyError> { ) -> Result<(), LemmyError> {
let post_read_form = PostReadForm { post_id, person_id }; PostRead::mark_as_read(pool, HashSet::from([post_id]), person_id)
PostRead::mark_as_read(pool, &post_read_form)
.await .await
.with_lemmy_type(LemmyErrorType::CouldntMarkPostAsRead) .with_lemmy_type(LemmyErrorType::CouldntMarkPostAsRead)?;
} Ok(())
#[tracing::instrument(skip_all)]
pub async fn mark_post_as_unread(
person_id: PersonId,
post_id: PostId,
pool: &mut DbPool<'_>,
) -> Result<usize, LemmyError> {
let post_read_form = PostReadForm { post_id, person_id };
PostRead::mark_as_unread(pool, &post_read_form)
.await
.with_lemmy_type(LemmyErrorType::CouldntMarkPostAsRead)
} }
pub fn check_user_valid(person: &Person) -> Result<(), LemmyError> { pub fn check_user_valid(person: &Person) -> Result<(), LemmyError> {

View file

@ -23,18 +23,12 @@ use lemmy_db_schema::{
}; };
use lemmy_db_views::structs::LocalUserView; use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::{ use lemmy_utils::{
error::{LemmyError, LemmyErrorType, LemmyResult}, error::{LemmyError, LemmyErrorType, LemmyResult, MAX_API_PARAM_ELEMENTS},
spawn_try_task, spawn_try_task,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tracing::info; use tracing::info;
/// Maximum number of follow/block URLs which can be imported at once, to prevent server overloading.
/// To import a larger backup, split it into multiple parts.
///
/// TODO: having the user manually split files will very be confusing
const MAX_URL_IMPORT_COUNT: usize = 1000;
/// Backup of user data. This struct should never be changed so that the data can be used as a /// Backup of user data. This struct should never be changed so that the data can be used as a
/// long-term backup in case the instance goes down unexpectedly. All fields are optional to allow /// long-term backup in case the instance goes down unexpectedly. All fields are optional to allow
/// importing partial backups. /// importing partial backups.
@ -138,8 +132,8 @@ pub async fn import_settings(
+ data.blocked_users.len() + data.blocked_users.len()
+ data.saved_posts.len() + data.saved_posts.len()
+ data.saved_comments.len(); + data.saved_comments.len();
if url_count > MAX_URL_IMPORT_COUNT { if url_count > MAX_API_PARAM_ELEMENTS {
Err(LemmyErrorType::UserBackupTooLarge)?; Err(LemmyErrorType::TooManyItems)?;
} }
spawn_try_task(async move { spawn_try_task(async move {
@ -434,7 +428,7 @@ mod tests {
assert_eq!( assert_eq!(
imported.err().unwrap().error_type, imported.err().unwrap().error_type,
LemmyErrorType::UserBackupTooLarge LemmyErrorType::TooManyItems
); );
LocalUser::delete(&mut context.pool(), export_user.local_user.id) LocalUser::delete(&mut context.pool(), export_user.local_user.id)

View file

@ -28,13 +28,14 @@ use crate::{
PostSavedForm, PostSavedForm,
PostUpdateForm, PostUpdateForm,
}, },
traits::{Crud, Likeable, Readable, Saveable}, traits::{Crud, Likeable, Saveable},
utils::{get_conn, naive_now, DbPool, DELETED_REPLACEMENT_TEXT, FETCH_LIMIT_MAX}, utils::{get_conn, naive_now, DbPool, DELETED_REPLACEMENT_TEXT, FETCH_LIMIT_MAX},
}; };
use ::url::Url; use ::url::Url;
use chrono::{Duration, Utc}; use chrono::{Duration, Utc};
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl, TextExpressionMethods}; use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl, TextExpressionMethods};
use diesel_async::RunQueryDsl; use diesel_async::RunQueryDsl;
use std::collections::HashSet;
#[async_trait] #[async_trait]
impl Crud for Post { impl Crud for Post {
@ -302,34 +303,38 @@ impl Saveable for PostSaved {
} }
} }
#[async_trait] impl PostRead {
impl Readable for PostRead { pub async fn mark_as_read(
type Form = PostReadForm;
async fn mark_as_read(
pool: &mut DbPool<'_>, pool: &mut DbPool<'_>,
post_read_form: &PostReadForm, post_ids: HashSet<PostId>,
) -> Result<Self, Error> { person_id: PersonId,
use crate::schema::post_read::dsl::{person_id, post_id, post_read}; ) -> Result<usize, Error> {
use crate::schema::post_read::dsl::post_read;
let conn = &mut get_conn(pool).await?; let conn = &mut get_conn(pool).await?;
let forms = post_ids
.into_iter()
.map(|post_id| PostReadForm { post_id, person_id })
.collect::<Vec<PostReadForm>>();
insert_into(post_read) insert_into(post_read)
.values(post_read_form) .values(forms)
.on_conflict((post_id, person_id)) .on_conflict_do_nothing()
.do_update() .execute(conn)
.set(post_read_form)
.get_result::<Self>(conn)
.await .await
} }
async fn mark_as_unread( pub async fn mark_as_unread(
pool: &mut DbPool<'_>, pool: &mut DbPool<'_>,
post_read_form: &PostReadForm, post_id_: HashSet<PostId>,
person_id_: PersonId,
) -> Result<usize, Error> { ) -> Result<usize, Error> {
use crate::schema::post_read::dsl::{person_id, post_id, post_read}; use crate::schema::post_read::dsl::{person_id, post_id, post_read};
let conn = &mut get_conn(pool).await?; let conn = &mut get_conn(pool).await?;
diesel::delete( diesel::delete(
post_read post_read
.filter(post_id.eq(post_read_form.post_id)) .filter(post_id.eq_any(post_id_))
.filter(person_id.eq(post_read_form.person_id)), .filter(person_id.eq(person_id_)),
) )
.execute(conn) .execute(conn)
.await .await
@ -352,16 +357,16 @@ mod tests {
PostLike, PostLike,
PostLikeForm, PostLikeForm,
PostRead, PostRead,
PostReadForm,
PostSaved, PostSaved,
PostSavedForm, PostSavedForm,
PostUpdateForm, PostUpdateForm,
}, },
}, },
traits::{Crud, Likeable, Readable, Saveable}, traits::{Crud, Likeable, Saveable},
utils::build_db_pool_for_tests, utils::build_db_pool_for_tests,
}; };
use serial_test::serial; use serial_test::serial;
use std::collections::HashSet;
#[tokio::test] #[tokio::test]
#[serial] #[serial]
@ -398,6 +403,13 @@ mod tests {
let inserted_post = Post::create(pool, &new_post).await.unwrap(); let inserted_post = Post::create(pool, &new_post).await.unwrap();
let new_post2 = PostInsertForm::builder()
.name("A test post 2".into())
.creator_id(inserted_person.id)
.community_id(inserted_community.id)
.build();
let inserted_post2 = Post::create(pool, &new_post2).await.unwrap();
let expected_post = Post { let expected_post = Post {
id: inserted_post.id, id: inserted_post.id,
name: "A test post".into(), name: "A test post".into(),
@ -455,19 +467,14 @@ mod tests {
}; };
// Post Read // Post Read
let post_read_form = PostReadForm { let marked_as_read = PostRead::mark_as_read(
post_id: inserted_post.id, pool,
person_id: inserted_person.id, HashSet::from([inserted_post.id, inserted_post2.id]),
}; inserted_person.id,
)
let inserted_post_read = PostRead::mark_as_read(pool, &post_read_form).await.unwrap(); .await
.unwrap();
let expected_post_read = PostRead { assert_eq!(2, marked_as_read);
id: inserted_post_read.id,
post_id: inserted_post.id,
person_id: inserted_person.id,
published: inserted_post_read.published,
};
let read_post = Post::read(pool, inserted_post.id).await.unwrap(); let read_post = Post::read(pool, inserted_post.id).await.unwrap();
@ -482,11 +489,21 @@ mod tests {
let like_removed = PostLike::remove(pool, inserted_person.id, inserted_post.id) let like_removed = PostLike::remove(pool, inserted_person.id, inserted_post.id)
.await .await
.unwrap(); .unwrap();
assert_eq!(1, like_removed);
let saved_removed = PostSaved::unsave(pool, &post_saved_form).await.unwrap(); let saved_removed = PostSaved::unsave(pool, &post_saved_form).await.unwrap();
let read_removed = PostRead::mark_as_unread(pool, &post_read_form) assert_eq!(1, saved_removed);
let read_removed = PostRead::mark_as_unread(
pool,
HashSet::from([inserted_post.id, inserted_post2.id]),
inserted_person.id,
)
.await .await
.unwrap(); .unwrap();
let num_deleted = Post::delete(pool, inserted_post.id).await.unwrap(); assert_eq!(2, read_removed);
let num_deleted = Post::delete(pool, inserted_post.id).await.unwrap()
+ Post::delete(pool, inserted_post2.id).await.unwrap();
assert_eq!(2, num_deleted);
Community::delete(pool, inserted_community.id) Community::delete(pool, inserted_community.id)
.await .await
.unwrap(); .unwrap();
@ -498,10 +515,5 @@ mod tests {
assert_eq!(expected_post, updated_post); assert_eq!(expected_post, updated_post);
assert_eq!(expected_post_like, inserted_post_like); assert_eq!(expected_post_like, inserted_post_like);
assert_eq!(expected_post_saved, inserted_post_saved); assert_eq!(expected_post_saved, inserted_post_saved);
assert_eq!(expected_post_read, inserted_post_read);
assert_eq!(1, like_removed);
assert_eq!(1, saved_removed);
assert_eq!(1, read_removed);
assert_eq!(1, num_deleted);
} }
} }

View file

@ -162,7 +162,7 @@ pub struct PostRead {
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))] #[cfg_attr(feature = "full", derive(Insertable, AsChangeset))]
#[cfg_attr(feature = "full", diesel(table_name = post_read))] #[cfg_attr(feature = "full", diesel(table_name = post_read))]
pub struct PostReadForm { pub(crate) struct PostReadForm {
pub post_id: PostId, pub post_id: PostId,
pub person_id: PersonId, pub person_id: PersonId,
} }

View file

@ -140,17 +140,6 @@ pub trait Blockable {
Self: Sized; Self: Sized;
} }
#[async_trait]
pub trait Readable {
type Form;
async fn mark_as_read(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
where
Self: Sized;
async fn mark_as_unread(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<usize, Error>
where
Self: Sized;
}
#[async_trait] #[async_trait]
pub trait Reportable { pub trait Reportable {
type Form; type Form;

View file

@ -15,6 +15,9 @@ pub struct LemmyError {
pub context: SpanTrace, pub context: SpanTrace,
} }
/// Maximum number of items in an array passed as API parameter. See [[LemmyErrorType::TooManyItems]]
pub const MAX_API_PARAM_ELEMENTS: usize = 1000;
impl<T> From<T> for LemmyError impl<T> From<T> for LemmyError
where where
T: Into<anyhow::Error>, T: Into<anyhow::Error>,
@ -215,8 +218,9 @@ pub enum LemmyErrorType {
InstanceBlockAlreadyExists, InstanceBlockAlreadyExists,
/// `jwt` cookie must be marked secure and httponly /// `jwt` cookie must be marked secure and httponly
AuthCookieInsecure, AuthCookieInsecure,
/// Thrown when an API call is submitted with more than 1000 array elements, see [[MAX_API_PARAM_ELEMENTS]]
TooManyItems,
CommunityHasNoFollowers, CommunityHasNoFollowers,
UserBackupTooLarge,
Unknown(String), Unknown(String),
} }