forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdispatch.ts
More file actions
380 lines (345 loc) · 12.1 KB
/
Copy pathdispatch.ts
File metadata and controls
380 lines (345 loc) · 12.1 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
// src/lib/notifications/dispatch.ts
import { eq } from "drizzle-orm"
import { MAM_BONUS_CAP } from "@/lib/adapters/constants"
import { db } from "@/lib/db"
import type { NotificationTargetRow } from "@/lib/db/schema"
import { notificationDeliveryState, notificationTargets } from "@/lib/db/schema"
import {
BUFFER_MILESTONE_DEFAULT_BYTES,
RATIO_DROP_DELTA_DEFAULT,
UNSATISFIED_LIMIT_PCT_DEFAULT,
VIP_EXPIRING_DAYS_DEFAULT,
} from "@/lib/limits"
import { log } from "@/lib/logger"
import { decryptNotificationConfig } from "@/lib/notifications/decrypt"
import { deliverDiscordWebhook } from "@/lib/notifications/deliver"
import { buildDiscordEmbed } from "@/lib/notifications/payload"
import {
type DiscordConfig,
isDiscordConfig,
type NotificationEventType,
type NotificationThresholds,
parseThresholds,
type SnapshotContext,
} from "@/lib/notifications/types"
import {
checkActiveHnrs,
checkAnniversaryMilestone,
checkBonusCapReached,
checkBufferMilestoneCrossed,
checkDownloadDisabled,
checkHnrIncrease,
checkRankChange,
checkRatioBelowMinimumTransition,
checkRatioDelta,
checkUnsatisfiedLimitApproaching,
checkVipExpiringSoon,
checkWarnedTransition,
checkZeroSeeding,
EVENT_SNOOZE_MS,
} from "@/lib/tracker-events"
export async function dispatchNotifications(
ctx: SnapshotContext,
encryptionKey: Buffer,
enabledTargets?: NotificationTargetRow[]
): Promise<void> {
// Use pre-fetched targets if provided, otherwise query (backward-compat)
let targets: NotificationTargetRow[]
if (enabledTargets) {
targets = enabledTargets
} else {
try {
targets = await db
.select()
.from(notificationTargets)
.where(eq(notificationTargets.enabled, true))
} catch (err) {
log.error(
`dispatchNotifications: failed to query targets: ${err instanceof Error ? err.message : "Unknown"}`
)
return
}
}
if (targets.length === 0) return
// Batch-fetch all cooldown state for this tracker in one query
let cooldownMap = new Map<string, Date | null>()
try {
const cooldownRows = await db
.select({
targetId: notificationDeliveryState.targetId,
eventType: notificationDeliveryState.eventType,
snoozedUntil: notificationDeliveryState.snoozedUntil,
})
.from(notificationDeliveryState)
.where(eq(notificationDeliveryState.trackerId, ctx.trackerId))
cooldownMap = new Map(cooldownRows.map((r) => [`${r.targetId}:${r.eventType}`, r.snoozedUntil]))
} catch (err) {
log.error(
`dispatchNotifications: failed to fetch cooldown state for tracker ${ctx.trackerId}: ${err instanceof Error ? err.message : "Unknown"}. Proceeding without cooldown checks.`
)
}
const now = new Date()
for (const target of targets) {
try {
// Scope filter. null means all trackers, array means specific tracker IDs
if (target.scope !== null && !target.scope.includes(ctx.trackerId)) continue
const events = detectEvents(ctx, target)
if (events.length === 0) continue
// Pre-compute anniversary label once per target (avoid double-calling checkAnniversaryMilestone)
const anniversaryLabel = events.includes("anniversary")
? checkAnniversaryMilestone(ctx.trackerJoinedAt)?.label
: undefined
const targetThresholds = parseThresholds(target.thresholds)
// Only Discord is currently supported. Skip unknown types
if (target.type !== "discord") {
log.warn(
`dispatchNotifications: skipping unsupported target type "${target.type}" for "${target.name}" (id=${target.id})`
)
continue
}
let config: DiscordConfig
try {
const raw = decryptNotificationConfig(target, encryptionKey)
if (!isDiscordConfig(raw)) {
log.error(
`dispatchNotifications: decrypted config is not a valid DiscordConfig for "${target.name}" (id=${target.id})`
)
continue
}
config = raw
} catch {
log.error(
`dispatchNotifications: cannot decrypt config for notification target "${target.name}" (id=${target.id})`
)
continue
}
// Collect per-target delivery results
let finalStatus: "delivered" | "failed" | "rate_limited" | null = null
let finalError: string | null = null
for (const event of events) {
try {
// Cooldown check. In-memory lookup from batched query
const snoozedUntil = cooldownMap.get(`${target.id}:${event}`)
if (snoozedUntil && now < snoozedUntil) continue
const embed = buildDiscordEmbed({
eventType: event,
trackerName: ctx.trackerName,
includeTrackerName: target.includeTrackerName,
storeUsernames: ctx.storeUsernames,
data: buildEventData(event, ctx, targetThresholds, anniversaryLabel),
})
const result = await deliverDiscordWebhook(target.id, config.webhookUrl, [embed])
// Track worst-case status for this target: failed > rate_limited > delivered
if (
!finalStatus ||
result.status === "failed" ||
(result.status === "rate_limited" && finalStatus === "delivered")
) {
finalStatus = result.status
finalError = result.error ?? null
}
// Record cooldown only if delivered successfully
if (result.success) {
try {
const snoozedUntilDate = new Date(now.getTime() + EVENT_SNOOZE_MS[event])
await db
.insert(notificationDeliveryState)
.values({
targetId: target.id,
trackerId: ctx.trackerId,
eventType: event,
lastNotifiedAt: now,
snoozedUntil: snoozedUntilDate,
})
.onConflictDoUpdate({
target: [
notificationDeliveryState.targetId,
notificationDeliveryState.trackerId,
notificationDeliveryState.eventType,
],
set: { lastNotifiedAt: now, snoozedUntil: snoozedUntilDate },
})
} catch (cooldownErr) {
log.error(
`dispatchNotifications: delivered "${event}" to "${target.name}" but failed to record cooldown (may re-send next cycle): ${cooldownErr instanceof Error ? cooldownErr.message : "Unknown"}`
)
}
}
} catch (err) {
log.error(
`dispatchNotifications: failed to deliver event "${event}" for target "${target.name}" (id=${target.id}): ${err instanceof Error ? err.message : "Unknown"}`
)
}
}
// Write delivery status
if (finalStatus) {
try {
await db
.update(notificationTargets)
.set({
lastDeliveryStatus: finalStatus,
lastDeliveryAt: now,
lastDeliveryError: finalError,
})
.where(eq(notificationTargets.id, target.id))
} catch (statusErr) {
log.error(
`dispatchNotifications: delivered to "${target.name}" but failed to write delivery status: ${statusErr instanceof Error ? statusErr.message : "Unknown"}`
)
}
}
} catch (err) {
log.error(
`dispatchNotifications: failed processing target "${target.name}" (id=${target.id}): ${err instanceof Error ? err.message : "Unknown"}`
)
}
}
}
export function detectEvents(
ctx: SnapshotContext,
target: NotificationTargetRow
): NotificationEventType[] {
const events: NotificationEventType[] = []
const thresholds = parseThresholds(target.thresholds)
if (
target.notifyRatioDrop &&
checkRatioDelta(
ctx.previousRatio,
ctx.currentRatio,
thresholds.ratioDropDelta ?? RATIO_DROP_DELTA_DEFAULT
)
) {
events.push("ratio_drop")
}
if (target.notifyHitAndRun && checkHnrIncrease(ctx.previousHnrs, ctx.currentHnrs)) {
events.push("hit_and_run")
}
if (target.notifyTrackerDown && ctx.trackerDown) {
events.push("tracker_down")
}
if (
target.notifyBufferMilestone &&
checkBufferMilestoneCrossed(
ctx.currentBufferBytes,
ctx.previousBufferBytes,
BigInt(thresholds.bufferMilestoneBytes ?? BUFFER_MILESTONE_DEFAULT_BYTES)
)
) {
events.push("buffer_milestone")
}
if (target.notifyWarned && checkWarnedTransition(ctx.previousWarned, ctx.currentWarned)) {
events.push("warned")
}
if (
target.notifyRatioDanger &&
checkRatioBelowMinimumTransition(ctx.previousRatio, ctx.currentRatio, ctx.minimumRatio)
) {
events.push("ratio_danger")
}
if (target.notifyZeroSeeding && checkZeroSeeding(ctx.currentSeedingCount, ctx.trackerIsActive)) {
events.push("zero_seeding")
}
if (target.notifyRankChange) {
const newGroup = checkRankChange(ctx.currentGroup, ctx.previousGroup)
if (newGroup) events.push("rank_change")
}
if (target.notifyAnniversary && checkAnniversaryMilestone(ctx.trackerJoinedAt)) {
events.push("anniversary")
}
if (target.notifyBonusCap) {
const capLimit = (thresholds?.bonusCapLimit as number | undefined) ?? MAM_BONUS_CAP
if (
checkBonusCapReached(
ctx.platformContext?.currentSeedbonus ?? null,
ctx.platformContext?.previousSeedbonus ?? null,
capLimit
)
) {
events.push("bonus_cap")
}
}
if (target.notifyVipExpiring) {
const days = (thresholds?.vipExpiringDays as number | undefined) ?? VIP_EXPIRING_DAYS_DEFAULT
if (checkVipExpiringSoon(ctx.platformContext?.vipUntil ?? null, days)) {
events.push("vip_expiring")
}
}
if (target.notifyUnsatisfiedLimit) {
const pct =
(thresholds?.unsatisfiedLimitPercent as number | undefined) ?? UNSATISFIED_LIMIT_PCT_DEFAULT
if (
checkUnsatisfiedLimitApproaching(
ctx.platformContext?.unsatisfiedCount ?? null,
ctx.platformContext?.unsatisfiedLimit ?? null,
pct
)
) {
events.push("unsatisfied_limit")
}
}
if (target.notifyActiveHnrs) {
if (
checkActiveHnrs(
ctx.platformContext?.inactiveHnrCount ?? null,
ctx.platformContext?.previousInactiveHnrCount ?? null
)
) {
events.push("active_hnrs")
}
}
if (target.notifyDownloadDisabled) {
if (
checkDownloadDisabled(
ctx.platformContext?.canDownload ?? null,
ctx.platformContext?.previousCanDownload ?? null
)
) {
events.push("download_disabled")
}
}
return events
}
export function buildEventData(
event: NotificationEventType,
ctx: SnapshotContext,
thresholds?: NotificationThresholds | null,
anniversaryLabel?: string
): Record<string, unknown> {
switch (event) {
case "ratio_drop":
return { previousRatio: ctx.previousRatio, currentRatio: ctx.currentRatio }
case "hit_and_run":
return { previousHnrs: ctx.previousHnrs, currentHnrs: ctx.currentHnrs }
case "tracker_down":
return { error: ctx.trackerError ?? "Unknown error" }
case "buffer_milestone":
return { bufferBytes: Number(ctx.currentBufferBytes ?? 0n) }
case "warned":
return {}
case "ratio_danger":
return { currentRatio: ctx.currentRatio, minimumRatio: ctx.minimumRatio }
case "zero_seeding":
return {}
case "rank_change":
return { newGroup: ctx.currentGroup, previousGroup: ctx.previousGroup }
case "anniversary":
return { label: anniversaryLabel ?? "Anniversary" }
case "bonus_cap": {
const effectiveCap = (thresholds?.bonusCapLimit as number | undefined) ?? MAM_BONUS_CAP
return { currentBonus: ctx.platformContext?.currentSeedbonus ?? null, capLimit: effectiveCap }
}
case "vip_expiring":
return { vipUntil: ctx.platformContext?.vipUntil ?? null }
case "unsatisfied_limit":
return {
count: ctx.platformContext?.unsatisfiedCount ?? null,
limit: ctx.platformContext?.unsatisfiedLimit ?? null,
}
case "active_hnrs":
return { count: ctx.platformContext?.inactiveHnrCount ?? null }
case "download_disabled":
return {}
default:
return {}
}
}