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.9 KB
Raw Blame
import { HttpMethod, httpClient, propsValidation } from '@activepieces/pieces-common';
import { createAction, isNil, Property } from '@activepieces/pieces-framework';
import * as z from 'zod/mini';
import { googleAuth } from '../..';
import { listPostsActionOutputSchema } from '../output-schemas';
import { googleBusinessCommon } from '../common/common';
import { localPostUtils } from '../common/local-post';

export const listPosts = createAction({
  name: 'list-posts',
  outputSchema: listPostsActionOutputSchema,
  classification: 'READ',
  displayName: 'List Posts',
  description: 'List the posts published on a location.',
  audience: 'both',
  aiMetadata: {
    description:
      'Returns the local posts published to a Google Business Profile location, newest first, following pagination up to Maximum Results. Use to read existing posts or to find a post name for Get, Update or Delete Post. Read-only and safe to repeat.',
    idempotent: true,
  },
  auth: googleAuth,
  propertyGroups: [
    {
      key: 'business',
      display: 'section',
      label: 'Business',
      icon: 'location',
      props: ['account', 'location'],
    },
    {
      key: 'results',
      display: 'section',
      label: 'Results',
      icon: 'sliders',
      props: ['maxResults'],
    },
  ],
  props: {
    account: googleBusinessCommon.account,
    location: googleBusinessCommon.location,
    maxResults: Property.Number({
      displayName: 'Maximum Results',
      description: 'Stop after this many posts. Default 100.',
      required: false,
      defaultValue: 100,
      display: 'stepper',
      min: 1,
      max: 1000,
      step: 1,
    }),
  },
  async run(ctx) {
    const { account, location, maxResults } = ctx.propsValue;

    await propsValidation.validateZod(ctx.propsValue, {
      maxResults: z.optional(z.number().check(z.gte(1))),
    });

    const limit = maxResults ?? DEFAULT_MAX_RESULTS;
    const localPosts: unknown[] = [];
    let pageToken: string | undefined;

    do {
      const remaining = limit - localPosts.length;
      const response = await httpClient.sendRequest<ListLocalPostsResponse>({
        url: `${localPostUtils.baseUrl}/${account}/${location}/localPosts`,
        method: HttpMethod.GET,
        headers: {
          Authorization: `Bearer ${ctx.auth.access_token}`,
        },
        queryParams: {
          pageSize: String(Math.min(remaining, MAX_PAGE_SIZE)),
          ...(isNil(pageToken) ? {} : { pageToken }),
        },
      });

      localPosts.push(...(response.body.localPosts ?? []));
      pageToken = response.body.nextPageToken;
    } while (!isNil(pageToken) && localPosts.length < limit);

    return {
      localPosts,
      ...(isNil(pageToken) ? {} : { nextPageToken: pageToken }),
    };
  },
});

const DEFAULT_MAX_RESULTS = 100;
const MAX_PAGE_SIZE = 100;

type ListLocalPostsResponse = {
  localPosts?: unknown[];
  nextPageToken?: string;
};