Open
Conversation
Owner
|
Thanks for the PR. I love the idea to filter emails before synching, but I think it would be more flexible if we simply add a query argument to the commandline tool. Then you could say
But you are also able to only sync emails without attachments, or for a specific user etc. WDYT? |
Contributor
Author
|
@marcboeker Yes that makes sense, especially if that use case is documented in the README. I'll update this PR. |
Contributor
Author
|
Updated PR. |
marcboeker
reviewed
Jun 30, 2025
Comment on lines
+309
to
+344
| service = _create_service(credentials) | ||
| labels = get_labels(service) | ||
| all_message_ids = [] | ||
| base_query = query if query else "" | ||
|
|
||
| if not full_sync: | ||
| # Fetch messages newer than the last indexed | ||
| last = db.last_indexed() | ||
| if last: | ||
| query.append(f"after:{int(last.timestamp())}") | ||
| new_query = f"{base_query} after:{int(last.timestamp())}".strip() | ||
| logging.info(f"Fetching new messages with query: {new_query}") | ||
| all_message_ids.extend( | ||
| get_message_ids_from_gmail(service, new_query, check_shutdown) | ||
| ) | ||
|
|
||
| # Fetch messages older than the first indexed (backfill) | ||
| first = db.first_indexed() | ||
| if first: | ||
| query.append(f"before:{int(first.timestamp())}") | ||
|
|
||
| service = _create_service(credentials) | ||
| labels = get_labels(service) | ||
| old_query = f"{base_query} before:{int(first.timestamp())}".strip() | ||
| logging.info(f"Backfilling old messages with query: {old_query}") | ||
| all_message_ids.extend( | ||
| get_message_ids_from_gmail(service, old_query, check_shutdown) | ||
| ) | ||
|
|
||
| all_message_ids = get_message_ids_from_gmail(service, query, check_shutdown) | ||
| # If it's the very first sync, there's no last or first, so run with the base query | ||
| if not last and not first: | ||
| logging.info(f"Performing initial sync with query: {base_query}") | ||
| all_message_ids.extend( | ||
| get_message_ids_from_gmail(service, base_query, check_shutdown) | ||
| ) | ||
| else: | ||
| # Full sync requested | ||
| logging.info(f"Performing full sync with query: {base_query}") | ||
| all_message_ids = get_message_ids_from_gmail( | ||
| service, base_query, check_shutdown | ||
| ) |
Owner
There was a problem hiding this comment.
Maybe we can simplify this a bit. WDYT?
try:
service = _create_service(credentials)
labels = get_labels(service)
base_query = query or ""
if full_sync:
# Full sync uses only the base query
query_str = base_query
logging.info(f"Performing full sync with query: {query_str}")
else:
# Build after/before filters into one compound query
last = db.last_indexed()
first = db.first_indexed()
filters = []
if last:
filters.append(f"after:{int(last.timestamp())}")
if first:
filters.append(f"before:{int(first.timestamp())}")
query_str = " ".join([base_query] + filters).strip()
logging.info(f"Fetching messages with compound query: {query_str}")
all_message_ids = get_message_ids_from_gmail(
service, query_str, check_shutdown
)
except Exception:
logging.exception("Failed to sync messages")
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
# only sync messages with the label "Transactional" $ uv run main.py sync --label Transactional --data-dir ./data