-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Notion - updated-page source improvements #14045
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
3fc8cb0
81a2f28
43753fb
7229087
c0657fa
9e1e3a9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,13 +2,14 @@ import notion from "../../notion.app.mjs"; | |
| import sampleEmit from "./test-event.mjs"; | ||
| import base from "../common/base.mjs"; | ||
| import constants from "../common/constants.mjs"; | ||
| import md5 from "md5"; | ||
|
|
||
| export default { | ||
| ...base, | ||
| key: "notion-updated-page", | ||
| name: "Updated Page in Database", /* eslint-disable-line pipedream/source-name */ | ||
| description: "Emit new event when a page in a database is updated. To select a specific page, use `Updated Page ID` instead", | ||
| version: "0.0.16", | ||
| version: "0.0.17", | ||
| type: "source", | ||
| dedupe: "unique", | ||
| props: { | ||
|
|
@@ -42,14 +43,26 @@ export default { | |
| async deploy() { | ||
| const properties = await this.getProperties(); | ||
| const propertyValues = {}; | ||
| const pagesStream = this.notion.getPages(this.databaseId); | ||
| const params = this.lastUpdatedSortParam(); | ||
| const pagesStream = this.notion.getPages(this.databaseId, params); | ||
| let count = 0; | ||
| let lastUpdatedTimestamp = 0; | ||
| for await (const page of pagesStream) { | ||
| propertyValues[page.id] = {}; | ||
| for (const property of properties) { | ||
| propertyValues[page.id][property] = JSON.stringify(page.properties[property]); | ||
| propertyValues[page.id][property] = md5(JSON.stringify(page.properties[property])); | ||
| } | ||
| lastUpdatedTimestamp = Math.max( | ||
| lastUpdatedTimestamp, | ||
| Date.parse(page?.last_edited_time), | ||
| ); | ||
| if (count < 25) { | ||
michelle0927 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| this.emitEvent(page); | ||
| } | ||
| count++; | ||
| } | ||
| this._setPropertyValues(propertyValues); | ||
| this.setLastUpdatedTimestamp(lastUpdatedTimestamp); | ||
| }, | ||
| }, | ||
| methods: { | ||
|
|
@@ -70,20 +83,30 @@ export default { | |
| generateMeta(obj, summary) { | ||
| const { id } = obj; | ||
| const title = this.notion.extractPageTitle(obj); | ||
| const ts = Date.now(); | ||
| const ts = Date.parse(obj.last_edited_time); | ||
| return { | ||
| id: `${id}-${ts}`, | ||
| summary: `${summary}: ${title} - ${id}`, | ||
| ts, | ||
| }; | ||
| }, | ||
| emitEvent(page) { | ||
| const meta = this.generateMeta(page, constants.summaries.PAGE_UPDATED); | ||
| this.$emit(page, meta); | ||
| }, | ||
michelle0927 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| async run() { | ||
| const lastCheckedTimestamp = this.getLastUpdatedTimestamp(); | ||
| const propertyValues = this._getPropertyValues(); | ||
|
|
||
| const params = { | ||
| ...this.lastUpdatedSortParam(), | ||
| filter: { | ||
| timestamp: "last_edited_time", | ||
| last_edited_time: { | ||
| on_or_after: new Date(lastCheckedTimestamp).toISOString(), | ||
| }, | ||
| }, | ||
| }; | ||
| let newLastUpdatedTimestamp = lastCheckedTimestamp; | ||
| const properties = await this.getProperties(); | ||
|
|
@@ -97,7 +120,7 @@ export default { | |
|
|
||
| let propertyChangeFound = false; | ||
| for (const property of properties) { | ||
| const currentProperty = JSON.stringify(page.properties[property]); | ||
| const currentProperty = md5(JSON.stringify(page.properties[property])); | ||
| if (!propertyValues[page.id] || currentProperty !== propertyValues[page.id][property]) { | ||
| propertyChangeFound = true; | ||
| propertyValues[page.id] = { | ||
|
|
@@ -114,8 +137,11 @@ export default { | |
| continue; | ||
| } | ||
|
|
||
| const meta = this.generateMeta(page, constants.summaries.PAGE_UPDATED); | ||
| this.$emit(page, meta); | ||
| this.emitEvent(page); | ||
|
|
||
| if (Date.parse(page?.last_edited_time) < lastCheckedTimestamp) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it make sense to add back the HTTP request param to filter out pages where There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @andrewjschuang Ah, I didn't notice there's an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, based on my tests the Notion API was reliably updating the |
||
| break; | ||
| } | ||
michelle0927 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| this.setLastUpdatedTimestamp(newLastUpdatedTimestamp); | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.