Skip to content

Commit

Permalink
chore: use list users to query users by emails
Browse files Browse the repository at this point in the history
Signed-off-by: Wei Zhang <[email protected]>
  • Loading branch information
zwpaper committed Dec 4, 2024
1 parent 5aa51f1 commit 90630c5
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 21 deletions.
13 changes: 12 additions & 1 deletion ee/tabby-db/src/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,20 @@ impl DbConn {

pub async fn list_users_with_filter(
&self,
emails: Option<Vec<String>>,
limit: Option<usize>,
skip_id: Option<i32>,
backwards: bool,
) -> Result<Vec<UserDAO>> {
let email_condition = emails.map(|emails| {
let emails = emails
.into_iter()
.map(|e| format!("'{}'", e))
.collect::<Vec<_>>()
.join(", ");
format!("email in ({emails})")
});

let users = query_paged_as!(
UserDAO,
"users",
Expand All @@ -145,7 +155,8 @@ impl DbConn {
],
limit,
skip_id,
backwards
backwards,
email_condition
)
.fetch_all(&self.pool)
.await?;
Expand Down
3 changes: 1 addition & 2 deletions ee/tabby-schema/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -680,8 +680,7 @@ type Query {
registrationToken: String!
me: UserSecured!
"List users, accessible for all login users."
users(after: String, before: String, first: Int, last: Int): UserConnection!
user(email: String): User!
users(emails: [String!], after: String, before: String, first: Int, last: Int): UserConnection!
invitations(after: String, before: String, first: Int, last: Int): InvitationConnection!
jobRuns(ids: [ID!], jobs: [String!], after: String, before: String, first: Int, last: Int): JobRunConnection!
jobRunStats(jobs: [String!]): JobStats!
Expand Down
1 change: 1 addition & 0 deletions ee/tabby-schema/src/schema/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ pub trait AuthenticationService: Send + Sync {

async fn list_users(
&self,
emails: Option<Vec<String>>,
after: Option<String>,
before: Option<String>,
first: Option<usize>,
Expand Down
17 changes: 2 additions & 15 deletions ee/tabby-schema/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ impl Query {
/// List users, accessible for all login users.
async fn users(
ctx: &Context,
emails: Option<Vec<String>>,
after: Option<String>,
before: Option<String>,
first: Option<i32>,
Expand All @@ -279,28 +280,14 @@ impl Query {
|after, before, first, last| async move {
ctx.locator
.auth()
.list_users(after, before, first, last)
.list_users(emails, after, before, first, last)
.await
.map(|users| users.into_iter().map(UserValue::UserSecured).collect())
},
)
.await
}

async fn user(ctx: &Context, email: Option<String>) -> Result<UserValue> {
check_user(ctx).await?;

if email.is_none() {
return Err(CoreError::InvalidInput(ValidationErrors::new()));
}

ctx.locator
.auth()
.get_user_by_email(email.unwrap().as_str())
.await
.map(UserValue::UserSecured)
}

async fn invitations(
ctx: &Context,
after: Option<String>,
Expand Down
3 changes: 2 additions & 1 deletion ee/tabby-webserver/src/service/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ impl AuthenticationService for AuthenticationServiceImpl {

async fn list_users(
&self,
emails: Option<Vec<String>>,
after: Option<String>,
before: Option<String>,
first: Option<usize>,
Expand All @@ -459,7 +460,7 @@ impl AuthenticationService for AuthenticationServiceImpl {

Ok(self
.db
.list_users_with_filter(skip_id, limit, backwards)
.list_users_with_filter(emails, skip_id, limit, backwards)
.await?
.into_iter()
.map(|x| UserSecured::new(self.db.clone(), x))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,11 @@ pub async fn list_github_pulls(
let url = pull.html_url.map(|url| url.to_string()).unwrap_or_else(|| pull.url);
let title = pull.title.clone().unwrap_or_default();
let body = pull.body.clone().unwrap_or_default();

let author = pull.user.as_ref().map(|user| user.login.clone()).unwrap_or_default();
println!("author: {}", author);
let email = if !author.is_empty() {
match octocrab.users(&author).profile().await {
Ok(profile) => {
println!("profile: {:?}", profile);
profile.email.unwrap_or_default()
}
Err(e) => {
Expand Down

0 comments on commit 90630c5

Please sign in to comment.