From ed55a25e623e3e9a2e27e0cc88d8721ff3816201 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Thu, 23 Jan 2025 10:08:07 -0500 Subject: [PATCH] Adding a slim comment_view variant, without post or community. (#5335) * Adding a slim comment_view variant, without post or community. - Fixes #2957 * Alternate version. * Only clone if check. * Revert "Alternate version." This reverts commit 64493959aaaf37f02feb7e021a150409b04aa08e. * Remove trait. --- crates/api_common/src/comment.rs | 10 ++++- crates/apub/src/api/list_comments.rs | 37 ++++++++++++++++--- crates/db_schema/src/lib.rs | 7 ---- crates/db_schema/src/traits.rs | 7 ++++ crates/db_views/src/comment_view.rs | 18 ++++++++- .../src/person_content_combined_view.rs | 2 +- .../src/person_saved_combined_view.rs | 2 +- crates/db_views/src/report_combined_view.rs | 2 +- crates/db_views/src/structs.rs | 21 +++++++++++ .../db_views_actor/src/inbox_combined_view.rs | 2 +- .../src/modlog_combined_view.rs | 2 +- src/api_routes_v4.rs | 3 +- 12 files changed, 92 insertions(+), 21 deletions(-) diff --git a/crates/api_common/src/comment.rs b/crates/api_common/src/comment.rs index 0d416e9f0..fd2b57f43 100644 --- a/crates/api_common/src/comment.rs +++ b/crates/api_common/src/comment.rs @@ -3,7 +3,7 @@ use lemmy_db_schema::{ CommentSortType, ListingType, }; -use lemmy_db_views::structs::{CommentView, VoteView}; +use lemmy_db_views::structs::{CommentSlimView, CommentView, VoteView}; use serde::{Deserialize, Serialize}; use serde_with::skip_serializing_none; #[cfg(feature = "full")] @@ -144,6 +144,14 @@ pub struct GetCommentsResponse { pub comments: Vec, } +#[derive(Debug, Serialize, Deserialize, Clone)] +#[cfg_attr(feature = "full", derive(TS))] +#[cfg_attr(feature = "full", ts(export))] +/// A slimmer comment list response, without the post or community. +pub struct GetCommentsSlimResponse { + pub comments: Vec, +} + #[skip_serializing_none] #[derive(Debug, Serialize, Deserialize, Clone, Copy, Default, PartialEq, Eq, Hash)] #[cfg_attr(feature = "full", derive(TS))] diff --git a/crates/apub/src/api/list_comments.rs b/crates/apub/src/api/list_comments.rs index 45d241073..3759d1a4b 100644 --- a/crates/apub/src/api/list_comments.rs +++ b/crates/apub/src/api/list_comments.rs @@ -7,7 +7,7 @@ use crate::{ use activitypub_federation::config::Data; use actix_web::web::{Json, Query}; use lemmy_api_common::{ - comment::{GetComments, GetCommentsResponse}, + comment::{GetComments, GetCommentsResponse, GetCommentsSlimResponse}, context::LemmyContext, utils::{check_conflicting_like_filters, check_private_instance}, }; @@ -17,16 +17,17 @@ use lemmy_db_schema::{ }; use lemmy_db_views::{ comment_view::CommentQuery, - structs::{LocalUserView, SiteView}, + structs::{CommentView, LocalUserView, SiteView}, }; use lemmy_utils::error::{LemmyErrorExt, LemmyErrorType, LemmyResult}; +/// A common fetcher for both the CommentView, and CommentSlimView. #[tracing::instrument(skip(context))] -pub async fn list_comments( +async fn list_comments_common( data: Query, context: Data, local_user_view: Option, -) -> LemmyResult> { +) -> LemmyResult> { let site_view = SiteView::read_local(&mut context.pool()).await?; check_private_instance(&local_user_view, &site_view.local_site)?; @@ -73,7 +74,7 @@ pub async fn list_comments( let post_id = data.post_id; let local_user = local_user_view.as_ref().map(|l| &l.local_user); - let comments = CommentQuery { + CommentQuery { listing_type, sort, max_depth, @@ -89,7 +90,31 @@ pub async fn list_comments( } .list(&site_view.site, &mut context.pool()) .await - .with_lemmy_type(LemmyErrorType::CouldntGetComments)?; + .with_lemmy_type(LemmyErrorType::CouldntGetComments) +} + +#[tracing::instrument(skip(context))] +pub async fn list_comments( + data: Query, + context: Data, + local_user_view: Option, +) -> LemmyResult> { + let comments = list_comments_common(data, context, local_user_view).await?; Ok(Json(GetCommentsResponse { comments })) } + +#[tracing::instrument(skip(context))] +pub async fn list_comments_slim( + data: Query, + context: Data, + local_user_view: Option, +) -> LemmyResult> { + let comments = list_comments_common(data, context, local_user_view) + .await? + .into_iter() + .map(CommentView::map_to_slim) + .collect(); + + Ok(Json(GetCommentsSlimResponse { comments })) +} diff --git a/crates/db_schema/src/lib.rs b/crates/db_schema/src/lib.rs index c077f758b..9c0f0ea0f 100644 --- a/crates/db_schema/src/lib.rs +++ b/crates/db_schema/src/lib.rs @@ -292,13 +292,6 @@ pub enum FederationMode { Disable, } -pub trait InternalToCombinedView { - type CombinedView; - - /// Maps the combined DB row to an enum - fn map_to_enum(self) -> Option; -} - /// Wrapper for assert_eq! macro. Checks that vec matches the given length, and prints the /// vec on failure. #[macro_export] diff --git a/crates/db_schema/src/traits.rs b/crates/db_schema/src/traits.rs index bc30c6fb9..f010d3d1b 100644 --- a/crates/db_schema/src/traits.rs +++ b/crates/db_schema/src/traits.rs @@ -197,3 +197,10 @@ pub trait ApubActor { where Self: Sized; } + +pub trait InternalToCombinedView { + type CombinedView; + + /// Maps the combined DB row to an enum + fn map_to_enum(self) -> Option; +} diff --git a/crates/db_views/src/comment_view.rs b/crates/db_views/src/comment_view.rs index 710a820e7..28e1f91be 100644 --- a/crates/db_views/src/comment_view.rs +++ b/crates/db_views/src/comment_view.rs @@ -1,4 +1,4 @@ -use crate::structs::CommentView; +use crate::structs::{CommentSlimView, CommentView}; use diesel::{ dsl::{exists, not}, pg::Pg, @@ -313,6 +313,22 @@ impl CommentView { } Ok(handle_deleted(res, is_admin)) } + + pub fn map_to_slim(self) -> CommentSlimView { + CommentSlimView { + comment: self.comment, + creator: self.creator, + counts: self.counts, + creator_banned_from_community: self.creator_banned_from_community, + banned_from_community: self.banned_from_community, + creator_is_moderator: self.creator_is_moderator, + creator_is_admin: self.creator_is_admin, + subscribed: self.subscribed, + saved: self.saved, + creator_blocked: self.creator_blocked, + my_vote: self.my_vote, + } + } } #[derive(Default)] diff --git a/crates/db_views/src/person_content_combined_view.rs b/crates/db_views/src/person_content_combined_view.rs index b20447b98..d28dc7bdf 100644 --- a/crates/db_views/src/person_content_combined_view.rs +++ b/crates/db_views/src/person_content_combined_view.rs @@ -41,8 +41,8 @@ use lemmy_db_schema::{ combined::person_content::{person_content_combined_keys as key, PersonContentCombined}, community::CommunityFollower, }, + traits::InternalToCombinedView, utils::{actions, actions_alias, functions::coalesce, get_conn, DbPool}, - InternalToCombinedView, PersonContentType, }; use lemmy_utils::error::LemmyResult; diff --git a/crates/db_views/src/person_saved_combined_view.rs b/crates/db_views/src/person_saved_combined_view.rs index 4e1ce8df7..590094279 100644 --- a/crates/db_views/src/person_saved_combined_view.rs +++ b/crates/db_views/src/person_saved_combined_view.rs @@ -38,8 +38,8 @@ use lemmy_db_schema::{ combined::person_saved::{person_saved_combined_keys as key, PersonSavedCombined}, community::CommunityFollower, }, + traits::InternalToCombinedView, utils::{actions, actions_alias, functions::coalesce, get_conn, DbPool}, - InternalToCombinedView, PersonContentType, }; use lemmy_utils::error::LemmyResult; diff --git a/crates/db_views/src/report_combined_view.rs b/crates/db_views/src/report_combined_view.rs index c082d7f74..335fccb81 100644 --- a/crates/db_views/src/report_combined_view.rs +++ b/crates/db_views/src/report_combined_view.rs @@ -47,8 +47,8 @@ use lemmy_db_schema::{ combined::report::{report_combined_keys as key, ReportCombined}, community::CommunityFollower, }, + traits::InternalToCombinedView, utils::{actions, actions_alias, functions::coalesce, get_conn, DbPool, ReverseTimestampKey}, - InternalToCombinedView, }; use lemmy_utils::error::LemmyResult; diff --git a/crates/db_views/src/structs.rs b/crates/db_views/src/structs.rs index eedaa7980..43eba36c7 100644 --- a/crates/db_views/src/structs.rs +++ b/crates/db_views/src/structs.rs @@ -87,6 +87,27 @@ pub struct CommentView { pub my_vote: Option, } +#[skip_serializing_none] +#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] +#[cfg_attr(feature = "full", derive(TS, Queryable))] +#[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))] +#[cfg_attr(feature = "full", ts(export))] +/// A slimmer comment view, without the post, or community. +pub struct CommentSlimView { + pub comment: Comment, + pub creator: Person, + pub counts: CommentAggregates, + pub creator_banned_from_community: bool, + pub banned_from_community: bool, + pub creator_is_moderator: bool, + pub creator_is_admin: bool, + pub subscribed: SubscribedType, + pub saved: bool, + pub creator_blocked: bool, + #[cfg_attr(feature = "full", ts(optional))] + pub my_vote: Option, +} + #[skip_serializing_none] #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] #[cfg_attr(feature = "full", derive(TS, Queryable))] diff --git a/crates/db_views_actor/src/inbox_combined_view.rs b/crates/db_views_actor/src/inbox_combined_view.rs index 5c0405742..8dec579e2 100644 --- a/crates/db_views_actor/src/inbox_combined_view.rs +++ b/crates/db_views_actor/src/inbox_combined_view.rs @@ -46,9 +46,9 @@ use lemmy_db_schema::{ combined::inbox::{inbox_combined_keys as key, InboxCombined}, community::CommunityFollower, }, + traits::InternalToCombinedView, utils::{actions, actions_alias, functions::coalesce, get_conn, DbPool}, InboxDataType, - InternalToCombinedView, }; use lemmy_utils::error::LemmyResult; diff --git a/crates/db_views_moderator/src/modlog_combined_view.rs b/crates/db_views_moderator/src/modlog_combined_view.rs index 4c6e619ea..cc349ec00 100644 --- a/crates/db_views_moderator/src/modlog_combined_view.rs +++ b/crates/db_views_moderator/src/modlog_combined_view.rs @@ -61,8 +61,8 @@ use lemmy_db_schema::{ post, }, source::combined::modlog::{modlog_combined_keys as key, ModlogCombined}, + traits::InternalToCombinedView, utils::{get_conn, DbPool}, - InternalToCombinedView, ModlogActionType, }; use lemmy_utils::error::LemmyResult; diff --git a/src/api_routes_v4.rs b/src/api_routes_v4.rs index 852e868fd..48710b0df 100644 --- a/src/api_routes_v4.rs +++ b/src/api_routes_v4.rs @@ -142,7 +142,7 @@ use lemmy_api_crud::{ }, }; use lemmy_apub::api::{ - list_comments::list_comments, + list_comments::{list_comments, list_comments_slim}, list_person_content::list_person_content, list_posts::list_posts, read_community::get_community, @@ -277,6 +277,7 @@ pub fn config(cfg: &mut ServiceConfig, rate_limit: &RateLimitCell) { .route("/like/list", get().to(list_comment_likes)) .route("/save", put().to(save_comment)) .route("/list", get().to(list_comments)) + .route("/list/slim", get().to(list_comments_slim)) .route("/report", post().to(create_comment_report)) .route("/report/resolve", put().to(resolve_comment_report)), )