From 03b056d4d926fb99917c75a9f98f5af33894deb6 Mon Sep 17 00:00:00 2001 From: Luan Cazarine Date: Thu, 12 Sep 2024 16:11:34 -0300 Subject: [PATCH] adhook init --- .../create-calendar-event.mjs | 67 +++++++ .../create-update-post/create-update-post.mjs | 72 +++++++ components/adhook/adhook.app.mjs | 181 +++++++++++++++++- components/adhook/package.json | 2 +- .../new-or-updated-post.mjs | 110 +++++++++++ .../adhook/sources/new-post/new-post.mjs | 98 ++++++++++ .../sources/updated-post/updated-post.mjs | 105 ++++++++++ 7 files changed, 631 insertions(+), 4 deletions(-) create mode 100644 components/adhook/actions/create-calendar-event/create-calendar-event.mjs create mode 100644 components/adhook/actions/create-update-post/create-update-post.mjs create mode 100644 components/adhook/sources/new-or-updated-post/new-or-updated-post.mjs create mode 100644 components/adhook/sources/new-post/new-post.mjs create mode 100644 components/adhook/sources/updated-post/updated-post.mjs diff --git a/components/adhook/actions/create-calendar-event/create-calendar-event.mjs b/components/adhook/actions/create-calendar-event/create-calendar-event.mjs new file mode 100644 index 0000000000000..7f73e9453c2ca --- /dev/null +++ b/components/adhook/actions/create-calendar-event/create-calendar-event.mjs @@ -0,0 +1,67 @@ +import adhook from "../../adhook.app.mjs"; +import { axios } from "@pipedream/platform"; + +export default { + key: "adhook-create-calendar-event", + name: "Create Calendar Event", + description: "Generates a personalized calendar event in AdHook. [See the documentation](https://app.adhook.io/api-doc/)", + version: "0.0.{{ts}}", + type: "action", + props: { + adhook, + eventName: { + propDefinition: [ + adhook, + "eventName", + ], + }, + startDate: { + propDefinition: [ + adhook, + "startDate", + ], + }, + endDate: { + propDefinition: [ + adhook, + "endDate", + ], + }, + eventDescription: { + propDefinition: [ + adhook, + "eventDescription", + ], + optional: true, + }, + attendees: { + propDefinition: [ + adhook, + "attendees", + ], + optional: true, + }, + attachments: { + propDefinition: [ + adhook, + "attachments", + ], + optional: true, + }, + }, + async run({ $ }) { + const response = await this.adhook.createCalendarEvent({ + data: { + eventName: this.eventName, + startDate: this.startDate, + endDate: this.endDate, + eventDescription: this.eventDescription, + attendees: this.attendees, + attachments: this.attachments, + }, + }); + + $.export("$summary", `Successfully created calendar event: ${this.eventName}`); + return response; + }, +}; diff --git a/components/adhook/actions/create-update-post/create-update-post.mjs b/components/adhook/actions/create-update-post/create-update-post.mjs new file mode 100644 index 0000000000000..761dc7ec35e84 --- /dev/null +++ b/components/adhook/actions/create-update-post/create-update-post.mjs @@ -0,0 +1,72 @@ +import adhook from "../../adhook.app.mjs"; +import { axios } from "@pipedream/platform"; + +export default { + key: "adhook-create-update-post", + name: "Create or Update Post", + description: "Adds a new post or modifies an existing post in Adhook. [See the documentation](https://app.adhook.io/api-doc/)", + version: "0.0.{{ts}}", + type: "action", + props: { + adhook, + postId: { + propDefinition: [ + adhook, + "postId", + ], + optional: true, + }, + postContent: { + propDefinition: [ + adhook, + "postContent", + ], + }, + visibility: { + propDefinition: [ + adhook, + "visibility", + ], + }, + postAttachments: { + propDefinition: [ + adhook, + "postAttachments", + ], + optional: true, + }, + mentionUsers: { + propDefinition: [ + adhook, + "mentionUsers", + ], + optional: true, + }, + }, + async run({ $ }) { + const data = { + postContent: this.postContent, + visibility: this.visibility, + postAttachments: this.postAttachments, + mentionUsers: this.mentionUsers, + }; + + if (this.postId) { + data.postId = this.postId; + } + + const response = await axios($, { + method: "POST", + url: `${this.adhook._baseUrl()}/posts`, + headers: { + Authorization: `Bearer ${this.adhook.$auth.oauth_access_token}`, + }, + data, + }); + + $.export("$summary", `Successfully ${this.postId + ? "updated" + : "created"} post`); + return response; + }, +}; diff --git a/components/adhook/adhook.app.mjs b/components/adhook/adhook.app.mjs index bfbca6eb55ab1..341a6c8c5d7d7 100644 --- a/components/adhook/adhook.app.mjs +++ b/components/adhook/adhook.app.mjs @@ -1,11 +1,186 @@ +import { axios } from "@pipedream/platform"; + export default { type: "app", app: "adhook", - propDefinitions: {}, + propDefinitions: { + postType: { + type: "string", + label: "Post Type", + description: "The type of the post", + optional: true, + }, + postAuthor: { + type: "string", + label: "Post Author", + description: "The author of the post", + optional: true, + }, + postTags: { + type: "string[]", + label: "Post Tags", + description: "Tags associated with the post", + optional: true, + }, + postId: { + type: "string", + label: "Post ID", + description: "The ID of the post", + }, + isNew: { + type: "boolean", + label: "Is New", + description: "Indicates if the post is new", + optional: true, + }, + updateType: { + type: "string", + label: "Update Type", + description: "Type of update made to the post", + optional: true, + }, + eventName: { + type: "string", + label: "Event Name", + description: "Name of the calendar event", + }, + startDate: { + type: "string", + label: "Start Date", + description: "Start date of the event", + }, + endDate: { + type: "string", + label: "End Date", + description: "End date of the event", + }, + eventDescription: { + type: "string", + label: "Event Description", + description: "Description of the event", + optional: true, + }, + attendees: { + type: "string[]", + label: "Attendees", + description: "List of attendees for the event", + optional: true, + }, + attachments: { + type: "string[]", + label: "Attachments", + description: "Attachments for the event", + optional: true, + }, + postContent: { + type: "string", + label: "Post Content", + description: "Content of the post", + }, + visibility: { + type: "string", + label: "Visibility", + description: "Visibility setting of the post", + }, + postAttachments: { + type: "string[]", + label: "Post Attachments", + description: "Attachments for the post", + optional: true, + }, + mentionUsers: { + type: "string[]", + label: "Mention Users", + description: "Users to mention in the post", + optional: true, + }, + }, methods: { - // this.$auth contains connected account data + _baseUrl() { + return "https://app.adhook.io/api"; + }, + async _makeRequest(opts = {}) { + const { + $ = this, method = "GET", path = "/", headers, ...otherOpts + } = opts; + return axios($, { + ...otherOpts, + method, + url: this._baseUrl() + path, + headers: { + ...headers, + Authorization: `Bearer ${this.$auth.oauth_access_token}`, + }, + }); + }, + async emitNewPostEvent(opts = {}) { + return this._makeRequest({ + method: "POST", + path: "/events/new_post", + data: { + postType: this.postType, + postAuthor: this.postAuthor, + postTags: this.postTags, + }, + ...opts, + }); + }, + async emitPostCreatedOrUpdatedEvent(opts = {}) { + return this._makeRequest({ + method: "POST", + path: "/events/post_created_or_updated", + data: { + postId: this.postId, + postAuthor: this.postAuthor, + postTags: this.postTags, + isNew: this.isNew, + }, + ...opts, + }); + }, + async emitPostUpdatedEvent(opts = {}) { + return this._makeRequest({ + method: "POST", + path: "/events/post_updated", + data: { + postId: this.postId, + postAuthor: this.postAuthor, + postTags: this.postTags, + updateType: this.updateType, + }, + ...opts, + }); + }, + async createCalendarEvent(opts = {}) { + return this._makeRequest({ + method: "POST", + path: "/events/calendar", + data: { + eventName: this.eventName, + startDate: this.startDate, + endDate: this.endDate, + eventDescription: this.eventDescription, + attendees: this.attendees, + attachments: this.attachments, + }, + ...opts, + }); + }, + async addOrUpdatePost(opts = {}) { + return this._makeRequest({ + method: "POST", + path: "/posts", + data: { + postContent: this.postContent, + visibility: this.visibility, + postAttachments: this.postAttachments, + mentionUsers: this.mentionUsers, + }, + ...opts, + }); + }, authKeys() { console.log(Object.keys(this.$auth)); }, }, -}; \ No newline at end of file +}; diff --git a/components/adhook/package.json b/components/adhook/package.json index bf2afd366cb66..7f87156e011f1 100644 --- a/components/adhook/package.json +++ b/components/adhook/package.json @@ -12,4 +12,4 @@ "publishConfig": { "access": "public" } -} \ No newline at end of file +} diff --git a/components/adhook/sources/new-or-updated-post/new-or-updated-post.mjs b/components/adhook/sources/new-or-updated-post/new-or-updated-post.mjs new file mode 100644 index 0000000000000..705effe572388 --- /dev/null +++ b/components/adhook/sources/new-or-updated-post/new-or-updated-post.mjs @@ -0,0 +1,110 @@ +import adhook from "../../adhook.app.mjs"; +import { axios } from "@pipedream/platform"; + +export default { + key: "adhook-new-or-updated-post", + name: "New or Updated Post", + description: "Emit new event when a new post is created or an existing post is updated. [See the documentation](https://app.adhook.io/api-doc/)", + version: "0.0.{{ts}}", + type: "source", + dedupe: "unique", + props: { + adhook, + db: "$.service.db", + timer: { + type: "$.interface.timer", + default: { + intervalSeconds: 60, + }, + }, + postId: { + propDefinition: [ + adhook, + "postId", + ], + }, + postAuthor: { + propDefinition: [ + adhook, + "postAuthor", + ], + optional: true, + }, + postTags: { + propDefinition: [ + adhook, + "postTags", + ], + optional: true, + }, + isNew: { + propDefinition: [ + adhook, + "isNew", + ], + optional: true, + }, + }, + hooks: { + async deploy() { + const events = await this.adhook.emitPostCreatedOrUpdatedEvent({ + postId: this.postId, + postAuthor: this.postAuthor, + postTags: this.postTags, + isNew: this.isNew, + }); + + for (const event of events.slice(0, 50)) { + this.$emit(event, { + id: event.postId, + summary: `Post ${event.isNew + ? "created" + : "updated"}: ${event.postId}`, + ts: Date.now(), + }); + } + }, + async activate() { + // Activate hook logic if needed + }, + async deactivate() { + // Deactivate hook logic if needed + }, + }, + methods: { + _getLastTimestamp() { + return this.db.get("lastTimestamp") || 0; + }, + _setLastTimestamp(ts) { + this.db.set("lastTimestamp", ts); + }, + }, + async run() { + const lastTimestamp = this._getLastTimestamp(); + const events = await this.adhook.emitPostCreatedOrUpdatedEvent({ + postId: this.postId, + postAuthor: this.postAuthor, + postTags: this.postTags, + isNew: this.isNew, + }); + + for (const event of events) { + const eventTimestamp = new Date(event.updatedAt).getTime(); + if (eventTimestamp > lastTimestamp) { + this.$emit(event, { + id: event.postId, + summary: `Post ${event.isNew + ? "created" + : "updated"}: ${event.postId}`, + ts: eventTimestamp, + }); + } + } + + if (events.length > 0) { + const latestEvent = events[0]; + const latestTimestamp = new Date(latestEvent.updatedAt).getTime(); + this._setLastTimestamp(latestTimestamp); + } + }, +}; diff --git a/components/adhook/sources/new-post/new-post.mjs b/components/adhook/sources/new-post/new-post.mjs new file mode 100644 index 0000000000000..88c84775c189b --- /dev/null +++ b/components/adhook/sources/new-post/new-post.mjs @@ -0,0 +1,98 @@ +import { axios } from "@pipedream/platform"; +import adhook from "../../adhook.app.mjs"; + +export default { + key: "adhook-new-post", + name: "New Post Created", + description: "Emit new event when a new post is created. [See the documentation](https://app.adhook.io/api-doc/)", + version: "0.0.{{ts}}", + type: "source", + dedupe: "unique", + props: { + adhook, + db: "$.service.db", + timer: { + type: "$.interface.timer", + default: { + intervalSeconds: 60, + }, + }, + postType: { + propDefinition: [ + adhook, + "postType", + ], + }, + postAuthor: { + propDefinition: [ + adhook, + "postAuthor", + ], + }, + postTags: { + propDefinition: [ + adhook, + "postTags", + ], + }, + }, + hooks: { + async deploy() { + const posts = await this.adhook.emitNewPostEvent({ + params: { + postType: this.postType, + postAuthor: this.postAuthor, + postTags: this.postTags, + }, + }); + + for (const post of posts.slice(0, 50)) { + this.$emit(post, { + id: post.id, + summary: `New Post: ${post.title}`, + ts: new Date(post.createdAt).getTime(), + }); + } + }, + async activate() { + // Activate hook, if needed + }, + async deactivate() { + // Deactivate hook, if needed + }, + }, + methods: { + _getLastTimestamp() { + return this.db.get("lastTimestamp") || 0; + }, + _setLastTimestamp(timestamp) { + this.db.set("lastTimestamp", timestamp); + }, + }, + async run() { + const lastTimestamp = this._getLastTimestamp(); + + const posts = await this.adhook.emitNewPostEvent({ + params: { + postType: this.postType, + postAuthor: this.postAuthor, + postTags: this.postTags, + }, + }); + + for (const post of posts) { + const postTimestamp = new Date(post.createdAt).getTime(); + if (postTimestamp > lastTimestamp) { + this.$emit(post, { + id: post.id, + summary: `New Post: ${post.title}`, + ts: postTimestamp, + }); + } + } + + if (posts.length > 0) { + this._setLastTimestamp(new Date(posts[0].createdAt).getTime()); + } + }, +}; diff --git a/components/adhook/sources/updated-post/updated-post.mjs b/components/adhook/sources/updated-post/updated-post.mjs new file mode 100644 index 0000000000000..6d480dd09fc53 --- /dev/null +++ b/components/adhook/sources/updated-post/updated-post.mjs @@ -0,0 +1,105 @@ +import { axios } from "@pipedream/platform"; +import adhook from "../../adhook.app.mjs"; + +export default { + key: "adhook-updated-post", + name: "Adhook Updated Post", + description: "Emit new event when a post is updated. [See the documentation](https://app.adhook.io/api-doc/)", + version: "0.0.{{ts}}", + type: "source", + dedupe: "unique", + props: { + adhook, + db: "$.service.db", + timer: { + type: "$.interface.timer", + default: { + intervalSeconds: 60, + }, + }, + postId: { + propDefinition: [ + adhook, + "postId", + ], + }, + postAuthor: { + propDefinition: [ + adhook, + "postAuthor", + ], + optional: true, + }, + postTags: { + propDefinition: [ + adhook, + "postTags", + ], + optional: true, + }, + updateType: { + propDefinition: [ + adhook, + "updateType", + ], + optional: true, + }, + }, + hooks: { + async deploy() { + const posts = await this.adhook.emitPostUpdatedEvent({ + data: { + postId: this.postId, + postAuthor: this.postAuthor, + postTags: this.postTags, + updateType: this.updateType, + }, + }); + + for (const post of posts.slice(0, 50)) { + this.$emit(post, { + id: post.id, + summary: `Post Updated: ${post.title}`, + ts: Date.parse(post.updated_at), + }); + } + }, + async activate() { + // Any activation logic if needed + }, + async deactivate() { + // Any deactivation logic if needed + }, + }, + methods: { + _getLastTimestamp() { + return this.db.get("lastTimestamp") || 0; + }, + _setLastTimestamp(ts) { + this.db.set("lastTimestamp", ts); + }, + }, + async run() { + const lastTimestamp = this._getLastTimestamp(); + const posts = await this.adhook.emitPostUpdatedEvent({ + data: { + postId: this.postId, + postAuthor: this.postAuthor, + postTags: this.postTags, + updateType: this.updateType, + }, + }); + + for (const post of posts) { + const postTimestamp = Date.parse(post.updated_at); + if (postTimestamp > lastTimestamp) { + this.$emit(post, { + id: post.id, + summary: `Post Updated: ${post.title}`, + ts: postTimestamp, + }); + this._setLastTimestamp(postTimestamp); + } + } + }, +};