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

5.3 KB
Raw Blame
import {
  createTrigger,
  isNil,
  Property,
  TriggerStrategy,
} from '@activepieces/pieces-framework';

import {
  AuthenticationType,
  HttpMethod,
  httpClient,
} from '@activepieces/pieces-common';
import { dropboxAuth } from '../auth';
import { dropboxCommon } from '../common';
import { newFolderTriggerOutputSchema } from '../output-schemas';

type DropboxListFolderEntry = {
  id: string;
  name: string;
  path_lower?: string;
  path_display?: string;
  ['.tag']: string;
};

type DropboxListFolderResponse = {
  entries: DropboxListFolderEntry[];
  cursor: string;
  has_more: boolean;
};

// Returns the latest cursor for a folder without fetching any entries.
// Designed for apps that only need to know about new changes going forward.
const getLatestCursor = async (
  accessToken: string,
  path: string,
  recursive: boolean,
): Promise<string> => {
  const res = await httpClient.sendRequest<{ cursor: string }>({
    method: HttpMethod.POST,
    url: 'https://api.dropboxapi.com/2/files/list_folder/get_latest_cursor',
    headers: { 'Content-Type': 'application/json' },
    body: { path, recursive, include_deleted: false },
    authentication: {
      type: AuthenticationType.BEARER_TOKEN,
      token: accessToken,
    },
  });
  return res.body.cursor;
};

// Calls list_folder/continue with a stored cursor and returns all new folder entries,
// plus the updated cursor to store for the next poll.
const getChangedFolders = async (
  accessToken: string,
  cursor: string,
): Promise<{ folders: DropboxListFolderEntry[]; newCursor: string }> => {
  const folders: DropboxListFolderEntry[] = [];
  let currentCursor = cursor;
  let hasMore = true;

  while (hasMore) {
    const res = await httpClient.sendRequest<DropboxListFolderResponse>({
      method: HttpMethod.POST,
      url: 'https://api.dropboxapi.com/2/files/list_folder/continue',
      headers: { 'Content-Type': 'application/json' },
      body: { cursor: currentCursor },
      authentication: {
        type: AuthenticationType.BEARER_TOKEN,
        token: accessToken,
      },
    });

    const newFolders = res.body.entries.filter((e) => e['.tag'] === 'folder');
    folders.push(...newFolders);
    currentCursor = res.body.cursor;
    hasMore = res.body.has_more;
  }

  return { folders, newCursor: currentCursor };
};

// Fetches recent folder entries for test mode — returns up to 5.
const listRecentFolders = async (
  accessToken: string,
  path: string,
  recursive: boolean,
): Promise<DropboxListFolderEntry[]> => {
  const res = await httpClient.sendRequest<DropboxListFolderResponse>({
    method: HttpMethod.POST,
    url: 'https://api.dropboxapi.com/2/files/list_folder',
    headers: { 'Content-Type': 'application/json' },
    body: { path, recursive, include_deleted: false, limit: 100 },
    authentication: {
      type: AuthenticationType.BEARER_TOKEN,
      token: accessToken,
    },
  });

  return res.body.entries.filter((e) => e['.tag'] === 'folder').slice(0, 5);
};

export const dropboxNewFolder = createTrigger({
  auth: dropboxAuth,
  name: 'new_folder',
  classification: 'READ',
  displayName: 'New Folder',
  description:
    'Triggers when a new folder is created inside a watched Dropbox folder.',
  aiMetadata: {
    description: 'Fires when a new folder appears inside the watched Dropbox folder path, detected by polling the folder for changes since the last cursor. Each fired event represents one newly created folder; enable recursive watching to also detect folders created within subfolders.',
  },

  type: TriggerStrategy.POLLING,

  outputSchema: newFolderTriggerOutputSchema,
  propertyGroups: [
    {
      key: 'watch',
      display: 'section',
      label: 'Folder to Watch',
      icon: 'file',
      props: ['path', 'recursive'],
    },
  ],
  props: {
    path: Property.ShortText({
      displayName: 'Path',
      description: 'Folder to watch, starting with /. Use / for the root.',
      placeholder: '/Clients',
      required: true,
      defaultValue: '',
    }),

    recursive: Property.Checkbox({
      displayName: 'Include Subfolders',
      description: 'Also fire for folders created inside subfolders.',
      required: false,
      defaultValue: false,
    }),
  },

  sampleData: {
    '.tag': 'folder',
    name: 'Project Docs',
    id: 'id:a4ayc_80_OEAAAAAAAAAXw',
    path_display: '/Work/Project Docs',
    path_lower: '/work/project docs',
  },

  onEnable: async (context) => {
    const cursor = await getLatestCursor(
      context.auth.access_token,
      dropboxCommon.toApiPath(context.propsValue.path || ''),
      context.propsValue.recursive ?? false,
    );
    if (context.isRepublish && !isNil(await context.store.get('cursor'))) {
      return;
    }
    await context.store.put('cursor', cursor);
  },

  onDisable: async (context) => {
    if (context.isRepublish) {
      return;
    }
    await context.store.delete('cursor');
  },

  run: async (context) => {
    const cursor = await context.store.get<string>('cursor');
    if (!cursor) return [];

    const { folders, newCursor } = await getChangedFolders(
      context.auth.access_token,
      cursor,
    );
    await context.store.put('cursor', newCursor);
    return folders;
  },

  test: async (context) => {
    return listRecentFolders(
      context.auth.access_token,
      dropboxCommon.toApiPath(context.propsValue.path || ''),
      context.propsValue.recursive ?? false,
    );
  },
});