@scopieflows/app-gmail 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/README.md +7 -0
  2. package/package.json +32 -0
  3. package/src/i18n/ca.json +63 -0
  4. package/src/i18n/de.json +66 -0
  5. package/src/i18n/es.json +66 -0
  6. package/src/i18n/fr.json +66 -0
  7. package/src/i18n/hi.json +63 -0
  8. package/src/i18n/id.json +63 -0
  9. package/src/i18n/ja.json +66 -0
  10. package/src/i18n/nl.json +66 -0
  11. package/src/i18n/pt.json +66 -0
  12. package/src/i18n/ru.json +63 -0
  13. package/src/i18n/translation.json +66 -0
  14. package/src/i18n/vi.json +63 -0
  15. package/src/i18n/zh.json +66 -0
  16. package/src/index.d.ts +2 -0
  17. package/src/index.js +78 -0
  18. package/src/index.js.map +1 -0
  19. package/src/lib/actions/create-draft-reply-action.d.ts +10 -0
  20. package/src/lib/actions/create-draft-reply-action.js +262 -0
  21. package/src/lib/actions/create-draft-reply-action.js.map +1 -0
  22. package/src/lib/actions/get-mail-action.d.ts +3 -0
  23. package/src/lib/actions/get-mail-action.js +37 -0
  24. package/src/lib/actions/get-mail-action.js.map +1 -0
  25. package/src/lib/actions/get-thread-action.d.ts +5 -0
  26. package/src/lib/actions/get-thread-action.js +44 -0
  27. package/src/lib/actions/get-thread-action.js.map +1 -0
  28. package/src/lib/actions/reply-to-email-action.d.ts +9 -0
  29. package/src/lib/actions/reply-to-email-action.js +195 -0
  30. package/src/lib/actions/reply-to-email-action.js.map +1 -0
  31. package/src/lib/actions/request-approval-in-email.d.ts +11 -0
  32. package/src/lib/actions/request-approval-in-email.js +176 -0
  33. package/src/lib/actions/request-approval-in-email.js.map +1 -0
  34. package/src/lib/actions/search-email-action.d.ts +15 -0
  35. package/src/lib/actions/search-email-action.js +180 -0
  36. package/src/lib/actions/search-email-action.js.map +1 -0
  37. package/src/lib/actions/send-email-action.d.ts +14 -0
  38. package/src/lib/actions/send-email-action.js +191 -0
  39. package/src/lib/actions/send-email-action.js.map +1 -0
  40. package/src/lib/common/data.d.ts +131 -0
  41. package/src/lib/common/data.js +208 -0
  42. package/src/lib/common/data.js.map +1 -0
  43. package/src/lib/common/models.d.ts +85 -0
  44. package/src/lib/common/models.js +26 -0
  45. package/src/lib/common/models.js.map +1 -0
  46. package/src/lib/common/props.d.ts +11 -0
  47. package/src/lib/common/props.js +229 -0
  48. package/src/lib/common/props.js.map +1 -0
  49. package/src/lib/triggers/new-attachment.d.ts +171 -0
  50. package/src/lib/triggers/new-attachment.js +128 -0
  51. package/src/lib/triggers/new-attachment.js.map +1 -0
  52. package/src/lib/triggers/new-conversation.d.ts +46 -0
  53. package/src/lib/triggers/new-conversation.js +310 -0
  54. package/src/lib/triggers/new-conversation.js.map +1 -0
  55. package/src/lib/triggers/new-email.d.ts +27 -0
  56. package/src/lib/triggers/new-email.js +117 -0
  57. package/src/lib/triggers/new-email.js.map +1 -0
  58. package/src/lib/triggers/new-label.d.ts +2 -0
  59. package/src/lib/triggers/new-label.js +79 -0
  60. package/src/lib/triggers/new-label.js.map +1 -0
  61. package/src/lib/triggers/new-labeled-email.d.ts +62 -0
  62. package/src/lib/triggers/new-labeled-email.js +142 -0
  63. package/src/lib/triggers/new-labeled-email.js.map +1 -0
