You've already forked authentication-service
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:
@ -232,7 +232,7 @@ impl RootQuery {
|
|||||||
|
|
||||||
let page = repo
|
let page = repo
|
||||||
.upstream_oauth_provider()
|
.upstream_oauth_provider()
|
||||||
.list_paginated(&pagination)
|
.list_paginated(pagination)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let mut connection = Connection::new(page.has_previous_page, page.has_next_page);
|
let mut connection = Connection::new(page.has_previous_page, page.has_next_page);
|
||||||
|
@ -99,7 +99,7 @@ impl User {
|
|||||||
|
|
||||||
let page = repo
|
let page = repo
|
||||||
.compat_sso_login()
|
.compat_sso_login()
|
||||||
.list_paginated(&self.0, &pagination)
|
.list_paginated(&self.0, pagination)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let mut connection = Connection::new(page.has_previous_page, page.has_next_page);
|
let mut connection = Connection::new(page.has_previous_page, page.has_next_page);
|
||||||
@ -146,7 +146,7 @@ impl User {
|
|||||||
|
|
||||||
let page = repo
|
let page = repo
|
||||||
.browser_session()
|
.browser_session()
|
||||||
.list_active_paginated(&self.0, &pagination)
|
.list_active_paginated(&self.0, pagination)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let mut connection = Connection::new(page.has_previous_page, page.has_next_page);
|
let mut connection = Connection::new(page.has_previous_page, page.has_next_page);
|
||||||
@ -193,7 +193,7 @@ impl User {
|
|||||||
|
|
||||||
let page = repo
|
let page = repo
|
||||||
.user_email()
|
.user_email()
|
||||||
.list_paginated(&self.0, &pagination)
|
.list_paginated(&self.0, pagination)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let mut connection = Connection::with_additional_fields(
|
let mut connection = Connection::with_additional_fields(
|
||||||
@ -244,7 +244,7 @@ impl User {
|
|||||||
|
|
||||||
let page = repo
|
let page = repo
|
||||||
.oauth2_session()
|
.oauth2_session()
|
||||||
.list_paginated(&self.0, &pagination)
|
.list_paginated(&self.0, pagination)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let mut connection = Connection::new(page.has_previous_page, page.has_next_page);
|
let mut connection = Connection::new(page.has_previous_page, page.has_next_page);
|
||||||
@ -295,7 +295,7 @@ impl User {
|
|||||||
|
|
||||||
let page = repo
|
let page = repo
|
||||||
.upstream_oauth_link()
|
.upstream_oauth_link()
|
||||||
.list_paginated(&self.0, &pagination)
|
.list_paginated(&self.0, pagination)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let mut connection = Connection::new(page.has_previous_page, page.has_next_page);
|
let mut connection = Connection::new(page.has_previous_page, page.has_next_page);
|
||||||
|
@ -68,7 +68,7 @@ pub trait CompatSsoLoginRepository: Send + Sync {
|
|||||||
async fn list_paginated(
|
async fn list_paginated(
|
||||||
&mut self,
|
&mut self,
|
||||||
user: &User,
|
user: &User,
|
||||||
pagination: &Pagination,
|
pagination: Pagination,
|
||||||
) -> Result<Page<CompatSsoLogin>, Self::Error>;
|
) -> Result<Page<CompatSsoLogin>, Self::Error>;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -351,7 +351,7 @@ impl<'c> CompatSsoLoginRepository for PgCompatSsoLoginRepository<'c> {
|
|||||||
async fn list_paginated(
|
async fn list_paginated(
|
||||||
&mut self,
|
&mut self,
|
||||||
user: &User,
|
user: &User,
|
||||||
pagination: &Pagination,
|
pagination: Pagination,
|
||||||
) -> Result<Page<CompatSsoLogin>, Self::Error> {
|
) -> Result<Page<CompatSsoLogin>, Self::Error> {
|
||||||
let mut query = QueryBuilder::new(
|
let mut query = QueryBuilder::new(
|
||||||
r#"
|
r#"
|
||||||
@ -371,7 +371,7 @@ impl<'c> CompatSsoLoginRepository for PgCompatSsoLoginRepository<'c> {
|
|||||||
query
|
query
|
||||||
.push(" WHERE user_id = ")
|
.push(" WHERE user_id = ")
|
||||||
.push_bind(Uuid::from(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
|
let edges: Vec<CompatSsoLoginLookup> = query
|
||||||
.build_query_as()
|
.build_query_as()
|
||||||
|
@ -45,7 +45,7 @@ pub trait OAuth2SessionRepository: Send + Sync {
|
|||||||
async fn list_paginated(
|
async fn list_paginated(
|
||||||
&mut self,
|
&mut self,
|
||||||
user: &User,
|
user: &User,
|
||||||
pagination: &Pagination,
|
pagination: Pagination,
|
||||||
) -> Result<Page<Session>, Self::Error>;
|
) -> Result<Page<Session>, Self::Error>;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -240,7 +240,7 @@ impl<'c> OAuth2SessionRepository for PgOAuth2SessionRepository<'c> {
|
|||||||
async fn list_paginated(
|
async fn list_paginated(
|
||||||
&mut self,
|
&mut self,
|
||||||
user: &User,
|
user: &User,
|
||||||
pagination: &Pagination,
|
pagination: Pagination,
|
||||||
) -> Result<Page<Session>, Self::Error> {
|
) -> Result<Page<Session>, Self::Error> {
|
||||||
let mut query = QueryBuilder::new(
|
let mut query = QueryBuilder::new(
|
||||||
r#"
|
r#"
|
||||||
|
@ -66,6 +66,7 @@ impl Pagination {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a [`Pagination`] which gets the first N items
|
/// Creates a [`Pagination`] which gets the first N items
|
||||||
|
#[must_use]
|
||||||
pub const fn first(first: usize) -> Self {
|
pub const fn first(first: usize) -> Self {
|
||||||
Self {
|
Self {
|
||||||
before: None,
|
before: None,
|
||||||
@ -76,6 +77,7 @@ impl Pagination {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a [`Pagination`] which gets the last N items
|
/// Creates a [`Pagination`] which gets the last N items
|
||||||
|
#[must_use]
|
||||||
pub const fn last(last: usize) -> Self {
|
pub const fn last(last: usize) -> Self {
|
||||||
Self {
|
Self {
|
||||||
before: None,
|
before: None,
|
||||||
@ -86,12 +88,14 @@ impl Pagination {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Get items before the given cursor
|
/// Get items before the given cursor
|
||||||
|
#[must_use]
|
||||||
pub const fn before(mut self, id: Ulid) -> Self {
|
pub const fn before(mut self, id: Ulid) -> Self {
|
||||||
self.before = Some(id);
|
self.before = Some(id);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get items after the given cursor
|
/// Get items after the given cursor
|
||||||
|
#[must_use]
|
||||||
pub const fn after(mut self, id: Ulid) -> Self {
|
pub const fn after(mut self, id: Ulid) -> Self {
|
||||||
self.after = Some(id);
|
self.after = Some(id);
|
||||||
self
|
self
|
||||||
@ -181,6 +185,7 @@ pub struct Page<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Page<T> {
|
impl<T> Page<T> {
|
||||||
|
#[must_use]
|
||||||
pub fn map<F, T2>(self, f: F) -> Page<T2>
|
pub fn map<F, T2>(self, f: F) -> Page<T2>
|
||||||
where
|
where
|
||||||
F: FnMut(T) -> T2,
|
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>
|
pub fn try_map<F, E, T2>(self, f: F) -> Result<Page<T2>, E>
|
||||||
where
|
where
|
||||||
F: FnMut(T) -> Result<T2, E>,
|
F: FnMut(T) -> Result<T2, E>,
|
||||||
@ -211,8 +217,7 @@ impl<T> Page<T> {
|
|||||||
pub trait QueryBuilderExt {
|
pub trait QueryBuilderExt {
|
||||||
/// Add cursor-based pagination to a query, as used in paginated GraphQL
|
/// Add cursor-based pagination to a query, as used in paginated GraphQL
|
||||||
/// connections
|
/// connections
|
||||||
fn generate_pagination(&mut self, id_field: &'static str, pagination: &Pagination)
|
fn generate_pagination(&mut self, id_field: &'static str, pagination: Pagination) -> &mut Self;
|
||||||
-> &mut Self;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, DB> QueryBuilderExt for QueryBuilder<'a, DB>
|
impl<'a, DB> QueryBuilderExt for QueryBuilder<'a, DB>
|
||||||
@ -221,11 +226,7 @@ where
|
|||||||
Uuid: sqlx::Type<DB> + sqlx::Encode<'a, DB>,
|
Uuid: sqlx::Type<DB> + sqlx::Encode<'a, DB>,
|
||||||
i64: sqlx::Type<DB> + sqlx::Encode<'a, DB>,
|
i64: sqlx::Type<DB> + sqlx::Encode<'a, DB>,
|
||||||
{
|
{
|
||||||
fn generate_pagination(
|
fn generate_pagination(&mut self, id_field: &'static str, pagination: Pagination) -> &mut Self {
|
||||||
&mut self,
|
|
||||||
id_field: &'static str,
|
|
||||||
pagination: &Pagination,
|
|
||||||
) -> &mut Self {
|
|
||||||
pagination.generate_pagination(self, id_field);
|
pagination.generate_pagination(self, id_field);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
@ -60,7 +60,7 @@ pub trait UpstreamOAuthLinkRepository: Send + Sync {
|
|||||||
async fn list_paginated(
|
async fn list_paginated(
|
||||||
&mut self,
|
&mut self,
|
||||||
user: &User,
|
user: &User,
|
||||||
pagination: &Pagination,
|
pagination: Pagination,
|
||||||
) -> Result<Page<UpstreamOAuthLink>, Self::Error>;
|
) -> Result<Page<UpstreamOAuthLink>, Self::Error>;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -272,7 +272,7 @@ impl<'c> UpstreamOAuthLinkRepository for PgUpstreamOAuthLinkRepository<'c> {
|
|||||||
async fn list_paginated(
|
async fn list_paginated(
|
||||||
&mut self,
|
&mut self,
|
||||||
user: &User,
|
user: &User,
|
||||||
pagination: &Pagination,
|
pagination: Pagination,
|
||||||
) -> Result<Page<UpstreamOAuthLink>, Self::Error> {
|
) -> Result<Page<UpstreamOAuthLink>, Self::Error> {
|
||||||
let mut query = QueryBuilder::new(
|
let mut query = QueryBuilder::new(
|
||||||
r#"
|
r#"
|
||||||
|
@ -161,7 +161,7 @@ mod tests {
|
|||||||
|
|
||||||
let links = repo
|
let links = repo
|
||||||
.upstream_oauth_link()
|
.upstream_oauth_link()
|
||||||
.list_paginated(&user, &Pagination::first(10))
|
.list_paginated(&user, Pagination::first(10))
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert!(!links.has_previous_page);
|
assert!(!links.has_previous_page);
|
||||||
@ -205,7 +205,7 @@ mod tests {
|
|||||||
// Lookup the first 10 items
|
// Lookup the first 10 items
|
||||||
let page = repo
|
let page = repo
|
||||||
.upstream_oauth_provider()
|
.upstream_oauth_provider()
|
||||||
.list_paginated(&Pagination::first(10))
|
.list_paginated(Pagination::first(10))
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
@ -217,7 +217,7 @@ mod tests {
|
|||||||
// Lookup the next 10 items
|
// Lookup the next 10 items
|
||||||
let page = repo
|
let page = repo
|
||||||
.upstream_oauth_provider()
|
.upstream_oauth_provider()
|
||||||
.list_paginated(&Pagination::first(10).after(ids[9]))
|
.list_paginated(Pagination::first(10).after(ids[9]))
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
@ -229,7 +229,7 @@ mod tests {
|
|||||||
// Lookup the last 10 items
|
// Lookup the last 10 items
|
||||||
let page = repo
|
let page = repo
|
||||||
.upstream_oauth_provider()
|
.upstream_oauth_provider()
|
||||||
.list_paginated(&Pagination::last(10))
|
.list_paginated(Pagination::last(10))
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
@ -241,7 +241,7 @@ mod tests {
|
|||||||
// Lookup the previous 10 items
|
// Lookup the previous 10 items
|
||||||
let page = repo
|
let page = repo
|
||||||
.upstream_oauth_provider()
|
.upstream_oauth_provider()
|
||||||
.list_paginated(&Pagination::last(10).before(ids[10]))
|
.list_paginated(Pagination::last(10).before(ids[10]))
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
@ -253,7 +253,7 @@ mod tests {
|
|||||||
// Lookup 10 items between two IDs
|
// Lookup 10 items between two IDs
|
||||||
let page = repo
|
let page = repo
|
||||||
.upstream_oauth_provider()
|
.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
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
@ -52,7 +52,7 @@ pub trait UpstreamOAuthProviderRepository: Send + Sync {
|
|||||||
/// Get a paginated list of upstream OAuth providers
|
/// Get a paginated list of upstream OAuth providers
|
||||||
async fn list_paginated(
|
async fn list_paginated(
|
||||||
&mut self,
|
&mut self,
|
||||||
pagination: &Pagination,
|
pagination: Pagination,
|
||||||
) -> Result<Page<UpstreamOAuthProvider>, Self::Error>;
|
) -> Result<Page<UpstreamOAuthProvider>, Self::Error>;
|
||||||
|
|
||||||
/// Get all upstream OAuth providers
|
/// Get all upstream OAuth providers
|
||||||
@ -240,7 +240,7 @@ impl<'c> UpstreamOAuthProviderRepository for PgUpstreamOAuthProviderRepository<'
|
|||||||
)]
|
)]
|
||||||
async fn list_paginated(
|
async fn list_paginated(
|
||||||
&mut self,
|
&mut self,
|
||||||
pagination: &Pagination,
|
pagination: Pagination,
|
||||||
) -> Result<Page<UpstreamOAuthProvider>, Self::Error> {
|
) -> Result<Page<UpstreamOAuthProvider>, Self::Error> {
|
||||||
let mut query = QueryBuilder::new(
|
let mut query = QueryBuilder::new(
|
||||||
r#"
|
r#"
|
||||||
|
@ -39,7 +39,7 @@ pub trait UserEmailRepository: Send + Sync {
|
|||||||
async fn list_paginated(
|
async fn list_paginated(
|
||||||
&mut self,
|
&mut self,
|
||||||
user: &User,
|
user: &User,
|
||||||
pagination: &Pagination,
|
pagination: Pagination,
|
||||||
) -> Result<Page<UserEmail>, Self::Error>;
|
) -> Result<Page<UserEmail>, Self::Error>;
|
||||||
async fn count(&mut self, user: &User) -> Result<usize, 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(
|
async fn list_paginated(
|
||||||
&mut self,
|
&mut self,
|
||||||
user: &User,
|
user: &User,
|
||||||
pagination: &Pagination,
|
pagination: Pagination,
|
||||||
) -> Result<Page<UserEmail>, DatabaseError> {
|
) -> Result<Page<UserEmail>, DatabaseError> {
|
||||||
let mut query = QueryBuilder::new(
|
let mut query = QueryBuilder::new(
|
||||||
r#"
|
r#"
|
||||||
@ -302,7 +302,7 @@ impl<'c> UserEmailRepository for PgUserEmailRepository<'c> {
|
|||||||
query
|
query
|
||||||
.push(" WHERE user_id = ")
|
.push(" WHERE user_id = ")
|
||||||
.push_bind(Uuid::from(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
|
let edges: Vec<UserEmailLookup> = query
|
||||||
.build_query_as()
|
.build_query_as()
|
||||||
|
@ -45,7 +45,7 @@ pub trait BrowserSessionRepository: Send + Sync {
|
|||||||
async fn list_active_paginated(
|
async fn list_active_paginated(
|
||||||
&mut self,
|
&mut self,
|
||||||
user: &User,
|
user: &User,
|
||||||
pagination: &Pagination,
|
pagination: Pagination,
|
||||||
) -> Result<Page<BrowserSession>, Self::Error>;
|
) -> Result<Page<BrowserSession>, Self::Error>;
|
||||||
async fn count_active(&mut self, user: &User) -> Result<usize, 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(
|
async fn list_active_paginated(
|
||||||
&mut self,
|
&mut self,
|
||||||
user: &User,
|
user: &User,
|
||||||
pagination: &Pagination,
|
pagination: Pagination,
|
||||||
) -> Result<Page<BrowserSession>, Self::Error> {
|
) -> Result<Page<BrowserSession>, Self::Error> {
|
||||||
// TODO: ordering of last authentication is wrong
|
// TODO: ordering of last authentication is wrong
|
||||||
let mut query = QueryBuilder::new(
|
let mut query = QueryBuilder::new(
|
||||||
|
Reference in New Issue
Block a user