Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

2.8 KB
Raw Blame
import { createTrigger, TriggerStrategy } from '@activepieces/pieces-framework';
import { gmailAuth, createGoogleClient } from '../auth';
import { gmail as googleGmail } from '@googleapis/gmail';
import { getFirstFiveOrAll } from '../common/data';
import { isNil } from '@activepieces/pieces-framework';
import { newLabelTriggerOutputSchema } from '../output-schemas';

const TRIGGER_KEY = 'labels';

export const gmailNewLabelTrigger = createTrigger({
  auth: gmailAuth,
  name: 'new_label',
  classification: 'READ',
  displayName: 'New Label',
  description: 'Triggers when a new label is created in Gmail.',
  aiMetadata: {
    description:
      'Fires when a new label is created in the connected Gmail account. Each event represents one newly added label not seen on a prior poll.',
  },
  props: {},
  outputSchema: newLabelTriggerOutputSchema,
  sampleData: {},
  type: TriggerStrategy.POLLING,
  async onEnable(context) {
    if (context.isRepublish && !isNil(await context.store.get(TRIGGER_KEY))) {
      return;
    }
    const authClient = await createGoogleClient(context.auth);
    const gmail = googleGmail({ version: 'v1', auth: authClient });

    const response = await gmail.users.labels.list({
      userId: 'me',
    });

    const labels = response.data.labels || [];

    const existingLabelIds = labels.map((label) => label.id);
    await context.store.put(TRIGGER_KEY, JSON.stringify(existingLabelIds));
  },
  async onDisable(context) {
    if (context.isRepublish) {
      return;
    }
    await context.store.delete(TRIGGER_KEY);
  },
  async test(context) {
    const authClient = await createGoogleClient(context.auth);
    const gmail = googleGmail({ version: 'v1', auth: authClient });

    const response = await gmail.users.labels.list({
      userId: 'me',
    });

    const labels = response.data.labels || [];

    return getFirstFiveOrAll(labels);
  },
  async run(context) {
    const existingIds = (await context.store.get<string>(TRIGGER_KEY)) ?? '[]';
    const parsedExistingIds = JSON.parse(existingIds) as string[];

    const authClient = await createGoogleClient(context.auth);

    const gmail = googleGmail({ version: 'v1', auth: authClient });

    const response = await gmail.users.labels.list({
      userId: 'me',
    });

    if (isNil(response.data.labels) || response.data.labels.length === 0) {
      await context.store.put(TRIGGER_KEY, '[]');
      return [];
    }

    const allCurrentIds = response.data.labels.map((label) => label.id);

    const newLables = response.data.labels.filter((label) => {
      const labelId = label.id ?? undefined;
      return labelId !== undefined && !parsedExistingIds.includes(labelId);
    });

    await context.store.put(TRIGGER_KEY, JSON.stringify(allCurrentIds));

    if (newLables.length === 0) {
      return [];
    }

    return newLables;
  },
});