add pagination cursor

This commit is contained in:
Dull Bananas 2024-11-30 21:03:51 -07:00
parent d8dda44010
commit c068cf6f30
3 changed files with 45 additions and 4 deletions

View file

@ -89,19 +89,19 @@ pub struct PersonMentionId(i32);
#[cfg_attr(feature = "full", derive(DieselNewType, TS))]
#[cfg_attr(feature = "full", ts(export))]
/// The comment report id.
pub struct CommentReportId(i32);
pub struct CommentReportId(pub i32);
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
#[cfg_attr(feature = "full", derive(DieselNewType, TS))]
#[cfg_attr(feature = "full", ts(export))]
/// The post report id.
pub struct PostReportId(i32);
pub struct PostReportId(pub i32);
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
#[cfg_attr(feature = "full", derive(DieselNewType, TS))]
#[cfg_attr(feature = "full", ts(export))]
/// The private message report id.
pub struct PrivateMessageReportId(i32);
pub struct PrivateMessageReportId(pub i32);
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
#[cfg_attr(feature = "full", derive(DieselNewType, TS))]

View file

@ -3,6 +3,7 @@ use crate::structs::{
LocalUserView,
PostReportView,
PrivateMessageReportView,
ReportCombinedPaginationCursor,
ReportCombinedView,
ReportCombinedViewInternal,
};
@ -13,6 +14,7 @@ use diesel::{
JoinOnDsl,
NullableExpressionMethods,
QueryDsl,
SelectableHelper,
};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
@ -36,7 +38,7 @@ use lemmy_db_schema::{
private_message_report,
report_combined,
},
source::community::CommunityFollower,
source::{combined::report::ReportCombined, community::CommunityFollower},
utils::{actions, actions_alias, functions::coalesce, get_conn, limit_and_offset, DbPool},
};
use lemmy_utils::error::LemmyResult;
@ -94,6 +96,39 @@ impl ReportCombinedViewInternal {
}
}
impl ReportCombinedPaginationCursor {
// get cursor for page that starts immediately after the given post
pub fn after_post(view: &ReportCombinedView) -> ReportCombinedPaginationCursor {
let (prefix, id) = match view {
ReportCombinedView::Comment(v) => ('C', v.comment_report.id.0),
ReportCombinedView::Post(v) => ('P', v.post_report.id.0),
ReportCombinedView::PrivateMessage(v) => ('M', v.private_message_report.id.0),
};
// 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
.select(ReportCombined::as_select())
.into_boxed();
let (prefix, id_str) = self.0.split_at_checked(1).ok_or_else(err_msg)?;
let id = i32::from_str_radix(id_str, 16).map_err(|_| err_msg())?;
query = match prefix {
"C" => query.filter(report_combined::comment_report_id.eq(id)),
"P" => query.filter(report_combined::post_report_id.eq(id)),
"M" => query.filter(report_combined::private_message_report_id.eq(id)),
_ => return Err(err_msg()),
};
let token = query.first(&mut get_conn(pool).await?).await?;
Ok(PaginationCursorData(token))
}
}
#[derive(Clone)]
pub struct PaginationCursorData(ReportCombined);
#[derive(Default)]
pub struct ReportCombinedQuery {
pub community_id: Option<CommunityId>,

View file

@ -126,6 +126,12 @@ pub struct PostReportView {
#[cfg_attr(feature = "full", ts(export))]
pub struct PaginationCursor(pub String);
/// like PaginationCursor but for the report_combined table
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "full", derive(ts_rs::TS))]
#[cfg_attr(feature = "full", ts(export))]
pub struct ReportCombinedPaginationCursor(pub String);
#[skip_serializing_none]
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "full", derive(TS, Queryable))]