forked from SmartDropLabs/smartdrop-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhookRepository.js
More file actions
197 lines (181 loc) · 6.14 KB
/
Copy pathwebhookRepository.js
File metadata and controls
197 lines (181 loc) · 6.14 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
'use strict';
/**
* Webhook repository.
*
* Schema mirrors the future PostgreSQL `webhooks` table so that swapping the
* Redis backing for a real DB only requires re-implementing this module:
*
* webhooks (
* id text primary key,
* url text not null,
* events text[] not null,
* secret text not null,
* active boolean not null default true,
* description text,
* created_at timestamptz not null default now(),
* updated_at timestamptz not null default now()
* )
*/
const crypto = require('crypto');
const cache = require('../services/cache');
const logger = require('../logger');
const { encryptSecret, decryptSecret, isEncrypted } = require('../services/webhookEncryption');
const IDS_KEY = 'webhooks:ids';
function key(id) {
return `webhook:${id}`;
}
function generateId() {
return `wh_${crypto.randomUUID().replace(/-/g, '').slice(0, 20)}`;
}
/**
* Decrypts `record.secret` for callers (webhookDispatcher needs the
* plaintext to sign deliveries; routes/webhooks.js needs it for
* secret_preview). Records written before encryption was introduced store
* the secret in plaintext (`isEncrypted` returns false for those) and are
* passed through unchanged, so existing webhooks keep working — they're
* transparently encrypted the next time they're written via `update()`.
*/
function decryptRecordSecret(secret) {
if (!isEncrypted(secret)) return secret;
try {
return decryptSecret(secret);
} catch (err) {
logger.error('webhookRepository: failed to decrypt webhook secret', { error: err.message });
return null;
}
}
function normalize(record) {
if (!record) return null;
return {
id: record.id,
url: record.url,
events: Array.isArray(record.events) ? [...record.events] : [],
secret: decryptRecordSecret(record.secret),
active: record.active !== false,
description: record.description || null,
filters: record.filters ? { ...record.filters } : null,
created_at: record.created_at,
updated_at: record.updated_at,
};
}
async function create({ url, events, secret, description, filters, owner_ip }) {
const id = generateId();
const now = new Date().toISOString();
const record = {
id,
url,
events,
secret: encryptSecret(secret),
active: true,
description: description || null,
filters: filters || null,
// Persisted so remove() can clean up the per-owner index, and so the
// per-subscriber cap enforced in routes/webhooks.js actually has an
// index to count.
owner_ip: owner_ip || null,
created_at: now,
updated_at: now,
};
const redis = cache.getClient();
await cache.set(key(id), record);
// A sorted set scored by creation time, not a plain set — mirrors
// airdropsService/alerts.js's own IDS_KEY pattern, so paginating (added
// below) walks a deterministic, newest-first order rather than
// whatever arbitrary order SMEMBERS happened to return (#131).
await redis.zadd(IDS_KEY, Date.parse(now), id);
if (owner_ip) {
await redis.zadd(`webhooks:owner:${owner_ip}`, Date.parse(now), id);
}
return normalize(record);
}
async function findById(id) {
try {
const record = await cache.get(key(id));
return normalize(record);
} catch (err) {
logger.error('webhookRepository.findById Redis error', { id, error: err.message });
return null;
}
}
/** Every webhook, unpaginated — for internal fan-out (listActiveForEvent
* below), which needs the complete set to notify every subscriber, not a
* page of it. Not exposed as a public list endpoint; see list() for that. */
async function listAll() {
try {
const redis = cache.getClient();
const ids = await redis.zrevrange(IDS_KEY, 0, -1);
const records = await Promise.all(ids.map((id) => cache.get(key(id))));
return records.filter(Boolean).map(normalize);
} catch (err) {
logger.error('webhookRepository.listAll Redis error', { error: err.message });
return [];
}
}
async function countByOwner(ownerIp) {
if (!ownerIp) return 0;
try {
const redis = cache.getClient();
return Number(await redis.zcard(`webhooks:owner:${ownerIp}`) || 0);
} catch (err) {
logger.error('webhookRepository.countByOwner Redis error', { ownerIp, error: err.message });
return 0;
}
}
/** Returns { webhooks, total } — see routes/webhooks.js for how this is
* wrapped in the canonical pagination envelope. */
async function list(page = 1, limit = 20) {
try {
const redis = cache.getClient();
const total = await redis.zcard(IDS_KEY);
const start = (page - 1) * limit;
const end = start + limit - 1;
const ids = await redis.zrevrange(IDS_KEY, start, end);
const records = await Promise.all(ids.map((id) => cache.get(key(id))));
return { webhooks: records.filter(Boolean).map(normalize), total };
} catch (err) {
logger.error('webhookRepository.list Redis error', { error: err.message });
return { webhooks: [], total: 0 };
}
}
async function listActiveForEvent(eventType, matcher) {
const all = await listAll();
return all.filter((w) => w.active && matcher(w.events, eventType));
}
async function update(id, patch) {
const existing = await cache.get(key(id));
if (!existing) return null;
const next = {
...existing,
...patch,
// A patched secret arrives as plaintext (validated by webhookPatchBodySchema);
// re-encrypt it the same way create() does. Omit patch.secret entirely and
// this correctly falls through to the existing (already-encrypted) value.
...(patch.secret ? { secret: encryptSecret(patch.secret) } : {}),
id: existing.id,
created_at: existing.created_at,
updated_at: new Date().toISOString(),
};
await cache.set(key(id), next);
return normalize(next);
}
async function remove(id) {
const redis = cache.getClient();
const existing = await cache.get(key(id));
if (!existing) return null;
await cache.del(key(id));
await redis.zrem(IDS_KEY, id);
if (existing.owner_ip) {
await redis.zrem(`webhooks:owner:${existing.owner_ip}`, id);
}
return normalize(existing);
}
module.exports = {
countByOwner,
create,
findById,
list,
listAll,
listActiveForEvent,
update,
remove,
};