diff --git a/components/asters/actions/list-labels/list-labels.mjs b/components/asters/actions/list-labels/list-labels.mjs new file mode 100644 index 0000000000000..8b11745ef6264 --- /dev/null +++ b/components/asters/actions/list-labels/list-labels.mjs @@ -0,0 +1,27 @@ +import asters from "../../asters.app.mjs"; + +export default { + key: "asters-list-labels", + name: "List Labels", + description: "Retrieve the list of all labels of a specific workspace. [See the documentation](https://docs.asters.ai/api/endpoints/labels)", + type: "action", + version: "0.0.1", + props: { + asters, + workspaceId: { + propDefinition: [ + asters, + "workspaceId", + ], + }, + }, + async run({ $ }) { + const { data } = await this.asters.listLabels({ + workspaceId: this.workspaceId, + }); + $.export("$summary", `Successfully retrieved ${data.length} label${data.length === 1 + ? "" + : "s"}`); + return data; + }, +}; diff --git a/components/asters/actions/list-posts-analytics/list-posts-analytics.mjs b/components/asters/actions/list-posts-analytics/list-posts-analytics.mjs new file mode 100644 index 0000000000000..a50387b6b9343 --- /dev/null +++ b/components/asters/actions/list-posts-analytics/list-posts-analytics.mjs @@ -0,0 +1,59 @@ +import asters from "../../asters.app.mjs"; + +export default { + key: "asters-list-posts-analytics", + name: "List Posts Analytics", + description: "Retrieve the list of posts' analytics of a social account. [See the documentation](https://docs.asters.ai/api/endpoints/analytics)", + type: "action", + version: "0.0.1", + props: { + asters, + workspaceId: { + propDefinition: [ + asters, + "workspaceId", + ], + }, + socialAccountId: { + propDefinition: [ + asters, + "socialAccountId", + (c) => ({ + workspaceId: c.workspaceId, + }), + ], + }, + fromDate: { + propDefinition: [ + asters, + "fromDate", + ], + }, + toDate: { + propDefinition: [ + asters, + "toDate", + ], + }, + }, + async run({ $ }) { + const posts = await this.asters.getPaginatedResources({ + fn: this.asters.listPostAnalytics, + args: { + data: { + socialAccountId: this.socialAccountId, + filters: { + date: { + from: this.fromDate, + to: this.toDate, + }, + }, + }, + }, + }); + $.export("$summary", `Successfully retrieved ${posts.length} post${posts.length === 1 + ? "" + : "s"}`); + return posts; + }, +}; diff --git a/components/asters/actions/list-posts/list-posts.mjs b/components/asters/actions/list-posts/list-posts.mjs new file mode 100644 index 0000000000000..4e1c67c1bdb70 --- /dev/null +++ b/components/asters/actions/list-posts/list-posts.mjs @@ -0,0 +1,59 @@ +import asters from "../../asters.app.mjs"; + +export default { + key: "asters-list-posts", + name: "List Posts", + description: "Retrieve a list of posts of a social profile. [See the documentation](https://docs.asters.ai/api/endpoints/posts)", + type: "action", + version: "0.0.1", + props: { + asters, + workspaceId: { + propDefinition: [ + asters, + "workspaceId", + ], + }, + socialAccountId: { + propDefinition: [ + asters, + "socialAccountId", + (c) => ({ + workspaceId: c.workspaceId, + }), + ], + }, + fromDate: { + propDefinition: [ + asters, + "fromDate", + ], + }, + toDate: { + propDefinition: [ + asters, + "toDate", + ], + }, + }, + async run({ $ }) { + const posts = await this.asters.getPaginatedResources({ + fn: this.asters.listPosts, + args: { + data: { + socialAccountId: this.socialAccountId, + filters: { + date: { + from: this.fromDate, + to: this.toDate, + }, + }, + }, + }, + }); + $.export("$summary", `Successfully retrieved ${posts.length} post${posts.length === 1 + ? "" + : "s"}`); + return posts; + }, +}; diff --git a/components/asters/actions/list-social-accounts/list-social-accounts.mjs b/components/asters/actions/list-social-accounts/list-social-accounts.mjs new file mode 100644 index 0000000000000..d72fe62ea3cc8 --- /dev/null +++ b/components/asters/actions/list-social-accounts/list-social-accounts.mjs @@ -0,0 +1,27 @@ +import asters from "../../asters.app.mjs"; + +export default { + key: "asters-list-social-accounts", + name: "List Social Accounts", + description: "Retrieve the list of all social accounts of a specific workspace. [See the documentation](https://docs.asters.ai/api/endpoints/social-accounts)", + type: "action", + version: "0.0.1", + props: { + asters, + workspaceId: { + propDefinition: [ + asters, + "workspaceId", + ], + }, + }, + async run({ $ }) { + const { data } = await this.asters.listSocialAccounts({ + workspaceId: this.workspaceId, + }); + $.export("$summary", `Successfully retrieved ${data.length} social account${data.length === 1 + ? "" + : "s"}`); + return data; + }, +}; diff --git a/components/asters/asters.app.mjs b/components/asters/asters.app.mjs index 2ceeb25264ab0..2ff0f1dd13451 100644 --- a/components/asters/asters.app.mjs +++ b/components/asters/asters.app.mjs @@ -1,11 +1,131 @@ +import { axios } from "@pipedream/platform"; + export default { type: "app", app: "asters", - propDefinitions: {}, + propDefinitions: { + workspaceId: { + type: "string", + label: "Workspace ID", + description: "The ID of a workspace", + async options() { + const { data: { workspaces = [] } } = await this.listWorkspaces(); + return workspaces.map((workspace) => ({ + label: workspace.name, + value: workspace._id, + })); + }, + }, + socialAccountId: { + type: "string", + label: "Social Account ID", + description: "The ID of a social account", + async options({ workspaceId }) { + const { data = [] } = await this.listSocialAccounts({ + workspaceId, + }); + return data.map((account) => ({ + label: account.name, + value: account.account_id, + })); + }, + }, + fromDate: { + type: "string", + label: "From Date", + description: "The date to start the search from (YYYY-MM-DD)", + }, + toDate: { + type: "string", + label: "To Date", + description: "The date to end the search (YYYY-MM-DD)", + }, + }, methods: { - // this.$auth contains connected account data - authKeys() { - console.log(Object.keys(this.$auth)); + _baseUrl() { + return "https://api.asters.ai/api/external/v1.0"; + }, + _makeRequest({ + $ = this, path, ...opts + }) { + return axios($, { + url: `${this._baseUrl()}${path}`, + headers: { + "x-api-key": `${this.$auth.api_key}`, + }, + ...opts, + }); + }, + listWorkspaces(opts = {}) { + return this._makeRequest({ + path: "/workspaces", + ...opts, + }); + }, + listSocialAccounts({ + workspaceId, ...opts + }) { + return this._makeRequest({ + path: `/workspaces/${workspaceId}/socialAccounts`, + ...opts, + }); + }, + listLabels({ + workspaceId, ...opts + }) { + return this._makeRequest({ + path: `/workspaces/${workspaceId}/labels`, + ...opts, + }); + }, + listPosts(opts = {}) { + return this._makeRequest({ + method: "POST", + path: "/data/posts", + ...opts, + }); + }, + listPostAnalytics(opts = {}) { + return this._makeRequest({ + method: "POST", + path: "/analytics/posts", + ...opts, + }); + }, + async *paginate({ + fn, args, max, + }) { + args = { + ...args, + data: { + ...args?.data, + page: 1, + }, + }; + let hasMore, count = 0; + do { + const { + data, pagination, + } = await fn(args); + if (!data?.length) { + return; + } + for (const item of data) { + yield item; + if (max && ++count >= max) { + return; + } + } + hasMore = pagination?.totalPages > args.data.page; + args.data.page++; + } while (hasMore); + }, + async getPaginatedResources(opts) { + const results = []; + for await (const item of this.paginate(opts)) { + results.push(item); + } + return results; }, }, }; diff --git a/components/asters/package.json b/components/asters/package.json index 36fd0c6e81a24..97d037eb0aa99 100644 --- a/components/asters/package.json +++ b/components/asters/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/asters", - "version": "0.0.1", + "version": "0.1.0", "description": "Pipedream Asters Components", "main": "asters.app.mjs", "keywords": [ @@ -11,5 +11,8 @@ "author": "Pipedream (https://pipedream.com/)", "publishConfig": { "access": "public" + }, + "dependencies": { + "@pipedream/platform": "^3.1.0" } -} \ No newline at end of file +} diff --git a/components/asters/sources/common/base.mjs b/components/asters/sources/common/base.mjs new file mode 100644 index 0000000000000..c9854610a2a99 --- /dev/null +++ b/components/asters/sources/common/base.mjs @@ -0,0 +1,39 @@ +import asters from "../../asters.app.mjs"; +import { + DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, ConfigurationError, +} from "@pipedream/platform"; + +export default { + props: { + asters, + db: "$.service.db", + timer: { + type: "$.interface.timer", + default: { + intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, + }, + }, + }, + methods: { + getResourceFn() { + throw new ConfigurationError("getResourceFn must be implemented"); + }, + getArgs() { + throw new ConfigurationError("getArgs must be implemented"); + }, + generateMeta() { + throw new ConfigurationError("generateMeta must be implemented"); + }, + processResources() { + throw new ConfigurationError("processResources must be implemented"); + }, + }, + async run() { + const resources = await this.asters.getPaginatedResources({ + fn: this.getResourceFn(), + args: this.getArgs(), + }); + + await this.processResources(resources); + }, +}; diff --git a/components/asters/sources/new-label-added/new-label-added.mjs b/components/asters/sources/new-label-added/new-label-added.mjs new file mode 100644 index 0000000000000..a420abc76c964 --- /dev/null +++ b/components/asters/sources/new-label-added/new-label-added.mjs @@ -0,0 +1,71 @@ +import common from "../common/base.mjs"; +import sampleEmit from "./test-event.mjs"; + +export default { + ...common, + key: "asters-new-label-added", + name: "New Label Added", + description: "Emit new event when a label is added to a post. [See the documentation](https://docs.asters.ai/api/endpoints/posts)", + type: "source", + version: "0.0.1", + dedupe: "unique", + props: { + ...common.props, + workspaceId: { + propDefinition: [ + common.props.asters, + "workspaceId", + ], + }, + socialAccountId: { + propDefinition: [ + common.props.asters, + "socialAccountId", + (c) => ({ + workspaceId: c.workspaceId, + }), + ], + }, + }, + methods: { + ...common.methods, + getResourceFn() { + return this.asters.listPosts; + }, + getArgs() { + return { + data: { + socialAccountId: this.socialAccountId, + filters: { + date: { + from: "1979-01-01", + to: new Date().toISOString(), + }, + }, + }, + }; + }, + async processResources(posts) { + const { data: allLabels } = await this.asters.listLabels({ + workspaceId: this.workspaceId, + }); + + for (const post of posts) { + const { labels = [] } = post; + for (const label of labels) { + const postLabel = allLabels.find((l) => l._id === label._id); + const meta = this.generateMeta(post, label); + this.$emit(postLabel, meta); + } + } + }, + generateMeta(post, label) { + return { + id: `${post._id}-${label._id}`, + summary: `New Label ${label._id} added to post ${post._id}`, + ts: Date.now(), + }; + }, + }, + sampleEmit, +}; diff --git a/components/asters/sources/new-label-added/test-event.mjs b/components/asters/sources/new-label-added/test-event.mjs new file mode 100644 index 0000000000000..4767a9eda016c --- /dev/null +++ b/components/asters/sources/new-label-added/test-event.mjs @@ -0,0 +1,5 @@ +export default { + "_id": "68656e18071c9698f12dcdeb", + "label": "label1", + "color": "blue" +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e27c4a5bf631d..526896c1bc517 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1048,7 +1048,11 @@ importers: specifier: ^1.5.1 version: 1.6.6 - components/asters: {} + components/asters: + dependencies: + '@pipedream/platform': + specifier: ^3.1.0 + version: 3.1.0 components/astica_ai: dependencies: @@ -29571,22 +29575,22 @@ packages: superagent@3.8.1: resolution: {integrity: sha512-VMBFLYgFuRdfeNQSMLbxGSLfmXL/xc+OO+BZp41Za/NRDBet/BNbkRJrYzCUu0u4GU0i/ml2dtT8b9qgkw9z6Q==} engines: {node: '>= 4.0'} - deprecated: Please upgrade to v7.0.2+ of superagent. We have fixed numerous issues with streams, form-data, attach(), filesystem errors not bubbling up (ENOENT on attach()), and all tests are now passing. See the releases tab for more information at . + deprecated: Please upgrade to v9.0.0+ as we have fixed a public vulnerability with formidable dependency. Note that v9.0.0+ requires Node.js v14.18.0+. See https://github.com/ladjs/superagent/pull/1800 for insight. This project is supported and maintained by the team at Forward Email @ https://forwardemail.net superagent@4.1.0: resolution: {integrity: sha512-FT3QLMasz0YyCd4uIi5HNe+3t/onxMyEho7C3PSqmti3Twgy2rXT4fmkTz6wRL6bTF4uzPcfkUCa8u4JWHw8Ag==} engines: {node: '>= 6.0'} - deprecated: Please upgrade to v7.0.2+ of superagent. We have fixed numerous issues with streams, form-data, attach(), filesystem errors not bubbling up (ENOENT on attach()), and all tests are now passing. See the releases tab for more information at . + deprecated: Please upgrade to v9.0.0+ as we have fixed a public vulnerability with formidable dependency. Note that v9.0.0+ requires Node.js v14.18.0+. See https://github.com/ladjs/superagent/pull/1800 for insight. This project is supported and maintained by the team at Forward Email @ https://forwardemail.net superagent@5.3.1: resolution: {integrity: sha512-wjJ/MoTid2/RuGCOFtlacyGNxN9QLMgcpYLDQlWFIhhdJ93kNscFonGvrpAHSCVjRVj++DGCglocF7Aej1KHvQ==} engines: {node: '>= 7.0.0'} - deprecated: Please upgrade to v7.0.2+ of superagent. We have fixed numerous issues with streams, form-data, attach(), filesystem errors not bubbling up (ENOENT on attach()), and all tests are now passing. See the releases tab for more information at . + deprecated: Please upgrade to v9.0.0+ as we have fixed a public vulnerability with formidable dependency. Note that v9.0.0+ requires Node.js v14.18.0+. See https://github.com/ladjs/superagent/pull/1800 for insight. This project is supported and maintained by the team at Forward Email @ https://forwardemail.net superagent@7.1.6: resolution: {integrity: sha512-gZkVCQR1gy/oUXr+kxJMLDjla434KmSOKbx5iGD30Ql+AkJQ/YlPKECJy2nhqOsHLjGHzoDTXNSjhnvWhzKk7g==} engines: {node: '>=6.4.0 <13 || >=14'} - deprecated: Please downgrade to v7.1.5 if you need IE/ActiveXObject support OR upgrade to v8.0.0 as we no longer support IE and published an incorrect patch version (see https://github.com/visionmedia/superagent/issues/1731) + deprecated: Please upgrade to v9.0.0+ as we have fixed a public vulnerability with formidable dependency. Note that v9.0.0+ requires Node.js v14.18.0+. See https://github.com/ladjs/superagent/pull/1800 for insight. This project is supported and maintained by the team at Forward Email @ https://forwardemail.net supports-color@2.0.0: resolution: {integrity: sha512-KKNVtd6pCYgPIKU4cp2733HWYCpplQhddZLBUryaAHou723x+FRzQ5Df824Fj+IyyuiQTRoub4SnIFfIcrp70g==}