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: user and user email repository
This commit is contained in:
@ -31,7 +31,8 @@ use async_graphql::{
|
||||
Context, Description, EmptyMutation, EmptySubscription, ID,
|
||||
};
|
||||
use mas_storage::{
|
||||
upstream_oauth2::UpstreamOAuthProviderRepository, Repository, UpstreamOAuthLinkRepository,
|
||||
upstream_oauth2::UpstreamOAuthProviderRepository, user::UserEmailRepository, Repository,
|
||||
UpstreamOAuthLinkRepository,
|
||||
};
|
||||
use model::CreationEvent;
|
||||
use sqlx::PgPool;
|
||||
@ -154,8 +155,11 @@ impl RootQuery {
|
||||
let Some(session) = session else { return Ok(None) };
|
||||
let current_user = session.user;
|
||||
|
||||
let user_email =
|
||||
mas_storage::user::lookup_user_email_by_id(&mut conn, ¤t_user, id).await?;
|
||||
let user_email = conn
|
||||
.user_email()
|
||||
.lookup(id)
|
||||
.await?
|
||||
.filter(|e| e.user_id == current_user.id);
|
||||
|
||||
Ok(user_email.map(UserEmail))
|
||||
}
|
||||
|
@ -15,7 +15,9 @@
|
||||
use anyhow::Context as _;
|
||||
use async_graphql::{Context, Object, ID};
|
||||
use chrono::{DateTime, Utc};
|
||||
use mas_storage::{upstream_oauth2::UpstreamOAuthProviderRepository, Repository};
|
||||
use mas_storage::{
|
||||
upstream_oauth2::UpstreamOAuthProviderRepository, user::UserRepository, Repository,
|
||||
};
|
||||
use sqlx::PgPool;
|
||||
|
||||
use super::{NodeType, User};
|
||||
@ -120,7 +122,10 @@ impl UpstreamOAuth2Link {
|
||||
// Fetch on-the-fly
|
||||
let database = ctx.data::<PgPool>()?;
|
||||
let mut conn = database.acquire().await?;
|
||||
mas_storage::user::lookup_user(&mut conn, *user_id).await?
|
||||
conn.user()
|
||||
.lookup(*user_id)
|
||||
.await?
|
||||
.context("User not found")?
|
||||
} else {
|
||||
return Ok(None);
|
||||
};
|
||||
|
@ -17,7 +17,7 @@ use async_graphql::{
|
||||
Context, Description, Object, ID,
|
||||
};
|
||||
use chrono::{DateTime, Utc};
|
||||
use mas_storage::{Repository, UpstreamOAuthLinkRepository};
|
||||
use mas_storage::{user::UserEmailRepository, Repository, UpstreamOAuthLinkRepository};
|
||||
use sqlx::PgPool;
|
||||
|
||||
use super::{
|
||||
@ -54,8 +54,14 @@ impl User {
|
||||
}
|
||||
|
||||
/// Primary email address of the user.
|
||||
async fn primary_email(&self) -> Option<UserEmail> {
|
||||
self.0.primary_email.clone().map(UserEmail)
|
||||
async fn primary_email(
|
||||
&self,
|
||||
ctx: &Context<'_>,
|
||||
) -> Result<Option<UserEmail>, async_graphql::Error> {
|
||||
let database = ctx.data::<PgPool>()?;
|
||||
let mut conn = database.acquire().await?;
|
||||
|
||||
Ok(conn.user_email().get_primary(&self.0).await?.map(UserEmail))
|
||||
}
|
||||
|
||||
/// Get the list of compatibility SSO logins, chronologically sorted
|
||||
@ -182,18 +188,17 @@ impl User {
|
||||
.map(|x: OpaqueCursor<NodeCursor>| x.extract_for_type(NodeType::UserEmail))
|
||||
.transpose()?;
|
||||
|
||||
let (has_previous_page, has_next_page, edges) =
|
||||
mas_storage::user::get_paginated_user_emails(
|
||||
&mut conn, &self.0, before_id, after_id, first, last,
|
||||
)
|
||||
let page = conn
|
||||
.user_email()
|
||||
.list_paginated(&self.0, before_id, after_id, first, last)
|
||||
.await?;
|
||||
|
||||
let mut connection = Connection::with_additional_fields(
|
||||
has_previous_page,
|
||||
has_next_page,
|
||||
page.has_previous_page,
|
||||
page.has_next_page,
|
||||
UserEmailsPagination(self.0.clone()),
|
||||
);
|
||||
connection.edges.extend(edges.into_iter().map(|u| {
|
||||
connection.edges.extend(page.edges.into_iter().map(|u| {
|
||||
Edge::new(
|
||||
OpaqueCursor(NodeCursor(NodeType::UserEmail, u.id)),
|
||||
UserEmail(u),
|
||||
@ -339,9 +344,9 @@ pub struct UserEmailsPagination(mas_data_model::User);
|
||||
#[Object]
|
||||
impl UserEmailsPagination {
|
||||
/// Identifies the total count of items in the connection.
|
||||
async fn total_count(&self, ctx: &Context<'_>) -> Result<i64, async_graphql::Error> {
|
||||
async fn total_count(&self, ctx: &Context<'_>) -> Result<usize, async_graphql::Error> {
|
||||
let mut conn = ctx.data::<PgPool>()?.acquire().await?;
|
||||
let count = mas_storage::user::count_user_emails(&mut conn, &self.0).await?;
|
||||
let count = conn.user_email().count(&self.0).await?;
|
||||
Ok(count)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user