Skip to content

Commit 7971ba9

Browse files
authored
Merge branch 'master' into 13927-github-actions-improvements
2 parents c08842a + 41b3962 commit 7971ba9

File tree

5 files changed

+105
-48
lines changed

5 files changed

+105
-48
lines changed

components/moaform/moaform.app.mjs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default {
2+
type: "app",
3+
app: "moaform",
4+
propDefinitions: {},
5+
methods: {
6+
// this.$auth contains connected account data
7+
authKeys() {
8+
console.log(Object.keys(this.$auth));
9+
},
10+
},
11+
};

components/moaform/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "@pipedream/moaform",
3+
"version": "0.0.1",
4+
"description": "Pipedream Moaform Components",
5+
"main": "moaform.app.mjs",
6+
"keywords": [
7+
"pipedream",
8+
"moaform"
9+
],
10+
"homepage": "https://pipedream.com/apps/moaform",
11+
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
12+
"publishConfig": {
13+
"access": "public"
14+
}
15+
}

components/notion/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/notion",
3-
"version": "0.1.24",
3+
"version": "0.2.0",
44
"description": "Pipedream Notion Components",
55
"main": "notion.app.mjs",
66
"keywords": [

components/notion/sources/updated-page/updated-page.mjs

Lines changed: 75 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import notion from "../../notion.app.mjs";
22
import sampleEmit from "./test-event.mjs";
33
import base from "../common/base.mjs";
44
import constants from "../common/constants.mjs";
5-
import md5 from "md5";
5+
import zlib from "zlib";
66

77
export 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

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)