mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-12-13 14:12:09 +00:00
add pagination cursor
This commit is contained in:
parent
d8dda44010
commit
c068cf6f30
|
@ -89,19 +89,19 @@ pub struct PersonMentionId(i32);
|
||||||
#[cfg_attr(feature = "full", derive(DieselNewType, TS))]
|
#[cfg_attr(feature = "full", derive(DieselNewType, TS))]
|
||||||
#[cfg_attr(feature = "full", ts(export))]
|
#[cfg_attr(feature = "full", ts(export))]
|
||||||
/// The comment report id.
|
/// The comment report id.
|
||||||
pub struct CommentReportId(i32);
|
pub struct CommentReportId(pub i32);
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
|
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
|
||||||
#[cfg_attr(feature = "full", derive(DieselNewType, TS))]
|
#[cfg_attr(feature = "full", derive(DieselNewType, TS))]
|
||||||
#[cfg_attr(feature = "full", ts(export))]
|
#[cfg_attr(feature = "full", ts(export))]
|
||||||
/// The post report id.
|
/// The post report id.
|
||||||
pub struct PostReportId(i32);
|
pub struct PostReportId(pub i32);
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
|
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
|
||||||
#[cfg_attr(feature = "full", derive(DieselNewType, TS))]
|
#[cfg_attr(feature = "full", derive(DieselNewType, TS))]
|
||||||
#[cfg_attr(feature = "full", ts(export))]
|
#[cfg_attr(feature = "full", ts(export))]
|
||||||
/// The private message report id.
|
/// The private message report id.
|
||||||
pub struct PrivateMessageReportId(i32);
|
pub struct PrivateMessageReportId(pub i32);
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
|
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
|
||||||
#[cfg_attr(feature = "full", derive(DieselNewType, TS))]
|
#[cfg_attr(feature = "full", derive(DieselNewType, TS))]
|
||||||
|
|
|
@ -3,6 +3,7 @@ use crate::structs::{
|
||||||
LocalUserView,
|
LocalUserView,
|
||||||
PostReportView,
|
PostReportView,
|
||||||
PrivateMessageReportView,
|
PrivateMessageReportView,
|
||||||
|
ReportCombinedPaginationCursor,
|
||||||
ReportCombinedView,
|
ReportCombinedView,
|
||||||
ReportCombinedViewInternal,
|
ReportCombinedViewInternal,
|
||||||
};
|
};
|
||||||
|
@ -13,6 +14,7 @@ use diesel::{
|
||||||
JoinOnDsl,
|
JoinOnDsl,
|
||||||
NullableExpressionMethods,
|
NullableExpressionMethods,
|
||||||
QueryDsl,
|
QueryDsl,
|
||||||
|
SelectableHelper,
|
||||||
};
|
};
|
||||||
use diesel_async::RunQueryDsl;
|
use diesel_async::RunQueryDsl;
|
||||||
use lemmy_db_schema::{
|
use lemmy_db_schema::{
|
||||||
|
@ -36,7 +38,7 @@ use lemmy_db_schema::{
|
||||||
private_message_report,
|
private_message_report,
|
||||||
report_combined,
|
report_combined,
|
||||||
},
|
},
|
||||||
source::community::CommunityFollower,
|
source::{combined::report::ReportCombined, community::CommunityFollower},
|
||||||
utils::{actions, actions_alias, functions::coalesce, get_conn, limit_and_offset, DbPool},
|
utils::{actions, actions_alias, functions::coalesce, get_conn, limit_and_offset, DbPool},
|
||||||
};
|
};
|
||||||
use lemmy_utils::error::LemmyResult;
|
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)]
|
#[derive(Default)]
|
||||||
pub struct ReportCombinedQuery {
|
pub struct ReportCombinedQuery {
|
||||||
pub community_id: Option<CommunityId>,
|
pub community_id: Option<CommunityId>,
|
||||||
|
|
|
@ -126,6 +126,12 @@ pub struct PostReportView {
|
||||||
#[cfg_attr(feature = "full", ts(export))]
|
#[cfg_attr(feature = "full", ts(export))]
|
||||||
pub struct PaginationCursor(pub String);
|
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]
|
#[skip_serializing_none]
|
||||||
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
|
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
|
||||||
#[cfg_attr(feature = "full", derive(TS, Queryable))]
|
#[cfg_attr(feature = "full", derive(TS, Queryable))]
|
||||||
|
|
Loading…
Reference in a new issue