fix(Notion Trigger (Beta) Node): Fix Notion trigger polling strategy

This commit is contained in:
Ugo Bataillard 2022-11-09 19:26:13 +09:00 committed by GitHub
parent bae3098e4e
commit 3b3405089d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 97 additions and 36 deletions

View file

@ -1,7 +1,10 @@
import { request } from 'http';
import { import {
IAuthenticateGeneric, IAuthenticateGeneric,
ICredentialDataDecryptedObject,
ICredentialTestRequest, ICredentialTestRequest,
ICredentialType, ICredentialType,
IHttpRequestOptions,
INodeProperties, INodeProperties,
} from 'n8n-workflow'; } from 'n8n-workflow';
@ -24,13 +27,25 @@ export class NotionApi implements ICredentialType {
url: '/users', url: '/users',
}, },
}; };
authenticate: IAuthenticateGeneric = { async authenticate(
type: 'generic', credentials: ICredentialDataDecryptedObject,
properties: { requestOptions: IHttpRequestOptions,
headers: { ): Promise<IHttpRequestOptions> {
Authorization: '=Bearer {{$credentials.apiKey}}', requestOptions.headers = {
'Notion-Version': '2021-05-13', ...requestOptions.headers,
}, Authorization: `Bearer ${credentials.apiKey} `,
}, };
};
// if version it's not set, set it to last one
// version is only set when the request is made from
// the notion node, or was set explicitly in the http node
if (!requestOptions.headers['Notion-Version']) {
requestOptions.headers = {
...requestOptions.headers,
'Notion-Version': '2022-02-22',
};
}
return requestOptions;
}
} }

View file

@ -32,7 +32,7 @@ export const blockOperations: INodeProperties[] = [
}, },
]; ];
export const blockFields = [ export const blockFields: INodeProperties[] = [
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* block:append */ /* block:append */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
@ -101,4 +101,4 @@ export const blockFields = [
default: 50, default: 50,
description: 'Max number of results to return', description: 'Max number of results to return',
}, },
] as INodeProperties[]; ];

View file

