From 10079f829535ef7eb289239c637ed052074748fb Mon Sep 17 00:00:00 2001 From: Dessalines Date: Mon, 23 Sep 2024 12:19:16 -0400 Subject: [PATCH 1/2] Removing a few SuccessResponses for PostHide and MarkPostAsRead. - This also removes the pointless multiple post_ids. These can be done as individual calls on the front end anyway. - Fixes #4755 --- crates/api/src/post/hide.rs | 33 ++++--- crates/api/src/post/mark_read.rs | 32 ++++--- crates/api_common/src/post.rs | 4 +- crates/api_common/src/utils.rs | 4 +- crates/db_schema/src/impls/post.rs | 135 ++++++++++++----------------- crates/db_views/src/post_view.rs | 6 +- 6 files changed, 99 insertions(+), 115 deletions(-) diff --git a/crates/api/src/post/hide.rs b/crates/api/src/post/hide.rs index f7c21ef31..58464421c 100644 --- a/crates/api/src/post/hide.rs +++ b/crates/api/src/post/hide.rs @@ -1,34 +1,39 @@ use actix_web::web::{Data, Json}; -use lemmy_api_common::{context::LemmyContext, post::HidePost, SuccessResponse}; +use lemmy_api_common::{ + context::LemmyContext, + post::{HidePost, PostResponse}, +}; use lemmy_db_schema::source::post::PostHide; -use lemmy_db_views::structs::LocalUserView; -use lemmy_utils::error::{LemmyErrorExt, LemmyErrorType, LemmyResult, MAX_API_PARAM_ELEMENTS}; -use std::collections::HashSet; +use lemmy_db_views::structs::{LocalUserView, PostView}; +use lemmy_utils::error::{LemmyErrorExt, LemmyErrorType, LemmyResult}; #[tracing::instrument(skip(context))] pub async fn hide_post( data: Json, context: Data, local_user_view: LocalUserView, -) -> LemmyResult> { - let post_ids = HashSet::from_iter(data.post_ids.clone()); - - if post_ids.len() > MAX_API_PARAM_ELEMENTS { - Err(LemmyErrorType::TooManyItems)?; - } - +) -> LemmyResult> { let person_id = local_user_view.person.id; + let post_id = data.post_id; // Mark the post as hidden / unhidden if data.hide { - PostHide::hide(&mut context.pool(), post_ids, person_id) + PostHide::hide(&mut context.pool(), post_id, person_id) .await .with_lemmy_type(LemmyErrorType::CouldntHidePost)?; } else { - PostHide::unhide(&mut context.pool(), post_ids, person_id) + PostHide::unhide(&mut context.pool(), post_id, person_id) .await .with_lemmy_type(LemmyErrorType::CouldntHidePost)?; } - Ok(Json(SuccessResponse::default())) + let post_view = PostView::read( + &mut context.pool(), + post_id, + Some(&local_user_view.local_user), + false, + ) + .await?; + + Ok(Json(PostResponse { post_view })) } diff --git a/crates/api/src/post/mark_read.rs b/crates/api/src/post/mark_read.rs index 3e534675a..893be56b6 100644 --- a/crates/api/src/post/mark_read.rs +++ b/crates/api/src/post/mark_read.rs @@ -1,34 +1,38 @@ use actix_web::web::{Data, Json}; -use lemmy_api_common::{context::LemmyContext, post::MarkPostAsRead, SuccessResponse}; +use lemmy_api_common::{ + context::LemmyContext, + post::{MarkPostAsRead, PostResponse}, +}; use lemmy_db_schema::source::post::PostRead; -use lemmy_db_views::structs::LocalUserView; -use lemmy_utils::error::{LemmyErrorExt, LemmyErrorType, LemmyResult, MAX_API_PARAM_ELEMENTS}; -use std::collections::HashSet; +use lemmy_db_views::structs::{LocalUserView, PostView}; +use lemmy_utils::error::{LemmyErrorExt, LemmyErrorType, LemmyResult}; #[tracing::instrument(skip(context))] pub async fn mark_post_as_read( data: Json, context: Data, local_user_view: LocalUserView, -) -> LemmyResult> { - let post_ids = HashSet::from_iter(data.post_ids.clone()); - - if post_ids.len() > MAX_API_PARAM_ELEMENTS { - Err(LemmyErrorType::TooManyItems)?; - } - +) -> LemmyResult> { let person_id = local_user_view.person.id; + let post_id = data.post_id; // Mark the post as read / unread if data.read { - PostRead::mark_as_read(&mut context.pool(), post_ids, person_id) + PostRead::mark_as_read(&mut context.pool(), post_id, person_id) .await .with_lemmy_type(LemmyErrorType::CouldntMarkPostAsRead)?; } else { - PostRead::mark_as_unread(&mut context.pool(), post_ids, person_id) + PostRead::mark_as_unread(&mut context.pool(), post_id, person_id) .await .with_lemmy_type(LemmyErrorType::CouldntMarkPostAsRead)?; } + let post_view = PostView::read( + &mut context.pool(), + post_id, + Some(&local_user_view.local_user), + false, + ) + .await?; - Ok(Json(SuccessResponse::default())) + Ok(Json(PostResponse { post_view })) } diff --git a/crates/api_common/src/post.rs b/crates/api_common/src/post.rs index 44436fa84..cc278d8e0 100644 --- a/crates/api_common/src/post.rs +++ b/crates/api_common/src/post.rs @@ -152,7 +152,7 @@ pub struct RemovePost { #[cfg_attr(feature = "full", ts(export))] /// Mark a post as read. pub struct MarkPostAsRead { - pub post_ids: Vec, + pub post_id: PostId, pub read: bool, } @@ -162,7 +162,7 @@ pub struct MarkPostAsRead { #[cfg_attr(feature = "full", ts(export))] /// Hide a post from list views pub struct HidePost { - pub post_ids: Vec, + pub post_id: PostId, pub hide: bool, } diff --git a/crates/api_common/src/utils.rs b/crates/api_common/src/utils.rs index db186247c..4b329d71b 100644 --- a/crates/api_common/src/utils.rs +++ b/crates/api_common/src/utils.rs @@ -59,7 +59,7 @@ use lemmy_utils::{ use moka::future::Cache; use regex::{escape, Regex, RegexSet}; use rosetta_i18n::{Language, LanguageId}; -use std::{collections::HashSet, sync::LazyLock}; +use std::sync::LazyLock; use tracing::warn; use url::{ParseError, Url}; use urlencoding::encode; @@ -154,7 +154,7 @@ pub async fn mark_post_as_read( post_id: PostId, pool: &mut DbPool<'_>, ) -> LemmyResult<()> { - PostRead::mark_as_read(pool, HashSet::from([post_id]), person_id) + PostRead::mark_as_read(pool, post_id, person_id) .await .with_lemmy_type(LemmyErrorType::CouldntMarkPostAsRead)?; Ok(()) diff --git a/crates/db_schema/src/impls/post.rs b/crates/db_schema/src/impls/post.rs index 43ccbc0a7..43bb0678c 100644 --- a/crates/db_schema/src/impls/post.rs +++ b/crates/db_schema/src/impls/post.rs @@ -38,7 +38,6 @@ use diesel::{ TextExpressionMethods, }; use diesel_async::RunQueryDsl; -use std::collections::HashSet; #[async_trait] impl Crud for Post { @@ -298,77 +297,67 @@ impl Saveable for PostSaved { impl PostRead { pub async fn mark_as_read( pool: &mut DbPool<'_>, - post_ids: HashSet, + post_id: PostId, person_id: PersonId, - ) -> Result { + ) -> Result { let conn = &mut get_conn(pool).await?; - let forms = post_ids - .into_iter() - .map(|post_id| PostReadForm { post_id, person_id }) - .collect::>(); + let form = PostReadForm { post_id, person_id }; + insert_into(post_read::table) - .values(forms) + .values(form) .on_conflict_do_nothing() - .execute(conn) + .get_result::(conn) .await } pub async fn mark_as_unread( pool: &mut DbPool<'_>, - post_id_: HashSet, + post_id_: PostId, person_id_: PersonId, ) -> Result { let conn = &mut get_conn(pool).await?; - diesel::delete( - post_read::table - .filter(post_read::post_id.eq_any(post_id_)) - .filter(post_read::person_id.eq(person_id_)), - ) - .execute(conn) - .await + let read_post = post_read::table + .filter(post_read::post_id.eq(post_id_)) + .filter(post_read::person_id.eq(person_id_)); + + diesel::delete(read_post).execute(conn).await } } impl PostHide { pub async fn hide( pool: &mut DbPool<'_>, - post_ids: HashSet, + post_id: PostId, person_id: PersonId, - ) -> Result { + ) -> Result { let conn = &mut get_conn(pool).await?; - let forms = post_ids - .into_iter() - .map(|post_id| PostHideForm { post_id, person_id }) - .collect::>(); + let form = PostHideForm { post_id, person_id }; insert_into(post_hide::table) - .values(forms) + .values(form) .on_conflict_do_nothing() - .execute(conn) + .get_result::(conn) .await } pub async fn unhide( pool: &mut DbPool<'_>, - post_id_: HashSet, + post_id_: PostId, person_id_: PersonId, ) -> Result { let conn = &mut get_conn(pool).await?; - diesel::delete( - post_hide::table - .filter(post_hide::post_id.eq_any(post_id_)) - .filter(post_hide::person_id.eq(person_id_)), - ) - .execute(conn) - .await + let hidden_post = post_hide::table + .filter(post_hide::post_id.eq(post_id_)) + .filter(post_hide::person_id.eq(person_id_)); + + diesel::delete(hidden_post).execute(conn).await } } #[cfg(test)] -#[allow(clippy::unwrap_used)] #[allow(clippy::indexing_slicing)] mod tests { @@ -391,24 +380,22 @@ mod tests { traits::{Crud, Likeable, Saveable}, utils::build_db_pool_for_tests, }; + use lemmy_utils::error::LemmyResult; use pretty_assertions::assert_eq; use serial_test::serial; - use std::collections::HashSet; use url::Url; #[tokio::test] #[serial] - async fn test_crud() { + async fn test_crud() -> LemmyResult<()> { let pool = &build_db_pool_for_tests().await; let pool = &mut pool.into(); - let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()) - .await - .unwrap(); + let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()).await?; let new_person = PersonInsertForm::test_form(inserted_instance.id, "jim"); - let inserted_person = Person::create(pool, &new_person).await.unwrap(); + let inserted_person = Person::create(pool, &new_person).await?; let new_community = CommunityInsertForm::new( inserted_instance.id, @@ -417,21 +404,21 @@ mod tests { "pubkey".to_string(), ); - let inserted_community = Community::create(pool, &new_community).await.unwrap(); + let inserted_community = Community::create(pool, &new_community).await?; let new_post = PostInsertForm::new( "A test post".into(), inserted_person.id, inserted_community.id, ); - let inserted_post = Post::create(pool, &new_post).await.unwrap(); + let inserted_post = Post::create(pool, &new_post).await?; let new_post2 = PostInsertForm::new( "A test post 2".into(), inserted_person.id, inserted_community.id, ); - let inserted_post2 = Post::create(pool, &new_post2).await.unwrap(); + let inserted_post2 = Post::create(pool, &new_post2).await?; let expected_post = Post { id: inserted_post.id, @@ -451,9 +438,7 @@ mod tests { embed_description: None, embed_video_url: None, thumbnail_url: None, - ap_id: Url::parse(&format!("https://lemmy-alpha/post/{}", inserted_post.id)) - .unwrap() - .into(), + ap_id: Url::parse(&format!("https://lemmy-alpha/post/{}", inserted_post.id))?.into(), local: true, language_id: Default::default(), featured_community: false, @@ -468,7 +453,7 @@ mod tests { score: 1, }; - let inserted_post_like = PostLike::like(pool, &post_like_form).await.unwrap(); + let inserted_post_like = PostLike::like(pool, &post_like_form).await?; let expected_post_like = PostLike { post_id: inserted_post.id, @@ -483,7 +468,7 @@ mod tests { person_id: inserted_person.id, }; - let inserted_post_saved = PostSaved::save(pool, &post_saved_form).await.unwrap(); + let inserted_post_saved = PostSaved::save(pool, &post_saved_form).await?; let expected_post_saved = PostSaved { post_id: inserted_post.id, @@ -491,54 +476,44 @@ mod tests { published: inserted_post_saved.published, }; - // Post Read - let marked_as_read = PostRead::mark_as_read( - pool, - HashSet::from([inserted_post.id, inserted_post2.id]), - inserted_person.id, - ) - .await - .unwrap(); - assert_eq!(2, marked_as_read); + // Mark 2 posts as read + PostRead::mark_as_read(pool, inserted_post.id, inserted_person.id).await?; + PostRead::mark_as_read(pool, inserted_post2.id, inserted_person.id).await?; - let read_post = Post::read(pool, inserted_post.id).await.unwrap(); + let read_post = Post::read(pool, inserted_post.id).await?; let new_post_update = PostUpdateForm { name: Some("A test post".into()), ..Default::default() }; - let updated_post = Post::update(pool, inserted_post.id, &new_post_update) - .await - .unwrap(); + let updated_post = Post::update(pool, inserted_post.id, &new_post_update).await?; - let like_removed = PostLike::remove(pool, inserted_person.id, inserted_post.id) - .await - .unwrap(); + let like_removed = PostLike::remove(pool, inserted_person.id, inserted_post.id).await?; 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?; 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 - .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(); + // mark some posts as unread + let read_removed_1 = + PostRead::mark_as_unread(pool, inserted_post.id, inserted_person.id).await?; + assert_eq!(1, read_removed_1); + let read_removed_2 = + PostRead::mark_as_unread(pool, inserted_post2.id, inserted_person.id).await?; + assert_eq!(1, read_removed_2); + + let num_deleted = + Post::delete(pool, inserted_post.id).await? + Post::delete(pool, inserted_post2.id).await?; assert_eq!(2, num_deleted); - Community::delete(pool, inserted_community.id) - .await - .unwrap(); - Person::delete(pool, inserted_person.id).await.unwrap(); - Instance::delete(pool, inserted_instance.id).await.unwrap(); + Community::delete(pool, inserted_community.id).await?; + Person::delete(pool, inserted_person.id).await?; + Instance::delete(pool, inserted_instance.id).await?; assert_eq!(expected_post, read_post); assert_eq!(expected_post, inserted_post); assert_eq!(expected_post, updated_post); assert_eq!(expected_post_like, inserted_post_like); assert_eq!(expected_post_saved, inserted_post_saved); + + Ok(()) } } diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index 5dee59538..9eb3a4715 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -787,7 +787,7 @@ mod tests { use lemmy_utils::error::LemmyResult; use pretty_assertions::assert_eq; use serial_test::serial; - use std::{collections::HashSet, time::Duration}; + use std::time::Duration; use url::Url; const POST_WITH_ANOTHER_TITLE: &str = "Another title"; @@ -1622,7 +1622,7 @@ mod tests { // Mark a post as read PostRead::mark_as_read( pool, - HashSet::from([data.inserted_bot_post.id]), + data.inserted_bot_post.id, data.local_user_view.person.id, ) .await?; @@ -1664,7 +1664,7 @@ mod tests { // Mark a post as hidden PostHide::hide( pool, - HashSet::from([data.inserted_bot_post.id]), + data.inserted_bot_post.id, data.local_user_view.person.id, ) .await?; From 622ce6c976adf69667abf8ad5b9dfbd0897d9998 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Mon, 23 Sep 2024 15:35:37 -0400 Subject: [PATCH 2/2] Fixing federation tests. --- api_tests/package.json | 2 +- api_tests/pnpm-lock.yaml | 10 +++++----- crates/db_schema/src/impls/post.rs | 8 ++++---- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/api_tests/package.json b/api_tests/package.json index cacd476ea..4dccaeced 100644 --- a/api_tests/package.json +++ b/api_tests/package.json @@ -27,7 +27,7 @@ "eslint": "^9.8.0", "eslint-plugin-prettier": "^5.1.3", "jest": "^29.5.0", - "lemmy-js-client": "0.20.0-alpha.11", + "lemmy-js-client": "0.20.0-alpha.12", "prettier": "^3.2.5", "ts-jest": "^29.1.0", "typescript": "^5.5.4", diff --git a/api_tests/pnpm-lock.yaml b/api_tests/pnpm-lock.yaml index f72450015..30ea38784 100644 --- a/api_tests/pnpm-lock.yaml +++ b/api_tests/pnpm-lock.yaml @@ -30,8 +30,8 @@ importers: specifier: ^29.5.0 version: 29.7.0(@types/node@22.3.0) lemmy-js-client: - specifier: 0.20.0-alpha.11 - version: 0.20.0-alpha.11 + specifier: 0.20.0-alpha.12 + version: 0.20.0-alpha.12 prettier: specifier: ^3.2.5 version: 3.3.3 @@ -1175,8 +1175,8 @@ packages: resolution: {integrity: sha512-eTIzlVOSUR+JxdDFepEYcBMtZ9Qqdef+rnzWdRZuMbOywu5tO2w2N7rqjoANZ5k9vywhL6Br1VRjUIgTQx4E8w==} engines: {node: '>=6'} - lemmy-js-client@0.20.0-alpha.11: - resolution: {integrity: sha512-iRSG4xHMjPDIreQqVIoJ5JrMY71uk07G0Zbgyf068xKbib22J3+i1x/XgCTs6tiHlqTnw1Ig/KRq7p7qJoA4uw==} + lemmy-js-client@0.20.0-alpha.12: + resolution: {integrity: sha512-+nknIpFAT25TnhObvPPCI0JhDxmTSfg3jbNm7f4RnwidRskjS8SraNFD4bXFfHf14lu61ZEiVe58+UhhRe2UdA==} leven@3.1.0: resolution: {integrity: sha512-qsda+H8jTaUaN/x5vzW2rzc+8Rw4TAQ/4KjB46IwK5VH+IlVeeeje/EoZRpiXvIqjFgK84QffqPztGI3VBLG1A==} @@ -3107,7 +3107,7 @@ snapshots: kleur@3.0.3: {} - lemmy-js-client@0.20.0-alpha.11: {} + lemmy-js-client@0.20.0-alpha.12: {} leven@3.1.0: {} diff --git a/crates/db_schema/src/impls/post.rs b/crates/db_schema/src/impls/post.rs index 43bb0678c..49e4582e7 100644 --- a/crates/db_schema/src/impls/post.rs +++ b/crates/db_schema/src/impls/post.rs @@ -299,7 +299,7 @@ impl PostRead { pool: &mut DbPool<'_>, post_id: PostId, person_id: PersonId, - ) -> Result { + ) -> Result { let conn = &mut get_conn(pool).await?; let form = PostReadForm { post_id, person_id }; @@ -307,7 +307,7 @@ impl PostRead { insert_into(post_read::table) .values(form) .on_conflict_do_nothing() - .get_result::(conn) + .execute(conn) .await } @@ -331,14 +331,14 @@ impl PostHide { pool: &mut DbPool<'_>, post_id: PostId, person_id: PersonId, - ) -> Result { + ) -> Result { let conn = &mut get_conn(pool).await?; let form = PostHideForm { post_id, person_id }; insert_into(post_hide::table) .values(form) .on_conflict_do_nothing() - .get_result::(conn) + .execute(conn) .await }