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.4 KB
Raw Blame
import {
  createTrigger,
  isNil,
  StoreScope,
  TriggerStrategy,
} from '@activepieces/pieces-framework';
import { spotifyCommon, makeClient } from '../common';
import { createHash } from 'crypto';

export default createTrigger({
  name: 'playlist_items_changed',
  classification: 'READ',
  displayName: 'Playlist Items Changed',
  description: 'Triggers when the items of a playlist change',
  aiMetadata: {
    description: 'Fires when the set of tracks in the watched playlist (identified by its id) changes between polls — items added, removed, or reordered. Represents a detected change to the playlist contents, emitting the full current item list.',
  },
  auth: spotifyCommon.authentication,
  type: TriggerStrategy.POLLING,
  props: {
    playlist_id: spotifyCommon.playlist_id(true),
  },
  sampleData: {},
  onEnable: async ({ store, auth, propsValue, isRepublish }) => {
    if (
      isRepublish &&
      !isNil(await store.get('playlist_changed_trigger_hash', StoreScope.FLOW))
    ) {
      return;
    }
    const client = makeClient({ auth });
    const items = await client.getAllPlaylistItems(
      propsValue.playlist_id as string
    );
    const hash = createHash('md5')
      .update(items.map((item) => item.track.id).join(','))
      .digest('hex');
    await store.put('playlist_changed_trigger_hash', hash, StoreScope.FLOW);
  },
  onDisable: async ({ store, auth, propsValue, isRepublish }) => {
    if (isRepublish) {
      return;
    }
    await store.delete('playlist_changed_trigger_hash', StoreScope.FLOW);
  },
  run: async ({ store, auth, propsValue }) => {
    const oldHash = await store.get(
      'playlist_changed_trigger_hash',
      StoreScope.FLOW
    );
    const client = makeClient({ auth });
    const items = await client.getAllPlaylistItems(
      propsValue.playlist_id as string
    );
    const newHash = createHash('md5')
      .update(items.map((item) => item.track.id).join(','))
      .digest('hex');
    if (oldHash != newHash) {
      await store.put(
        'playlist_changed_trigger_hash',
        newHash,
        StoreScope.FLOW
      );
      return [{ total: items.length, items }];
    }
    return [];
  },
  test: async ({ auth, propsValue }) => {
    const client = makeClient({ auth });
    const items = await client.getAllPlaylistItems(
      propsValue.playlist_id as string
    );
    return [{ total: items.length, items }];
  },
});