1
0
mirror of https://github.com/matrix-org/matrix-authentication-service.git synced 2025-07-29 22:01:14 +03:00

storage: don't use references for pagination

This commit is contained in:
Quentin Gliech
2023-01-17 16:44:22 +01:00
parent 5e32c218d5
commit b33a330b5f
10 changed files with 34 additions and 33 deletions

View File

@ -68,7 +68,7 @@ pub trait CompatSsoLoginRepository: Send + Sync {
async fn list_paginated(
&mut self,
user: &User,
pagination: &Pagination,
pagination: Pagination,
) -> Result<Page<CompatSsoLogin>, Self::Error>;
}
@ -351,7 +351,7 @@ impl<'c> CompatSsoLoginRepository for PgCompatSsoLoginRepository<'c> {
async fn list_paginated(
&mut self,
user: &User,
pagination: &Pagination,
pagination: Pagination,
) -> Result<Page<CompatSsoLogin>, Self::Error> {
let mut query = QueryBuilder::new(
r#"
@ -371,7 +371,7 @@ impl<'c> CompatSsoLoginRepository for PgCompatSsoLoginRepository<'c> {
query
.push(" WHERE user_id = ")
.push_bind(Uuid::from(user.id))
.generate_pagination("cl.compat_sso_login_id", &pagination);
.generate_pagination("cl.compat_sso_login_id", pagination);
let edges: Vec<CompatSsoLoginLookup> = query
.build_query_as()

View File

@ -45,7 +45,7 @@ pub trait OAuth2SessionRepository: Send + Sync {
async fn list_paginated(
&mut self,
user: &User,
pagination: &Pagination,
pagination: Pagination,
) -> Result<Page<Session>, Self::Error>;
}
@ -240,7 +240,7 @@ impl<'c> OAuth2SessionRepository for PgOAuth2SessionRepository<'c> {
async fn list_paginated(
&mut self,
user: &User,
pagination: &Pagination,
pagination: Pagination,
) -> Result<Page<Session>, Self::Error> {
let mut query = QueryBuilder::new(
r#"

View File

@ -66,6 +66,7 @@ impl Pagination {
}
/// Creates a [`Pagination`] which gets the first N items
#[must_use]
pub const fn first(first: usize) -> Self {
Self {
before: None,
@ -76,6 +77,7 @@ impl Pagination {
}
/// Creates a [`Pagination`] which gets the last N items
#[must_use]
pub const fn last(last: usize) -> Self {
Self {
before: None,
@ -86,12 +88,14 @@ impl Pagination {
}
/// Get items before the given cursor
#[must_use]
pub const fn before(mut self, id: Ulid) -> Self {
self.before = Some(id);
self
}
/// Get items after the given cursor
#[must_use]
pub const fn after(mut self, id: Ulid) -> Self {
self.after = Some(id);
self
@ -181,6 +185,7 @@ pub struct Page<T> {
}
impl<T> Page<T> {
#[must_use]
pub fn map<F, T2>(self, f: F) -> Page<T2>
where
F: FnMut(T) -> T2,
@ -193,6 +198,7 @@ impl<T> Page<T> {
}
}
#[must_use]
pub fn try_map<F, E, T2>(self, f: F) -> Result<Page<T2>, E>
where
F: FnMut(T) -> Result<T2, E>,
@ -211,8 +217,7 @@ impl<T> Page<T> {
pub trait QueryBuilderExt {
/// Add cursor-based pagination to a query, as used in paginated GraphQL
/// connections
fn generate_pagination(&mut self, id_field: &'static str, pagination: &Pagination)
-> &mut Self;
fn generate_pagination(&mut self, id_field: &'static str, pagination: Pagination) -> &mut Self;
}
impl<'a, DB> QueryBuilderExt for QueryBuilder<'a, DB>
@ -221,11 +226,7 @@ where
Uuid: sqlx::Type<DB> + sqlx::Encode<'a, DB>,
i64: sqlx::Type<DB> + sqlx::Encode<'a, DB>,
{
fn generate_pagination(
&mut self,
id_field: &'static str,
pagination: &Pagination,
) -> &mut Self {
fn generate_pagination(&mut self, id_field: &'static str, pagination: Pagination) -> &mut Self {
pagination.generate_pagination(self, id_field);
self
}

View File

@ -60,7 +60,7 @@ pub trait UpstreamOAuthLinkRepository: Send + Sync {
async fn list_paginated(
&mut self,
user: &User,
pagination: &Pagination,
pagination: Pagination,
) -> Result<Page<UpstreamOAuthLink>, Self::Error>;
}
@ -272,7 +272,7 @@ impl<'c> UpstreamOAuthLinkRepository for PgUpstreamOAuthLinkRepository<'c> {
async fn list_paginated(
&mut self,
user: &User,
pagination: &Pagination,
pagination: Pagination,
) -> Result<Page<UpstreamOAuthLink>, Self::Error> {
let mut query = QueryBuilder::new(
r#"

View File

@ -161,7 +161,7 @@ mod tests {
let links = repo
.upstream_oauth_link()
.list_paginated(&user, &Pagination::first(10))
.list_paginated(&user, Pagination::first(10))
.await
.unwrap();
assert!(!links.has_previous_page);
@ -205,7 +205,7 @@ mod tests {
// Lookup the first 10 items
let page = repo
.upstream_oauth_provider()
.list_paginated(&Pagination::first(10))
.list_paginated(Pagination::first(10))
.await
.unwrap();
@ -217,7 +217,7 @@ mod tests {
// Lookup the next 10 items
let page = repo
.upstream_oauth_provider()
.list_paginated(&Pagination::first(10).after(ids[9]))
.list_paginated(Pagination::first(10).after(ids[9]))
.await
.unwrap();
@ -229,7 +229,7 @@ mod tests {
// Lookup the last 10 items
let page = repo
.upstream_oauth_provider()
.list_paginated(&Pagination::last(10))
.list_paginated(Pagination::last(10))
.await
.unwrap();
@ -241,7 +241,7 @@ mod tests {
// Lookup the previous 10 items
let page = repo
.upstream_oauth_provider()
.list_paginated(&Pagination::last(10).before(ids[10]))
.list_paginated(Pagination::last(10).before(ids[10]))
.await
.unwrap();
@ -253,7 +253,7 @@ mod tests {
// Lookup 10 items between two IDs
let page = repo
.upstream_oauth_provider()
.list_paginated(&Pagination::first(10).after(ids[5]).before(ids[8]))
.list_paginated(Pagination::first(10).after(ids[5]).before(ids[8]))
.await
.unwrap();

View File

@ -52,7 +52,7 @@ pub trait UpstreamOAuthProviderRepository: Send + Sync {
/// Get a paginated list of upstream OAuth providers
async fn list_paginated(
&mut self,
pagination: &Pagination,
pagination: Pagination,
) -> Result<Page<UpstreamOAuthProvider>, Self::Error>;
/// Get all upstream OAuth providers
@ -240,7 +240,7 @@ impl<'c> UpstreamOAuthProviderRepository for PgUpstreamOAuthProviderRepository<'
)]
async fn list_paginated(
&mut self,
pagination: &Pagination,
pagination: Pagination,
) -> Result<Page<UpstreamOAuthProvider>, Self::Error> {
let mut query = QueryBuilder::new(
r#"

View File

@ -39,7 +39,7 @@ pub trait UserEmailRepository: Send + Sync {
async fn list_paginated(
&mut self,
user: &User,
pagination: &Pagination,
pagination: Pagination,
) -> Result<Page<UserEmail>, Self::Error>;
async fn count(&mut self, user: &User) -> Result<usize, Self::Error>;
@ -286,7 +286,7 @@ impl<'c> UserEmailRepository for PgUserEmailRepository<'c> {
async fn list_paginated(
&mut self,
user: &User,
pagination: &Pagination,
pagination: Pagination,
) -> Result<Page<UserEmail>, DatabaseError> {
let mut query = QueryBuilder::new(
r#"
@ -302,7 +302,7 @@ impl<'c> UserEmailRepository for PgUserEmailRepository<'c> {
query
.push(" WHERE user_id = ")
.push_bind(Uuid::from(user.id))
.generate_pagination("ue.user_email_id", &pagination);
.generate_pagination("ue.user_email_id", pagination);
let edges: Vec<UserEmailLookup> = query
.build_query_as()

View File

@ -45,7 +45,7 @@ pub trait BrowserSessionRepository: Send + Sync {
async fn list_active_paginated(
&mut self,
user: &User,
pagination: &Pagination,
pagination: Pagination,
) -> Result<Page<BrowserSession>, Self::Error>;
async fn count_active(&mut self, user: &User) -> Result<usize, Self::Error>;
@ -261,7 +261,7 @@ impl<'c> BrowserSessionRepository for PgBrowserSessionRepository<'c> {
async fn list_active_paginated(
&mut self,
user: &User,
pagination: &Pagination,
pagination: Pagination,
) -> Result<Page<BrowserSession>, Self::Error> {
// TODO: ordering of last authentication is wrong
let mut query = QueryBuilder::new(