@ -85,6 +85,19 @@ export const databaseFields: INodeProperties[] = [
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* database:getAll */ /* database:getAll */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
{
displayName:
'In Notion, make sure you <a href="https://www.notion.so/help/add-and-manage-connections-with-the-api#add-connections-to-pages" target="_blank">share your database with your integration</a> . Otherwise it won\'t be accessible, or listed here.',
name: 'notionNotice',
type: 'notice',
default: '',
displayOptions: {
show: {
resource: ['database'],
operation: ['getAll'],
},
},
},
{ {
displayName: 'Return All', displayName: 'Return All',
name: 'returnAll', name: 'returnAll',

View file

@ -81,7 +81,7 @@ export const databasePageOperations: INodeProperties[] = [
}, },
]; ];
export const databasePageFields = [ export const databasePageFields: INodeProperties[] = [
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* databasePage:create */ /* databasePage:create */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */

View file

@ -219,12 +219,11 @@ function getTexts(
annotations: text.annotationUi, annotations: text.annotationUi,
}); });
} else { } else {
//@ts-ignore
results.push({ results.push({
type: 'mention', type: 'mention',
mention: { mention: {
type: text.mentionType, type: text.mentionType,
//@ts-ignore //@ts-expect-error any
[text.mentionType]: { id: text[text.mentionType] as string }, [text.mentionType]: { id: text[text.mentionType] as string },
}, },
annotations: text.annotationUi, annotations: text.annotationUi,
@ -252,9 +251,8 @@ export function formatBlocks(blocks: IDataObject[]) {
[block.type as string]: { [block.type as string]: {
...(block.type === 'to_do' ? { checked: block.checked } : {}), ...(block.type === 'to_do' ? { checked: block.checked } : {}),
// prettier-ignore // prettier-ignore
//@ts-expect-error
// tslint:disable-next-line: no-any // tslint:disable-next-line: no-any
text: (block.richText === false) ? formatText(block.textContent).text : getTexts(block.text.text as any || []), text: (block.richText === false) ? formatText(block.textContent as string).text : getTexts((block.text as IDataObject).text as any || []),
}, },
}); });
} }
@ -266,7 +264,7 @@ function getPropertyKeyValue(value: any, type: string, timezone: string, version
// tslint:disable-next-line: no-any // tslint:disable-next-line: no-any
const ignoreIfEmpty = <T>(v: T, cb: (v: T) => any) => const ignoreIfEmpty = <T>(v: T, cb: (v: T) => any) =>
!v && value.ignoreIfEmpty ? undefined : cb(v); !v && value.ignoreIfEmpty ? undefined : cb(v);
let result = {}; let result: IDataObject = {};
switch (type) { switch (type) {
case 'rich_text': case 'rich_text':
@ -362,7 +360,6 @@ function getPropertyKeyValue(value: any, type: string, timezone: string, version
//if the date was left empty, set it to null so it resets the value in notion //if the date was left empty, set it to null so it resets the value in notion
if (value.date === '' || (value.dateStart === '' && value.dateEnd === '')) { if (value.date === '' || (value.dateStart === '' && value.dateEnd === '')) {
//@ts-ignore
result.date = null; result.date = null;
} }

View file

@ -51,11 +51,11 @@ export class NotionTrigger implements INodeType {
}, },
], ],
required: true, required: true,
default: '', default: 'pageAddedToDatabase',
}, },
{ {
displayName: displayName:
"In Notion, make sure you share your database with your integration. Otherwise it won't be accessible, or listed here.", 'In Notion, make sure you <a href="https://www.notion.so/help/add-and-manage-connections-with-the-api#add-connections-to-pages" target="_blank">share your database with your integration</a> . Otherwise it won\'t be accessible, or listed here.',
name: 'notionNotice', name: 'notionNotice',
type: 'notice', type: 'notice',
default: '', default: '',
@ -127,16 +127,24 @@ export class NotionTrigger implements INodeType {
const event = this.getNodeParameter('event') as string; const event = this.getNodeParameter('event') as string;
const simple = this.getNodeParameter('simple') as boolean; const simple = this.getNodeParameter('simple') as boolean;
const now = moment().utc().format(); const lastTimeChecked = webhookData.lastTimeChecked
? moment(webhookData.lastTimeChecked as string)
: moment().set({ second: 0, millisecond: 0 }); // Notion timestamp accuracy is only down to the minute
const startDate = (webhookData.lastTimeChecked as string) || now; // update lastTimeChecked to now
webhookData.lastTimeChecked = moment().set({ second: 0, millisecond: 0 });
const endDate = now; // because Notion timestamp accuracy is only down to the minute some duplicates can be fetch
const possibleDuplicates = (webhookData.possibleDuplicates as string[]) ?? [];
webhookData.lastTimeChecked = endDate;
const sortProperty = event === 'pageAddedToDatabase' ? 'created_time' : 'last_edited_time'; const sortProperty = event === 'pageAddedToDatabase' ? 'created_time' : 'last_edited_time';
const option: IDataObject = {
headers: {
'Notion-Version': '2022-02-22',
},
};
const body: IDataObject = { const body: IDataObject = {
page_size: 1, page_size: 1,
sorts: [ sorts: [
@ -145,6 +153,14 @@ export class NotionTrigger implements INodeType {
direction: 'descending', direction: 'descending',
}, },
], ],
...(this.getMode() !== 'manual' && {
filter: {
timestamp: sortProperty,
[sortProperty]: {
on_or_after: lastTimeChecked.utc().format(),
},
},
}),
}; };
let records: IDataObject[] = []; let records: IDataObject[] = [];
@ -157,6 +173,9 @@ export class NotionTrigger implements INodeType {
'POST', 'POST',
`/databases/${databaseId}/query`, `/databases/${databaseId}/query`,
body, body,
{},
'',
option,
); );
if (this.getMode() === 'manual') { if (this.getMode() === 'manual') {
@ -169,7 +188,7 @@ export class NotionTrigger implements INodeType {
} }
// if something changed after the last check // if something changed after the last check
if (Object.keys(data[0]).length !== 0 && webhookData.lastRecordProccesed !== data[0].id) { if (Array.isArray(data) && data.length && Object.keys(data[0]).length !== 0) {
do { do {
body.page_size = 10; body.page_size = 10;
const { results, has_more, next_cursor } = await notionApiRequest.call( const { results, has_more, next_cursor } = await notionApiRequest.call(
@ -177,29 +196,46 @@ export class NotionTrigger implements INodeType {
'POST', 'POST',
`/databases/${databaseId}/query`, `/databases/${databaseId}/query`,
body, body,
{},
'',
option,
); );
records.push.apply(records, results); records.push(...results);
hasMore = has_more; hasMore = has_more;
if (next_cursor !== null) { if (next_cursor !== null) {
body['start_cursor'] = next_cursor; body['start_cursor'] = next_cursor;
} }
// Only stop when we reach records strictly before last recorded time to be sure we catch records from the same minute
} while ( } while (
!moment(records[records.length - 1][sortProperty] as string).isSameOrBefore(startDate) && !moment(records[records.length - 1][sortProperty] as string).isBefore(lastTimeChecked) &&
hasMore === true hasMore === true
); );
if (this.getMode() !== 'manual') { // Filter out already processed left over records:
records = records.filter((record: IDataObject) => // with a time strictly before the last record processed
moment(record[sortProperty] as string).isBetween(startDate, endDate), // or from the same minute not present in the list of processed records
); records = records.filter(
(record: IDataObject) => !possibleDuplicates.includes(record.id as string),
);
// Save the time of the most recent record processed
if (records[0]) {
const latestTimestamp = moment(records[0][sortProperty] as string);
// Save record ids with the same timestamp as the latest processed records
webhookData.possibleDuplicates = records
.filter((record: IDataObject) =>
moment(record[sortProperty] as string).isSame(latestTimestamp),
)
.map((record: IDataObject) => record.id);
} else {
webhookData.possibleDuplicates = undefined;
} }
if (simple === true) { if (simple === true) {
records = simplifyObjects(records, false, 1); records = simplifyObjects(records, false, 1);
} }
webhookData.lastRecordProccesed = data[0].id;
if (Array.isArray(records) && records.length) { if (Array.isArray(records) && records.length) {
return [this.helpers.returnJsonArray(records)]; return [this.helpers.returnJsonArray(records)];
} }

View file

@ -71,7 +71,7 @@ export const pageOperations: INodeProperties[] = [
}, },
]; ];
export const pageFields = [ export const pageFields: INodeProperties[] = [
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* page:archive */ /* page:archive */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
@ -355,4 +355,4 @@ export const pageFields = [
}, },
], ],
}, },
] as INodeProperties[]; ];