@@ -0,0 +1,171 @@
1
+ import { TriggerStrategy } from '@scopieflows/pieces-framework';
2
+ import { GmailLabel } from '../common/models';
3
+ export declare const gmailNewAttachmentTrigger: import("@scopieflows/pieces-framework").ITrigger<TriggerStrategy.WEBHOOK, import("@scopieflows/pieces-framework").OAuth2Property<import("@scopieflows/pieces-framework").OAuth2Props>, {
4
+ from: {
5
+ description: string;
6
+ displayName: string;
7
+ required: false;
8
+ valueSchema: string;
9
+ type: import("@scopieflows/pieces-framework").PropertyType.SHORT_TEXT;
10
+ defaultValue?: string | undefined;
11
+ };
12
+ to: {
13
+ description: string;
14
+ displayName: string;
15
+ required: false;
16
+ valueSchema: string;
17
+ type: import("@scopieflows/pieces-framework").PropertyType.SHORT_TEXT;
18
+ defaultValue?: string | undefined;
19
+ };
20
+ subject: import("@scopieflows/pieces-framework").ShortTextProperty<false>;
21
+ label: {
22
+ description: string;
23
+ displayName: string;
24
+ required: false;
25
+ auth: import("@scopieflows/pieces-framework").OAuth2Property<import("@scopieflows/pieces-framework").OAuth2Props>;
26
+ refreshers: string[];
27
+ refreshOnSearch?: boolean;
28
+ options: (propsValue: Record<string, unknown> & {
29
+ auth?: import("dist/packages/shared/src").OAuth2ConnectionValueWithApp | undefined;
30
+ }, ctx: import("@scopieflows/pieces-framework").PropertyContext) => Promise<import("@scopieflows/pieces-framework").DropdownState<GmailLabel>>;
31
+ valueSchema: GmailLabel;
32
+ type: import("@scopieflows/pieces-framework").PropertyType.DROPDOWN;
33
+ defaultValue?: unknown;
34
+ };
35
+ category: {
36
+ description: string;
37
+ displayName: string;
38
+ required: false;
39
+ options: import("@scopieflows/pieces-framework").DropdownState<string>;
40
+ valueSchema: string;
41
+ type: import("@scopieflows/pieces-framework").PropertyType.STATIC_DROPDOWN;
42
+ defaultValue?: unknown;
43
+ };
44
+ filenameExtension: import("@scopieflows/pieces-framework").ShortTextProperty<false>;
45
+ }> | import("@scopieflows/pieces-framework").ITrigger<TriggerStrategy.POLLING, import("@scopieflows/pieces-framework").OAuth2Property<import("@scopieflows/pieces-framework").OAuth2Props>, {
46
+ from: {
47
+ description: string;
48
+ displayName: string;
49
+ required: false;
50
+ valueSchema: string;
51
+ type: import("@scopieflows/pieces-framework").PropertyType.SHORT_TEXT;
52
+ defaultValue?: string | undefined;
53
+ };
54
+ to: {
55
+ description: string;
56
+ displayName: string;
57
+ required: false;
58
+ valueSchema: string;
59
+ type: import("@scopieflows/pieces-framework").PropertyType.SHORT_TEXT;
60
+ defaultValue?: string | undefined;
61
+ };
62
+ subject: import("@scopieflows/pieces-framework").ShortTextProperty<false>;
63
+ label: {
64
+ description: string;
65
+ displayName: string;
66
+ required: false;
67
+ auth: import("@scopieflows/pieces-framework").OAuth2Property<import("@scopieflows/pieces-framework").OAuth2Props>;
68
+ refreshers: string[];
69
+ refreshOnSearch?: boolean;
70
+ options: (propsValue: Record<string, unknown> & {
71
+ auth?: import("dist/packages/shared/src").OAuth2ConnectionValueWithApp | undefined;
72
+ }, ctx: import("@scopieflows/pieces-framework").PropertyContext) => Promise<import("@scopieflows/pieces-framework").DropdownState<GmailLabel>>;
73
+ valueSchema: GmailLabel;
74
+ type: import("@scopieflows/pieces-framework").PropertyType.DROPDOWN;
75
+ defaultValue?: unknown;
76
+ };
77
+ category: {
78
+ description: string;
79
+ displayName: string;
80
+ required: false;
81
+ options: import("@scopieflows/pieces-framework").DropdownState<string>;
82
+ valueSchema: string;
83
+ type: import("@scopieflows/pieces-framework").PropertyType.STATIC_DROPDOWN;
84
+ defaultValue?: unknown;
85
+ };
86
+ filenameExtension: import("@scopieflows/pieces-framework").ShortTextProperty<false>;
87
+ }> | import("@scopieflows/pieces-framework").ITrigger<TriggerStrategy.MANUAL, import("@scopieflows/pieces-framework").OAuth2Property<import("@scopieflows/pieces-framework").OAuth2Props>, {
88
+ from: {
89
+ description: string;
90
+ displayName: string;
91
+ required: false;
92
+ valueSchema: string;
93
+ type: import("@scopieflows/pieces-framework").PropertyType.SHORT_TEXT;
94
+ defaultValue?: string | undefined;
95
+ };
96
+ to: {
97
+ description: string;
98
+ displayName: string;
99
+ required: false;
100
+ valueSchema: string;
101
+ type: import("@scopieflows/pieces-framework").PropertyType.SHORT_TEXT;
102
+ defaultValue?: string | undefined;
103
+ };
104
+ subject: import("@scopieflows/pieces-framework").ShortTextProperty<false>;
105
+ label: {
106
+ description: string;
107
+ displayName: string;
108
+ required: false;
109
+ auth: import("@scopieflows/pieces-framework").OAuth2Property<import("@scopieflows/pieces-framework").OAuth2Props>;
110
+ refreshers: string[];
111
+ refreshOnSearch?: boolean;
112
+ options: (propsValue: Record<string, unknown> & {
113
+ auth?: import("dist/packages/shared/src").OAuth2ConnectionValueWithApp | undefined;
114
+ }, ctx: import("@scopieflows/pieces-framework").PropertyContext) => Promise<import("@scopieflows/pieces-framework").DropdownState<GmailLabel>>;
115
+ valueSchema: GmailLabel;
116
+ type: import("@scopieflows/pieces-framework").PropertyType.DROPDOWN;
117
+ defaultValue?: unknown;
118
+ };
119
+ category: {
120
+ description: string;
121
+ displayName: string;
122
+ required: false;
123
+ options: import("@scopieflows/pieces-framework").DropdownState<string>;
124
+ valueSchema: string;
125
+ type: import("@scopieflows/pieces-framework").PropertyType.STATIC_DROPDOWN;
126
+ defaultValue?: unknown;
127
+ };
128
+ filenameExtension: import("@scopieflows/pieces-framework").ShortTextProperty<false>;
129
+ }> | import("@scopieflows/pieces-framework").ITrigger<TriggerStrategy.APP_WEBHOOK, import("@scopieflows/pieces-framework").OAuth2Property<import("@scopieflows/pieces-framework").OAuth2Props>, {
130
+ from: {
131
+ description: string;
132
+ displayName: string;
133
+ required: false;
134
+ valueSchema: string;
135
+ type: import("@scopieflows/pieces-framework").PropertyType.SHORT_TEXT;
136
+ defaultValue?: string | undefined;
137
+ };
138
+ to: {
139
+ description: string;
140
+ displayName: string;
141
+ required: false;
142
+ valueSchema: string;
143
+ type: import("@scopieflows/pieces-framework").PropertyType.SHORT_TEXT;
144
+ defaultValue?: string | undefined;
145
+ };
146
+ subject: import("@scopieflows/pieces-framework").ShortTextProperty<false>;
147
+ label: {
148
+ description: string;
149
+ displayName: string;
150
+ required: false;
151
+ auth: import("@scopieflows/pieces-framework").OAuth2Property<import("@scopieflows/pieces-framework").OAuth2Props>;
152
+ refreshers: string[];
153
+ refreshOnSearch?: boolean;
154
+ options: (propsValue: Record<string, unknown> & {
155
+ auth?: import("dist/packages/shared/src").OAuth2ConnectionValueWithApp | undefined;
156
+ }, ctx: import("@scopieflows/pieces-framework").PropertyContext) => Promise<import("@scopieflows/pieces-framework").DropdownState<GmailLabel>>;
157
+ valueSchema: GmailLabel;
158
+ type: import("@scopieflows/pieces-framework").PropertyType.DROPDOWN;
159
+ defaultValue?: unknown;
160
+ };
161
+ category: {
162
+ description: string;
163
+ displayName: string;
164
+ required: false;
165
+ options: import("@scopieflows/pieces-framework").DropdownState<string>;
166
+ valueSchema: string;
167
+ type: import("@scopieflows/pieces-framework").PropertyType.STATIC_DROPDOWN;
168
+ defaultValue?: unknown;
169
+ };
170
+ filenameExtension: import("@scopieflows/pieces-framework").ShortTextProperty<false>;
171
+ }>;
@@ -0,0 +1,128 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.gmailNewAttachmentTrigger = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const pieces_framework_1 = require("@scopieflows/pieces-framework");
6
+ const props_1 = require("../common/props");
7
+ const __1 = require("../../");
8
+ const googleapis_1 = require("googleapis");
9
+ const googleapis_common_1 = require("googleapis-common");
10
+ const data_1 = require("../common/data");
11
+ const dayjs_1 = tslib_1.__importDefault(require("dayjs"));
12
+ exports.gmailNewAttachmentTrigger = (0, pieces_framework_1.createTrigger)({
13
+ auth: __1.gmailAuth,
14
+ name: 'new_attachment',
15
+ displayName: 'New Attachment',
16
+ description: 'Triggers when an email with an attachment arrives.',
17
+ props: {
18
+ from: Object.assign(Object.assign({}, props_1.GmailProps.from), { description: 'Filter by sender email.', displayName: 'From', required: false }),
19
+ to: Object.assign(Object.assign({}, props_1.GmailProps.to), { description: 'Filter by recipient email.', displayName: 'To', required: false }),
20
+ subject: pieces_framework_1.Property.ShortText({
21
+ displayName: 'Subject Contains',
22
+ description: 'Only trigger for emails containing this text in the subject.',
23
+ required: false,
24
+ }),
25
+ label: Object.assign(Object.assign({}, props_1.GmailProps.label), { description: 'Filter by Gmail label.', displayName: 'Label', required: false }),
26
+ category: Object.assign(Object.assign({}, props_1.GmailProps.category), { description: 'Filter by Gmail category.', displayName: 'Category', required: false }),
27
+ filenameExtension: pieces_framework_1.Property.ShortText({
28
+ displayName: 'File Extension',
29
+ description: 'Only trigger for attachments with this file extension (e.g., pdf, jpg, docx).',
30
+ required: false,
31
+ }),
32
+ },
33
+ sampleData: {},
34
+ type: pieces_framework_1.TriggerStrategy.POLLING,
35
+ onEnable(context) {
36
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
37
+ yield context.store.put('lastPoll', Date.now());
38
+ });
39
+ },
40
+ onDisable(context) {
41
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
42
+ return;
43
+ });
44
+ },
45
+ run(context) {
46
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
47
+ var _a;
48
+ const lastFetchEpochMS = (_a = (yield context.store.get('lastPoll'))) !== null && _a !== void 0 ? _a : 0;
49
+ const items = yield pollRecentMessages({
50
+ auth: context.auth,
51
+ props: context.propsValue,
52
+ files: context.files,
53
+ lastFetchEpochMS: lastFetchEpochMS,
54
+ });
55
+ const newLastEpochMilliSeconds = items.reduce((acc, item) => Math.max(acc, item.epochMilliSeconds), lastFetchEpochMS);
56
+ yield context.store.put('lastPoll', newLastEpochMilliSeconds);
57
+ return items
58
+ .filter((f) => f.epochMilliSeconds > lastFetchEpochMS)
59
+ .map((item) => item.data);
60
+ });
61
+ },
62
+ test(context) {
63
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
64
+ var _a;
65
+ const lastFetchEpochMS = (_a = (yield context.store.get('lastPoll'))) !== null && _a !== void 0 ? _a : 0;
66
+ const items = yield pollRecentMessages({
67
+ auth: context.auth,
68
+ props: context.propsValue,
69
+ files: context.files,
70
+ lastFetchEpochMS: lastFetchEpochMS,
71
+ });
72
+ return (0, data_1.getFirstFiveOrAll)(items.map((item) => item.data));
73
+ });
74
+ },
75
+ });
76
+ function pollRecentMessages(_a) {
77
+ return tslib_1.__awaiter(this, arguments, void 0, function* ({ auth, props, files, lastFetchEpochMS, }) {
78
+ const authClient = new googleapis_common_1.OAuth2Client();
79
+ authClient.setCredentials(auth);
80
+ const gmail = googleapis_1.google.gmail({ version: 'v1', auth: authClient });
81
+ // construct query
82
+ const query = ['has:attachment'];
83
+ const maxResults = lastFetchEpochMS === 0 ? 5 : 100;
84
+ const afterUnixSeconds = Math.floor(lastFetchEpochMS / 1000);
85
+ if (props.from)
86
+ query.push(`from:(${props.from})`);
87
+ if (props.to)
88
+ query.push(`to:(${props.to})`);
89
+ if (props.subject)
90
+ query.push(`subject:(${props.subject})`);
91
+ if (props.label)
92
+ query.push(`label:${props.label.name}`);
93
+ if (props.category)
94
+ query.push(`category:${props.category}`);
95
+ if (props.filenameExtension)
96
+ query.push(`filename:${props.filenameExtension}`);
97
+ if (afterUnixSeconds != null && afterUnixSeconds > 0)
98
+ query.push(`after:${afterUnixSeconds}`);
99
+ // List Messages
100
+ const messagesResponse = yield gmail.users.messages.list({
101
+ userId: 'me',
102
+ q: query.join(' '),
103
+ maxResults,
104
+ });
105
+ const pollingResponse = [];
106
+ for (const message of messagesResponse.data.messages || []) {
107
+ const rawMailResponse = yield gmail.users.messages.get({
108
+ userId: 'me',
109
+ id: message.id,
110
+ format: 'raw',
111
+ });
112
+ const parsedMailResponse = yield (0, data_1.parseStream)(Buffer.from(rawMailResponse.data.raw, 'base64').toString('utf-8'));
113
+ const { attachments } = parsedMailResponse, restOfParsedMailResponse = tslib_1.__rest(parsedMailResponse, ["attachments"]);
114
+ const parsedAttachments = yield (0, data_1.convertAttachment)(attachments, files);
115
+ for (const attachment of parsedAttachments) {
116
+ pollingResponse.push({
117
+ epochMilliSeconds: (0, dayjs_1.default)(restOfParsedMailResponse.date).valueOf(),
118
+ data: {
119
+ attachment,
120
+ message: Object.assign({ id: message.id }, restOfParsedMailResponse),
121
+ },
122
+ });
123
+ }
124
+ }
125
+ return pollingResponse;
126
+ });
127
+ }
128
+ //# sourceMappingURL=new-attachment.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"new-attachment.js","sourceRoot":"","sources":["../../../../../../../../packages/pieces/community/gmail/src/lib/triggers/new-attachment.ts"],"names":[],"mappings":";;;;AAAA,oEAMuC;AACvC,2CAA6C;AAC7C,8BAAmC;AACnC,2CAAoC;AACpC,yDAAiD;AACjD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EAAE,IACX,wBAAwB,CAC5B;qBACF;iBACF,CAAC,CAAC;YACL,CAAC;QACH,CAAC;QAED,OAAO,eAAe,CAAC;IACzB,CAAC;CAAA"}
@@ -0,0 +1,46 @@
1
+ import { TriggerStrategy } from '@scopieflows/pieces-framework';
2
+ export declare const gmailNewConversationTrigger: import("@scopieflows/pieces-framework").ITrigger<TriggerStrategy.WEBHOOK, import("@scopieflows/pieces-framework").OAuth2Property<import("@scopieflows/pieces-framework").OAuth2Props>, {
3
+ from: {
4
+ description: string;
5
+ displayName: string;
6
+ required: false;
7
+ valueSchema: string;
8
+ type: import("@scopieflows/pieces-framework").PropertyType.SHORT_TEXT;
9
+ defaultValue?: string | undefined;
10
+ };
11
+ subject: import("@scopieflows/pieces-framework").ShortTextProperty<false>;
12
+ maxAgeHours: import("@scopieflows/pieces-framework").NumberProperty<false>;
13
+ }> | import("@scopieflows/pieces-framework").ITrigger<TriggerStrategy.POLLING, import("@scopieflows/pieces-framework").OAuth2Property<import("@scopieflows/pieces-framework").OAuth2Props>, {
14
+ from: {
15
+ description: string;
16
+ displayName: string;
17
+ required: false;
18
+ valueSchema: string;
19
+ type: import("@scopieflows/pieces-framework").PropertyType.SHORT_TEXT;
20
+ defaultValue?: string | undefined;
21
+ };
22
+ subject: import("@scopieflows/pieces-framework").ShortTextProperty<false>;
23
+ maxAgeHours: import("@scopieflows/pieces-framework").NumberProperty<false>;
24
+ }> | import("@scopieflows/pieces-framework").ITrigger<TriggerStrategy.MANUAL, import("@scopieflows/pieces-framework").OAuth2Property<import("@scopieflows/pieces-framework").OAuth2Props>, {
25
+ from: {
26
+ description: string;
27
+ displayName: string;
28
+ required: false;
29
+ valueSchema: string;
30
+ type: import("@scopieflows/pieces-framework").PropertyType.SHORT_TEXT;
31
+ defaultValue?: string | undefined;
32
+ };
33
+ subject: import("@scopieflows/pieces-framework").ShortTextProperty<false>;
34
+ maxAgeHours: import("@scopieflows/pieces-framework").NumberProperty<false>;
35
+ }> | import("@scopieflows/pieces-framework").ITrigger<TriggerStrategy.APP_WEBHOOK, import("@scopieflows/pieces-framework").OAuth2Property<import("@scopieflows/pieces-framework").OAuth2Props>, {
36
+ from: {
37
+ description: string;
38
+ displayName: string;
39
+ required: false;
40
+ valueSchema: string;
41
+ type: import("@scopieflows/pieces-framework").PropertyType.SHORT_TEXT;
42
+ defaultValue?: string | undefined;
43
+ };
44
+ subject: import("@scopieflows/pieces-framework").ShortTextProperty<false>;
45
+ maxAgeHours: import("@scopieflows/pieces-framework").NumberProperty<false>;
46
+ }>;
@@ -0,0 +1,310 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.gmailNewConversationTrigger = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const pieces_framework_1 = require("@scopieflows/pieces-framework");
6
+ const props_1 = require("../common/props");
7
+ const __1 = require("../../");
8
+ const googleapis_1 = require("googleapis");
9
+ const googleapis_common_1 = require("googleapis-common");
10
+ const data_1 = require("../common/data");
11
+ function enrichNewConversation(_a) {
12
+ return tslib_1.__awaiter(this, arguments, void 0, function* ({ gmail, threadId, files, conversationInfo, }) {
13
+ var _b;
14
+ const threadResponse = yield gmail.users.threads.get({
15
+ userId: 'me',
16
+ id: threadId,
17
+ format: 'full',
18
+ });
19
+ const thread = threadResponse.data;
20
+ const messages = thread.messages || [];
21
+ const firstMessage = messages[0];
22
+ if (!(firstMessage === null || firstMessage === void 0 ? void 0 : firstMessage.id)) {
23
+ throw new Error('No messages found in thread');
24
+ }
25
+ const rawMessageResponse = yield gmail.users.messages.get({
26
+ userId: 'me',
27
+ id: firstMessage.id,
28
+ format: 'raw',
29
+ });
30
+ const parsedFirstMessage = yield (0, data_1.parseStream)(Buffer.from(rawMessageResponse.data.raw, 'base64').toString('utf-8'));
31
+ const headers = ((_b = firstMessage.payload) === null || _b === void 0 ? void 0 : _b.headers) || [];
32
+ const headerMap = headers.reduce((acc, header) => {
33
+ if (header.name && header.value) {
34
+ acc[header.name.toLowerCase()] = header.value;
35
+ }
36
+ return acc;
37
+ }, {});
38
+ return {
39
+ conversation: {
40
+ threadId: threadId,
41
+ messageCount: messages.length,
42
+ snippet: thread.snippet || '',
43
+ historyId: thread.historyId,
44
+ participants: extractParticipants(messages),
45
+ subject: headerMap['subject'] || '',
46
+ starter: {
47
+ from: headerMap['from'] || '',
48
+ to: headerMap['to'] || '',
49
+ cc: headerMap['cc'] || '',
50
+ bcc: headerMap['bcc'] || '',
51
+ date: headerMap['date'] || '',
52
+ messageId: firstMessage.id,
53
+ },
54
+ },
55
+ firstMessage: Object.assign(Object.assign({}, parsedFirstMessage), { messageId: firstMessage.id, attachments: yield (0, data_1.convertAttachment)(parsedFirstMessage.attachments, files) }),
56
+ conversationInfo: Object.assign(Object.assign({}, conversationInfo), { triggeredAt: Date.now() }),
57
+ };
58
+ });
59
+ }
60
+ function extractParticipants(messages) {
61
+ const participants = {
62
+ from: new Set(),
63
+ to: new Set(),
64
+ cc: new Set(),
65
+ };
66
+ messages.forEach((message) => {
67
+ var _a;
68
+ const headers = ((_a = message.payload) === null || _a === void 0 ? void 0 : _a.headers) || [];
69
+ headers.forEach((header) => {
70
+ if (header.name && header.value) {
71
+ const name = header.name.toLowerCase();
72
+ const value = header.value;
73
+ if (name === 'from') {
74
+ participants.from.add(value);
75
+ }
76
+ else if (name === 'to') {
77
+ value
78
+ .split(',')
79
+ .forEach((email) => participants.to.add(email.trim()));
80
+ }
81
+ else if (name === 'cc') {
82
+ value
83
+ .split(',')
84
+ .forEach((email) => participants.cc.add(email.trim()));
85
+ }
86
+ }
87
+ });
88
+ });
89
+ return {
90
+ from: participants.from,
91
+ to: participants.to,
92
+ cc: participants.cc,
93
+ };
94
+ }
95
+ exports.gmailNewConversationTrigger = (0, pieces_framework_1.createTrigger)({
96
+ auth: __1.gmailAuth,
97
+ name: 'new_conversation',
98
+ displayName: 'New Conversation',
99
+ description: 'Triggers when a new email conversation (thread) begins',
100
+ props: {
101
+ from: Object.assign(Object.assign({}, props_1.GmailProps.from), { description: 'Filter by sender email (optional)', displayName: 'From', required: false }),
102
+ subject: pieces_framework_1.Property.ShortText({
103
+ displayName: 'Subject Contains',
104
+ description: 'Only trigger for conversations containing this text in the subject (optional)',
105
+ required: false,
106
+ }),
107
+ maxAgeHours: pieces_framework_1.Property.Number({
108
+ displayName: 'Maximum Age (Hours)',
109
+ description: 'Only trigger for conversations started within this many hours',
110
+ required: false,
111
+ defaultValue: 24,
112
+ }),
113
+ },
114
+ sampleData: {},
115
+ type: pieces_framework_1.TriggerStrategy.POLLING,
116
+ onEnable: (context) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
117
+ const authClient = new googleapis_common_1.OAuth2Client();
118
+ authClient.setCredentials(context.auth);
119
+ const gmail = googleapis_1.google.gmail({ version: 'v1', auth: authClient });
120
+ const profile = yield gmail.users.getProfile({ userId: 'me' });
121
+ yield context.store.put('lastHistoryId', profile.data.historyId);
122
+ yield context.store.put('processedThreads', []);
123
+ }),
124
+ onDisable: (context) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
125
+ yield context.store.delete('lastHistoryId');
126
+ yield context.store.delete('processedThreads');
127
+ }),
128
+ run: (context) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
129
+ var _a, _b, _c, _d;
130
+ const authClient = new googleapis_common_1.OAuth2Client();
131
+ authClient.setCredentials(context.auth);
132
+ const gmail = googleapis_1.google.gmail({ version: 'v1', auth: authClient });
133
+ const lastHistoryId = yield context.store.get('lastHistoryId');
134
+ const processedThreads = (yield context.store.get('processedThreads')) || [];
135
+ const maxAge = (context.propsValue.maxAgeHours || 24) * 60 * 60 * 1000;
136
+ const cutoffTime = Date.now() - maxAge;
137
+ try {
138
+ const historyResponse = yield gmail.users.history.list({
139
+ userId: 'me',
140
+ startHistoryId: lastHistoryId,
141
+ historyTypes: ['messageAdded'],
142
+ maxResults: 100,
143
+ });
144
+ const newConversations = [];
145
+ if (historyResponse.data.history) {
146
+ for (const history of historyResponse.data.history) {
147
+ if (history.messagesAdded) {
148
+ for (const added of history.messagesAdded) {
149
+ const threadId = (_a = added.message) === null || _a === void 0 ? void 0 : _a.threadId;
150
+ if (threadId && !processedThreads.includes(threadId)) {
151
+ const threadResponse = yield gmail.users.threads.get({
152
+ userId: 'me',
153
+ id: threadId,
154
+ format: 'minimal',
155
+ });
156
+ if (((_b = threadResponse.data.messages) === null || _b === void 0 ? void 0 : _b.length) === 1) {
157
+ newConversations.push(threadId);
158
+ }
159
+ }
160
+ }
161
+ }
162
+ }
163
+ }
164
+ const results = [];
165
+ for (const threadId of newConversations) {
166
+ const threadResponse = yield gmail.users.threads.get({
167
+ userId: 'me',
168
+ id: threadId,
169
+ format: 'full',
170
+ });
171
+ const thread = threadResponse.data;
172
+ const firstMessage = (_c = thread.messages) === null || _c === void 0 ? void 0 : _c[0];
173
+ if (!firstMessage)
174
+ continue;
175
+ const messageDate = parseInt(firstMessage.internalDate || '0');
176
+ if (messageDate < cutoffTime)
177
+ continue;
178
+ const headers = ((_d = firstMessage.payload) === null || _d === void 0 ? void 0 : _d.headers) || [];
179
+ const headerMap = {};
180
+ headers.forEach((h) => {
181
+ if (h.name && h.value) {
182
+ headerMap[h.name.toLowerCase()] = h.value;
183
+ }
184
+ });
185
+ if (context.propsValue.from) {
186
+ const from = headerMap['from'] || '';
187
+ if (!from.toLowerCase().includes(context.propsValue.from.toLowerCase())) {
188
+ continue;
189
+ }
190
+ }
191
+ if (context.propsValue.subject) {
192
+ const subject = headerMap['subject'] || '';
193
+ if (!subject
194
+ .toLowerCase()
195
+ .includes(context.propsValue.subject.toLowerCase())) {
196
+ continue;
197
+ }
198
+ }
199
+ const rawResponse = yield gmail.users.messages.get({
200
+ userId: 'me',
201
+ id: firstMessage.id,
202
+ format: 'raw',
203
+ });
204
+ const parsedMessage = yield (0, data_1.parseStream)(Buffer.from(rawResponse.data.raw, 'base64').toString('utf-8'));
205
+ results.push({
206
+ id: `conversation_${threadId}`,
207
+ data: {
208
+ thread: {
209
+ id: threadId,
210
+ snippet: thread.snippet,
211
+ messageCount: 1,
212
+ },
213
+ message: Object.assign(Object.assign({}, parsedMessage), { id: firstMessage.id, threadId: threadId, date: new Date(messageDate).toISOString(), attachments: yield (0, data_1.convertAttachment)(parsedMessage.attachments, context.files) }),
214
+ conversation: {
215
+ starter: {
216
+ from: headerMap['from'],
217
+ to: headerMap['to'],
218
+ subject: headerMap['subject'],
219
+ date: headerMap['date'],
220
+ },
221
+ },
222
+ },
223
+ });
224
+ processedThreads.push(threadId);
225
+ }
226
+ if (historyResponse.data.historyId) {
227
+ yield context.store.put('lastHistoryId', historyResponse.data.historyId);
228
+ }
229
+ const recentThreads = processedThreads.slice(-1000);
230
+ yield context.store.put('processedThreads', recentThreads);
231
+ return results;
232
+ }
233
+ catch (error) {
234
+ if (error.code === 404) {
235
+ const profile = yield gmail.users.getProfile({ userId: 'me' });
236
+ yield context.store.put('lastHistoryId', profile.data.historyId);
237
+ return [];
238
+ }
239
+ throw error;
240
+ }
241
+ }),
242
+ test: (context) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
243
+ var _a, _b;
244
+ const authClient = new googleapis_common_1.OAuth2Client();
245
+ authClient.setCredentials(context.auth);
246
+ const gmail = googleapis_1.google.gmail({ version: 'v1', auth: authClient });
247
+ const maxAge = (context.propsValue.maxAgeHours || 24) * 60 * 60 * 1000;
248
+ const cutoffSeconds = Math.floor((Date.now() - maxAge) / 1000);
249
+ let query = `after:${cutoffSeconds}`;
250
+ if (context.propsValue.from) {
251
+ query += ` from:(${context.propsValue.from})`;
252
+ }
253
+ if (context.propsValue.subject) {
254
+ query += ` subject:(${context.propsValue.subject})`;
255
+ }
256
+ const threadsResponse = yield gmail.users.threads.list({
257
+ userId: 'me',
258
+ q: query,
259
+ maxResults: 5,
260
+ });
261
+ const results = [];
262
+ if (threadsResponse.data.threads) {
263
+ for (const thread of threadsResponse.data.threads) {
264
+ const threadId = thread.id;
265
+ const fullThread = yield gmail.users.threads.get({
266
+ userId: 'me',
267
+ id: threadId,
268
+ format: 'full',
269
+ });
270
+ if (((_a = fullThread.data.messages) === null || _a === void 0 ? void 0 : _a.length) === 1) {
271
+ const firstMessage = fullThread.data.messages[0];
272
+ const headers = ((_b = firstMessage.payload) === null || _b === void 0 ? void 0 : _b.headers) || [];
273
+ const headerMap = {};
274
+ headers.forEach((h) => {
275
+ if (h.name && h.value) {
276
+ headerMap[h.name.toLowerCase()] = h.value;
277
+ }
278
+ });
279
+ const rawResponse = yield gmail.users.messages.get({
280
+ userId: 'me',
281
+ id: firstMessage.id,
282
+ format: 'raw',
283
+ });
284
+ const parsedMessage = yield (0, data_1.parseStream)(Buffer.from(rawResponse.data.raw, 'base64').toString('utf-8'));
285
+ results.push({
286
+ id: `test_conversation_${threadId}`,
287
+ data: {
288
+ thread: {
289
+ id: threadId,
290
+ snippet: fullThread.data.snippet,
291
+ messageCount: 1,
292
+ },
293
+ message: Object.assign(Object.assign({}, parsedMessage), { id: firstMessage.id, threadId: threadId, date: new Date(parseInt(firstMessage.internalDate || '0')).toISOString(), attachments: yield (0, data_1.convertAttachment)(parsedMessage.attachments, context.files) }),
294
+ conversation: {
295
+ starter: {
296
+ from: headerMap['from'],
297
+ to: headerMap['to'],
298
+ subject: headerMap['subject'],
299
+ date: headerMap['date'],
300
+ },
301
+ },
302
+ },
303
+ });
304
+ }
305
+ }
306
+ }
307
+ return results;
308
+ }),
309
+ });
310
+ //# sourceMappingURL=new-conversation.js.map