@@ -2,7 +2,6 @@ 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" ;
65
76export default {
87 ...base ,
@@ -41,26 +40,27 @@ export default {
4140 } ,
4241 hooks : {
4342 async deploy ( ) {
44- const properties = await this . getProperties ( ) ;
43+ const propertiesToCheck = await this . getPropertiesToCheck ( ) ;
4544 const propertyValues = { } ;
4645 const params = this . lastUpdatedSortParam ( ) ;
4746 const pagesStream = this . notion . getPages ( this . databaseId , params ) ;
4847 let count = 0 ;
4948 let lastUpdatedTimestamp = 0 ;
5049 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 ;
50+ for ( const propertyName of propertiesToCheck ) {
51+ const currentValue = this . maybeRemoveFileSubItems ( page . properties [ propertyName ] ) ;
52+ propertyValues [ page . id ] = {
53+ ...propertyValues [ page . id ] ,
54+ [ propertyName ] : currentValue ,
55+ } ;
5556 }
5657 lastUpdatedTimestamp = Math . max (
5758 lastUpdatedTimestamp ,
58- Date . parse ( page ? .last_edited_time ) ,
59+ Date . parse ( page . last_edited_time ) ,
5960 ) ;
60- if ( count < 25 ) {
61- this . emitEvent ( page ) ;
61+ if ( count ++ < 25 ) {
62+ this . emitEvent ( page , [ ] ) ;
6263 }
63- count ++ ;
6464 }
6565 this . _setPropertyValues ( propertyValues ) ;
6666 this . setLastUpdatedTimestamp ( lastUpdatedTimestamp ) ;
@@ -74,18 +74,13 @@ export default {
7474 _setPropertyValues ( propertyValues ) {
7575 this . db . set ( "propertyValues" , propertyValues ) ;
7676 } ,
77- async getProperties ( ) {
77+ async getPropertiesToCheck ( ) {
7878 if ( this . properties ?. length ) {
7979 return this . properties ;
8080 }
8181 const { properties } = await this . notion . retrieveDatabase ( this . databaseId ) ;
8282 return Object . keys ( properties ) ;
8383 } ,
84- calculateHash ( property ) {
85- const clone = structuredClone ( property ) ;
86- this . maybeRemoveFileSubItems ( clone ) ;
87- return md5 ( JSON . stringify ( clone ) ) ;
88- } ,
8984 maybeRemoveFileSubItems ( property ) {
9085 // Files & Media type:
9186 // `url` and `expiry_time` are constantly updated by Notion, so ignore these fields
@@ -96,6 +91,7 @@ export default {
9691 }
9792 }
9893 }
94+ return property ;
9995 } ,
10096 generateMeta ( obj , summary ) {
10197 const { id } = obj ;
@@ -107,9 +103,13 @@ export default {
107103 ts,
108104 } ;
109105 } ,
110- emitEvent ( page ) {
106+ emitEvent ( page , changes ) {
111107 const meta = this . generateMeta ( page , constants . summaries . PAGE_UPDATED ) ;
112- this . $emit ( page , meta ) ;
108+ const event = {
109+ page,
110+ changes,
111+ } ;
112+ this . $emit ( event , meta ) ;
113113 } ,
114114 } ,
115115 async run ( ) {
@@ -126,38 +126,53 @@ export default {
126126 } ,
127127 } ;
128128 let newLastUpdatedTimestamp = lastCheckedTimestamp ;
129- const properties = await this . getProperties ( ) ;
129+ const propertiesToCheck = await this . getPropertiesToCheck ( ) ;
130130 const pagesStream = this . notion . getPages ( this . databaseId , params ) ;
131131
132132 for await ( const page of pagesStream ) {
133+ const changes = [ ] ;
134+ let propertyHasChanged = false ;
133135 newLastUpdatedTimestamp = Math . max (
134136 newLastUpdatedTimestamp ,
135- Date . parse ( page ? .last_edited_time ) ,
137+ Date . parse ( page . last_edited_time ) ,
136138 ) ;
137139
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 ;
140+ for ( const propertyName of propertiesToCheck ) {
141+ const previousValue = structuredClone ( propertyValues [ page . id ] ?. [ propertyName ] ) ;
142+ const currentValue = this . maybeRemoveFileSubItems ( page . properties [ propertyName ] ) ;
143+
144+ const pageExistsInDB = propertyValues [ page . id ] != null ;
145+ const propertyChanged = JSON . stringify ( previousValue ) !== JSON . stringify ( currentValue ) ;
146+
147+ if ( pageExistsInDB && propertyChanged ) {
148+ propertyHasChanged = true ;
144149 propertyValues [ page . id ] = {
145150 ...propertyValues [ page . id ] ,
146- [ propertyName ] : hash ,
151+ [ propertyName ] : currentValue ,
147152 } ;
153+ changes . push ( {
154+ previousValue,
155+ currentValue,
156+ } ) ;
148157 }
149- }
150- if ( ! propertyChangeFound && Date . parse ( page ?. last_edited_time ) <= lastCheckedTimestamp ) {
151- continue ;
152- }
153158
154- if ( ! this . includeNewPages && page ?. last_edited_time === page ?. created_time ) {
155- continue ;
159+ if ( ! pageExistsInDB && this . includeNewPages ) {
160+ propertyHasChanged = true ;
161+ propertyValues [ page . id ] = {
162+ [ propertyName ] : currentValue ,
163+ } ;
164+ changes . push ( {
165+ previousValue,
166+ currentValue,
167+ } ) ;
168+ }
156169 }
157170
158- this . emitEvent ( page ) ;
171+ if ( propertyHasChanged && lastCheckedTimestamp <= Date . parse ( page . last_edited_time ) ) {
172+ this . emitEvent ( page , changes ) ;
173+ }
159174
160- if ( Date . parse ( page ? .last_edited_time ) < lastCheckedTimestamp ) {
175+ if ( Date . parse ( page . last_edited_time ) < lastCheckedTimestamp ) {
161176 break ;
162177 }
163178 }
0 commit comments