Skip to content

--reply-to fails with thread IDs (only accepts message IDs) #1

Description

@willtmc

Bug

The --reply-to flag fails when given a thread ID (which is what gmcli search returns). It only works with message IDs.

Steps to reproduce

# Search returns thread IDs
gmcli user@example.com search "in:inbox" --max 1
# Returns: 19b18de0c8734107 (thread ID)

# Using that thread ID with --reply-to fails
gmcli user@example.com drafts create \
  --to "someone@example.com" \
  --subject "Re: Test" \
  --body "Reply text" \
  --reply-to "19b18de0c8734107"
# Error: Requested entity was not found.

# But using the message ID works
gmcli user@example.com thread 19b18de0c8734107
# Shows Message-ID: 19b18fc8d2776818

gmcli user@example.com drafts create \
  --to "someone@example.com" \
  --subject "Re: Test" \
  --body "Reply text" \
  --reply-to "19b18fc8d2776818"
# Draft created: r1234567890

Expected behavior

--reply-to should accept either a thread ID or message ID, since the CLI primarily works with thread IDs.

Suggested fix

In gmail-service.js, detect if the ID is a thread ID and fetch the last message from that thread:

// If replying to a specific message, fetch its headers
if (options.replyToMessageId) {
    let messageIdToFetch = options.replyToMessageId;
    try {
        await gmail.users.messages.get({
            userId: "me",
            id: messageIdToFetch,
            format: "minimal",
        });
    } catch (e) {
        // Probably a thread ID - get the thread and use the last message
        const thread = await gmail.users.threads.get({
            userId: "me",
            id: options.replyToMessageId,
            format: "minimal",
        });
        if (thread.data.messages && thread.data.messages.length > 0) {
            messageIdToFetch = thread.data.messages[thread.data.messages.length - 1].id;
        }
    }
    const msg = await gmail.users.messages.get({
        userId: "me",
        id: messageIdToFetch,
        format: "metadata",
        metadataHeaders: ["Message-ID", "References"],
    });
    // ... rest of the code
}

This needs to be applied in both createDraft and sendMessage functions.

I've tested this patch locally and it works for both thread IDs and message IDs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions