Skip to content

Commit aaa90c3

Browse files
committed
refactor(slack-nats): use concrete SDK types instead of trait generics
Signed-off-by: Yordis Prieto <yordis.prieto@gmail.com>
1 parent f39e81e commit aaa90c3

4 files changed

Lines changed: 151 additions & 1420 deletions

File tree

rsworkspace/crates/slack-enricher/src/handle.rs

Lines changed: 34 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use slack_types::events::{
1717
SlackReactionEvent, SlackSlashCommandEvent, SlackThreadBroadcastEvent, SlackViewClosedEvent,
1818
SlackViewSubmissionEvent,
1919
};
20-
use trogon_nats::PublishClient;
20+
use async_nats::jetstream;
2121

2222
use crate::config::EnricherConfig;
2323

@@ -36,14 +36,12 @@ pub struct EnrichContext {
3636

3737
// ── Events API dispatch ─────────────────────────────────────────────────────
3838

39-
pub async fn handle_raw_event<C: PublishClient>(
40-
js: &C,
39+
pub async fn handle_raw_event(
40+
js: &jetstream::Context,
4141
ctx: &EnrichContext,
4242
payload: &[u8],
4343
nats_headers: &async_nats::HeaderMap,
44-
) where
45-
C::PublishError: 'static,
46-
{
44+
) {
4745
let event_type = nats_headers
4846
.get("X-Slack-Event-Type")
4947
.map(|v| v.as_str());
@@ -92,13 +90,11 @@ pub async fn handle_raw_event<C: PublishClient>(
9290

9391
// ── Interaction dispatch ────────────────────────────────────────────────────
9492

95-
pub async fn handle_raw_interaction<C: PublishClient>(
96-
js: &C,
93+
pub async fn handle_raw_interaction(
94+
js: &jetstream::Context,
9795
ctx: &EnrichContext,
9896
payload: &[u8],
99-
) where
100-
C::PublishError: 'static,
101-
{
97+
) {
10298
let body: serde_json::Value = match serde_json::from_slice(payload) {
10399
Ok(v) => v,
104100
Err(e) => {
@@ -204,13 +200,11 @@ pub async fn handle_raw_interaction<C: PublishClient>(
204200

205201
// ── Slash command dispatch ──────────────────────────────────────────────────
206202

207-
pub async fn handle_raw_command<C: PublishClient>(
208-
js: &C,
203+
pub async fn handle_raw_command(
204+
js: &jetstream::Context,
209205
ctx: &EnrichContext,
210206
payload: &[u8],
211-
) where
212-
C::PublishError: 'static,
213-
{
207+
) {
214208
let body_str = std::str::from_utf8(payload).unwrap_or("");
215209
let params: HashMap<String, String> = form_urlencoded::parse(body_str.as_bytes())
216210
.map(|(k, v)| (k.into_owned(), v.into_owned()))
@@ -235,13 +229,11 @@ pub async fn handle_raw_command<C: PublishClient>(
235229

236230
// ── Event handlers ──────────────────────────────────────────────────────────
237231

238-
async fn handle_message_event<C: PublishClient>(
239-
js: &C,
232+
async fn handle_message_event(
233+
js: &jetstream::Context,
240234
ctx: &EnrichContext,
241235
event: &serde_json::Value,
242-
) where
243-
C::PublishError: 'static,
244-
{
236+
) {
245237
if event["bot_id"].is_string() && !ctx.config.allow_bots {
246238
return;
247239
}
@@ -384,13 +376,11 @@ async fn handle_message_event<C: PublishClient>(
384376
}
385377
}
386378

387-
async fn handle_app_mention_event<C: PublishClient>(
388-
js: &C,
379+
async fn handle_app_mention_event(
380+
js: &jetstream::Context,
389381
ctx: &EnrichContext,
390382
event: &serde_json::Value,
391-
) where
392-
C::PublishError: 'static,
393-
{
383+
) {
394384
let user = event["user"].as_str().unwrap_or_default().to_string();
395385
let channel = event["channel"].as_str().unwrap_or_default().to_string();
396386
let raw_text = event["text"].as_str().unwrap_or_default().to_string();
@@ -433,14 +423,12 @@ async fn handle_app_mention_event<C: PublishClient>(
433423
}
434424
}
435425

436-
async fn handle_reaction_event<C: PublishClient>(
437-
js: &C,
426+
async fn handle_reaction_event(
427+
js: &jetstream::Context,
438428
ctx: &EnrichContext,
439429
event: &serde_json::Value,
440430
added: bool,
441-
) where
442-
C::PublishError: 'static,
443-
{
431+
) {
444432
let item = &event["item"];
445433
let channel = if item["type"].as_str() == Some("message") {
446434
item["channel"].as_str().map(String::from)
@@ -468,14 +456,12 @@ async fn handle_reaction_event<C: PublishClient>(
468456
}
469457
}
470458

471-
async fn handle_member_event<C: PublishClient>(
472-
js: &C,
459+
async fn handle_member_event(
460+
js: &jetstream::Context,
473461
ctx: &EnrichContext,
474462
event: &serde_json::Value,
475463
joined: bool,
476-
) where
477-
C::PublishError: 'static,
478-
{
464+
) {
479465
let ev = SlackMemberEvent {
480466
user: event["user"].as_str().unwrap_or_default().to_string(),
481467
channel: event["channel"].as_str().unwrap_or_default().to_string(),
@@ -490,14 +476,12 @@ async fn handle_member_event<C: PublishClient>(
490476
}
491477
}
492478

493-
async fn handle_channel_event<C: PublishClient>(
494-
js: &C,
479+
async fn handle_channel_event(
480+
js: &jetstream::Context,
495481
ctx: &EnrichContext,
496482
event: &serde_json::Value,
497483
kind: ChannelEventKind,
498-
) where
499-
C::PublishError: 'static,
500-
{
484+
) {
501485
let (channel_id, channel_name, user) = match kind {
502486
ChannelEventKind::Created | ChannelEventKind::Renamed => {
503487
let ch = &event["channel"];
@@ -526,13 +510,11 @@ async fn handle_channel_event<C: PublishClient>(
526510
}
527511
}
528512

529-
async fn handle_app_home_event<C: PublishClient>(
530-
js: &C,
513+
async fn handle_app_home_event(
514+
js: &jetstream::Context,
531515
ctx: &EnrichContext,
532516
event: &serde_json::Value,
533-
) where
534-
C::PublishError: 'static,
535-
{
517+
) {
536518
let ev = SlackAppHomeOpenedEvent {
537519
user: event["user"].as_str().unwrap_or_default().to_string(),
538520
tab: event["tab"].as_str().unwrap_or_default().to_string(),
@@ -544,13 +526,11 @@ async fn handle_app_home_event<C: PublishClient>(
544526
}
545527
}
546528

547-
async fn handle_link_shared_event<C: PublishClient>(
548-
js: &C,
529+
async fn handle_link_shared_event(
530+
js: &jetstream::Context,
549531
ctx: &EnrichContext,
550532
event: &serde_json::Value,
551-
) where
552-
C::PublishError: 'static,
553-
{
533+
) {
554534
let links = event["links"]
555535
.as_array()
556536
.map(|arr| {
@@ -575,14 +555,12 @@ async fn handle_link_shared_event<C: PublishClient>(
575555
}
576556
}
577557

578-
async fn handle_pin_event<C: PublishClient>(
579-
js: &C,
558+
async fn handle_pin_event(
559+
js: &jetstream::Context,
580560
ctx: &EnrichContext,
581561
event: &serde_json::Value,
582562
kind: PinEventKind,
583-
) where
584-
C::PublishError: 'static,
585-
{
563+
) {
586564
let channel = event["channel_id"]
587565
.as_str()
588566
.or_else(|| event["channel"].as_str())

0 commit comments

Comments
 (0)