Skip to content

Commit 910f3b9

Browse files
committed
sources updates
1 parent 82de7be commit 910f3b9

File tree

28 files changed

+1267
-154
lines changed

28 files changed

+1267
-154
lines changed

components/trello/sources/card-archived/card-archived.mjs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import common from "../common/common-webhook.mjs";
2+
import sampleEmit from "./test-event.mjs";
23

34
export default {
45
...common,
@@ -16,14 +17,14 @@ export default {
1617
],
1718
},
1819
lists: {
19-
optional: true,
2020
propDefinition: [
2121
common.props.app,
2222
"lists",
2323
(c) => ({
2424
board: c.board,
2525
}),
2626
],
27+
description: "If specified, events will only be emitted when a card in one of the selected lists is archived",
2728
},
2829
},
2930
methods: {
@@ -37,12 +38,12 @@ export default {
3738
getSortField() {
3839
return "dateLastActivity";
3940
},
40-
isCorrectEventType(event) {
41-
return event.body?.action?.display?.translationKey === "action_archived_card";
41+
isCorrectEventType({ display }) {
42+
return display?.translationKey === "action_archived_card";
4243
},
43-
getResult(event) {
44+
getResult({ data }) {
4445
return this.app.getCard({
45-
cardId: event.body?.action?.data?.card?.id,
46+
cardId: data?.card?.id,
4647
});
4748
},
4849
isRelevant({ result: card }) {
@@ -53,4 +54,5 @@ export default {
5354
);
5455
},
5556
},
57+
sampleEmit,
5658
};
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
export default {
2+
"id": "5f4ee830046a92139596173a",
3+
"badges": {
4+
"attachmentsByType": {
5+
"trello": {
6+
"board": 0,
7+
"card": 0
8+
}
9+
},
10+
"externalSource": null,
11+
"location": false,
12+
"votes": 0,
13+
"viewingMemberVoted": false,
14+
"subscribed": false,
15+
"fogbugz": "",
16+
"checkItems": 0,
17+
"checkItemsChecked": 0,
18+
"checkItemsEarliestDue": null,
19+
"comments": 0,
20+
"attachments": 0,
21+
"description": false,
22+
"due": null,
23+
"dueComplete": false,
24+
"start": null,
25+
"lastUpdatedByAi": false
26+
},
27+
"checkItemStates": [],
28+
"closed": true,
29+
"dueComplete": false,
30+
"dateLastActivity": "2020-09-02T00:34:22.602Z",
31+
"desc": "",
32+
"descData": null,
33+
"due": null,
34+
"dueReminder": null,
35+
"email": null,
36+
"idBoard": "5f4d7be6c45c22583f75fa02",
37+
"idChecklists": [],
38+
"idList": "5f4d7f78bdd7ce4d2d25fdda",
39+
"idMembers": [],
40+
"idMembersVoted": [],
41+
"idShort": 10,
42+
"idAttachmentCover": null,
43+
"labels": [],
44+
"idLabels": [],
45+
"manualCoverAttachment": false,
46+
"name": "a new card",
47+
"pinned": false,
48+
"pos": 65535,
49+
"shortLink": "eBapV0gL",
50+
"shortUrl": "https://trello.com/c/eBapV0gL",
51+
"start": null,
52+
"subscribed": false,
53+
"url": "https://trello.com/c/eBapV0gL/10-a-new-card",
54+
"cover": {
55+
"idAttachment": null,
56+
"color": null,
57+
"idUploadedBackground": null,
58+
"size": "normal",
59+
"brightness": "light",
60+
"idPlugin": null
61+
},
62+
"isTemplate": false,
63+
"cardRole": null
64+
}
Lines changed: 107 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
1+
import taskScheduler from "../../../pipedream/sources/new-scheduled-tasks/new-scheduled-tasks.mjs";
2+
import trello from "../../trello.app.mjs";
13
import ms from "ms";
2-
import common from "../common/common-polling.mjs";
34
import constants from "../../common/constants.mjs";
5+
import sampleEmit from "./test-event.mjs";
46

57
export default {
6-
...common,
78
key: "trello-card-due-date-reminder",
89
name: "Card Due Date Reminder", /* eslint-disable-line pipedream/source-name */
910
description: "Emit new event at a specified time before a card is due.",
1011
version: "0.1.0",
1112
type: "source",
1213
dedupe: "unique",
1314
props: {
14-
...common.props,
15+
pipedream: taskScheduler.props.pipedream,
16+
trello,
17+
db: "$.service.db",
18+
http: "$.interface.http",
1519
board: {
1620
propDefinition: [
17-
common.props.app,
21+
trello,
1822
"board",
1923
],
2024
},
@@ -24,10 +28,42 @@ export default {
2428
description: "How far before the due time the event should trigger. For example, `5 minutes`, `10 minutes`, `1 hour`.",
2529
default: "5 minutes",
2630
options: constants.NOTIFICATION_TIMES,
31+
reloadProps: true,
32+
},
33+
},
34+
async additionalProps() {
35+
const props = {};
36+
if (this.timeBefore) {
37+
props.timer = {
38+
type: "$.interface.timer",
39+
description: "Poll the API to schedule alerts for any newly created events",
40+
default: {
41+
intervalSeconds: ms(this.timeBefore) / 1000,
42+
},
43+
};
44+
}
45+
return props;
46+
},
47+
hooks: {
48+
async deactivate() {
49+
const ids = this._getScheduledEventIds();
50+
if (!ids?.length) {
51+
return;
52+
}
53+
for (const id of ids) {
54+
if (await this.deleteEvent({
55+
body: {
56+
id,
57+
},
58+
})) {
59+
console.log("Cancelled scheduled event");
60+
}
61+
}
62+
this._setScheduledEventIds();
2763
},
2864
},
2965
methods: {
30-
...common.methods,
66+
...taskScheduler.methods,
3167
generateMeta({
3268
id, name: summary,
3369
}, now) {
@@ -37,32 +73,87 @@ export default {
3773
ts: now,
3874
};
3975
},
76+
_getScheduledEventIds() {
77+
return this.db.get("scheduledEventIds");
78+
},
79+
_setScheduledEventIds(ids) {
80+
this.db.set("scheduledEventIds", ids);
81+
},
82+
_getScheduledCardIds() {
83+
return this.db.get("scheduledCardIds");
84+
},
85+
_setScheduledCardIds(ids) {
86+
this.db.set("scheduledCardIds", ids);
87+
},
88+
_hasDeployed() {
89+
const result = this.db.get("hasDeployed");
90+
this.db.set("hasDeployed", true);
91+
return result;
92+
},
4093
emitEvent(card, now) {
4194
const meta = this.generateMeta(card, now);
4295
this.$emit(card, meta);
4396
},
4497
},
4598
async run(event) {
46-
const boardId = this.board;
4799
const now = event.timestamp * 1000;
48100

49-
const timeBeforeMs = ms(this.timeBefore);
50-
if (!timeBeforeMs) {
51-
throw new Error(`Invalid timeBefore value: ${this.timeBefore}`);
101+
// self subscribe only on the first time
102+
if (!this._hasDeployed()) {
103+
await this.selfSubscribe();
104+
}
105+
106+
let scheduledEventIds = this._getScheduledEventIds() || [];
107+
108+
// incoming scheduled event
109+
if (event.$channel === this.selfChannel()) {
110+
const remainingScheduledEventIds = scheduledEventIds.filter((id) => id !== event["$id"]);
111+
this._setScheduledEventIds(remainingScheduledEventIds);
112+
this.emitEvent(event, now);
113+
return;
52114
}
53115

54-
const cards = await this.app.getCards({
55-
boardId,
116+
// schedule new events
117+
const scheduledCardIds = this._getScheduledCardIds() || {};
118+
const cards = await this.trello.getCards({
119+
boardId: this.board,
56120
});
121+
57122
for (const card of cards) {
58-
if (!card.due) {
123+
const dueDate = card.due
124+
? new Date(card.due)
125+
: null;
126+
if (!dueDate || dueDate.getTime() < Date.now()) {
127+
delete scheduledCardIds[card.id];
59128
continue;
60129
}
61-
const due = Date.parse(card.due);
62-
const notifyAt = due - timeBeforeMs;
63-
if (notifyAt <= now) {
64-
this.emitEvent(card, now);
130+
131+
const later = new Date(dueDate.getTime() - ms(this.timeBefore));
132+
133+
if (scheduledCardIds[card.id]) {
134+
// reschedule if card's due date has changed
135+
if (card.due !== scheduledCardIds[card.id].dueDate) {
136+
await this.deleteEvent({
137+
body: {
138+
id: scheduledCardIds[card.id].eventId,
139+
},
140+
});
141+
scheduledEventIds = scheduledEventIds
142+
.filter((id) => id !== scheduledCardIds[card.id].eventId);
143+
} else {
144+
continue;
145+
}
65146
}
147+
148+
const scheduledEventId = this.emitScheduleEvent(card, later);
149+
scheduledEventIds.push(scheduledEventId);
150+
scheduledCardIds[card.id] = {
151+
eventId: scheduledEventId,
152+
dueDate: card.due,
153+
};
66154
}
155+
this._setScheduledEventIds(scheduledEventIds);
156+
this._setScheduledCardIds(scheduledCardIds);
67157
},
158+
sampleEmit,
68159
};
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
export default {
2+
"id": "61818a303e545129af23695d",
3+
"badges": {
4+
"attachmentsByType": {
5+
"trello": {
6+
"board": 0,
7+
"card": 0
8+
}
9+
},
10+
"externalSource": null,
11+
"location": false,
12+
"votes": 0,
13+
"viewingMemberVoted": false,
14+
"subscribed": true,
15+
"fogbugz": "",
16+
"checkItems": 1,
17+
"checkItemsChecked": 1,
18+
"checkItemsEarliestDue": null,
19+
"comments": 2,
20+
"attachments": 1,
21+
"description": false,
22+
"due": "2024-10-01T19:20:00.000Z",
23+
"dueComplete": false,
24+
"start": null,
25+
"lastUpdatedByAi": false
26+
},
27+
"checkItemStates": [
28+
{
29+
"idCheckItem": "6181a5f18e239e6aff9799a4",
30+
"state": "complete"
31+
}
32+
],
33+
"closed": false,
34+
"dueComplete": false,
35+
"dateLastActivity": "2024-10-01T19:08:51.312Z",
36+
"desc": "",
37+
"descData": {
38+
"emoji": {}
39+
},
40+
"due": "2024-10-01T19:20:00.000Z",
41+
"dueReminder": -1,
42+
"email": null,
43+
"idBoard": "5f4d7be6c45c22583f75fa02",
44+
"idChecklists": [
45+
"6181a5ef401ae66c357732b4"
46+
],
47+
"idList": "5f4d7f78bdd7ce4d2d25fdda",
48+
"idMembers": [],
49+
"idMembersVoted": [],
50+
"idShort": 68,
51+
"idAttachmentCover": "62c884dce32d46579ecfd5f0",
52+
"labels": [],
53+
"idLabels": [],
54+
"manualCoverAttachment": false,
55+
"name": "test",
56+
"pinned": false,
57+
"pos": 131071,
58+
"shortLink": "QVLahShU",
59+
"shortUrl": "https://trello.com/c/QVLahShU",
60+
"start": null,
61+
"subscribed": true,
62+
"url": "https://trello.com/c/QVLahShU/68-sfsfdsfd",
63+
"cover": {
64+
"idAttachment": "62c884dce32d46579ecfd5f0",
65+
"color": null,
66+
"idUploadedBackground": null,
67+
"size": "normal",
68+
"brightness": "dark",
69+
"idPlugin": null
70+
},
71+
"isTemplate": false,
72+
"cardRole": null,
73+
"$channel": "self",
74+
"$id": "62377726-40b9-41a8-85c7-b01f3f5f8fbe"
75+
}

0 commit comments

Comments
 (0)