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,
) -> LemmyResult<Json<ListReportsResponse>> {
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?;

View file

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

View file

@ -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<i64>,
#[cfg_attr(feature = "full", ts(optional))]
pub limit: Option<i64>,
/// Only shows the unresolved reports
#[cfg_attr(feature = "full", ts(optional))]
pub unresolved_only: Option<bool>,
/// 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<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)]

View file

@ -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<PaginationCursorData, Error> {
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<CommunityId>,
pub page: Option<i64>,
pub limit: Option<i64>,
pub unresolved_only: bool,
pub unresolved_only: Option<bool>,
pub page_after: Option<PaginationCursorData>,
pub page_back: bool,
pub page_back: Option<bool>,
}
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)

View file

@ -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(