-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtracker-events.ts
More file actions
223 lines (191 loc) · 8.97 KB
/
tracker-events.ts
File metadata and controls
223 lines (191 loc) · 8.97 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
// src/lib/tracker-events.ts
//
// Functions: checkRatioBelowMinimum, checkRatioDelta, checkRatioBelowMinimumTransition,
// checkTrackerError, checkWarnedTransition, checkZeroSeeding,
// checkHnrIncrease, checkBufferMilestoneCrossed, checkRankChange,
// checkAnniversaryMilestone, checkBonusCapReached, checkVipExpiringSoon,
// checkUnsatisfiedLimitApproaching, checkActiveHnrs, checkDownloadDisabled,
// EVENT_SNOOZE_MS
//
// Shared pure-function event detection checks. No framework imports, no DB imports.
// Importable from both client-side dashboard code and server-side scheduler code.
import type { NotificationEventType } from "@/lib/notifications/types"
import { isRedacted } from "@/lib/privacy"
// ─── Ratio ───────────────────────────────────────────────────────────────────
export function checkRatioBelowMinimum(
ratio: number | null | undefined,
minimumRatio: number | undefined
): boolean {
if (ratio === null || ratio === undefined) return false
if (minimumRatio === undefined) return false
return Number.isFinite(minimumRatio) && ratio < minimumRatio
}
export function checkRatioDelta(
previousRatio: number | null,
currentRatio: number | null,
delta: number
): boolean {
if (previousRatio === null || currentRatio === null) return false
return previousRatio - currentRatio >= delta
}
export function checkRatioBelowMinimumTransition(
previousRatio: number | null,
currentRatio: number | null,
minimumRatio: number | undefined
): boolean {
if (currentRatio === null || minimumRatio === undefined) return false
if (!Number.isFinite(minimumRatio)) return false
const belowNow = currentRatio < minimumRatio
const wasAbove = previousRatio === null || previousRatio >= minimumRatio
return belowNow && wasAbove
}
// ─── Tracker state ───────────────────────────────────────────────────────────
export function checkTrackerError(
lastError: string | null,
pausedAt: string | null,
userPausedAt?: string | null
): { paused: boolean; pausedByUser: boolean; hasError: boolean } {
if (userPausedAt) return { paused: true, pausedByUser: true, hasError: false }
if (pausedAt) return { paused: true, pausedByUser: false, hasError: false }
if (lastError) return { paused: false, pausedByUser: false, hasError: true }
return { paused: false, pausedByUser: false, hasError: false }
}
export function checkWarnedTransition(
previousWarned: boolean | null | undefined,
currentWarned: boolean | null | undefined
): boolean {
if (currentWarned !== true) return false
return previousWarned !== true // fires on false→true AND null→true (first poll)
}
export function checkZeroSeeding(
seedingCount: number | null | undefined,
isActive: boolean
): boolean {
if (!isActive) return false
return seedingCount === 0
}
// ─── Comparative (delta-based, server-side only) ─────────────────────────────
export function checkHnrIncrease(previousHnrs: number | null, currentHnrs: number | null): boolean {
if (previousHnrs === null || currentHnrs === null) return false
return currentHnrs > previousHnrs
}
export function checkBufferMilestoneCrossed(
currentBufferBytes: bigint | null,
previousBufferBytes: bigint | null,
milestoneBytes: bigint
): boolean {
if (currentBufferBytes === null) return false
const previous = previousBufferBytes ?? 0n
return currentBufferBytes >= milestoneBytes && previous < milestoneBytes
}
export function checkRankChange(
currentGroup: string | null | undefined,
previousGroup: string | null | undefined
): string | null {
if (!currentGroup || !previousGroup) return null
if (isRedacted(currentGroup) || isRedacted(previousGroup)) return null
if (currentGroup === previousGroup) return null
return currentGroup
}
// ─── Time-based ──────────────────────────────────────────────────────────────
const ANNIVERSARY_WINDOW_DAYS = 3
export function checkAnniversaryMilestone(
joinedAt: string | null | undefined
): { label: string } | null {
if (!joinedAt) return null
const joined = new Date(`${joinedAt}T00:00:00`)
if (Number.isNaN(joined.getTime())) return null
const today = new Date()
today.setHours(0, 0, 0, 0)
const candidates: { date: Date; label: string }[] = []
// 1 month
const m1 = new Date(joined)
m1.setMonth(m1.getMonth() + 1)
candidates.push({ date: m1, label: "1 month anniversary" })
// 6 months
const m6 = new Date(joined)
m6.setMonth(m6.getMonth() + 6)
candidates.push({ date: m6, label: "6 month anniversary" })
// Annual milestones
const yearsSinceJoin = today.getFullYear() - joined.getFullYear()
for (let y = 1; y <= Math.max(yearsSinceJoin + 1, 1); y++) {
const ann = new Date(joined)
ann.setFullYear(ann.getFullYear() + y)
candidates.push({ date: ann, label: `${y}-year anniversary` })
}
for (const { date, label } of candidates) {
const diffMs = Math.abs(today.getTime() - date.getTime())
const diffDays = diffMs / (1000 * 60 * 60 * 24)
if (diffDays <= ANNIVERSARY_WINDOW_DAYS) {
return { label }
}
}
return null
}
// ─── MAM-specific events ──────────────────────────────────────────────────────
/** Fires when seedbonus hits or exceeds the cap. Transition-based: only fires if previous was below cap. */
export function checkBonusCapReached(
currentBonus: number | null | undefined,
previousBonus: number | null | undefined,
capLimit: number
): boolean {
if (currentBonus == null) return false
if (previousBonus != null && previousBonus >= capLimit) return false
return currentBonus >= capLimit
}
/** Fires when VIP expiry is within N days from now. */
export function checkVipExpiringSoon(
vipUntil: string | null | undefined,
thresholdDays: number
): boolean {
if (!vipUntil) return false
const expiry = new Date(vipUntil)
if (Number.isNaN(expiry.getTime())) return false
const daysRemaining = (expiry.getTime() - Date.now()) / (1000 * 60 * 60 * 24)
return daysRemaining > 0 && daysRemaining <= thresholdDays
}
/** Fires when unsatisfied count reaches or exceeds the percent threshold of the limit. */
export function checkUnsatisfiedLimitApproaching(
unsatisfiedCount: number | null | undefined,
unsatisfiedLimit: number | null | undefined,
percentThreshold: number
): boolean {
if (unsatisfiedCount == null || unsatisfiedLimit == null || unsatisfiedLimit === 0) return false
return (unsatisfiedCount / unsatisfiedLimit) * 100 >= percentThreshold
}
/** Fires when inactive HnR count increases (transition-based). */
export function checkActiveHnrs(
inactiveHnrCount: number | null | undefined,
previousInactiveHnrCount: number | null | undefined
): boolean {
if (inactiveHnrCount == null || inactiveHnrCount <= 0) return false
if (previousInactiveHnrCount != null && previousInactiveHnrCount >= inactiveHnrCount) return false
return true
}
// ─── Download Privileges ────────────────────────────────────────────────────
export function checkDownloadDisabled(
canDownload: boolean | null,
previousCanDownload: boolean | null
): boolean {
if (canDownload === null || previousCanDownload === null) return false
return previousCanDownload === true && canDownload === false
}
// ─── Snooze durations ────────────────────────────────────────────────────────
// Per-event-type snooze duration map. Events with different urgency/frequency profiles
// get different cooldown windows to avoid notification spam.
export const EVENT_SNOOZE_MS: Record<NotificationEventType, number> = {
ratio_drop: 6 * 60 * 60 * 1000, // 6 hours
hit_and_run: 6 * 60 * 60 * 1000, // 6 hours
tracker_down: 6 * 60 * 60 * 1000, // 6 hours
buffer_milestone: 6 * 60 * 60 * 1000, // 6 hours
warned: 6 * 60 * 60 * 1000, // 6 hours
ratio_danger: 24 * 60 * 60 * 1000, // 24 hours — state-based, fires while below minimum
zero_seeding: 24 * 60 * 60 * 1000, // 24 hours — state-based, fires while at 0 seeds
rank_change: 7 * 24 * 60 * 60 * 1000, // 7 days — rare event, one notification per change
anniversary: 7 * 24 * 60 * 60 * 1000, // 7 days — longer than the ±3-day detection window
bonus_cap: 24 * 60 * 60 * 1000, // 24 hours
vip_expiring: 24 * 60 * 60 * 1000, // 24 hours
unsatisfied_limit: 6 * 60 * 60 * 1000, // 6 hours
active_hnrs: 6 * 60 * 60 * 1000, // 6 hours
download_disabled: 6 * 60 * 60 * 1000, // 6 hours
}