This repository was archived by the owner on Feb 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpack.ts
More file actions
599 lines (573 loc) · 16.2 KB
/
pack.ts
File metadata and controls
599 lines (573 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
import * as coda from "@codahq/packs-sdk";
export const pack = coda.newPack();
// NOTE: replace this with your own Mattermost instance URL.
const MATTERMOST_BASE_URL = "https://coda-pack.cloud.mattermost.com";
console.log("MATTERMOST_BASE_URL", MATTERMOST_BASE_URL)
// strip MATTERMOST_BASE_URL of https prefix to keep only the domain name
const MATTERMOST_DOMAIN = MATTERMOST_BASE_URL.replace("https://", "");
pack.addNetworkDomain(MATTERMOST_DOMAIN);
// Set up OAuth authentication for the pack.
pack.setUserAuthentication({
type: coda.AuthenticationType.OAuth2,
authorizationUrl: `${MATTERMOST_BASE_URL}/oauth/authorize`,
tokenUrl: `${MATTERMOST_BASE_URL}/oauth/access_token`,
});
// Build a PostMessage formula that posts a message to a Mattermost channel.
pack.addFormula({
name: "PostMessage",
description: "Sends the given message to the specified Mattermost channel. Return the message object.",
parameters: [
coda.makeParameter({
type: coda.ParameterType.String,
description: "the channel ID to post in",
name: "channel_id",
optional: false,
}),
coda.makeParameter({
type: coda.ParameterType.Html,
description: "the message contents, can be formatted with Markdown",
name: "message",
optional: false,
}),
],
isAction: true,
execute: async function ([channel_id, message], context) {
const { fetcher } = context;
const response = await fetcher.fetch({
url: `${MATTERMOST_BASE_URL}/api/v4/posts`,
headers: {
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify({
channel_id: channel_id,
message: message,
})
});
return response.body;
},
resultType: coda.ValueType.String,
});
// Use the same structure as the above pack.addFormula to build a Channel formula that gets details from the provided channel id
pack.addFormula({
name: "Channel",
description: "Gets details about the specified Mattermost channel.",
parameters: [
coda.makeParameter({
type: coda.ParameterType.String,
description: "the channel ID to get details for",
name: "channel_id",
optional: false
}),
],
execute: async function ([channel_id], context) {
const { fetcher } = context;
const response = await fetcher.fetch({
url: `${MATTERMOST_BASE_URL}/api/v4/channels/${channel_id}`,
method: "GET"
});
return response.body;
},
resultType: coda.ValueType.Object,
schema: undefined
});
// Convert a User API response to a User schema
function toUser(user: any) {
let result: any = {
id: user.id,
username: user.username,
email: user.email,
first_name: user.first_name,
last_name: user.last_name,
nickname: user.nickname,
position: user.position,
roles: user.roles,
locale: user.locale,
};
return result;
}
// Define UserSchema from coda.makeObjectSchema which defines the schema of a user from a Mattermost response.
const UserSchema = coda.makeObjectSchema({
type: coda.ValueType.Object,
idProperty: "id",
displayProperty: "username",
featuredProperties: ["id", "username", "email", "first_name", "last_name", "nickname", "position"],
properties: {
id: {
type: coda.ValueType.String,
description: "User ID",
},
username: {
type: coda.ValueType.String,
description: "Username",
},
email: {
type: coda.ValueType.String,
description: "Email",
},
first_name: {
type: coda.ValueType.String,
description: "First name",
},
last_name: {
type: coda.ValueType.String,
description: "Last name",
},
nickname: {
type: coda.ValueType.String,
description: "Nickname",
},
position: {
type: coda.ValueType.String,
description: "Position",
},
roles: {
type: coda.ValueType.String,
description: "Roles",
},
locale: {
type: coda.ValueType.String,
description: "Locale",
},
},
});
// Build a sync table that lists all users in a Mattermost instance.
pack.addSyncTable({
name: "Users",
schema: UserSchema,
identityName: "User",
formula: {
name: "SyncUsers",
description: "List all users in a Mattermost instance.",
parameters: [
// create parameter for team_id
coda.makeParameter({
type: coda.ParameterType.String,
description: "the team ID to list users from",
name: "team_id",
optional: true,
}),
// create parameter for page and per_page
coda.makeParameter({
type: coda.ParameterType.String,
description: "the page number to list users from",
name: "page",
optional: true,
}),
coda.makeParameter({
type: coda.ParameterType.String,
description: "the number of users per page",
name: "per_page",
optional: true,
suggestedValue: "100",
}),
],
execute: async function ([team_id], context) {
// make a GET request to the Mattermost API list all the active users in team with ID CODERPUSH_TEAM_ID, 100 users per page
const { fetcher } = context;
const response = await fetcher.fetch({
url: `${MATTERMOST_BASE_URL}/api/v4/users?in_team=${team_id}&per_page=100&active=true`,
method: "GET"
});
// convert the response to a User schema
let results = [];
for (let user of response.body) {
results.push(toUser(user))
}
return {
result: results
}
},
},
});
// Define ChannelSchema from coda.makeObjectSchema which uses the follow sample response:
//
// {
// "id": "string",
// "create_at": 0,
// "update_at": 0,
// "delete_at": 0,
// "team_id": "string",
// "type": "string",
// "display_name": "string",
// "name": "string",
// "header": "string",
// "purpose": "string",
// "last_post_at": 0,
// "total_msg_count": 0,
// "extra_update_at": 0,
// "creator_id": "string"
// }
const ChannelSchema = coda.makeObjectSchema({
type: coda.ValueType.Object,
idProperty: "id",
displayProperty: "display_name",
featuredProperties: ["id", "display_name", "name", "header", "purpose", "total_msg_count"],
properties: {
id: {
type: coda.ValueType.String,
description: "Channel ID",
},
create_at: {
type: coda.ValueType.Number,
description: "Create at",
},
update_at: {
type: coda.ValueType.Number,
description: "Update at",
},
delete_at: {
type: coda.ValueType.Number,
description: "Delete at",
},
team_id: {
type: coda.ValueType.String,
description: "Team ID",
},
type: {
type: coda.ValueType.String,
description: "Type",
},
display_name: {
type: coda.ValueType.String,
description: "Display name",
},
name: {
type: coda.ValueType.String,
description: "Name",
},
header: {
type: coda.ValueType.String,
description: "Header",
},
purpose: {
type: coda.ValueType.String,
description: "Purpose",
},
last_post_at: {
type: coda.ValueType.Number,
description: "Last post at",
},
total_msg_count: {
type: coda.ValueType.Number,
description: "Total message count",
},
extra_update_at: {
type: coda.ValueType.Number,
description: "Extra update at",
},
creator_id: {
type: coda.ValueType.String,
description: "Creator ID",
},
},
});
// Similar to the above sync table named Users, build a sync table named Channels
// that lists all public channels in a Mattermost instance.
// It should take team_id as a required parameter, and per_page and page as optional parameters.
pack.addSyncTable({
name: "Channels",
schema: ChannelSchema,
identityName: "Channel",
formula: {
name: "SyncChannels",
description: "List all public channels in a Mattermost instance.",
parameters: [
coda.makeParameter({
type: coda.ParameterType.String,
description: "the team ID to list channels from",
name: "team_id",
optional: false,
}),
coda.makeParameter({
type: coda.ParameterType.String,
description: "the page number to list channels from",
name: "page",
optional: true,
}),
coda.makeParameter({
type: coda.ParameterType.String,
description: "the number of channels per page",
name: "per_page",
optional: true,
suggestedValue: "100",
}),
],
execute: async function ([team_id, page, per_page], context) {
const { fetcher } = context;
const response = await fetcher.fetch({
url: `${MATTERMOST_BASE_URL}/api/v4/teams/${team_id}/channels?page=${page}&per_page=${per_page}`,
method: "GET"
});
let results = [];
for (let channel of response.body) {
results.push(channel)
}
return {
result: results
}
},
},
});
// Define a TeamSchema from coda.makeObjectSchema which uses the follow structure:
// {
// "id": "string",
// "create_at": 0,
// "update_at": 0,
// "delete_at": 0,
// "display_name": "string",
// "name": "string",
// "description": "string",
// "email": "string",
// "type": "string",
// "allowed_domains": "string",
// "invite_id": "string",
// "allow_open_invite": true,
// "policy_id": "string"
// }
const TeamSchema = coda.makeObjectSchema({
type: coda.ValueType.Object,
idProperty: "id",
displayProperty: "display_name",
featuredProperties: ["id", "display_name", "name", "description", "email"],
properties: {
id: {
type: coda.ValueType.String,
description: "Team ID",
},
create_at: {
type: coda.ValueType.Number,
description: "Create at",
},
update_at: {
type: coda.ValueType.Number,
description: "Update at",
},
delete_at: {
type: coda.ValueType.Number,
description: "Delete at",
},
display_name: {
type: coda.ValueType.String,
description: "Display name",
},
name: {
type: coda.ValueType.String,
description: "Name",
},
description: {
type: coda.ValueType.String,
description: "Description",
},
email: {
type: coda.ValueType.String,
description: "Email",
},
type: {
type: coda.ValueType.String,
description: "Type",
},
allowed_domains: {
type: coda.ValueType.String,
description: "Allowed domains",
},
invite_id: {
type: coda.ValueType.String,
description: "Invite ID",
},
allow_open_invite: {
type: coda.ValueType.Boolean,
description: "Allow open invite",
},
policy_id: {
type: coda.ValueType.String,
description: "Policy ID",
},
},
});
// Similar to the above sync table named Users, build a sync table named Teams
// that lists all teams in a Mattermost instance.
// It should take per_page and page as optional parameters.
pack.addSyncTable({
name: "Teams",
schema: TeamSchema,
identityName: "Team",
formula: {
name: "SyncTeams",
description: "List all teams in a Mattermost instance.",
parameters: [
coda.makeParameter({
type: coda.ParameterType.String,
description: "the page number to list teams from",
name: "page",
optional: true,
}),
coda.makeParameter({
type: coda.ParameterType.String,
name: "per_page",
description: "the number of teams per page",
optional: true,
})
],
execute: async function ([page, per_page], context) {
// complete the formula here
const { fetcher } = context;
const response = await fetcher.fetch({
url: `${MATTERMOST_BASE_URL}/api/v4/teams?page=${page}&per_page=${per_page}`,
method: "GET"
});
let results = [];
for (let team of response.body) {
results.push(team)
}
return {
result: results
}
},
},
});
// make a PostSchema from the following object, using makeObjectSchema
// {
// "id": "string",
// "create_at": 0,
// "update_at": 0,
// "delete_at": 0,
// "edit_at": 0,
// "user_id": "string",
// "channel_id": "string",
// "root_id": "string",
// "original_id": "string",
// "message": "string",
// "type": "string",
// "props": { },
// "hashtag": "string",
// "file_ids": [
// "string"
// ],
// "pending_post_id": "string",
// "metadata": {
// "embeds": [],
// "emojis": [],
// "files": [],
// "images": { },
// "reactions": []
// }
// }
const PostSchema = coda.makeObjectSchema({
type: coda.ValueType.Object,
idProperty: "id",
displayProperty: "message",
properties: {
id: { type: coda.ValueType.String },
create_at: { type: coda.ValueType.Number },
update_at: { type: coda.ValueType.Number },
delete_at: { type: coda.ValueType.Number },
edit_at: { type: coda.ValueType.Number },
user_id: { type: coda.ValueType.String },
channel_id: { type: coda.ValueType.String },
root_id: { type: coda.ValueType.String },
original_id: { type: coda.ValueType.String },
message: { type: coda.ValueType.String },
type: { type: coda.ValueType.String },
hashtag: { type: coda.ValueType.String },
file_ids: { type: coda.ValueType.Array, items: { type: coda.ValueType.String } },
pending_post_id: { type: coda.ValueType.String },
},
});
// similar to Channels sync table above, build a sync table for Posts
// but channel_id is the required parameter.
// use the PostSchema defined above
pack.addSyncTable({
name: "Posts",
schema: PostSchema,
identityName: "Post",
formula: {
name: "SyncPosts",
description: "List all posts in a Mattermost instance.",
parameters: [
coda.makeParameter({
type: coda.ParameterType.String,
name: "channel_id",
description: "the channel_id to list posts from",
optional: false,
}),
coda.makeParameter({
type: coda.ParameterType.String,
description: "the page number to list posts from",
name: "page",
optional: true,
}),
coda.makeParameter({
type: coda.ParameterType.String,
name: "per_page",
description: "the number of posts per page",
optional: true,
}),
],
execute: async function ([channel_id, page, per_page], context) {
// complete the formula here
const { fetcher } = context;
const response = await fetcher.fetch({
url: `${MATTERMOST_BASE_URL}/api/v4/channels/${channel_id}/posts?page=${page}&per_page=${per_page}`,
method: "GET"
});
console.log("response body: ", response.body)
console.log("posts: ", response.body.posts)
const posts = response.body.posts
// posts is an object with post id as key and post object as value
// convert this to an array of post objects
const postsArray = Object.values(posts)
// return postsArray
return {
result: postsArray
}
},
},
});
// add sync table ChannelUsers that lists all users in a channel
// channel_id is the required parameter
// use the UserSchema defined above
pack.addSyncTable({
name: "ChannelUsers",
schema: UserSchema,
identityName: "User",
formula: {
name: "SyncChannelUsers",
description: "List all users in a Mattermost channel.",
parameters: [
coda.makeParameter({
type: coda.ParameterType.String,
name: "channel_id",
description: "the channel_id to list users from",
optional: false,
}),
coda.makeParameter({
type: coda.ParameterType.String,
description: "the page number to list users from",
name: "page",
optional: true,
}),
coda.makeParameter({
type: coda.ParameterType.String,
name: "per_page",
description: "the number of users per page",
optional: true,
}),
],
execute: async function ([channel_id, page, per_page], context) {
// complete the formula here
const { fetcher } = context;
const response = await fetcher.fetch({
url: `${MATTERMOST_BASE_URL}/api/v4/channels/${channel_id}/members?page=${page}&per_page=${per_page}`,
method: "GET"
});
let results = [];
for (let user of response.body) {
results.push(user)
}
return {
result: results
}
},
},
});