|
| 1 | +import { RunResponse } from '@app/definitions/definition' |
| 2 | +import { OperationTrigger } from '@app/definitions/operation-trigger' |
| 3 | +import { OperationRunOptions } from 'apps/runner/src/services/operation-runner.service' |
| 4 | +import { JSONSchema7 } from 'json-schema' |
| 5 | + |
| 6 | +export class NewCollectionBulkTrigger extends OperationTrigger { |
| 7 | + idKey = 'items[].address' |
| 8 | + key = 'newCollectionBulk' |
| 9 | + name = 'New Collection (Bulk)' |
| 10 | + description = 'Triggers when someone collects one of your posts or comments' |
| 11 | + version = '1.0.0' |
| 12 | + skipAuth = true |
| 13 | + unlisted = true |
| 14 | + |
| 15 | + inputs: JSONSchema7 = { |
| 16 | + required: ['publicationId'], |
| 17 | + properties: { |
| 18 | + publicationId: { |
| 19 | + title: 'Publication ID', |
| 20 | + type: 'string', |
| 21 | + description: |
| 22 | + 'The ID of the post or comment to watch for collections. If empty, it will trigger on any collection.', |
| 23 | + }, |
| 24 | + }, |
| 25 | + } |
| 26 | + outputs: JSONSchema7 = { |
| 27 | + properties: { |
| 28 | + address: { |
| 29 | + type: 'string', |
| 30 | + }, |
| 31 | + defaultProfile: { |
| 32 | + type: 'object', |
| 33 | + properties: { |
| 34 | + id: { |
| 35 | + type: 'string', |
| 36 | + }, |
| 37 | + handle: { |
| 38 | + type: 'string', |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | + async run({ inputs, fetchAll }: OperationRunOptions): Promise<RunResponse | null> { |
| 46 | + const { publicationId } = inputs |
| 47 | + const collections = fetchAll ? await this.fetchAll(publicationId) : await this.fetchLatest(publicationId) |
| 48 | + return { |
| 49 | + outputs: { |
| 50 | + items: collections, |
| 51 | + }, |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + async fetchLatest(publicationId: string) { |
| 56 | + const url = `https://api.apireum.com/v1/lens/posts/${publicationId}/collections?key=${process.env.APIREUM_API_KEY}` |
| 57 | + const res = await fetch(url) |
| 58 | + const data = await res.json() |
| 59 | + return data.collections |
| 60 | + } |
| 61 | + |
| 62 | + async fetchAll(publicationId: string, cursor = '') { |
| 63 | + const url = `https://api.apireum.com/v1/lens/posts/${publicationId}/collections?key=${process.env.APIREUM_API_KEY}&limit=50&cursor=${cursor}` |
| 64 | + const res = await fetch(url) |
| 65 | + const data = await res.json() |
| 66 | + if (data.collections && data.collections.length >= 50) { |
| 67 | + return [...data.collections, ...(await this.fetchAll(publicationId, data.cursor))] |
| 68 | + } |
| 69 | + return data.collections ? data.collections : [] |
| 70 | + } |
| 71 | +} |
0 commit comments