context-drop 0.0.0-stage → 0.3.0
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.
- package/MIT-LICENSE +21 -0
- package/README.md +710 -2
- package/app/assets/javascripts/context_drop/context_drop.js +9949 -0
- package/dist/app/javascript/context_drop/api-client.d.ts +53 -0
- package/dist/app/javascript/context_drop/api-client.js +470 -0
- package/dist/app/javascript/context_drop/element-picker.d.ts +111 -0
- package/dist/app/javascript/context_drop/element-picker.js +1428 -0
- package/dist/app/javascript/context_drop/icons.d.ts +34 -0
- package/dist/app/javascript/context_drop/icons.js +34 -0
- package/dist/app/javascript/context_drop/index.d.ts +7 -0
- package/dist/app/javascript/context_drop/index.js +9 -0
- package/dist/app/javascript/context_drop/markdown.d.ts +7 -0
- package/dist/app/javascript/context_drop/markdown.js +129 -0
- package/dist/app/javascript/context_drop/sanitizer.d.ts +38 -0
- package/dist/app/javascript/context_drop/sanitizer.js +769 -0
- package/dist/app/javascript/context_drop/screenshot.d.ts +21 -0
- package/dist/app/javascript/context_drop/screenshot.js +245 -0
- package/dist/app/javascript/context_drop/styles.d.ts +1 -0
- package/dist/app/javascript/context_drop/styles.js +2196 -0
- package/dist/app/javascript/context_drop/types.d.ts +316 -0
- package/dist/app/javascript/context_drop/types.js +1 -0
- package/dist/app/javascript/context_drop/widget.d.ts +206 -0
- package/dist/app/javascript/context_drop/widget.js +3752 -0
- package/dist/src/cli.d.ts +3 -0
- package/dist/src/cli.js +207 -0
- package/dist/src/index.d.ts +5 -0
- package/dist/src/index.js +5 -0
- package/dist/src/mcp/dispatcher.d.ts +42 -0
- package/dist/src/mcp/dispatcher.js +895 -0
- package/dist/src/mcp/environment-router.d.ts +38 -0
- package/dist/src/mcp/environment-router.js +174 -0
- package/dist/src/mcp/index.d.ts +24 -0
- package/dist/src/mcp/index.js +75 -0
- package/dist/src/mcp/remote-client.d.ts +25 -0
- package/dist/src/mcp/remote-client.js +98 -0
- package/dist/src/next/index.d.ts +13 -0
- package/dist/src/next/index.js +25 -0
- package/dist/src/next/server.d.ts +1 -0
- package/dist/src/next/server.js +1 -0
- package/dist/src/react/index.d.ts +104 -0
- package/dist/src/react/index.js +144 -0
- package/dist/src/server/handler.d.ts +48 -0
- package/dist/src/server/handler.js +1180 -0
- package/dist/src/server/index.d.ts +8 -0
- package/dist/src/server/index.js +8 -0
- package/dist/src/server/sanitizer.d.ts +15 -0
- package/dist/src/server/sanitizer.js +201 -0
- package/dist/src/server/storage/local.d.ts +22 -0
- package/dist/src/server/storage/local.js +827 -0
- package/dist/src/server/storage/memory.d.ts +8 -0
- package/dist/src/server/storage/memory.js +387 -0
- package/dist/src/server/storage/postgres.d.ts +27 -0
- package/dist/src/server/storage/postgres.js +629 -0
- package/dist/src/server/types.d.ts +227 -0
- package/dist/src/server/types.js +7 -0
- package/dist/src/server/upload-validator.d.ts +16 -0
- package/dist/src/server/upload-validator.js +196 -0
- package/dist/src/widget/index.d.ts +1 -0
- package/dist/src/widget/index.js +1 -0
- package/package.json +99 -4
|
@@ -0,0 +1,827 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { matchesFeedbackFilter, matchesOwnFeedback, toFeedbackSummary } from "./memory.js";
|
|
6
|
+
function tryOpenNodeSqlite(dbPath) {
|
|
7
|
+
try {
|
|
8
|
+
const req = createRequire(import.meta.url);
|
|
9
|
+
const sqliteMod = req("node:sqlite");
|
|
10
|
+
if (sqliteMod && typeof sqliteMod.DatabaseSync === "function") {
|
|
11
|
+
const db = new sqliteMod.DatabaseSync(dbPath);
|
|
12
|
+
db.exec("PRAGMA journal_mode = WAL;");
|
|
13
|
+
db.exec("PRAGMA busy_timeout = 5000;");
|
|
14
|
+
db.exec("PRAGMA foreign_keys = ON;");
|
|
15
|
+
return db;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
// Fallback to JSON file store when node:sqlite is unavailable
|
|
20
|
+
}
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
function createDiskBlobStore(blobsDir) {
|
|
24
|
+
return {
|
|
25
|
+
async putBlob({ key, data }) {
|
|
26
|
+
fs.mkdirSync(blobsDir, { recursive: true });
|
|
27
|
+
const safeKey = key.replace(/[^a-zA-Z0-9._-]/g, "_");
|
|
28
|
+
const targetPath = path.join(blobsDir, safeKey);
|
|
29
|
+
fs.writeFileSync(targetPath, data);
|
|
30
|
+
return { key: safeKey };
|
|
31
|
+
},
|
|
32
|
+
async getBlob(key) {
|
|
33
|
+
const safeKey = key.replace(/[^a-zA-Z0-9._-]/g, "_");
|
|
34
|
+
const targetPath = path.join(blobsDir, safeKey);
|
|
35
|
+
if (!fs.existsSync(targetPath))
|
|
36
|
+
return null;
|
|
37
|
+
const buf = fs.readFileSync(targetPath);
|
|
38
|
+
return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
39
|
+
},
|
|
40
|
+
async deleteBlob(key) {
|
|
41
|
+
const safeKey = key.replace(/[^a-zA-Z0-9._-]/g, "_");
|
|
42
|
+
const targetPath = path.join(blobsDir, safeKey);
|
|
43
|
+
if (fs.existsSync(targetPath)) {
|
|
44
|
+
fs.unlinkSync(targetPath);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function resolveOrCreateSecret(storageDir, explicitSecret) {
|
|
50
|
+
const fromEnv = explicitSecret || process.env.CONTEXT_DROP_SECRET;
|
|
51
|
+
if (fromEnv && fromEnv.trim().length > 0) {
|
|
52
|
+
return crypto.createHash("sha256").update(fromEnv.trim()).digest();
|
|
53
|
+
}
|
|
54
|
+
const secretPath = path.join(storageDir, ".blob_secret");
|
|
55
|
+
if (fs.existsSync(secretPath)) {
|
|
56
|
+
return crypto.createHash("sha256").update(fs.readFileSync(secretPath, "utf8").trim()).digest();
|
|
57
|
+
}
|
|
58
|
+
const generated = crypto.randomBytes(32).toString("hex");
|
|
59
|
+
fs.writeFileSync(secretPath, generated, { encoding: "utf8", mode: 0o600 });
|
|
60
|
+
return crypto.createHash("sha256").update(generated).digest();
|
|
61
|
+
}
|
|
62
|
+
export function createLocalStorageAdapter(options = {}) {
|
|
63
|
+
const storageDir = path.resolve(options.dir || process.env.CONTEXT_DROP_STORAGE_DIR || path.join(process.cwd(), ".context-drop"));
|
|
64
|
+
fs.mkdirSync(storageDir, { recursive: true });
|
|
65
|
+
const gitignorePath = path.join(storageDir, ".gitignore");
|
|
66
|
+
if (!fs.existsSync(gitignorePath)) {
|
|
67
|
+
fs.writeFileSync(gitignorePath, "*\n", "utf8");
|
|
68
|
+
}
|
|
69
|
+
const blobsDir = path.join(storageDir, "blobs");
|
|
70
|
+
fs.mkdirSync(blobsDir, { recursive: true });
|
|
71
|
+
const blobStore = options.blobStore ?? createDiskBlobStore(blobsDir);
|
|
72
|
+
const secretKey = resolveOrCreateSecret(storageDir, options.secret);
|
|
73
|
+
function signId(rawId) {
|
|
74
|
+
const sig = crypto.createHmac("sha256", secretKey).update(rawId).digest("hex");
|
|
75
|
+
return `${rawId}.${sig}`;
|
|
76
|
+
}
|
|
77
|
+
function verifySignedId(token) {
|
|
78
|
+
const dotIdx = token.lastIndexOf(".");
|
|
79
|
+
if (dotIdx <= 0)
|
|
80
|
+
return null;
|
|
81
|
+
const rawId = token.slice(0, dotIdx);
|
|
82
|
+
const provided = token.slice(dotIdx + 1);
|
|
83
|
+
const expected = crypto.createHmac("sha256", secretKey).update(rawId).digest("hex");
|
|
84
|
+
if (provided.length !== expected.length)
|
|
85
|
+
return null;
|
|
86
|
+
if (!crypto.timingSafeEqual(Buffer.from(provided), Buffer.from(expected))) {
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
return rawId;
|
|
90
|
+
}
|
|
91
|
+
const requestedBackend = options.backend ?? "auto";
|
|
92
|
+
const dbPath = path.join(storageDir, "context_drop.sqlite");
|
|
93
|
+
const sqliteDb = requestedBackend === "json" ? null : tryOpenNodeSqlite(dbPath);
|
|
94
|
+
if (requestedBackend === "sqlite" && !sqliteDb) {
|
|
95
|
+
throw new Error("ContextDrop SQLite backend requested, but node:sqlite is unavailable in this Node runtime.");
|
|
96
|
+
}
|
|
97
|
+
if (sqliteDb) {
|
|
98
|
+
sqliteDb.exec(`
|
|
99
|
+
CREATE TABLE IF NOT EXISTS context_drop_pending_blobs (
|
|
100
|
+
id TEXT PRIMARY KEY,
|
|
101
|
+
filename TEXT NOT NULL,
|
|
102
|
+
content_type TEXT NOT NULL,
|
|
103
|
+
byte_size INTEGER NOT NULL,
|
|
104
|
+
dimensions_json TEXT,
|
|
105
|
+
blob_key TEXT NOT NULL,
|
|
106
|
+
created_at TEXT NOT NULL
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
CREATE TABLE IF NOT EXISTS context_drop_feedbacks (
|
|
110
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
111
|
+
environment TEXT NOT NULL,
|
|
112
|
+
status TEXT NOT NULL DEFAULT 'new',
|
|
113
|
+
message TEXT,
|
|
114
|
+
page_url TEXT NOT NULL,
|
|
115
|
+
user_agent TEXT,
|
|
116
|
+
session_id TEXT,
|
|
117
|
+
submitter_json TEXT NOT NULL,
|
|
118
|
+
metadata_json TEXT NOT NULL,
|
|
119
|
+
created_at TEXT NOT NULL,
|
|
120
|
+
updated_at TEXT NOT NULL,
|
|
121
|
+
deleted_at TEXT
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
CREATE INDEX IF NOT EXISTS idx_cd_feedbacks_env_status
|
|
125
|
+
ON context_drop_feedbacks (environment, status, created_at DESC);
|
|
126
|
+
|
|
127
|
+
CREATE TABLE IF NOT EXISTS context_drop_element_references (
|
|
128
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
129
|
+
feedback_id INTEGER NOT NULL REFERENCES context_drop_feedbacks(id) ON DELETE CASCADE,
|
|
130
|
+
client_ref TEXT,
|
|
131
|
+
tag_name TEXT NOT NULL,
|
|
132
|
+
selector TEXT NOT NULL,
|
|
133
|
+
dom_path TEXT,
|
|
134
|
+
text_content TEXT,
|
|
135
|
+
attributes_json TEXT NOT NULL,
|
|
136
|
+
bounding_rect_json TEXT NOT NULL,
|
|
137
|
+
viewport_json TEXT NOT NULL,
|
|
138
|
+
metadata_json TEXT NOT NULL,
|
|
139
|
+
captured_at TEXT NOT NULL
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
CREATE TABLE IF NOT EXISTS context_drop_attachments (
|
|
143
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
144
|
+
feedback_id INTEGER NOT NULL REFERENCES context_drop_feedbacks(id) ON DELETE CASCADE,
|
|
145
|
+
element_reference_id INTEGER REFERENCES context_drop_element_references(id) ON DELETE SET NULL,
|
|
146
|
+
attachment_type TEXT NOT NULL,
|
|
147
|
+
filename TEXT,
|
|
148
|
+
content_type TEXT,
|
|
149
|
+
byte_size INTEGER,
|
|
150
|
+
blob_key TEXT,
|
|
151
|
+
metadata_json TEXT NOT NULL,
|
|
152
|
+
created_at TEXT NOT NULL
|
|
153
|
+
);
|
|
154
|
+
`);
|
|
155
|
+
const cols = sqliteDb.prepare("PRAGMA table_info(context_drop_feedbacks)").all();
|
|
156
|
+
if (!cols.some((c) => String(c["name"]) === "deleted_at")) {
|
|
157
|
+
sqliteDb.exec("ALTER TABLE context_drop_feedbacks ADD COLUMN deleted_at TEXT;");
|
|
158
|
+
}
|
|
159
|
+
const hydrateFeedbackRow = (row) => {
|
|
160
|
+
const fbId = Number(row["id"]);
|
|
161
|
+
const elRows = sqliteDb
|
|
162
|
+
.prepare("SELECT * FROM context_drop_element_references WHERE feedback_id = ? ORDER BY id ASC")
|
|
163
|
+
.all(fbId);
|
|
164
|
+
const attRows = sqliteDb
|
|
165
|
+
.prepare("SELECT * FROM context_drop_attachments WHERE feedback_id = ? ORDER BY id ASC")
|
|
166
|
+
.all(fbId);
|
|
167
|
+
const elements = elRows.map((el) => ({
|
|
168
|
+
id: Number(el["id"]),
|
|
169
|
+
feedback_id: fbId,
|
|
170
|
+
client_ref: el["client_ref"] ?? null,
|
|
171
|
+
tag_name: String(el["tag_name"]),
|
|
172
|
+
selector: String(el["selector"]),
|
|
173
|
+
dom_path: el["dom_path"] ?? null,
|
|
174
|
+
text_content: el["text_content"] ?? null,
|
|
175
|
+
attributes_snapshot: JSON.parse(String(el["attributes_json"] || "{}")),
|
|
176
|
+
bounding_rect: JSON.parse(String(el["bounding_rect_json"] || "{}")),
|
|
177
|
+
viewport: JSON.parse(String(el["viewport_json"] || "{}")),
|
|
178
|
+
metadata: JSON.parse(String(el["metadata_json"] || "{}")),
|
|
179
|
+
captured_at: String(el["captured_at"])
|
|
180
|
+
}));
|
|
181
|
+
const attachments = attRows.map((att) => ({
|
|
182
|
+
id: Number(att["id"]),
|
|
183
|
+
feedback_id: fbId,
|
|
184
|
+
element_reference_id: att["element_reference_id"] !== null && att["element_reference_id"] !== undefined
|
|
185
|
+
? Number(att["element_reference_id"])
|
|
186
|
+
: null,
|
|
187
|
+
attachment_type: att["attachment_type"] === "screenshot" ? "screenshot" : "image",
|
|
188
|
+
filename: att["filename"] ?? null,
|
|
189
|
+
content_type: att["content_type"] ?? null,
|
|
190
|
+
byte_size: att["byte_size"] !== null && att["byte_size"] !== undefined
|
|
191
|
+
? Number(att["byte_size"])
|
|
192
|
+
: null,
|
|
193
|
+
blob_key: att["blob_key"] ?? null,
|
|
194
|
+
metadata: JSON.parse(String(att["metadata_json"] || "{}")),
|
|
195
|
+
created_at: String(att["created_at"])
|
|
196
|
+
}));
|
|
197
|
+
return {
|
|
198
|
+
id: fbId,
|
|
199
|
+
environment: String(row["environment"]),
|
|
200
|
+
status: String(row["status"]),
|
|
201
|
+
message: row["message"] ?? null,
|
|
202
|
+
page_url: String(row["page_url"]),
|
|
203
|
+
user_agent: row["user_agent"] ?? null,
|
|
204
|
+
session_id: row["session_id"] ?? null,
|
|
205
|
+
submitter: JSON.parse(String(row["submitter_json"] || "{}")),
|
|
206
|
+
metadata: JSON.parse(String(row["metadata_json"] || "{}")),
|
|
207
|
+
element_references: elements,
|
|
208
|
+
attachments,
|
|
209
|
+
created_at: String(row["created_at"]),
|
|
210
|
+
updated_at: String(row["updated_at"]),
|
|
211
|
+
deleted_at: row["deleted_at"] ?? null
|
|
212
|
+
};
|
|
213
|
+
};
|
|
214
|
+
return {
|
|
215
|
+
getOwnershipSecret() {
|
|
216
|
+
return secretKey;
|
|
217
|
+
},
|
|
218
|
+
async savePendingBlob(blob) {
|
|
219
|
+
const rawId = crypto.randomUUID();
|
|
220
|
+
const stored = await blobStore.putBlob({
|
|
221
|
+
key: `pending_${rawId}`,
|
|
222
|
+
data: blob.data,
|
|
223
|
+
contentType: blob.content_type,
|
|
224
|
+
filename: blob.filename
|
|
225
|
+
});
|
|
226
|
+
sqliteDb
|
|
227
|
+
.prepare(`INSERT INTO context_drop_pending_blobs
|
|
228
|
+
(id, filename, content_type, byte_size, dimensions_json, blob_key, created_at)
|
|
229
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)`)
|
|
230
|
+
.run(rawId, blob.filename, blob.content_type, blob.byte_size, blob.dimensions ? JSON.stringify(blob.dimensions) : null, stored.key, new Date().toISOString());
|
|
231
|
+
return { signed_blob_id: signId(rawId) };
|
|
232
|
+
},
|
|
233
|
+
async consumePendingBlob(signedBlobId) {
|
|
234
|
+
const rawId = verifySignedId(signedBlobId);
|
|
235
|
+
if (!rawId)
|
|
236
|
+
return null;
|
|
237
|
+
const row = sqliteDb
|
|
238
|
+
.prepare("SELECT * FROM context_drop_pending_blobs WHERE id = ?")
|
|
239
|
+
.get(rawId);
|
|
240
|
+
if (!row)
|
|
241
|
+
return null;
|
|
242
|
+
sqliteDb.prepare("DELETE FROM context_drop_pending_blobs WHERE id = ?").run(rawId);
|
|
243
|
+
const blobKey = String(row["blob_key"]);
|
|
244
|
+
const bytes = await blobStore.getBlob(blobKey);
|
|
245
|
+
if (blobStore.deleteBlob) {
|
|
246
|
+
await blobStore.deleteBlob(blobKey);
|
|
247
|
+
}
|
|
248
|
+
if (!bytes)
|
|
249
|
+
return null;
|
|
250
|
+
return {
|
|
251
|
+
data: bytes,
|
|
252
|
+
filename: String(row["filename"]),
|
|
253
|
+
content_type: String(row["content_type"]),
|
|
254
|
+
byte_size: Number(row["byte_size"]),
|
|
255
|
+
dimensions: row["dimensions_json"]
|
|
256
|
+
? JSON.parse(String(row["dimensions_json"]))
|
|
257
|
+
: null
|
|
258
|
+
};
|
|
259
|
+
},
|
|
260
|
+
async createFeedback(input) {
|
|
261
|
+
const now = new Date().toISOString();
|
|
262
|
+
const fbRes = sqliteDb
|
|
263
|
+
.prepare(`INSERT INTO context_drop_feedbacks
|
|
264
|
+
(environment, status, message, page_url, user_agent, session_id, submitter_json, metadata_json, created_at, updated_at)
|
|
265
|
+
VALUES (?, 'new', ?, ?, ?, ?, ?, ?, ?, ?)`)
|
|
266
|
+
.run(input.environment, input.message, input.page_url, input.user_agent, input.session_id, JSON.stringify(input.submitter || {}), JSON.stringify(input.metadata || {}), now, now);
|
|
267
|
+
const fbId = Number(fbRes.lastInsertRowid);
|
|
268
|
+
const elIdByClientRef = new Map();
|
|
269
|
+
const insertElStmt = sqliteDb.prepare(`INSERT INTO context_drop_element_references
|
|
270
|
+
(feedback_id, client_ref, tag_name, selector, dom_path, text_content, attributes_json, bounding_rect_json, viewport_json, metadata_json, captured_at)
|
|
271
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`);
|
|
272
|
+
input.element_references.forEach((el, idx) => {
|
|
273
|
+
const elRes = insertElStmt.run(fbId, el.client_ref, el.tag_name, el.selector, el.dom_path, el.text_content, JSON.stringify(el.attributes_snapshot || {}), JSON.stringify(el.bounding_rect || {}), JSON.stringify(el.viewport || {}), JSON.stringify(el.metadata || {}), el.captured_at);
|
|
274
|
+
const elId = Number(elRes.lastInsertRowid);
|
|
275
|
+
const key = el.client_ref || String(idx);
|
|
276
|
+
elIdByClientRef.set(key, elId);
|
|
277
|
+
});
|
|
278
|
+
const insertAttStmt = sqliteDb.prepare(`INSERT INTO context_drop_attachments
|
|
279
|
+
(feedback_id, element_reference_id, attachment_type, filename, content_type, byte_size, blob_key, metadata_json, created_at)
|
|
280
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`);
|
|
281
|
+
for (const att of input.attachments) {
|
|
282
|
+
const linkedElId = att.element_client_ref
|
|
283
|
+
? elIdByClientRef.get(att.element_client_ref) ?? null
|
|
284
|
+
: null;
|
|
285
|
+
let blobKey = null;
|
|
286
|
+
if (att.data) {
|
|
287
|
+
const saved = await blobStore.putBlob({
|
|
288
|
+
key: `att_${fbId}_${crypto.randomUUID()}`,
|
|
289
|
+
data: att.data,
|
|
290
|
+
contentType: att.content_type || "image/png",
|
|
291
|
+
filename: att.filename || "attachment.png"
|
|
292
|
+
});
|
|
293
|
+
blobKey = saved.key;
|
|
294
|
+
}
|
|
295
|
+
insertAttStmt.run(fbId, linkedElId, att.attachment_type, att.filename, att.content_type, att.byte_size, blobKey, JSON.stringify(att.metadata || {}), now);
|
|
296
|
+
}
|
|
297
|
+
const created = await this.getFeedback(fbId);
|
|
298
|
+
if (!created) {
|
|
299
|
+
throw new Error("Failed to load created ContextDrop feedback record");
|
|
300
|
+
}
|
|
301
|
+
return created;
|
|
302
|
+
},
|
|
303
|
+
async listFeedbacks(filter) {
|
|
304
|
+
const limit = Math.max(1, Math.min(100, filter.limit ?? 20));
|
|
305
|
+
const offset = Math.max(0, filter.offset ?? 0);
|
|
306
|
+
const rows = sqliteDb
|
|
307
|
+
.prepare("SELECT * FROM context_drop_feedbacks ORDER BY created_at DESC, id DESC")
|
|
308
|
+
.all();
|
|
309
|
+
const hydrated = rows.map(hydrateFeedbackRow).filter((fb) => matchesFeedbackFilter(fb, filter));
|
|
310
|
+
return {
|
|
311
|
+
total: hydrated.length,
|
|
312
|
+
records: hydrated.slice(offset, offset + limit).map(toFeedbackSummary)
|
|
313
|
+
};
|
|
314
|
+
},
|
|
315
|
+
async listOwnFeedbacks(query) {
|
|
316
|
+
const limit = Math.max(1, Math.min(50, query.limit ?? 20));
|
|
317
|
+
const rows = sqliteDb
|
|
318
|
+
.prepare("SELECT * FROM context_drop_feedbacks WHERE deleted_at IS NULL ORDER BY created_at DESC, id DESC")
|
|
319
|
+
.all();
|
|
320
|
+
const matching = rows.map(hydrateFeedbackRow).filter((fb) => matchesOwnFeedback(fb, query));
|
|
321
|
+
return {
|
|
322
|
+
total: matching.length,
|
|
323
|
+
records: matching.slice(0, limit)
|
|
324
|
+
};
|
|
325
|
+
},
|
|
326
|
+
async getFeedback(id) {
|
|
327
|
+
const row = sqliteDb
|
|
328
|
+
.prepare("SELECT * FROM context_drop_feedbacks WHERE id = ?")
|
|
329
|
+
.get(Number(id));
|
|
330
|
+
if (!row)
|
|
331
|
+
return null;
|
|
332
|
+
return hydrateFeedbackRow(row);
|
|
333
|
+
},
|
|
334
|
+
async updateFeedback(id, input) {
|
|
335
|
+
const fbId = Number(id);
|
|
336
|
+
const existing = await this.getFeedback(fbId);
|
|
337
|
+
if (!existing || existing.deleted_at)
|
|
338
|
+
return null;
|
|
339
|
+
const now = new Date().toISOString();
|
|
340
|
+
const nextMessage = input.message !== undefined ? input.message : existing.message;
|
|
341
|
+
const nextMetadata = input.metadata !== undefined ? input.metadata : existing.metadata;
|
|
342
|
+
sqliteDb
|
|
343
|
+
.prepare("UPDATE context_drop_feedbacks SET message = ?, metadata_json = ?, updated_at = ? WHERE id = ?")
|
|
344
|
+
.run(nextMessage, JSON.stringify(nextMetadata || {}), now, fbId);
|
|
345
|
+
const elIdByRef = new Map();
|
|
346
|
+
if (input.element_references !== undefined) {
|
|
347
|
+
sqliteDb
|
|
348
|
+
.prepare("DELETE FROM context_drop_element_references WHERE feedback_id = ?")
|
|
349
|
+
.run(fbId);
|
|
350
|
+
const insertElStmt = sqliteDb.prepare(`INSERT INTO context_drop_element_references
|
|
351
|
+
(feedback_id, client_ref, tag_name, selector, dom_path, text_content, attributes_json, bounding_rect_json, viewport_json, metadata_json, captured_at)
|
|
352
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`);
|
|
353
|
+
input.element_references.forEach((el, idx) => {
|
|
354
|
+
const elRes = insertElStmt.run(fbId, el.client_ref, el.tag_name, el.selector, el.dom_path, el.text_content, JSON.stringify(el.attributes_snapshot || {}), JSON.stringify(el.bounding_rect || {}), JSON.stringify(el.viewport || {}), JSON.stringify(el.metadata || {}), el.captured_at);
|
|
355
|
+
elIdByRef.set(el.client_ref || String(idx), Number(elRes.lastInsertRowid));
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
else {
|
|
359
|
+
existing.element_references.forEach((el, idx) => {
|
|
360
|
+
elIdByRef.set(el.client_ref || String(idx), Number(el.id));
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
if (input.attachments !== undefined) {
|
|
364
|
+
const retainedIds = new Set();
|
|
365
|
+
const prevById = new Map();
|
|
366
|
+
for (const a of existing.attachments) {
|
|
367
|
+
prevById.set(String(a.id), a);
|
|
368
|
+
}
|
|
369
|
+
for (const spec of input.attachments) {
|
|
370
|
+
if (spec.kind === "retain") {
|
|
371
|
+
const prev = prevById.get(String(spec.existing_id));
|
|
372
|
+
if (!prev)
|
|
373
|
+
continue;
|
|
374
|
+
retainedIds.add(Number(prev.id));
|
|
375
|
+
const linkedElId = spec.element_client_ref !== undefined
|
|
376
|
+
? spec.element_client_ref
|
|
377
|
+
? elIdByRef.get(spec.element_client_ref) ?? null
|
|
378
|
+
: null
|
|
379
|
+
: input.element_references === undefined && prev.element_reference_id !== null
|
|
380
|
+
? Number(prev.element_reference_id)
|
|
381
|
+
: null;
|
|
382
|
+
const nextAttMeta = spec.update_metadata && spec.metadata ? spec.metadata : prev.metadata;
|
|
383
|
+
sqliteDb
|
|
384
|
+
.prepare("UPDATE context_drop_attachments SET element_reference_id = ?, metadata_json = ? WHERE id = ?")
|
|
385
|
+
.run(linkedElId, JSON.stringify(nextAttMeta || {}), Number(prev.id));
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
for (const prev of existing.attachments) {
|
|
389
|
+
if (!retainedIds.has(Number(prev.id))) {
|
|
390
|
+
sqliteDb
|
|
391
|
+
.prepare("DELETE FROM context_drop_attachments WHERE id = ?")
|
|
392
|
+
.run(Number(prev.id));
|
|
393
|
+
if (prev.blob_key && blobStore.deleteBlob) {
|
|
394
|
+
await blobStore.deleteBlob(prev.blob_key);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
const insertAttStmt = sqliteDb.prepare(`INSERT INTO context_drop_attachments
|
|
399
|
+
(feedback_id, element_reference_id, attachment_type, filename, content_type, byte_size, blob_key, metadata_json, created_at)
|
|
400
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`);
|
|
401
|
+
for (const spec of input.attachments) {
|
|
402
|
+
if (spec.kind !== "create")
|
|
403
|
+
continue;
|
|
404
|
+
const linkedElId = spec.element_client_ref
|
|
405
|
+
? elIdByRef.get(spec.element_client_ref) ?? null
|
|
406
|
+
: null;
|
|
407
|
+
let blobKey = null;
|
|
408
|
+
if (spec.data) {
|
|
409
|
+
const saved = await blobStore.putBlob({
|
|
410
|
+
key: `att_${fbId}_${crypto.randomUUID()}`,
|
|
411
|
+
data: spec.data,
|
|
412
|
+
contentType: spec.content_type || "image/png",
|
|
413
|
+
filename: spec.filename || "attachment.png"
|
|
414
|
+
});
|
|
415
|
+
blobKey = saved.key;
|
|
416
|
+
}
|
|
417
|
+
insertAttStmt.run(fbId, linkedElId, spec.attachment_type, spec.filename, spec.content_type, spec.byte_size, blobKey, JSON.stringify(spec.metadata || {}), now);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
return this.getFeedback(fbId);
|
|
421
|
+
},
|
|
422
|
+
async softDeleteFeedback(id) {
|
|
423
|
+
const fbId = Number(id);
|
|
424
|
+
const existing = await this.getFeedback(fbId);
|
|
425
|
+
if (!existing || existing.deleted_at)
|
|
426
|
+
return null;
|
|
427
|
+
const now = new Date().toISOString();
|
|
428
|
+
sqliteDb
|
|
429
|
+
.prepare("UPDATE context_drop_feedbacks SET deleted_at = ?, updated_at = ? WHERE id = ? AND deleted_at IS NULL")
|
|
430
|
+
.run(now, now, fbId);
|
|
431
|
+
return this.getFeedback(fbId);
|
|
432
|
+
},
|
|
433
|
+
async getAttachment(attachmentId) {
|
|
434
|
+
const row = sqliteDb
|
|
435
|
+
.prepare(`SELECT a.*, e.selector AS element_selector
|
|
436
|
+
FROM context_drop_attachments a
|
|
437
|
+
LEFT JOIN context_drop_element_references e ON e.id = a.element_reference_id
|
|
438
|
+
WHERE a.id = ?`)
|
|
439
|
+
.get(Number(attachmentId));
|
|
440
|
+
if (!row)
|
|
441
|
+
return null;
|
|
442
|
+
const blobKey = row["blob_key"] ?? null;
|
|
443
|
+
const bytes = blobKey ? await blobStore.getBlob(blobKey) : null;
|
|
444
|
+
const attachment = {
|
|
445
|
+
id: Number(row["id"]),
|
|
446
|
+
feedback_id: Number(row["feedback_id"]),
|
|
447
|
+
element_reference_id: row["element_reference_id"] !== null && row["element_reference_id"] !== undefined
|
|
448
|
+
? Number(row["element_reference_id"])
|
|
449
|
+
: null,
|
|
450
|
+
attachment_type: row["attachment_type"] === "screenshot" ? "screenshot" : "image",
|
|
451
|
+
filename: row["filename"] ?? null,
|
|
452
|
+
content_type: row["content_type"] ?? null,
|
|
453
|
+
byte_size: row["byte_size"] !== null && row["byte_size"] !== undefined
|
|
454
|
+
? Number(row["byte_size"])
|
|
455
|
+
: null,
|
|
456
|
+
blob_key: blobKey,
|
|
457
|
+
metadata: JSON.parse(String(row["metadata_json"] || "{}")),
|
|
458
|
+
created_at: String(row["created_at"])
|
|
459
|
+
};
|
|
460
|
+
return {
|
|
461
|
+
attachment,
|
|
462
|
+
element_selector: row["element_selector"] ?? null,
|
|
463
|
+
data: bytes
|
|
464
|
+
};
|
|
465
|
+
},
|
|
466
|
+
async getAttachmentBytes(attachmentId) {
|
|
467
|
+
const res = await this.getAttachment(attachmentId);
|
|
468
|
+
return res?.data ?? null;
|
|
469
|
+
},
|
|
470
|
+
async updateFeedbackStatus(id, status) {
|
|
471
|
+
const existing = await this.getFeedback(id);
|
|
472
|
+
if (!existing || existing.deleted_at)
|
|
473
|
+
return null;
|
|
474
|
+
const now = new Date().toISOString();
|
|
475
|
+
sqliteDb
|
|
476
|
+
.prepare("UPDATE context_drop_feedbacks SET status = ?, updated_at = ? WHERE id = ?")
|
|
477
|
+
.run(status, now, Number(id));
|
|
478
|
+
const updated = await this.getFeedback(id);
|
|
479
|
+
if (!updated)
|
|
480
|
+
return null;
|
|
481
|
+
return {
|
|
482
|
+
previous_status: existing.status,
|
|
483
|
+
feedback: updated
|
|
484
|
+
};
|
|
485
|
+
}
|
|
486
|
+
};
|
|
487
|
+
}
|
|
488
|
+
// Fallback atomic JSON file store
|
|
489
|
+
const jsonFilePath = path.join(storageDir, "feedbacks.json");
|
|
490
|
+
function readJsonStore() {
|
|
491
|
+
if (!fs.existsSync(jsonFilePath)) {
|
|
492
|
+
return {
|
|
493
|
+
nextFeedbackId: 1,
|
|
494
|
+
nextElementId: 1,
|
|
495
|
+
nextAttachmentId: 1,
|
|
496
|
+
pendingBlobs: {},
|
|
497
|
+
feedbacks: []
|
|
498
|
+
};
|
|
499
|
+
}
|
|
500
|
+
try {
|
|
501
|
+
const raw = fs.readFileSync(jsonFilePath, "utf8");
|
|
502
|
+
return JSON.parse(raw);
|
|
503
|
+
}
|
|
504
|
+
catch {
|
|
505
|
+
return {
|
|
506
|
+
nextFeedbackId: 1,
|
|
507
|
+
nextElementId: 1,
|
|
508
|
+
nextAttachmentId: 1,
|
|
509
|
+
pendingBlobs: {},
|
|
510
|
+
feedbacks: []
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
function writeJsonStore(state) {
|
|
515
|
+
const tmpPath = `${jsonFilePath}.${process.pid}.${Date.now()}.tmp`;
|
|
516
|
+
fs.writeFileSync(tmpPath, JSON.stringify(state, null, 2), "utf8");
|
|
517
|
+
fs.renameSync(tmpPath, jsonFilePath);
|
|
518
|
+
}
|
|
519
|
+
return {
|
|
520
|
+
getOwnershipSecret() {
|
|
521
|
+
return secretKey;
|
|
522
|
+
},
|
|
523
|
+
async savePendingBlob(blob) {
|
|
524
|
+
const rawId = crypto.randomUUID();
|
|
525
|
+
const stored = await blobStore.putBlob({
|
|
526
|
+
key: `pending_${rawId}`,
|
|
527
|
+
data: blob.data,
|
|
528
|
+
contentType: blob.content_type,
|
|
529
|
+
filename: blob.filename
|
|
530
|
+
});
|
|
531
|
+
const state = readJsonStore();
|
|
532
|
+
state.pendingBlobs[rawId] = {
|
|
533
|
+
filename: blob.filename,
|
|
534
|
+
content_type: blob.content_type,
|
|
535
|
+
byte_size: blob.byte_size,
|
|
536
|
+
dimensions: blob.dimensions,
|
|
537
|
+
blob_key: stored.key,
|
|
538
|
+
created_at: new Date().toISOString()
|
|
539
|
+
};
|
|
540
|
+
writeJsonStore(state);
|
|
541
|
+
return { signed_blob_id: signId(rawId) };
|
|
542
|
+
},
|
|
543
|
+
async consumePendingBlob(signedBlobId) {
|
|
544
|
+
const rawId = verifySignedId(signedBlobId);
|
|
545
|
+
if (!rawId)
|
|
546
|
+
return null;
|
|
547
|
+
const state = readJsonStore();
|
|
548
|
+
const entry = state.pendingBlobs[rawId];
|
|
549
|
+
if (!entry)
|
|
550
|
+
return null;
|
|
551
|
+
delete state.pendingBlobs[rawId];
|
|
552
|
+
writeJsonStore(state);
|
|
553
|
+
const bytes = await blobStore.getBlob(entry.blob_key);
|
|
554
|
+
if (blobStore.deleteBlob) {
|
|
555
|
+
await blobStore.deleteBlob(entry.blob_key);
|
|
556
|
+
}
|
|
557
|
+
if (!bytes)
|
|
558
|
+
return null;
|
|
559
|
+
return {
|
|
560
|
+
data: bytes,
|
|
561
|
+
filename: entry.filename,
|
|
562
|
+
content_type: entry.content_type,
|
|
563
|
+
byte_size: entry.byte_size,
|
|
564
|
+
dimensions: entry.dimensions
|
|
565
|
+
};
|
|
566
|
+
},
|
|
567
|
+
async createFeedback(input) {
|
|
568
|
+
const state = readJsonStore();
|
|
569
|
+
const id = state.nextFeedbackId++;
|
|
570
|
+
const now = new Date().toISOString();
|
|
571
|
+
const elementsByRef = new Map();
|
|
572
|
+
const storedElements = input.element_references.map((el, idx) => {
|
|
573
|
+
const elRec = {
|
|
574
|
+
id: state.nextElementId++,
|
|
575
|
+
feedback_id: id,
|
|
576
|
+
client_ref: el.client_ref,
|
|
577
|
+
tag_name: el.tag_name,
|
|
578
|
+
selector: el.selector,
|
|
579
|
+
dom_path: el.dom_path,
|
|
580
|
+
text_content: el.text_content,
|
|
581
|
+
attributes_snapshot: { ...el.attributes_snapshot },
|
|
582
|
+
bounding_rect: { ...el.bounding_rect },
|
|
583
|
+
viewport: { ...el.viewport },
|
|
584
|
+
metadata: { ...el.metadata },
|
|
585
|
+
captured_at: el.captured_at
|
|
586
|
+
};
|
|
587
|
+
elementsByRef.set(el.client_ref || String(idx), elRec);
|
|
588
|
+
return elRec;
|
|
589
|
+
});
|
|
590
|
+
const storedAttachments = [];
|
|
591
|
+
for (const att of input.attachments) {
|
|
592
|
+
const attId = state.nextAttachmentId++;
|
|
593
|
+
const linkedEl = att.element_client_ref
|
|
594
|
+
? elementsByRef.get(att.element_client_ref) ?? null
|
|
595
|
+
: null;
|
|
596
|
+
let blobKey = null;
|
|
597
|
+
if (att.data) {
|
|
598
|
+
const saved = await blobStore.putBlob({
|
|
599
|
+
key: `att_${id}_${attId}_${crypto.randomUUID()}`,
|
|
600
|
+
data: att.data,
|
|
601
|
+
contentType: att.content_type || "image/png",
|
|
602
|
+
filename: att.filename || "attachment.png"
|
|
603
|
+
});
|
|
604
|
+
blobKey = saved.key;
|
|
605
|
+
}
|
|
606
|
+
storedAttachments.push({
|
|
607
|
+
id: attId,
|
|
608
|
+
feedback_id: id,
|
|
609
|
+
element_reference_id: linkedEl ? linkedEl.id : null,
|
|
610
|
+
attachment_type: att.attachment_type,
|
|
611
|
+
filename: att.filename,
|
|
612
|
+
content_type: att.content_type,
|
|
613
|
+
byte_size: att.byte_size,
|
|
614
|
+
blob_key: blobKey,
|
|
615
|
+
metadata: { ...att.metadata },
|
|
616
|
+
created_at: now
|
|
617
|
+
});
|
|
618
|
+
}
|
|
619
|
+
const record = {
|
|
620
|
+
id,
|
|
621
|
+
environment: input.environment,
|
|
622
|
+
status: "new",
|
|
623
|
+
message: input.message,
|
|
624
|
+
page_url: input.page_url,
|
|
625
|
+
user_agent: input.user_agent,
|
|
626
|
+
session_id: input.session_id,
|
|
627
|
+
submitter: { ...input.submitter },
|
|
628
|
+
metadata: { ...input.metadata },
|
|
629
|
+
element_references: storedElements,
|
|
630
|
+
attachments: storedAttachments,
|
|
631
|
+
created_at: now,
|
|
632
|
+
updated_at: now,
|
|
633
|
+
deleted_at: null
|
|
634
|
+
};
|
|
635
|
+
state.feedbacks.push(record);
|
|
636
|
+
writeJsonStore(state);
|
|
637
|
+
return structuredClone(record);
|
|
638
|
+
},
|
|
639
|
+
async listFeedbacks(filter) {
|
|
640
|
+
const state = readJsonStore();
|
|
641
|
+
const limit = Math.max(1, Math.min(100, filter.limit ?? 20));
|
|
642
|
+
const offset = Math.max(0, filter.offset ?? 0);
|
|
643
|
+
const matching = state.feedbacks
|
|
644
|
+
.filter((fb) => matchesFeedbackFilter(fb, filter))
|
|
645
|
+
.sort((a, b) => {
|
|
646
|
+
const dt = Date.parse(b.created_at) - Date.parse(a.created_at);
|
|
647
|
+
return dt !== 0 ? dt : Number(b.id) - Number(a.id);
|
|
648
|
+
});
|
|
649
|
+
return {
|
|
650
|
+
total: matching.length,
|
|
651
|
+
records: matching.slice(offset, offset + limit).map(toFeedbackSummary)
|
|
652
|
+
};
|
|
653
|
+
},
|
|
654
|
+
async listOwnFeedbacks(query) {
|
|
655
|
+
const state = readJsonStore();
|
|
656
|
+
const limit = Math.max(1, Math.min(50, query.limit ?? 20));
|
|
657
|
+
const matching = state.feedbacks
|
|
658
|
+
.filter((fb) => matchesOwnFeedback(fb, query))
|
|
659
|
+
.sort((a, b) => {
|
|
660
|
+
const dt = Date.parse(b.created_at) - Date.parse(a.created_at);
|
|
661
|
+
return dt !== 0 ? dt : Number(b.id) - Number(a.id);
|
|
662
|
+
});
|
|
663
|
+
return {
|
|
664
|
+
total: matching.length,
|
|
665
|
+
records: matching.slice(0, limit).map((fb) => structuredClone(fb))
|
|
666
|
+
};
|
|
667
|
+
},
|
|
668
|
+
async getFeedback(id) {
|
|
669
|
+
const state = readJsonStore();
|
|
670
|
+
const found = state.feedbacks.find((fb) => String(fb.id) === String(id));
|
|
671
|
+
return found ? structuredClone(found) : null;
|
|
672
|
+
},
|
|
673
|
+
async updateFeedback(id, input) {
|
|
674
|
+
const state = readJsonStore();
|
|
675
|
+
const found = state.feedbacks.find((fb) => String(fb.id) === String(id));
|
|
676
|
+
if (!found || found.deleted_at)
|
|
677
|
+
return null;
|
|
678
|
+
const now = new Date().toISOString();
|
|
679
|
+
if (input.message !== undefined) {
|
|
680
|
+
found.message = input.message;
|
|
681
|
+
}
|
|
682
|
+
if (input.metadata !== undefined) {
|
|
683
|
+
found.metadata = { ...input.metadata };
|
|
684
|
+
}
|
|
685
|
+
const elementsByRef = new Map();
|
|
686
|
+
if (input.element_references !== undefined) {
|
|
687
|
+
found.element_references = input.element_references.map((el, idx) => {
|
|
688
|
+
const elRec = {
|
|
689
|
+
id: state.nextElementId++,
|
|
690
|
+
feedback_id: found.id,
|
|
691
|
+
client_ref: el.client_ref,
|
|
692
|
+
tag_name: el.tag_name,
|
|
693
|
+
selector: el.selector,
|
|
694
|
+
dom_path: el.dom_path,
|
|
695
|
+
text_content: el.text_content,
|
|
696
|
+
attributes_snapshot: { ...el.attributes_snapshot },
|
|
697
|
+
bounding_rect: { ...el.bounding_rect },
|
|
698
|
+
viewport: { ...el.viewport },
|
|
699
|
+
metadata: { ...el.metadata },
|
|
700
|
+
captured_at: el.captured_at
|
|
701
|
+
};
|
|
702
|
+
elementsByRef.set(el.client_ref || String(idx), elRec);
|
|
703
|
+
return elRec;
|
|
704
|
+
});
|
|
705
|
+
}
|
|
706
|
+
else {
|
|
707
|
+
found.element_references.forEach((el, idx) => {
|
|
708
|
+
elementsByRef.set(el.client_ref || String(idx), el);
|
|
709
|
+
});
|
|
710
|
+
}
|
|
711
|
+
if (input.attachments !== undefined) {
|
|
712
|
+
const prevById = new Map();
|
|
713
|
+
for (const a of found.attachments) {
|
|
714
|
+
prevById.set(String(a.id), a);
|
|
715
|
+
}
|
|
716
|
+
const nextAttachments = [];
|
|
717
|
+
const retainedBlobKeys = new Set();
|
|
718
|
+
for (const spec of input.attachments) {
|
|
719
|
+
if (spec.kind === "retain") {
|
|
720
|
+
const prev = prevById.get(String(spec.existing_id));
|
|
721
|
+
if (!prev)
|
|
722
|
+
continue;
|
|
723
|
+
if (prev.blob_key)
|
|
724
|
+
retainedBlobKeys.add(prev.blob_key);
|
|
725
|
+
const linkedEl = spec.element_client_ref !== undefined
|
|
726
|
+
? spec.element_client_ref
|
|
727
|
+
? elementsByRef.get(spec.element_client_ref) ?? null
|
|
728
|
+
: null
|
|
729
|
+
: input.element_references === undefined
|
|
730
|
+
? found.element_references.find((e) => e.id === prev.element_reference_id) ?? null
|
|
731
|
+
: null;
|
|
732
|
+
nextAttachments.push({
|
|
733
|
+
...prev,
|
|
734
|
+
element_reference_id: linkedEl ? linkedEl.id : null,
|
|
735
|
+
metadata: spec.update_metadata && spec.metadata ? { ...spec.metadata } : { ...prev.metadata }
|
|
736
|
+
});
|
|
737
|
+
}
|
|
738
|
+
else {
|
|
739
|
+
const attId = state.nextAttachmentId++;
|
|
740
|
+
const linkedEl = spec.element_client_ref
|
|
741
|
+
? elementsByRef.get(spec.element_client_ref) ?? null
|
|
742
|
+
: null;
|
|
743
|
+
let blobKey = null;
|
|
744
|
+
if (spec.data) {
|
|
745
|
+
const saved = await blobStore.putBlob({
|
|
746
|
+
key: `att_${found.id}_${attId}_${crypto.randomUUID()}`,
|
|
747
|
+
data: spec.data,
|
|
748
|
+
contentType: spec.content_type || "image/png",
|
|
749
|
+
filename: spec.filename || "attachment.png"
|
|
750
|
+
});
|
|
751
|
+
blobKey = saved.key;
|
|
752
|
+
retainedBlobKeys.add(blobKey);
|
|
753
|
+
}
|
|
754
|
+
nextAttachments.push({
|
|
755
|
+
id: attId,
|
|
756
|
+
feedback_id: found.id,
|
|
757
|
+
element_reference_id: linkedEl ? linkedEl.id : null,
|
|
758
|
+
attachment_type: spec.attachment_type,
|
|
759
|
+
filename: spec.filename,
|
|
760
|
+
content_type: spec.content_type,
|
|
761
|
+
byte_size: spec.byte_size,
|
|
762
|
+
blob_key: blobKey,
|
|
763
|
+
metadata: { ...spec.metadata },
|
|
764
|
+
created_at: now
|
|
765
|
+
});
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
for (const prev of found.attachments) {
|
|
769
|
+
if (prev.blob_key && !retainedBlobKeys.has(prev.blob_key) && blobStore.deleteBlob) {
|
|
770
|
+
await blobStore.deleteBlob(prev.blob_key);
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
found.attachments = nextAttachments;
|
|
774
|
+
}
|
|
775
|
+
found.updated_at = now;
|
|
776
|
+
writeJsonStore(state);
|
|
777
|
+
return structuredClone(found);
|
|
778
|
+
},
|
|
779
|
+
async softDeleteFeedback(id) {
|
|
780
|
+
const state = readJsonStore();
|
|
781
|
+
const found = state.feedbacks.find((fb) => String(fb.id) === String(id));
|
|
782
|
+
if (!found || found.deleted_at)
|
|
783
|
+
return null;
|
|
784
|
+
const now = new Date().toISOString();
|
|
785
|
+
found.deleted_at = now;
|
|
786
|
+
found.updated_at = now;
|
|
787
|
+
writeJsonStore(state);
|
|
788
|
+
return structuredClone(found);
|
|
789
|
+
},
|
|
790
|
+
async getAttachment(attachmentId) {
|
|
791
|
+
const state = readJsonStore();
|
|
792
|
+
for (const fb of state.feedbacks) {
|
|
793
|
+
const att = fb.attachments.find((a) => String(a.id) === String(attachmentId));
|
|
794
|
+
if (att) {
|
|
795
|
+
const linkedEl = att.element_reference_id !== null
|
|
796
|
+
? fb.element_references.find((e) => e.id === att.element_reference_id)
|
|
797
|
+
: undefined;
|
|
798
|
+
const bytes = att.blob_key ? await blobStore.getBlob(att.blob_key) : null;
|
|
799
|
+
return {
|
|
800
|
+
attachment: structuredClone(att),
|
|
801
|
+
element_selector: linkedEl?.selector ?? null,
|
|
802
|
+
data: bytes
|
|
803
|
+
};
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
return null;
|
|
807
|
+
},
|
|
808
|
+
async getAttachmentBytes(attachmentId) {
|
|
809
|
+
const res = await this.getAttachment(attachmentId);
|
|
810
|
+
return res?.data ?? null;
|
|
811
|
+
},
|
|
812
|
+
async updateFeedbackStatus(id, status) {
|
|
813
|
+
const state = readJsonStore();
|
|
814
|
+
const found = state.feedbacks.find((fb) => String(fb.id) === String(id));
|
|
815
|
+
if (!found || found.deleted_at)
|
|
816
|
+
return null;
|
|
817
|
+
const previous_status = found.status;
|
|
818
|
+
found.status = status;
|
|
819
|
+
found.updated_at = new Date().toISOString();
|
|
820
|
+
writeJsonStore(state);
|
|
821
|
+
return {
|
|
822
|
+
previous_status,
|
|
823
|
+
feedback: structuredClone(found)
|
|
824
|
+
};
|
|
825
|
+
}
|
|
826
|
+
};
|
|
827
|
+
}
|