@@ -2,14 +2,14 @@ import notion from "../../notion.app.mjs";
22import sampleEmit from "./test-event.mjs" ;
33import base from "../common/base.mjs" ;
44import constants from "../common/constants.mjs" ;
5- import md5 from "md5 " ;
5+ import zlib from "zlib " ;
66
77export default {
88 ...base ,
99 key : "notion-updated-page" ,
1010 name : "Updated Page in Database" , /* eslint-disable-line pipedream/source-name */
1111 description : "Emit new event when a page in a database is updated. To select a specific page, use `Updated Page ID` instead" ,
12- version : "0.0.19 " ,
12+ version : "0.1.0 " ,
1313 type : "source" ,
1414 dedupe : "unique" ,
1515 props : {
@@ -20,6 +20,12 @@ export default {
2020 "databaseId" ,
2121 ] ,
2222 } ,
23+ includeNewPages : {
24+ type : "boolean" ,
25+ label : "Include New Pages" ,
26+ description : "Set to `true` to emit events when pages are created. Set to `false` to ignore new pages." ,
27+ default : true ,
28+ } ,
2329 properties : {
2430 propDefinition : [
2531 notion ,
@@ -32,35 +38,30 @@ export default {
3238 description : "Only emit events when one or more of the selected properties have changed" ,
3339 optional : true ,
3440 } ,
35- includeNewPages : {
36- type : "boolean" ,
37- label : "Include New Pages" ,
38- description : "Set to `true` to emit events when pages are created. Set to `false` to ignore new pages." ,
39- default : true ,
40- } ,
4141 } ,
4242 hooks : {
4343 async deploy ( ) {
44- const properties = await this . getProperties ( ) ;
44+ const propertiesToCheck = await this . getPropertiesToCheck ( ) ;
4545 const propertyValues = { } ;
4646 const params = this . lastUpdatedSortParam ( ) ;
4747 const pagesStream = this . notion . getPages ( this . databaseId , params ) ;
4848 let count = 0 ;
4949 let lastUpdatedTimestamp = 0 ;
5050 for await ( const page of pagesStream ) {
51- propertyValues [ page . id ] = { } ;
52- for ( const propertyName of properties ) {
53- const hash = this . calculateHash ( page . properties [ propertyName ] ) ;
54- propertyValues [ page . id ] [ propertyName ] = hash ;
51+ for ( const propertyName of propertiesToCheck ) {
52+ const currentValue = this . maybeRemoveFileSubItems ( page . properties [ propertyName ] ) ;
53+ propertyValues [ page . id ] = {
54+ ...propertyValues [ page . id ] ,
55+ [ propertyName ] : currentValue ,
56+ } ;
5557 }
5658 lastUpdatedTimestamp = Math . max (
5759 lastUpdatedTimestamp ,
58- Date . parse ( page ? .last_edited_time ) ,
60+ Date . parse ( page . last_edited_time ) ,
5961 ) ;
60- if ( count < 25 ) {
62+ if ( count ++ < 25 ) {
6163 this . emitEvent ( page ) ;
6264 }
63- count ++ ;
6465 }
6566 this . _setPropertyValues ( propertyValues ) ;
6667 this . setLastUpdatedTimestamp ( lastUpdatedTimestamp ) ;
@@ -69,23 +70,23 @@ export default {
6970 methods : {
7071 ...base . methods ,
7172 _getPropertyValues ( ) {
72- return this . db . get ( "propertyValues" ) ;
73+ const compressed = this . db . get ( "propertyValues" ) ;
74+ const buffer = Buffer . from ( compressed , "base64" ) ;
75+ const decompressed = zlib . inflateSync ( buffer ) . toString ( ) ;
76+ return JSON . parse ( decompressed ) ;
7377 } ,
7478 _setPropertyValues ( propertyValues ) {
75- this . db . set ( "propertyValues" , propertyValues ) ;
79+ const string = JSON . stringify ( propertyValues ) ;
80+ const compressed = zlib . deflateSync ( string ) . toString ( "base64" ) ;
81+ this . db . set ( "propertyValues" , compressed ) ;
7682 } ,
77- async getProperties ( ) {
83+ async getPropertiesToCheck ( ) {
7884 if ( this . properties ?. length ) {
7985 return this . properties ;
8086 }
8187 const { properties } = await this . notion . retrieveDatabase ( this . databaseId ) ;
8288 return Object . keys ( properties ) ;
8389 } ,
84- calculateHash ( property ) {
85- const clone = structuredClone ( property ) ;
86- this . maybeRemoveFileSubItems ( clone ) ;
87- return md5 ( JSON . stringify ( clone ) ) ;
88- } ,
8990 maybeRemoveFileSubItems ( property ) {
9091 // Files & Media type:
9192 // `url` and `expiry_time` are constantly updated by Notion, so ignore these fields
@@ -96,20 +97,27 @@ export default {
9697 }
9798 }
9899 }
100+ return property ;
99101 } ,
100102 generateMeta ( obj , summary ) {
101103 const { id } = obj ;
102104 const title = this . notion . extractPageTitle ( obj ) ;
103105 const ts = Date . now ( ) ;
104106 return {
105107 id : `${ id } -${ ts } ` ,
106- summary : `${ summary } : ${ title } - ${ id } ` ,
108+ summary : `${ summary } : ${ title } ` ,
107109 ts,
108110 } ;
109111 } ,
110- emitEvent ( page ) {
111- const meta = this . generateMeta ( page , constants . summaries . PAGE_UPDATED ) ;
112- this . $emit ( page , meta ) ;
112+ emitEvent ( page , changes = [ ] , isNewPage = true ) {
113+ const meta = isNewPage
114+ ? this . generateMeta ( page , constants . summaries . PAGE_ADDED )
115+ : this . generateMeta ( page , constants . summaries . PAGE_UPDATED ) ;
116+ const event = {
117+ page,
118+ changes,
119+ } ;
120+ this . $emit ( event , meta ) ;
113121 } ,
114122 } ,
115123 async run ( ) {
@@ -126,39 +134,59 @@ export default {
126134 } ,
127135 } ;
128136 let newLastUpdatedTimestamp = lastCheckedTimestamp ;
129- const properties = await this . getProperties ( ) ;
137+ const propertiesToCheck = await this . getPropertiesToCheck ( ) ;
130138 const pagesStream = this . notion . getPages ( this . databaseId , params ) ;
131139
132140 for await ( const page of pagesStream ) {
141+ const changes = [ ] ;
142+ let isNewPage = false ;
143+ let propertyHasChanged = false ;
144+
133145 newLastUpdatedTimestamp = Math . max (
134146 newLastUpdatedTimestamp ,
135- Date . parse ( page ? .last_edited_time ) ,
147+ Date . parse ( page . last_edited_time ) ,
136148 ) ;
137149
138- let propertyChangeFound = false ;
139- for ( const propertyName of properties ) {
140- const hash = this . calculateHash ( page . properties [ propertyName ] ) ;
141- const dbValue = propertyValues [ page . id ] ?. [ propertyName ] ;
142- if ( ! propertyValues [ page . id ] || hash !== dbValue ) {
143- propertyChangeFound = true ;
150+ if ( lastCheckedTimestamp > Date . parse ( page . last_edited_time ) ) {
151+ break ;
152+ }
153+
154+ for ( const propertyName of propertiesToCheck ) {
155+ const previousValue = structuredClone ( propertyValues [ page . id ] ?. [ propertyName ] ) ;
156+ const currentValue = this . maybeRemoveFileSubItems ( page . properties [ propertyName ] ) ;
157+
158+ const pageExistsInDB = propertyValues [ page . id ] != null ;
159+ const propertyChanged = JSON . stringify ( previousValue ) !== JSON . stringify ( currentValue ) ;
160+
161+ if ( pageExistsInDB && propertyChanged ) {
162+ propertyHasChanged = true ;
144163 propertyValues [ page . id ] = {
145164 ...propertyValues [ page . id ] ,
146- [ propertyName ] : hash ,
165+ [ propertyName ] : currentValue ,
147166 } ;
167+ changes . push ( {
168+ property : propertyName ,
169+ previousValue,
170+ currentValue,
171+ } ) ;
148172 }
149- }
150- if ( ! propertyChangeFound && Date . parse ( page ?. last_edited_time ) <= lastCheckedTimestamp ) {
151- continue ;
152- }
153173
154- if ( ! this . includeNewPages && page ?. last_edited_time === page ?. created_time ) {
155- continue ;
174+ if ( ! pageExistsInDB && this . includeNewPages ) {
175+ isNewPage = true ;
176+ propertyHasChanged = true ;
177+ propertyValues [ page . id ] = {
178+ [ propertyName ] : currentValue ,
179+ } ;
180+ changes . push ( {
181+ property : propertyName ,
182+ previousValue,
183+ currentValue,
184+ } ) ;
185+ }
156186 }
157187
158- this . emitEvent ( page ) ;
159-
160- if ( Date . parse ( page ?. last_edited_time ) < lastCheckedTimestamp ) {
161- break ;
188+ if ( propertyHasChanged ) {
189+ this . emitEvent ( page , changes , isNewPage ) ;
162190 }
163191 }
164192
0 commit comments