1
0
mirror of https://github.com/matrix-org/matrix-authentication-service.git synced 2025-11-20 12:02:22 +03:00

storage: wrap the postgres repository in a struct

This commit is contained in:
Quentin Gliech
2023-01-13 18:03:37 +01:00
parent 488a666a8d
commit 195203823a
44 changed files with 505 additions and 548 deletions

View File

@@ -32,9 +32,9 @@ use async_graphql::{
};
use mas_storage::{
oauth2::OAuth2ClientRepository,
upstream_oauth2::UpstreamOAuthProviderRepository,
upstream_oauth2::{UpstreamOAuthLinkRepository, UpstreamOAuthProviderRepository},
user::{BrowserSessionRepository, UserEmailRepository},
Repository, UpstreamOAuthLinkRepository,
PgRepository, Repository,
};
use model::CreationEvent;
use sqlx::PgPool;
@@ -93,10 +93,9 @@ impl RootQuery {
id: ID,
) -> Result<Option<OAuth2Client>, async_graphql::Error> {
let id = NodeType::OAuth2Client.extract_ulid(&id)?;
let database = ctx.data::<PgPool>()?;
let mut conn = database.acquire().await?;
let mut repo = PgRepository::from_pool(ctx.data::<PgPool>()?).await?;
let client = conn.oauth2_client().lookup(id).await?;
let client = repo.oauth2_client().lookup(id).await?;
Ok(client.map(OAuth2Client))
}
@@ -124,13 +123,12 @@ impl RootQuery {
) -> Result<Option<BrowserSession>, async_graphql::Error> {
let id = NodeType::BrowserSession.extract_ulid(&id)?;
let session = ctx.data_opt::<mas_data_model::BrowserSession>().cloned();
let database = ctx.data::<PgPool>()?;
let mut conn = database.acquire().await?;
let mut repo = PgRepository::from_pool(ctx.data::<PgPool>()?).await?;
let Some(session) = session else { return Ok(None) };
let current_user = session.user;
let browser_session = conn.browser_session().lookup(id).await?;
let browser_session = repo.browser_session().lookup(id).await?;
let ret = browser_session.and_then(|browser_session| {
if browser_session.user.id == current_user.id {
@@ -151,13 +149,12 @@ impl RootQuery {
) -> Result<Option<UserEmail>, async_graphql::Error> {
let id = NodeType::UserEmail.extract_ulid(&id)?;
let session = ctx.data_opt::<mas_data_model::BrowserSession>().cloned();
let database = ctx.data::<PgPool>()?;
let mut conn = database.acquire().await?;
let mut repo = PgRepository::from_pool(ctx.data::<PgPool>()?).await?;
let Some(session) = session else { return Ok(None) };
let current_user = session.user;
let user_email = conn
let user_email = repo
.user_email()
.lookup(id)
.await?
@@ -174,13 +171,12 @@ impl RootQuery {
) -> Result<Option<UpstreamOAuth2Link>, async_graphql::Error> {
let id = NodeType::UpstreamOAuth2Link.extract_ulid(&id)?;
let session = ctx.data_opt::<mas_data_model::BrowserSession>().cloned();
let database = ctx.data::<PgPool>()?;
let mut conn = database.acquire().await?;
let mut repo = PgRepository::from_pool(ctx.data::<PgPool>()?).await?;
let Some(session) = session else { return Ok(None) };
let current_user = session.user;
let link = conn.upstream_oauth_link().lookup(id).await?;
let link = repo.upstream_oauth_link().lookup(id).await?;
// Ensure that the link belongs to the current user
let link = link.filter(|link| link.user_id == Some(current_user.id));
@@ -195,10 +191,9 @@ impl RootQuery {
id: ID,
) -> Result<Option<UpstreamOAuth2Provider>, async_graphql::Error> {
let id = NodeType::UpstreamOAuth2Provider.extract_ulid(&id)?;
let database = ctx.data::<PgPool>()?;
let mut conn = database.acquire().await?;
let mut repo = PgRepository::from_pool(ctx.data::<PgPool>()?).await?;
let provider = conn.upstream_oauth_provider().lookup(id).await?;
let provider = repo.upstream_oauth_provider().lookup(id).await?;
Ok(provider.map(UpstreamOAuth2Provider::new))
}
@@ -215,7 +210,7 @@ impl RootQuery {
#[graphql(desc = "Returns the first *n* elements from the list.")] first: Option<i32>,
#[graphql(desc = "Returns the last *n* elements from the list.")] last: Option<i32>,
) -> Result<Connection<Cursor, UpstreamOAuth2Provider>, async_graphql::Error> {
let database = ctx.data::<PgPool>()?;
let mut repo = PgRepository::from_pool(ctx.data::<PgPool>()?).await?;
query(
after,
@@ -223,7 +218,6 @@ impl RootQuery {
first,
last,
|after, before, first, last| async move {
let mut conn = database.acquire().await?;
let after_id = after
.map(|x: OpaqueCursor<NodeCursor>| {
x.extract_for_type(NodeType::UpstreamOAuth2Provider)
@@ -235,7 +229,7 @@ impl RootQuery {
})
.transpose()?;
let page = conn
let page = repo
.upstream_oauth_provider()
.list_paginated(before_id, after_id, first, last)
.await?;