From 7fdbb58e98b52a805f4221f4a80527e6e972052c Mon Sep 17 00:00:00 2001 From: Dessalines Date: Mon, 2 Dec 2024 13:28:26 -0500 Subject: [PATCH] Fixing migration and paged API. --- .../api/src/reports/report_combined/list.rs | 16 +++++++---- crates/api_common/src/post.rs | 2 ++ crates/api_common/src/reports/combined.rs | 12 ++++----- crates/db_views/src/report_combined_view.rs | 27 +++++-------------- .../down.sql | 0 .../up.sql | 0 src/api_routes_http.rs | 2 +- 7 files changed, 27 insertions(+), 32 deletions(-) rename migrations/{2024-11-26-115042_add_combined_tables => 2024-12-02-181601_add_combined_tables}/down.sql (100%) rename migrations/{2024-11-26-115042_add_combined_tables => 2024-12-02-181601_add_combined_tables}/up.sql (100%) diff --git a/crates/api/src/reports/report_combined/list.rs b/crates/api/src/reports/report_combined/list.rs index 7fab9919b..12548d189 100644 --- a/crates/api/src/reports/report_combined/list.rs +++ b/crates/api/src/reports/report_combined/list.rs @@ -16,17 +16,23 @@ pub async fn list_reports( local_user_view: LocalUserView, ) -> LemmyResult> { let community_id = data.community_id; - let unresolved_only = data.unresolved_only.unwrap_or_default(); + let unresolved_only = data.unresolved_only; check_community_mod_of_any_or_admin_action(&local_user_view, &mut context.pool()).await?; - let page = data.page; - let limit = data.limit; + // parse pagination token + let page_after = if let Some(pa) = &data.page_cursor { + Some(pa.read(&mut context.pool()).await?) + } else { + None + }; + let page_back = data.page_back; + let reports = ReportCombinedQuery { community_id, unresolved_only, - page, - limit, + page_after, + page_back, } .list(&mut context.pool(), &local_user_view) .await?; diff --git a/crates/api_common/src/post.rs b/crates/api_common/src/post.rs index 310c5c03e..8d8d3e080 100644 --- a/crates/api_common/src/post.rs +++ b/crates/api_common/src/post.rs @@ -116,6 +116,8 @@ pub struct GetPosts { pub no_comments_only: Option, #[cfg_attr(feature = "full", ts(optional))] pub page_cursor: Option, + #[cfg_attr(feature = "full", ts(optional))] + pub page_back: Option, } #[skip_serializing_none] diff --git a/crates/api_common/src/reports/combined.rs b/crates/api_common/src/reports/combined.rs index 17d6a0505..69d928830 100644 --- a/crates/api_common/src/reports/combined.rs +++ b/crates/api_common/src/reports/combined.rs @@ -1,26 +1,26 @@ use lemmy_db_schema::newtypes::CommunityId; -use lemmy_db_views::structs::ReportCombinedView; +use lemmy_db_views::structs::{ReportCombinedPaginationCursor, ReportCombinedView}; use serde::{Deserialize, Serialize}; use serde_with::skip_serializing_none; #[cfg(feature = "full")] use ts_rs::TS; #[skip_serializing_none] -#[derive(Debug, Serialize, Deserialize, Clone, Copy, Default, PartialEq, Eq, Hash)] +#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq, Eq, Hash)] #[cfg_attr(feature = "full", derive(TS))] #[cfg_attr(feature = "full", ts(export))] /// List reports. pub struct ListReports { - #[cfg_attr(feature = "full", ts(optional))] - pub page: Option, - #[cfg_attr(feature = "full", ts(optional))] - pub limit: Option, /// Only shows the unresolved reports #[cfg_attr(feature = "full", ts(optional))] pub unresolved_only: Option, /// if no community is given, it returns reports for all communities moderated by the auth user #[cfg_attr(feature = "full", ts(optional))] pub community_id: Option, + #[cfg_attr(feature = "full", ts(optional))] + pub page_cursor: Option, + #[cfg_attr(feature = "full", ts(optional))] + pub page_back: Option, } #[derive(Debug, Serialize, Deserialize, Clone)] diff --git a/crates/db_views/src/report_combined_view.rs b/crates/db_views/src/report_combined_view.rs index 4d0d3e342..55bf533a9 100644 --- a/crates/db_views/src/report_combined_view.rs +++ b/crates/db_views/src/report_combined_view.rs @@ -43,15 +43,7 @@ use lemmy_db_schema::{ combined::report::{report_combined_keys as key, ReportCombined}, community::CommunityFollower, }, - utils::{ - actions, - actions_alias, - functions::coalesce, - get_conn, - limit_and_offset, - DbPool, - ReverseTimestampKey, - }, + utils::{actions, actions_alias, functions::coalesce, get_conn, DbPool, ReverseTimestampKey}, }; use lemmy_utils::error::LemmyResult; @@ -119,6 +111,7 @@ impl ReportCombinedPaginationCursor { // hex encoding to prevent ossification ReportCombinedPaginationCursor(format!("{prefix}{id:x}")) } + pub async fn read(&self, pool: &mut DbPool<'_>) -> Result { let err_msg = || Error::QueryBuilderError("Could not parse pagination token".into()); let mut query = report_combined::table @@ -144,11 +137,9 @@ pub struct PaginationCursorData(ReportCombined); #[derive(Default)] pub struct ReportCombinedQuery { pub community_id: Option, - pub page: Option, - pub limit: Option, - pub unresolved_only: bool, + pub unresolved_only: Option, pub page_after: Option, - pub page_back: bool, + pub page_back: Option, } impl ReportCombinedQuery { @@ -291,15 +282,11 @@ impl ReportCombinedQuery { query = query.filter(community_actions::became_moderator.is_not_null()); } - let (limit, offset) = limit_and_offset(options.page, options.limit)?; - - query = query.limit(limit).offset(offset); - let mut query = PaginatedQueryBuilder::new(query); let page_after = options.page_after.map(|c| c.0); - if options.page_back { + if options.page_back.unwrap_or_default() { query = query.before(page_after).limit_and_offset_from_end(); } else { query = query.after(page_after); @@ -307,7 +294,7 @@ impl ReportCombinedQuery { // If viewing all reports, order by newest, but if viewing unresolved only, show the oldest // first (FIFO) - if options.unresolved_only { + if options.unresolved_only.unwrap_or_default() { query = query .filter( post_report::resolved @@ -601,7 +588,7 @@ mod tests { // Do a batch read of timmys reports // It should only show saras, which is unresolved let reports_after_resolve = ReportCombinedQuery { - unresolved_only: true, + unresolved_only: Some(true), ..Default::default() } .list(pool, &timmy_view) diff --git a/migrations/2024-11-26-115042_add_combined_tables/down.sql b/migrations/2024-12-02-181601_add_combined_tables/down.sql similarity index 100% rename from migrations/2024-11-26-115042_add_combined_tables/down.sql rename to migrations/2024-12-02-181601_add_combined_tables/down.sql diff --git a/migrations/2024-11-26-115042_add_combined_tables/up.sql b/migrations/2024-12-02-181601_add_combined_tables/up.sql similarity index 100% rename from migrations/2024-11-26-115042_add_combined_tables/up.sql rename to migrations/2024-12-02-181601_add_combined_tables/up.sql diff --git a/src/api_routes_http.rs b/src/api_routes_http.rs index baf45f5f9..452fb9870 100644 --- a/src/api_routes_http.rs +++ b/src/api_routes_http.rs @@ -286,7 +286,7 @@ pub fn config(cfg: &mut ServiceConfig, rate_limit: &RateLimitCell) { .service( scope("report") .wrap(rate_limit.message()) - .route("/list", web::get().to(list_reports)), + .route("/list", get().to(list_reports)), ) // Private Message .service(