Skip to content

Commit

Permalink
Ensure gmail works for personal accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
pablonyx committed Dec 22, 2024
1 parent edb877f commit 2e7fae2
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions backend/onyx/connectors/gmail/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from google.oauth2.credentials import Credentials as OAuthCredentials # type: ignore
from google.oauth2.service_account import Credentials as ServiceAccountCredentials # type: ignore
from googleapiclient.errors import HttpError

from onyx.configs.app_configs import INDEX_BATCH_SIZE
from onyx.configs.constants import DocumentSource
Expand Down Expand Up @@ -249,17 +250,36 @@ def load_credentials(self, credentials: dict[str, Any]) -> dict[str, str] | None
return new_creds_dict

def _get_all_user_emails(self) -> list[str]:
admin_service = get_admin_service(self.creds, self.primary_admin_email)
emails = []
for user in execute_paginated_retrieval(
retrieval_function=admin_service.users().list,
list_key="users",
fields=USER_FIELDS,
domain=self.google_domain,
):
if email := user.get("primaryEmail"):
emails.append(email)
return emails
"""
List all user emails if we are on a Google Workspace domain.
If the domain is gmail.com, or if we attempt to call the Admin SDK and
get a 404, fall back to using the single user.
"""

try:
admin_service = get_admin_service(self.creds, self.primary_admin_email)
emails = []
for user in execute_paginated_retrieval(
retrieval_function=admin_service.users().list,
list_key="users",
fields=USER_FIELDS,
domain=self.google_domain,
):
if email := user.get("primaryEmail"):
emails.append(email)
return emails

except HttpError as e:
if e.resp.status == 404:
logger.warning(
"Received 404 from Admin SDK; this may indicate a personal Gmail account "
"with no Workspace domain. Falling back to single user."
)
return [self.primary_admin_email]
raise

except Exception:
raise

def _fetch_threads(
self,
Expand All @@ -284,8 +304,6 @@ def _fetch_threads(
fields=THREAD_FIELDS,
id=thread["id"],
)
# full_threads is an iterator containing a single thread
# so we need to convert it to a list and grab the first element
full_thread = list(full_threads)[0]
doc = thread_to_document(full_thread)
if doc is None:
Expand Down

0 comments on commit 2e7fae2

Please sign in to comment.