@credal/actions 0.2.111 → 0.2.112
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.
|
@@ -4,13 +4,14 @@ export interface SlackSearchMessage {
|
|
|
4
4
|
channelId: string;
|
|
5
5
|
ts: string;
|
|
6
6
|
text?: string;
|
|
7
|
-
|
|
7
|
+
userEmail?: string;
|
|
8
8
|
permalink?: string;
|
|
9
9
|
/** If thread: full thread (root first). If not thread: small context window around the hit. */
|
|
10
10
|
context?: Array<{
|
|
11
11
|
ts: string;
|
|
12
12
|
text?: string;
|
|
13
|
-
|
|
13
|
+
userEmail?: string;
|
|
14
|
+
userName?: string;
|
|
14
15
|
}>;
|
|
15
16
|
}
|
|
16
17
|
declare const searchSlack: slackUserSearchSlackFunction;
|
|
@@ -12,6 +12,34 @@ import { MISSING_AUTH_TOKEN } from "../../util/missingAuthConstants.js";
|
|
|
12
12
|
import pLimit from "p-limit";
|
|
13
13
|
const HIT_ENRICH_POOL = 10;
|
|
14
14
|
const limitHit = pLimit(HIT_ENRICH_POOL);
|
|
15
|
+
class SlackUserCache {
|
|
16
|
+
constructor(client) {
|
|
17
|
+
this.client = client;
|
|
18
|
+
this.cache = new Map();
|
|
19
|
+
this.client = client;
|
|
20
|
+
}
|
|
21
|
+
get(id) {
|
|
22
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
23
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
24
|
+
const result = this.cache.get(id);
|
|
25
|
+
if (result)
|
|
26
|
+
return result;
|
|
27
|
+
const res = yield this.client.users.info({ user: id });
|
|
28
|
+
const u = {
|
|
29
|
+
name: (_e = (_c = (_b = (_a = res.user) === null || _a === void 0 ? void 0 : _a.profile) === null || _b === void 0 ? void 0 : _b.display_name) !== null && _c !== void 0 ? _c : (_d = res.user) === null || _d === void 0 ? void 0 : _d.real_name) !== null && _e !== void 0 ? _e : "",
|
|
30
|
+
email: (_h = (_g = (_f = res.user) === null || _f === void 0 ? void 0 : _f.profile) === null || _g === void 0 ? void 0 : _g.email) !== null && _h !== void 0 ? _h : "",
|
|
31
|
+
};
|
|
32
|
+
if (res.user && id && res.user.name) {
|
|
33
|
+
this.cache.set(id, u);
|
|
34
|
+
return u;
|
|
35
|
+
}
|
|
36
|
+
return undefined;
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
set(id, { email, name }) {
|
|
40
|
+
this.cache.set(id, { email, name });
|
|
41
|
+
}
|
|
42
|
+
}
|
|
15
43
|
/* ===================== Helpers ===================== */
|
|
16
44
|
function normalizeChannelOperand(ch) {
|
|
17
45
|
const s = ch.trim();
|
|
@@ -33,7 +61,7 @@ function timeFilter(range) {
|
|
|
33
61
|
return "";
|
|
34
62
|
}
|
|
35
63
|
}
|
|
36
|
-
function lookupUserIdsByEmail(client, emails) {
|
|
64
|
+
function lookupUserIdsByEmail(client, emails, slackUserCache) {
|
|
37
65
|
return __awaiter(this, void 0, void 0, function* () {
|
|
38
66
|
const ids = [];
|
|
39
67
|
const tasks = emails.map((raw) => __awaiter(this, void 0, void 0, function* () {
|
|
@@ -43,6 +71,9 @@ function lookupUserIdsByEmail(client, emails) {
|
|
|
43
71
|
return null;
|
|
44
72
|
const res = yield client.users.lookupByEmail({ email });
|
|
45
73
|
const id = (_a = res.user) === null || _a === void 0 ? void 0 : _a.id;
|
|
74
|
+
if (res.user && id && res.user.name) {
|
|
75
|
+
slackUserCache.set(id, { email, name: res.user.name });
|
|
76
|
+
}
|
|
46
77
|
if (id)
|
|
47
78
|
return id;
|
|
48
79
|
return null;
|
|
@@ -132,10 +163,11 @@ const searchSlack = (_a) => __awaiter(void 0, [_a], void 0, function* ({ params,
|
|
|
132
163
|
throw new Error(MISSING_AUTH_TOKEN);
|
|
133
164
|
}
|
|
134
165
|
const client = new WebClient(authParams.authToken);
|
|
166
|
+
const slackUserCache = new SlackUserCache(client);
|
|
135
167
|
const { emails, channel, topic, timeRange, limit } = params;
|
|
136
168
|
const parts = [];
|
|
137
169
|
if (emails === null || emails === void 0 ? void 0 : emails.length) {
|
|
138
|
-
const userIds = yield lookupUserIdsByEmail(client, emails);
|
|
170
|
+
const userIds = yield lookupUserIdsByEmail(client, emails, slackUserCache);
|
|
139
171
|
const { user_id: myUserId } = yield client.auth.test();
|
|
140
172
|
if (!myUserId)
|
|
141
173
|
throw new Error("Failed to get my user ID.");
|
|
@@ -164,17 +196,20 @@ const searchSlack = (_a) => __awaiter(void 0, [_a], void 0, function* ({ params,
|
|
|
164
196
|
const count = Math.max(1, Math.min(100, limit));
|
|
165
197
|
const searchRes = yield client.search.messages({ query, count, highlight: true });
|
|
166
198
|
const matches = (_c = (_b = searchRes.messages) === null || _b === void 0 ? void 0 : _b.matches) !== null && _c !== void 0 ? _c : [];
|
|
167
|
-
const
|
|
168
|
-
var _a, _b;
|
|
169
|
-
|
|
199
|
+
const hitsPromises = matches.slice(0, limit).map((m) => __awaiter(void 0, void 0, void 0, function* () {
|
|
200
|
+
var _a, _b, _c, _d;
|
|
201
|
+
const user = m.user ? yield slackUserCache.get(m.user) : undefined;
|
|
202
|
+
return {
|
|
170
203
|
channelId: ((_a = m.channel) === null || _a === void 0 ? void 0 : _a.id) || ((_b = m.channel) === null || _b === void 0 ? void 0 : _b.name) || "",
|
|
171
204
|
ts: m.ts,
|
|
172
205
|
text: m.text,
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
206
|
+
userEmail: (_c = user === null || user === void 0 ? void 0 : user.email) !== null && _c !== void 0 ? _c : undefined,
|
|
207
|
+
userName: (_d = user === null || user === void 0 ? void 0 : user.name) !== null && _d !== void 0 ? _d : undefined,
|
|
208
|
+
};
|
|
209
|
+
}));
|
|
210
|
+
const hits = yield Promise.all(hitsPromises);
|
|
176
211
|
const tasks = hits.map(h => limitHit(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
177
|
-
var _a, _b, _c, _d;
|
|
212
|
+
var _a, _b, _c, _d, _e, _f;
|
|
178
213
|
if (!h.ts)
|
|
179
214
|
return null;
|
|
180
215
|
try {
|
|
@@ -186,12 +221,26 @@ const searchSlack = (_a) => __awaiter(void 0, [_a], void 0, function* ({ params,
|
|
|
186
221
|
fetchThread(client, h.channelId, rootTs),
|
|
187
222
|
getPermalink(client, h.channelId, rootTs),
|
|
188
223
|
]);
|
|
189
|
-
const
|
|
224
|
+
const contextPromises = thread
|
|
225
|
+
.filter(t => t.ts)
|
|
226
|
+
.map((t) => __awaiter(void 0, void 0, void 0, function* () {
|
|
227
|
+
var _a, _b;
|
|
228
|
+
const user = t.user ? yield slackUserCache.get(t.user) : undefined;
|
|
229
|
+
return {
|
|
230
|
+
ts: t.ts,
|
|
231
|
+
text: t.text,
|
|
232
|
+
userEmail: (_a = user === null || user === void 0 ? void 0 : user.email) !== null && _a !== void 0 ? _a : undefined,
|
|
233
|
+
userName: (_b = user === null || user === void 0 ? void 0 : user.name) !== null && _b !== void 0 ? _b : undefined,
|
|
234
|
+
};
|
|
235
|
+
}));
|
|
236
|
+
const context = yield Promise.all(contextPromises);
|
|
237
|
+
const user = anchor.user ? yield slackUserCache.get(anchor.user) : undefined;
|
|
190
238
|
return {
|
|
191
239
|
channelId: h.channelId,
|
|
192
240
|
ts: rootTs,
|
|
193
241
|
text: (_a = anchor.text) !== null && _a !== void 0 ? _a : h.text,
|
|
194
|
-
|
|
242
|
+
userEmail: (_b = user === null || user === void 0 ? void 0 : user.email) !== null && _b !== void 0 ? _b : h.userEmail,
|
|
243
|
+
userName: (_c = user === null || user === void 0 ? void 0 : user.name) !== null && _c !== void 0 ? _c : h.userName,
|
|
195
244
|
context,
|
|
196
245
|
permalink,
|
|
197
246
|
};
|
|
@@ -202,24 +251,39 @@ const searchSlack = (_a) => __awaiter(void 0, [_a], void 0, function* ({ params,
|
|
|
202
251
|
fetchContextWindow(client, h.channelId, h.ts),
|
|
203
252
|
getPermalink(client, h.channelId, h.ts),
|
|
204
253
|
]);
|
|
205
|
-
const
|
|
254
|
+
const contextPromises = ctx
|
|
255
|
+
.filter(t => t.ts)
|
|
256
|
+
.map((t) => __awaiter(void 0, void 0, void 0, function* () {
|
|
257
|
+
var _a, _b;
|
|
258
|
+
const user = t.user ? yield slackUserCache.get(t.user) : undefined;
|
|
259
|
+
return {
|
|
260
|
+
ts: t.ts,
|
|
261
|
+
text: t.text,
|
|
262
|
+
userEmail: (_a = user === null || user === void 0 ? void 0 : user.email) !== null && _a !== void 0 ? _a : undefined,
|
|
263
|
+
userName: (_b = user === null || user === void 0 ? void 0 : user.name) !== null && _b !== void 0 ? _b : undefined,
|
|
264
|
+
};
|
|
265
|
+
}));
|
|
266
|
+
const context = yield Promise.all(contextPromises);
|
|
267
|
+
const user = (anchor === null || anchor === void 0 ? void 0 : anchor.user) ? yield slackUserCache.get(anchor.user) : undefined;
|
|
206
268
|
return {
|
|
207
269
|
channelId: h.channelId,
|
|
208
270
|
ts: h.ts,
|
|
209
|
-
text: (
|
|
210
|
-
|
|
271
|
+
text: (_d = anchor === null || anchor === void 0 ? void 0 : anchor.text) !== null && _d !== void 0 ? _d : h.text,
|
|
272
|
+
userEmail: (_e = user === null || user === void 0 ? void 0 : user.email) !== null && _e !== void 0 ? _e : h.userEmail,
|
|
273
|
+
userName: (_f = user === null || user === void 0 ? void 0 : user.name) !== null && _f !== void 0 ? _f : h.userName,
|
|
211
274
|
context,
|
|
212
275
|
permalink,
|
|
213
276
|
};
|
|
214
277
|
}
|
|
215
278
|
}
|
|
216
|
-
catch (
|
|
279
|
+
catch (_g) {
|
|
217
280
|
// fallback minimal object; still in parallel
|
|
218
281
|
return {
|
|
219
282
|
channelId: h.channelId,
|
|
220
283
|
ts: h.ts,
|
|
221
284
|
text: h.text,
|
|
222
|
-
|
|
285
|
+
userEmail: h.userEmail,
|
|
286
|
+
userName: h.userName,
|
|
223
287
|
permalink: yield getPermalink(client, h.channelId, h.ts),
|
|
224
288
|
};
|
|
225
289
|
}
|