Fixing migration and paged API.

This commit is contained in:
Dessalines 2024-12-02 13:28:26 -05:00
parent f25d34656f
commit 7fdbb58e98
7 changed files with 27 additions and 32 deletions

View file

@ -16,17 +16,23 @@ pub async fn list_reports(
local_user_view: LocalUserView, local_user_view: LocalUserView,
) -> LemmyResult<Json<ListReportsResponse>> { ) -> LemmyResult<Json<ListReportsResponse>> {
let community_id = data.community_id; 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?; check_community_mod_of_any_or_admin_action(&local_user_view, &mut context.pool()).await?;
let page = data.page; // parse pagination token
let limit = data.limit; 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 { let reports = ReportCombinedQuery {
community_id, community_id,
unresolved_only, unresolved_only,
page, page_after,
limit, page_back,
} }
.list(&mut context.pool(), &local_user_view) .list(&mut context.pool(), &local_user_view)
.await?; .await?;

View file

@ -116,6 +116,8 @@ pub struct GetPosts {
pub no_comments_only: Option<bool>, pub no_comments_only: Option<bool>,
#[cfg_attr(feature = "full", ts(optional))] #[cfg_attr(feature = "full", ts(optional))]
pub page_cursor: Option<PaginationCursor>, pub page_cursor: Option<PaginationCursor>,
#[cfg_attr(feature = "full", ts(optional))]
pub page_back: Option<bool>,
} }
#[skip_serializing_none] #[skip_serializing_none]

View file

@ -1,26 +1,26 @@
use lemmy_db_schema::newtypes::CommunityId; 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::{Deserialize, Serialize};
use serde_with::skip_serializing_none; use serde_with::skip_serializing_none;
#[cfg(feature = "full")] #[cfg(feature = "full")]
use ts_rs::TS; use ts_rs::TS;
#[skip_serializing_none] #[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", derive(TS))]
#[cfg_attr(feature = "full", ts(export))] #[cfg_attr(feature = "full", ts(export))]
/// List reports. /// List reports.
pub struct ListReports { pub struct ListReports {
#[cfg_attr(feature = "full", ts(optional))]
pub page: Option<i64>,
#[cfg_attr(feature = "full", ts(optional))]
pub limit: Option<i64>,
/// Only shows the unresolved reports /// Only shows the unresolved reports
#[cfg_attr(feature = "full", ts(optional))] #[cfg_attr(feature = "full", ts(optional))]
pub unresolved_only: Option<bool>, pub unresolved_only: Option<bool>,
/// if no community is given, it returns reports for all communities moderated by the auth user /// if no community is given, it returns reports for all communities moderated by the auth user
#[cfg_attr(feature = "full", ts(optional))] #[cfg_attr(feature = "full", ts(optional))]
pub community_id: Option<CommunityId>, pub community_id: Option<CommunityId>,
#[cfg_attr(feature = "full", ts(optional))]
pub page_cursor: Option<ReportCombinedPaginationCursor>,
#[cfg_attr(feature = "full", ts(optional))]
pub page_back: Option<bool>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]

View file

@ -43,15 +43,7 @@ use lemmy_db_schema::{
combined::report::{report_combined_keys as key, ReportCombined}, combined::report::{report_combined_keys as key, ReportCombined},
community::CommunityFollower, community::CommunityFollower,
}, },
utils::{ utils::{actions, actions_alias, functions::coalesce, get_conn, DbPool, ReverseTimestampKey},
actions,
actions_alias,
functions::coalesce,
get_conn,
limit_and_offset,
DbPool,
ReverseTimestampKey,
},
}; };
use lemmy_utils::error::LemmyResult; use lemmy_utils::error::LemmyResult;
@ -119,6 +111,7 @@ impl ReportCombinedPaginationCursor {
// hex encoding to prevent ossification // hex encoding to prevent ossification
ReportCombinedPaginationCursor(format!("{prefix}{id:x}")) ReportCombinedPaginationCursor(format!("{prefix}{id:x}"))
} }
pub async fn read(&self, pool: &mut DbPool<'_>) -> Result<PaginationCursorData, Error> { pub async fn read(&self, pool: &mut DbPool<'_>) -> Result<PaginationCursorData, Error> {
let err_msg = || Error::QueryBuilderError("Could not parse pagination token".into()); let err_msg = || Error::QueryBuilderError("Could not parse pagination token".into());
let mut query = report_combined::table let mut query = report_combined::table
@ -144,11 +137,9 @@ pub struct PaginationCursorData(ReportCombined);
#[derive(Default)] #[derive(Default)]
pub struct ReportCombinedQuery { pub struct ReportCombinedQuery {
pub community_id: Option<CommunityId>, pub community_id: Option<CommunityId>,
pub page: Option<i64>, pub unresolved_only: Option<bool>,
pub limit: Option<i64>,
pub unresolved_only: bool,
pub page_after: Option<PaginationCursorData>, pub page_after: Option<PaginationCursorData>,
pub page_back: bool, pub page_back: Option<bool>,
} }
impl ReportCombinedQuery { impl ReportCombinedQuery {
@ -291,15 +282,11 @@ impl ReportCombinedQuery {
query = query.filter(community_actions::became_moderator.is_not_null()); 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 mut query = PaginatedQueryBuilder::new(query);
let page_after = options.page_after.map(|c| c.0); 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(); query = query.before(page_after).limit_and_offset_from_end();
} else { } else {
query = query.after(page_after); 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 // If viewing all reports, order by newest, but if viewing unresolved only, show the oldest
// first (FIFO) // first (FIFO)
if options.unresolved_only { if options.unresolved_only.unwrap_or_default() {
query = query query = query
.filter( .filter(
post_report::resolved post_report::resolved
@ -601,7 +588,7 @@ mod tests {
// Do a batch read of timmys reports // Do a batch read of timmys reports
// It should only show saras, which is unresolved // It should only show saras, which is unresolved
let reports_after_resolve = ReportCombinedQuery { let reports_after_resolve = ReportCombinedQuery {
unresolved_only: true, unresolved_only: Some(true),
..Default::default() ..Default::default()
} }
.list(pool, &timmy_view) .list(pool, &timmy_view)

View file

@ -286,7 +286,7 @@ pub fn config(cfg: &mut ServiceConfig, rate_limit: &RateLimitCell) {
.service( .service(
scope("report") scope("report")
.wrap(rate_limit.message()) .wrap(rate_limit.message())
.route("/list", web::get().to(list_reports)), .route("/list", get().to(list_reports)),
) )
// Private Message // Private Message
.service( .service(