diff --git a/packages/rule-handler/src/filter.ts b/packages/rule-handler/src/filter.ts index e55226a1d..907c8d88f 100644 --- a/packages/rule-handler/src/filter.ts +++ b/packages/rule-handler/src/filter.ts @@ -14,7 +14,7 @@ interface Edge { interface Page { id: string - labels: Label[] + labels?: Label[] // labels is optional in the API response isArchived: boolean readingProgressPercent: number } diff --git a/packages/rule-handler/src/rule.ts b/packages/rule-handler/src/rule.ts index c8e35a4dc..e8e5a7ce0 100644 --- a/packages/rule-handler/src/rule.ts +++ b/packages/rule-handler/src/rule.ts @@ -93,38 +93,43 @@ export const triggerActions = async ( rule.actions.forEach((action) => { switch (action.type) { case RuleActionType.AddLabel: { - const existingLabelIds = filteredPage.labels.map((label) => label.id) - const newLabelIds = action.params - if (newLabelIds.every((id) => existingLabelIds.includes(id))) { - // All labels are already set - return - } + const existingLabelIds = + filteredPage.labels?.map((label) => label.id) || [] + const labelIdsToSet = [...existingLabelIds] - // combine existing labels with new labels in a set to avoid duplicates - const labelIds = new Set([...existingLabelIds, ...newLabelIds]) + // combine existing labels with new labels to avoid duplicates + action.params.forEach((newLabelId) => { + if (!labelIdsToSet.includes(newLabelId)) { + labelIdsToSet.push(newLabelId) + } + }) - actionPromises.push( - setLabels(apiEndpoint, authToken, data.id, Array.from(labelIds)) + // call the api if it has new labels to set + return ( + labelIdsToSet.length > existingLabelIds.length && + actionPromises.push( + setLabels(apiEndpoint, authToken, data.id, labelIdsToSet) + ) ) - break } case RuleActionType.Archive: - !filteredPage.isArchived && + return ( + !filteredPage.isArchived && actionPromises.push(archivePage(apiEndpoint, authToken, data.id)) - break + ) case RuleActionType.MarkAsRead: - filteredPage.readingProgressPercent < 100 && + return ( + filteredPage.readingProgressPercent < 100 && actionPromises.push(markPageAsRead(apiEndpoint, authToken, data.id)) - break + ) case RuleActionType.SendNotification: - actionPromises.push( + return actionPromises.push( sendNotification( apiEndpoint, authToken, 'New page added to your feed' ) ) - break } }) }