Skip to content

Commit

Permalink
Merge pull request #247 from LemmyNet/main
Browse files Browse the repository at this point in the history
[pull] master from LemmyNet:main
  • Loading branch information
pull[bot] authored Jan 23, 2025
2 parents a83db3a + 8ee81a3 commit 0d99330
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 26 deletions.
10 changes: 9 additions & 1 deletion crates/api_common/src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -144,6 +144,14 @@ pub struct GetCommentsResponse {
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]
#[derive(Debug, Serialize, Deserialize, Clone, Copy, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "full", derive(TS))]
Expand Down
37 changes: 31 additions & 6 deletions crates/apub/src/api/list_comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand All @@ -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<GetComments>,
context: Data<LemmyContext>,
local_user_view: Option<LocalUserView>,
) -> LemmyResult<Json<GetCommentsResponse>> {
) -> LemmyResult<Vec<CommentView>> {
let site_view = SiteView::read_local(&mut context.pool()).await?;
check_private_instance(&local_user_view, &site_view.local_site)?;

Expand Down Expand Up @@ -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,
Expand All @@ -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<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 }))
}

#[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 }))
}
7 changes: 0 additions & 7 deletions crates/db_schema/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self::CombinedView>;
}

/// Wrapper for assert_eq! macro. Checks that vec matches the given length, and prints the
/// vec on failure.
#[macro_export]
Expand Down
11 changes: 6 additions & 5 deletions crates/db_schema/src/schema_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use diesel::{
use diesel_migrations::MigrationHarness;
use lemmy_utils::{error::LemmyResult, settings::SETTINGS};
use std::time::Instant;
use tracing::log::debug;

diesel::table! {
pg_namespace (nspname) {
Expand Down Expand Up @@ -66,7 +67,7 @@ impl MigrationHarnessWrapper<'_> {
.map(|d| d.to_string())
.unwrap_or_default();
let name = migration.name();
println!("{duration} run {name}");
debug!("{duration} run {name}");

result
}
Expand Down Expand Up @@ -111,7 +112,7 @@ impl MigrationHarness<Pg> for MigrationHarnessWrapper<'_> {
.map(|d| d.to_string())
.unwrap_or_default();
let name = migration.name();
println!("{duration} revert {name}");
debug!("{duration} revert {name}");

result
}
Expand Down Expand Up @@ -191,9 +192,9 @@ pub fn run(options: Options) -> LemmyResult<Branch> {

// Block concurrent attempts to run migrations until `conn` is closed, and disable the
// trigger that prevents the Diesel CLI from running migrations
println!("Waiting for lock...");
debug!("Waiting for lock...");
conn.batch_execute("SELECT pg_advisory_lock(0);")?;
println!("Running Database migrations (This may take a long time)...");
debug!("Running Database migrations (This may take a long time)...");

// Drop `r` schema, so migrations don't need to be made to work both with and without things in
// it existing
Expand Down Expand Up @@ -226,7 +227,7 @@ pub fn run(options: Options) -> LemmyResult<Branch> {
Branch::ReplaceableSchemaNotRebuilt
};

println!("Database migrations complete.");
debug!("Database migrations complete.");

Ok(output)
}
Expand Down
7 changes: 7 additions & 0 deletions crates/db_schema/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self::CombinedView>;
}
18 changes: 17 additions & 1 deletion crates/db_views/src/comment_view.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::structs::CommentView;
use crate::structs::{CommentSlimView, CommentView};
use diesel::{
dsl::{exists, not},
pg::Pg,
Expand Down Expand Up @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion crates/db_views/src/person_content_combined_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion crates/db_views/src/person_saved_combined_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion crates/db_views/src/report_combined_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
21 changes: 21 additions & 0 deletions crates/db_views/src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,27 @@ pub struct CommentView {
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]
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "full", derive(TS, Queryable))]
Expand Down
2 changes: 1 addition & 1 deletion crates/db_views_actor/src/inbox_combined_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion crates/db_views_moderator/src/modlog_combined_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/api_routes_v4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)),
)
Expand Down

0 comments on commit 0d99330

Please sign in to comment.