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 64493959aa.

* Remove trait.
This commit is contained in:
Dessalines 2025-01-23 10:08:07 -05:00 committed by GitHub
parent 4e46332bf4
commit ed55a25e62
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 92 additions and 21 deletions

View file

@ -3,7 +3,7 @@ use lemmy_db_schema::{
CommentSortType, CommentSortType,
ListingType, ListingType,
}; };
use lemmy_db_views::structs::{CommentView, VoteView}; use lemmy_db_views::structs::{CommentSlimView, CommentView, VoteView};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none; use serde_with::skip_serializing_none;
#[cfg(feature = "full")] #[cfg(feature = "full")]
@ -144,6 +144,14 @@ pub struct GetCommentsResponse {
pub comments: Vec<CommentView>, pub comments: Vec<CommentView>,
} }
#[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<CommentSlimView>,
}
#[skip_serializing_none] #[skip_serializing_none]
#[derive(Debug, Serialize, Deserialize, Clone, Copy, Default, PartialEq, Eq, Hash)] #[derive(Debug, Serialize, Deserialize, Clone, Copy, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "full", derive(TS))] #[cfg_attr(feature = "full", derive(TS))]

View file

@ -7,7 +7,7 @@ use crate::{
use activitypub_federation::config::Data; use activitypub_federation::config::Data;
use actix_web::web::{Json, Query}; use actix_web::web::{Json, Query};
use lemmy_api_common::{ use lemmy_api_common::{
comment::{GetComments, GetCommentsResponse}, comment::{GetComments, GetCommentsResponse, GetCommentsSlimResponse},
context::LemmyContext, context::LemmyContext,
utils::{check_conflicting_like_filters, check_private_instance}, utils::{check_conflicting_like_filters, check_private_instance},
}; };
@ -17,16 +17,17 @@ use lemmy_db_schema::{
}; };
use lemmy_db_views::{ use lemmy_db_views::{
comment_view::CommentQuery, comment_view::CommentQuery,
structs::{LocalUserView, SiteView}, structs::{CommentView, LocalUserView, SiteView},
}; };
use lemmy_utils::error::{LemmyErrorExt, LemmyErrorType, LemmyResult}; use lemmy_utils::error::{LemmyErrorExt, LemmyErrorType, LemmyResult};
/// A common fetcher for both the CommentView, and CommentSlimView.
#[tracing::instrument(skip(context))] #[tracing::instrument(skip(context))]
pub async fn list_comments( async fn list_comments_common(
data: Query<GetComments>, data: Query<GetComments>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: Option<LocalUserView>, local_user_view: Option<LocalUserView>,
) -> LemmyResult<Json<GetCommentsResponse>> { ) -> LemmyResult<Vec<CommentView>> {
let site_view = SiteView::read_local(&mut context.pool()).await?; let site_view = SiteView::read_local(&mut context.pool()).await?;
check_private_instance(&local_user_view, &site_view.local_site)?; 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 post_id = data.post_id;
let local_user = local_user_view.as_ref().map(|l| &l.local_user); let local_user = local_user_view.as_ref().map(|l| &l.local_user);
let comments = CommentQuery { CommentQuery {
listing_type, listing_type,
sort, sort,
max_depth, max_depth,
@ -89,7 +90,31 @@ pub async fn list_comments(
} }
.list(&site_view.site, &mut context.pool()) .list(&site_view.site, &mut context.pool())
.await .await
.with_lemmy_type(LemmyErrorType::CouldntGetComments)?; .with_lemmy_type(LemmyErrorType::CouldntGetComments)
}
#[tracing::instrument(skip(context))]
pub async fn list_comments(
data: Query<GetComments>,
context: Data<LemmyContext>,
local_user_view: Option<LocalUserView>,
) -> LemmyResult<Json<GetCommentsResponse>> {
let comments = list_comments_common(data, context, local_user_view).await?;
Ok(Json(GetCommentsResponse { comments })) Ok(Json(GetCommentsResponse { comments }))
} }
#[tracing::instrument(skip(context))]
pub async fn list_comments_slim(
data: Query<GetComments>,
context: Data<LemmyContext>,
local_user_view: Option<LocalUserView>,
) -> LemmyResult<Json<GetCommentsSlimResponse>> {
let comments = list_comments_common(data, context, local_user_view)
.await?
.into_iter()
.map(CommentView::map_to_slim)
.collect();
Ok(Json(GetCommentsSlimResponse { comments }))
}

View file

@ -292,13 +292,6 @@ pub enum FederationMode {
Disable, Disable,
} }
pub trait InternalToCombinedView {
type CombinedView;
/// Maps the combined DB row to an enum
fn map_to_enum(self) -> Option<Self::CombinedView>;
}
/// Wrapper for assert_eq! macro. Checks that vec matches the given length, and prints the /// Wrapper for assert_eq! macro. Checks that vec matches the given length, and prints the
/// vec on failure. /// vec on failure.
#[macro_export] #[macro_export]

View file

@ -197,3 +197,10 @@ pub trait ApubActor {
where where
Self: Sized; Self: Sized;
} }
pub trait InternalToCombinedView {
type CombinedView;
/// Maps the combined DB row to an enum
fn map_to_enum(self) -> Option<Self::CombinedView>;
}

View file

@ -1,4 +1,4 @@
use crate::structs::CommentView; use crate::structs::{CommentSlimView, CommentView};
use diesel::{ use diesel::{
dsl::{exists, not}, dsl::{exists, not},
pg::Pg, pg::Pg,
@ -313,6 +313,22 @@ impl CommentView {
} }
Ok(handle_deleted(res, is_admin)) 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)] #[derive(Default)]

View file

@ -41,8 +41,8 @@ use lemmy_db_schema::{
combined::person_content::{person_content_combined_keys as key, PersonContentCombined}, combined::person_content::{person_content_combined_keys as key, PersonContentCombined},
community::CommunityFollower, community::CommunityFollower,
}, },
traits::InternalToCombinedView,
utils::{actions, actions_alias, functions::coalesce, get_conn, DbPool}, utils::{actions, actions_alias, functions::coalesce, get_conn, DbPool},
InternalToCombinedView,
PersonContentType, PersonContentType,
}; };
use lemmy_utils::error::LemmyResult; use lemmy_utils::error::LemmyResult;

View file

@ -38,8 +38,8 @@ use lemmy_db_schema::{
combined::person_saved::{person_saved_combined_keys as key, PersonSavedCombined}, combined::person_saved::{person_saved_combined_keys as key, PersonSavedCombined},
community::CommunityFollower, community::CommunityFollower,
}, },
traits::InternalToCombinedView,
utils::{actions, actions_alias, functions::coalesce, get_conn, DbPool}, utils::{actions, actions_alias, functions::coalesce, get_conn, DbPool},
InternalToCombinedView,
PersonContentType, PersonContentType,
}; };
use lemmy_utils::error::LemmyResult; use lemmy_utils::error::LemmyResult;

View file

@ -47,8 +47,8 @@ use lemmy_db_schema::{
combined::report::{report_combined_keys as key, ReportCombined}, combined::report::{report_combined_keys as key, ReportCombined},
community::CommunityFollower, community::CommunityFollower,
}, },
traits::InternalToCombinedView,
utils::{actions, actions_alias, functions::coalesce, get_conn, DbPool, ReverseTimestampKey}, utils::{actions, actions_alias, functions::coalesce, get_conn, DbPool, ReverseTimestampKey},
InternalToCombinedView,
}; };
use lemmy_utils::error::LemmyResult; use lemmy_utils::error::LemmyResult;

View file

@ -87,6 +87,27 @@ pub struct CommentView {
pub my_vote: Option<i16>, pub my_vote: Option<i16>,
} }
#[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<i16>,
}
#[skip_serializing_none] #[skip_serializing_none]
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "full", derive(TS, Queryable))] #[cfg_attr(feature = "full", derive(TS, Queryable))]

View file

@ -46,9 +46,9 @@ use lemmy_db_schema::{
combined::inbox::{inbox_combined_keys as key, InboxCombined}, combined::inbox::{inbox_combined_keys as key, InboxCombined},
community::CommunityFollower, community::CommunityFollower,
}, },
traits::InternalToCombinedView,
utils::{actions, actions_alias, functions::coalesce, get_conn, DbPool}, utils::{actions, actions_alias, functions::coalesce, get_conn, DbPool},
InboxDataType, InboxDataType,
InternalToCombinedView,
}; };
use lemmy_utils::error::LemmyResult; use lemmy_utils::error::LemmyResult;

View file

@ -61,8 +61,8 @@ use lemmy_db_schema::{
post, post,
}, },
source::combined::modlog::{modlog_combined_keys as key, ModlogCombined}, source::combined::modlog::{modlog_combined_keys as key, ModlogCombined},
traits::InternalToCombinedView,
utils::{get_conn, DbPool}, utils::{get_conn, DbPool},
InternalToCombinedView,
ModlogActionType, ModlogActionType,
}; };
use lemmy_utils::error::LemmyResult; use lemmy_utils::error::LemmyResult;

View file

@ -142,7 +142,7 @@ use lemmy_api_crud::{
}, },
}; };
use lemmy_apub::api::{ use lemmy_apub::api::{
list_comments::list_comments, list_comments::{list_comments, list_comments_slim},
list_person_content::list_person_content, list_person_content::list_person_content,
list_posts::list_posts, list_posts::list_posts,
read_community::get_community, 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("/like/list", get().to(list_comment_likes))
.route("/save", put().to(save_comment)) .route("/save", put().to(save_comment))
.route("/list", get().to(list_comments)) .route("/list", get().to(list_comments))
.route("/list/slim", get().to(list_comments_slim))
.route("/report", post().to(create_comment_report)) .route("/report", post().to(create_comment_report))
.route("/report/resolve", put().to(resolve_comment_report)), .route("/report/resolve", put().to(resolve_comment_report)),
) )