import pMap from "p-map" import * as z from "zod" import { defineAction } from "../../../automation/actions" import { getGmailApi, toMessageMetadata, } from "@automate.ax/integration-contracts/gmail" import { GMAIL_MODIFY_REQUIREMENT } from "../lib/scopes" import { MESSAGE_IDS_SCHEMA, MESSAGE_METADATA_SCHEMA, } from "@automate.ax/integration-contracts/gmail" /** * Restores one or many Gmail messages from Trash. * * Returns Gmail's updated metadata for every message. */ export const untrashGmailMessages = defineAction("Untrash Gmail messages") .describe("Restores one or many Gmail messages from Trash.") .account("google", GMAIL_MODIFY_REQUIREMENT) .input( z.object({ /** One ID or up to 1,000 immutable Gmail message IDs. */ messageIds: MESSAGE_IDS_SCHEMA, }), ) .output(MESSAGE_METADATA_SCHEMA.array()) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => { const gmailApi = getGmailApi(account.secret) return await pMap( input.messageIds, async (messageId) => toMessageMetadata( ( await gmailApi.users.messages.untrash({ id: messageId, userId: "me", }) ).data, "restored message", ), { concurrency: 20 }, ) })