context_drop 0.2.0 → 0.3.1

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.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +149 -12
  3. data/Rakefile +110 -1
  4. data/app/assets/javascripts/context_drop/context_drop.js +1759 -105
  5. data/app/controllers/context_drop/application_controller.rb +52 -3
  6. data/app/controllers/context_drop/configs_controller.rb +10 -1
  7. data/app/controllers/context_drop/dashboard/feedbacks_controller.rb +99 -14
  8. data/app/controllers/context_drop/feedbacks_controller.rb +536 -17
  9. data/app/helpers/context_drop/dashboard_helper.rb +4 -0
  10. data/app/javascript/context_drop/__tests__/api-client.test.ts +255 -0
  11. data/app/javascript/context_drop/__tests__/widget.test.ts +355 -1
  12. data/app/javascript/context_drop/api-client.ts +343 -22
  13. data/app/javascript/context_drop/element-picker.ts +1 -1
  14. data/app/javascript/context_drop/icons.ts +6 -0
  15. data/app/javascript/context_drop/screenshot.ts +1 -1
  16. data/app/javascript/context_drop/styles.ts +311 -0
  17. data/app/javascript/context_drop/types.ts +141 -6
  18. data/app/javascript/context_drop/widget.ts +1072 -35
  19. data/app/models/context_drop/attachment.rb +5 -0
  20. data/app/models/context_drop/comment.rb +50 -0
  21. data/app/models/context_drop/feedback.rb +81 -11
  22. data/app/services/context_drop/ownership_token.rb +114 -0
  23. data/app/views/context_drop/dashboard/feedbacks/index.html.erb +14 -0
  24. data/app/views/context_drop/dashboard/feedbacks/show.html.erb +169 -2
  25. data/app/views/layouts/context_drop/application.html.erb +131 -7
  26. data/config/routes.rb +16 -1
  27. data/db/migrate/20261113000005_add_deleted_at_to_context_drop_feedbacks.rb +11 -0
  28. data/db/migrate/20261116000001_create_context_drop_comments_and_update_statuses.rb +44 -0
  29. data/lib/context_drop/agent/orchestrator.rb +1 -1
  30. data/lib/context_drop/configuration.rb +26 -0
  31. data/lib/context_drop/mcp/dispatcher.rb +2 -1
  32. data/lib/context_drop/mcp/prompts/investigate_feedback.rb +5 -3
  33. data/lib/context_drop/mcp/server.rb +1 -0
  34. data/lib/context_drop/mcp/tools/add_feedback_comment.rb +139 -0
  35. data/lib/context_drop/mcp/tools/get_feedback.rb +45 -6
  36. data/lib/context_drop/mcp/tools/get_feedback_attachment.rb +3 -0
  37. data/lib/context_drop/mcp/tools/list_feedbacks.rb +23 -3
  38. data/lib/context_drop/mcp/tools/update_feedback_status.rb +5 -3
  39. data/lib/context_drop/version.rb +1 -1
  40. metadata +7 -2
@@ -3,7 +3,36 @@
3
3
  module ContextDrop
4
4
  class FeedbacksController < ApplicationController
5
5
  before_action :enforce_submission_authorization!
6
- before_action :enforce_rate_limit!
6
+ before_action :enforce_rate_limit!, only: %i[create update destroy create_comment]
7
+
8
+ OWNER_ALLOWED_STATUSES = %w[reviewed dismissed].freeze
9
+ MAX_OWN_FEEDBACKS_LIMIT = 50
10
+ DEFAULT_OWN_FEEDBACKS_LIMIT = 20
11
+
12
+ def index
13
+ raw_limit = params[:limit] ? params[:limit].to_i : DEFAULT_OWN_FEEDBACKS_LIMIT
14
+ limit = raw_limit.clamp(1, MAX_OWN_FEEDBACKS_LIMIT)
15
+
16
+ scope = own_active_feedbacks_scope
17
+ total = scope.count
18
+ raw_tokens = combined_raw_ownership_tokens
19
+
20
+ records = scope
21
+ .includes(:element_references, :attachments, comments: :attachments)
22
+ .recent
23
+ .limit(limit)
24
+ .map do |fb|
25
+ existing_tok = ContextDrop::OwnershipToken.find_token_for(raw_tokens, fb.id)
26
+ tok = existing_tok || ContextDrop::OwnershipToken.generate(fb.id)
27
+ serialize_own_feedback(fb, ownership_token: tok)
28
+ end
29
+
30
+ render json: {
31
+ total_count: total,
32
+ total: total,
33
+ feedbacks: records
34
+ }, status: :ok
35
+ end
7
36
 
8
37
  def create
9
38
  payload = extract_feedback_payload
@@ -33,7 +62,7 @@ module ContextDrop
33
62
 
34
63
  feedback_attrs = {
35
64
  submitter: current_context_drop_submitter,
36
- session_id: hashed_session_identifier,
65
+ session_id: hashed_session_identifier(initialize_if_missing: true),
37
66
  message: payload["message"].to_s.strip.presence,
38
67
  page_url: (payload["page_url"].presence || request.referer).to_s.strip,
39
68
  user_agent: (payload["user_agent"].presence || request.user_agent).to_s.strip.presence,
@@ -59,15 +88,7 @@ module ContextDrop
59
88
  att_hash = stringify_hash(raw_att)
60
89
  att_type = (att_hash["attachment_type"] || att_hash["type"] || "image").to_s.strip.downcase
61
90
  att_meta = ContextDrop::MetadataSanitizer.sanitize_attachment_metadata(att_hash["metadata"])
62
-
63
- linked_ref_key = (
64
- att_hash["element_reference_ref"] ||
65
- att_hash["element_attachment_ref"] ||
66
- att_hash["client_ref"] ||
67
- att_meta["element_attachment_ref"] ||
68
- att_meta["client_ref"]
69
- ).to_s.strip.presence
70
-
91
+ linked_ref_key = extract_linked_element_ref_key(att_hash, att_meta)
71
92
  linked_el = linked_ref_key ? element_refs_by_client_ref[linked_ref_key] : nil
72
93
  file_source = resolve_attachment_file_source(att_hash)
73
94
 
@@ -80,7 +101,7 @@ module ContextDrop
80
101
 
81
102
  att_record = feedback.attachments.build(
82
103
  attachment_type: att_type,
83
- element_reference: linked_el,
104
+ element_reference: (linked_el && !linked_el.new_record? ? linked_el : nil),
84
105
  metadata: att_meta
85
106
  )
86
107
  pending_file_attachments << [att_record, linked_el, file_source]
@@ -104,20 +125,516 @@ module ContextDrop
104
125
 
105
126
  return if performed?
106
127
 
107
- invoke_on_feedback_created(feedback)
128
+ feedback.reload
129
+ invoke_lifecycle_callback(:on_feedback_created, feedback)
130
+ ownership_token = ContextDrop::OwnershipToken.generate(feedback.id)
131
+
132
+ render json: {
133
+ id: feedback.id,
134
+ environment: feedback.environment_name,
135
+ status: feedback.status,
136
+ attachments_count: feedback.attachments.size,
137
+ element_references_count: feedback.element_references.size,
138
+ ownership_token: ownership_token,
139
+ feedback: serialize_own_feedback(feedback, ownership_token: ownership_token),
140
+ created_at: feedback.created_at.iso8601,
141
+ updated_at: feedback.updated_at.iso8601
142
+ }, status: :created
143
+ end
144
+
145
+ def update
146
+ feedback = ContextDrop::Feedback.includes(:element_references, :attachments, comments: :attachments).find_by(id: params[:id])
147
+ if feedback.nil? || feedback.deleted?
148
+ render json: { error: "Feedback not found" }, status: :not_found
149
+ return
150
+ end
151
+
152
+ payload = extract_feedback_payload
153
+ unless owns_feedback?(feedback, payload_token: payload["ownership_token"])
154
+ render json: { error: "Forbidden" }, status: :forbidden
155
+ return
156
+ end
157
+
158
+ has_elements_key = payload.key?("element_references")
159
+ has_attachments_key = payload.key?("attachments")
160
+ has_status_key = payload.key?("status")
161
+ requested_status = has_status_key ? payload["status"].to_s.strip : nil
162
+
163
+ if has_status_key && !OWNER_ALLOWED_STATUSES.include?(requested_status)
164
+ render json: { error: "Invalid status '#{requested_status}'" }, status: :unprocessable_entity
165
+ return
166
+ end
167
+ raw_elements = has_elements_key ? Array(payload["element_references"]) : nil
168
+ raw_attachments = has_attachments_key ? Array(payload["attachments"]) : nil
169
+
170
+ if has_attachments_key && raw_attachments.size > ContextDrop.config.max_attachments.to_i
171
+ render json: {
172
+ error: "Attachments count (#{raw_attachments.size}) exceeds maximum allowed (#{ContextDrop.config.max_attachments})"
173
+ }, status: :unprocessable_entity
174
+ return
175
+ end
176
+
177
+ if has_elements_key && raw_elements.size > ContextDrop.config.max_element_references.to_i
178
+ render json: {
179
+ error: "Element references count (#{raw_elements.size}) exceeds maximum allowed (#{ContextDrop.config.max_element_references})"
180
+ }, status: :unprocessable_entity
181
+ return
182
+ end
183
+
184
+ ContextDrop::Feedback.transaction do
185
+ content_edited = false
186
+
187
+ if payload.key?("message")
188
+ feedback.message = payload["message"].to_s.strip.presence
189
+ content_edited = true
190
+ end
191
+
192
+ if payload.key?("metadata")
193
+ sanitized_meta = ContextDrop::MetadataSanitizer.sanitize_feedback_metadata(payload["metadata"])
194
+ sanitized_meta["environment"] = feedback.environment_name
195
+ feedback.metadata = sanitized_meta
196
+ content_edited = true
197
+ end
198
+
199
+ content_edited = true if has_elements_key || has_attachments_key
200
+
201
+ if has_status_key
202
+ feedback.status = requested_status
203
+ elsif content_edited
204
+ feedback.status = "updated"
205
+ end
206
+
207
+ element_refs_by_client_ref = {}
208
+ if has_elements_key
209
+ sanitized_elements = raw_elements.map do |raw_el|
210
+ ContextDrop::MetadataSanitizer.sanitize_element_reference(raw_el)
211
+ end
212
+
213
+ feedback.element_references.each(&:mark_for_destruction)
214
+ sanitized_elements.each_with_index do |sanitized_el, idx|
215
+ el_record = feedback.element_references.build(sanitized_el)
216
+ client_key = sanitized_el[:client_ref].presence || idx.to_s
217
+ element_refs_by_client_ref[client_key] = el_record
218
+ end
219
+ else
220
+ feedback.element_references.each_with_index do |el_record, idx|
221
+ client_key = el_record.client_ref.presence || idx.to_s
222
+ element_refs_by_client_ref[client_key] = el_record
223
+ end
224
+ end
225
+
226
+ retained_attachment_updates = []
227
+ pending_file_attachments = []
228
+
229
+ if has_attachments_key
230
+ existing_by_id = feedback.attachments.index_by { |a| a.id.to_s }
231
+ retained_ids = {}
232
+
233
+ raw_attachments.each do |raw_att|
234
+ att_hash = stringify_hash(raw_att)
235
+ has_new_binary = att_hash["signed_blob_id"].present? ||
236
+ att_hash["file"].present? ||
237
+ att_hash["data_url"].present? ||
238
+ att_hash["data"].present?
239
+ existing_id = (att_hash["existing_id"] || (!has_new_binary ? att_hash["id"] : nil)).to_s.strip
240
+
241
+ if existing_id.present?
242
+ existing_att = existing_by_id[existing_id]
243
+ unless existing_att
244
+ render json: { error: "Attachment does not belong to this feedback" }, status: :unprocessable_entity
245
+ raise ActiveRecord::Rollback
246
+ end
247
+
248
+ retained_ids[existing_id] = true
249
+ has_custom_meta = att_hash.key?("metadata") && att_hash["metadata"].is_a?(Hash)
250
+ att_meta = if has_custom_meta
251
+ merged_meta = (existing_att.metadata.is_a?(Hash) ? existing_att.metadata : {}).merge(
252
+ ContextDrop::MetadataSanitizer.sanitize_attachment_metadata(att_hash["metadata"])
253
+ )
254
+ merged_meta
255
+ else
256
+ existing_att.metadata.is_a?(Hash) ? existing_att.metadata : {}
257
+ end
258
+
259
+ linked_ref_key = extract_linked_element_ref_key(att_hash, att_meta)
260
+ linked_el = if linked_ref_key
261
+ element_refs_by_client_ref[linked_ref_key]
262
+ elsif !has_elements_key
263
+ existing_att.element_reference
264
+ end
265
+
266
+ retained_attachment_updates << [existing_att, linked_el, att_meta, has_custom_meta]
267
+ else
268
+ att_type = (att_hash["attachment_type"] || att_hash["type"] || "image").to_s.strip.downcase
269
+ att_meta = ContextDrop::MetadataSanitizer.sanitize_attachment_metadata(att_hash["metadata"])
270
+ linked_ref_key = extract_linked_element_ref_key(att_hash, att_meta)
271
+ linked_el = linked_ref_key ? element_refs_by_client_ref[linked_ref_key] : nil
272
+ file_source = resolve_attachment_file_source(att_hash)
273
+
274
+ if file_source[:dimensions] && att_meta["image_dimensions"].nil?
275
+ att_meta["image_dimensions"] = {
276
+ "width" => file_source[:dimensions][0],
277
+ "height" => file_source[:dimensions][1]
278
+ }
279
+ end
280
+
281
+ att_record = feedback.attachments.build(
282
+ attachment_type: att_type,
283
+ element_reference: (linked_el && !linked_el.new_record? ? linked_el : nil),
284
+ metadata: att_meta
285
+ )
286
+ pending_file_attachments << [att_record, linked_el, file_source]
287
+ end
288
+ end
289
+
290
+ feedback.attachments.each do |att|
291
+ next if att.new_record?
292
+ next if retained_ids[att.id.to_s]
293
+
294
+ att.mark_for_destruction
295
+ end
296
+ end
297
+
298
+ unless feedback.valid?
299
+ render json: { error: feedback.errors.full_messages.join(", "), errors: feedback.errors.full_messages },
300
+ status: :unprocessable_entity
301
+ raise ActiveRecord::Rollback
302
+ end
303
+
304
+ feedback.element_references.select(&:marked_for_destruction?).each(&:destroy!)
305
+ feedback.attachments.select(&:marked_for_destruction?).each(&:destroy!)
306
+ feedback.updated_at = Time.current
307
+ feedback.save!
308
+
309
+ retained_attachment_updates.each do |existing_att, linked_el, att_meta, has_custom_meta|
310
+ attrs_to_update = { element_reference_id: linked_el&.id }
311
+ attrs_to_update[:metadata] = att_meta if has_custom_meta
312
+ existing_att.update_columns(attrs_to_update)
313
+ end
314
+
315
+ pending_file_attachments.each do |att_record, linked_el, file_source|
316
+ if linked_el && att_record.element_reference_id.nil?
317
+ att_record.update_column(:element_reference_id, linked_el.id)
318
+ end
319
+ attach_binary_to_record!(att_record, file_source)
320
+ end
321
+ end
322
+
323
+ return if performed?
324
+
325
+ feedback.reload
326
+ invoke_lifecycle_callback(:on_feedback_updated, feedback)
327
+
328
+ raw_tokens = combined_raw_ownership_tokens(payload["ownership_token"])
329
+ ownership_token = ContextDrop::OwnershipToken.find_token_for(raw_tokens, feedback.id) ||
330
+ ContextDrop::OwnershipToken.generate(feedback.id)
108
331
 
109
332
  render json: {
110
333
  id: feedback.id,
111
334
  environment: feedback.environment_name,
112
335
  status: feedback.status,
336
+ message: feedback.message,
113
337
  attachments_count: feedback.attachments.size,
114
338
  element_references_count: feedback.element_references.size,
115
- created_at: feedback.created_at.iso8601
339
+ ownership_token: ownership_token,
340
+ feedback: serialize_own_feedback(feedback, ownership_token: ownership_token),
341
+ created_at: feedback.created_at.iso8601,
342
+ updated_at: feedback.updated_at.iso8601
343
+ }, status: :ok
344
+ end
345
+
346
+ def destroy
347
+ feedback = ContextDrop::Feedback.find_by(id: params[:id])
348
+ if feedback.nil? || feedback.deleted?
349
+ render json: { error: "Feedback not found" }, status: :not_found
350
+ return
351
+ end
352
+
353
+ payload = extract_feedback_payload
354
+ unless owns_feedback?(feedback, payload_token: payload["ownership_token"])
355
+ render json: { error: "Forbidden" }, status: :forbidden
356
+ return
357
+ end
358
+
359
+ feedback.soft_delete!
360
+ invoke_lifecycle_callback(:on_feedback_deleted, feedback)
361
+
362
+ render json: {
363
+ id: feedback.id,
364
+ deleted: true,
365
+ deleted_at: feedback.deleted_at_value&.iso8601
366
+ }, status: :ok
367
+ end
368
+
369
+ def create_comment
370
+ feedback = ContextDrop::Feedback.includes(:element_references, :attachments, comments: :attachments).find_by(id: params[:id])
371
+ if feedback.nil? || feedback.deleted?
372
+ render json: { error: "Feedback not found" }, status: :not_found
373
+ return
374
+ end
375
+
376
+ payload = extract_comment_payload
377
+ unless owns_feedback?(feedback, payload_token: payload["ownership_token"])
378
+ render json: { error: "Forbidden" }, status: :forbidden
379
+ return
380
+ end
381
+
382
+ raw_attachments = Array(payload["attachments"])
383
+ if raw_attachments.size > ContextDrop.config.max_attachments.to_i
384
+ render json: {
385
+ error: "Attachments count (#{raw_attachments.size}) exceeds maximum allowed (#{ContextDrop.config.max_attachments})"
386
+ }, status: :unprocessable_entity
387
+ return
388
+ end
389
+
390
+ submitter = current_context_drop_submitter
391
+ author_label = if submitter
392
+ if submitter.respond_to?(:name) && submitter.name.present?
393
+ submitter.name.to_s
394
+ elsif submitter.respond_to?(:email) && submitter.email.present?
395
+ submitter.email.to_s
396
+ else
397
+ "#{submitter.class.name}##{submitter.id}"
398
+ end
399
+ else
400
+ "You"
401
+ end
402
+
403
+ comment = nil
404
+ ContextDrop::Feedback.transaction do
405
+ comment = feedback.comments.build(
406
+ author_type: submitter&.class&.name,
407
+ author_id: submitter&.respond_to?(:id) ? submitter.id&.to_s : nil,
408
+ author_label: author_label,
409
+ body: payload["body"].to_s.strip.presence,
410
+ metadata: {}
411
+ )
412
+
413
+ pending_file_attachments = []
414
+ raw_attachments.each do |raw_att|
415
+ att_hash = stringify_hash(raw_att)
416
+ att_type = (att_hash["attachment_type"] || att_hash["type"] || "image").to_s.strip.downcase
417
+ att_type = "image" unless ContextDrop::Attachment::ATTACHMENT_TYPES.include?(att_type)
418
+ att_meta = ContextDrop::MetadataSanitizer.sanitize_attachment_metadata(att_hash["metadata"])
419
+ file_source = resolve_attachment_file_source(att_hash)
420
+
421
+ if file_source[:dimensions] && att_meta["image_dimensions"].nil?
422
+ att_meta["image_dimensions"] = {
423
+ "width" => file_source[:dimensions][0],
424
+ "height" => file_source[:dimensions][1]
425
+ }
426
+ end
427
+
428
+ att_record = comment.attachments.build(
429
+ feedback: feedback,
430
+ attachment_type: att_type,
431
+ metadata: att_meta
432
+ )
433
+ pending_file_attachments << [att_record, file_source]
434
+ end
435
+
436
+ unless comment.valid?
437
+ render json: { error: comment.errors.full_messages.join(", "), errors: comment.errors.full_messages },
438
+ status: :unprocessable_entity
439
+ raise ActiveRecord::Rollback
440
+ end
441
+
442
+ comment.save!
443
+
444
+ pending_file_attachments.each do |att_record, file_source|
445
+ attach_binary_to_record!(att_record, file_source)
446
+ end
447
+
448
+ feedback.update!(status: "updated")
449
+ end
450
+
451
+ return if performed?
452
+
453
+ feedback.reload
454
+ invoke_lifecycle_callback(:on_feedback_updated, feedback)
455
+
456
+ raw_tokens = combined_raw_ownership_tokens(payload["ownership_token"])
457
+ ownership_token = ContextDrop::OwnershipToken.find_token_for(raw_tokens, feedback.id) ||
458
+ ContextDrop::OwnershipToken.generate(feedback.id)
459
+
460
+ render json: {
461
+ id: comment.id,
462
+ feedback_id: feedback.id,
463
+ status: feedback.status,
464
+ comment: serialize_comment(comment),
465
+ feedback: serialize_own_feedback(feedback, ownership_token: ownership_token),
466
+ created_at: comment.created_at&.iso8601
116
467
  }, status: :created
117
468
  end
118
469
 
470
+ def attachment
471
+ feedback = ContextDrop::Feedback.find_by(id: params[:feedback_id])
472
+ if feedback.nil? || feedback.deleted?
473
+ head :not_found
474
+ return
475
+ end
476
+
477
+ unless owns_feedback?(feedback)
478
+ head :forbidden
479
+ return
480
+ end
481
+
482
+ att = ContextDrop::Attachment.where(feedback_id: feedback.id).find_by(id: params[:id])
483
+ if att.nil? || !att.respond_to?(:file) || !att.file.attached?
484
+ head :not_found
485
+ return
486
+ end
487
+
488
+ response.set_header("Cache-Control", "private, max-age=3600")
489
+ response.set_header("Content-Security-Policy", "default-src 'none'; sandbox")
490
+ send_data att.file.download,
491
+ type: att.file.content_type || "image/png",
492
+ disposition: "inline",
493
+ filename: att.file.filename.to_s
494
+ end
495
+
119
496
  private
120
497
 
498
+ def own_active_feedbacks_scope
499
+ base = ContextDrop::Feedback.active
500
+ clauses = []
501
+
502
+ submitter = current_context_drop_submitter
503
+ if submitter && submitter.respond_to?(:id) && !submitter.id.nil?
504
+ clauses << base.where(submitter_type: submitter.class.name, submitter_id: submitter.id)
505
+ end
506
+
507
+ anon_base = base.where(submitter_id: nil)
508
+ sid = hashed_session_identifier(initialize_if_missing: false)
509
+ clauses << anon_base.where(session_id: sid) if sid.present?
510
+
511
+ token_ids = ContextDrop::OwnershipToken
512
+ .extract_verified_ids(combined_raw_ownership_tokens)
513
+ .map(&:to_i)
514
+ .select(&:positive?)
515
+ clauses << anon_base.where(id: token_ids) if token_ids.any?
516
+
517
+ return ContextDrop::Feedback.none if clauses.empty?
518
+
519
+ clauses.reduce { |acc, rel| acc.or(rel) }
520
+ end
521
+
522
+ def owns_feedback?(feedback, payload_token: nil)
523
+ submitter = current_context_drop_submitter
524
+
525
+ if feedback.submitter_id.present? || feedback.submitter_type.present?
526
+ return false unless submitter && submitter.respond_to?(:id) && !submitter.id.nil?
527
+
528
+ return feedback.submitter_type.to_s == submitter.class.name.to_s &&
529
+ feedback.submitter_id.to_s == submitter.id.to_s
530
+ end
531
+
532
+ raw_tokens = combined_raw_ownership_tokens(payload_token)
533
+ return true if ContextDrop::OwnershipToken.find_token_for(raw_tokens, feedback.id)
534
+
535
+ sid = hashed_session_identifier(initialize_if_missing: false)
536
+ return false if sid.blank? || feedback.session_id.blank?
537
+
538
+ ActiveSupport::SecurityUtils.secure_compare(feedback.session_id.to_s, sid.to_s)
539
+ end
540
+
541
+ def combined_raw_ownership_tokens(extra_token = nil)
542
+ [
543
+ extra_token,
544
+ request.headers["X-Context-Drop-Ownership-Token"],
545
+ request.headers["X-Context-Drop-Ownership-Tokens"],
546
+ params[:ownership_token],
547
+ params[:ownership_tokens]
548
+ ].compact.map(&:to_s).reject(&:empty?).join(",")
549
+ end
550
+
551
+ def serialize_own_feedback(feedback, ownership_token: nil)
552
+ elements_by_id = feedback.element_references.index_by(&:id)
553
+
554
+ {
555
+ id: feedback.id,
556
+ environment: feedback.environment_name,
557
+ status: feedback.status,
558
+ message: feedback.message,
559
+ page_url: feedback.page_url,
560
+ ownership_token: ownership_token,
561
+ created_at: feedback.created_at&.iso8601,
562
+ updated_at: feedback.updated_at&.iso8601,
563
+ element_references: feedback.element_references.map do |el|
564
+ {
565
+ id: el.id,
566
+ client_ref: el.client_ref,
567
+ tag_name: el.tag_name,
568
+ selector: el.selector,
569
+ dom_path: el.dom_path,
570
+ text_content: el.text_content,
571
+ attributes_snapshot: el.attributes_snapshot || {},
572
+ bounding_rect: el.bounding_rect || {},
573
+ viewport: el.viewport || {},
574
+ metadata: el.metadata || {},
575
+ captured_at: el.captured_at&.iso8601
576
+ }
577
+ end,
578
+ attachments: feedback.attachments.map { |att| serialize_attachment(att, elements_by_id) },
579
+ comments_count: feedback.comments.size,
580
+ comments: feedback.comments.map { |comment| serialize_comment(comment) }
581
+ }
582
+ end
583
+
584
+ def serialize_attachment(att, elements_by_id = {})
585
+ attached = att.respond_to?(:file) && att.file.attached?
586
+ linked_el = att.element_reference_id ? elements_by_id[att.element_reference_id] : att.element_reference
587
+ {
588
+ id: att.id,
589
+ attachment_type: att.attachment_type,
590
+ element_reference_id: att.element_reference_id,
591
+ element_reference_ref: linked_el&.client_ref,
592
+ filename: attached ? att.file.filename.to_s : nil,
593
+ content_type: attached ? att.file.content_type : nil,
594
+ byte_size: attached ? att.file.byte_size : nil,
595
+ metadata: att.metadata || {},
596
+ created_at: att.created_at&.iso8601
597
+ }
598
+ end
599
+
600
+ def serialize_comment(comment)
601
+ {
602
+ id: comment.id,
603
+ feedback_id: comment.feedback_id,
604
+ author_label: comment.display_author_label,
605
+ author_type: comment.author_type,
606
+ body: comment.body,
607
+ attachments: comment.attachments.map { |att| serialize_attachment(att) },
608
+ created_at: comment.created_at&.iso8601,
609
+ updated_at: comment.updated_at&.iso8601
610
+ }
611
+ end
612
+
613
+ def extract_comment_payload
614
+ raw = params[:comment].is_a?(ActionController::Parameters) || params[:comment].is_a?(Hash) ? params[:comment] : params
615
+ hash = stringify_hash(raw)
616
+
617
+ if hash["attachments"].is_a?(String)
618
+ begin
619
+ hash["attachments"] = JSON.parse(hash["attachments"])
620
+ rescue JSON::ParserError
621
+ # Leave as-is if not valid JSON string
622
+ end
623
+ end
624
+
625
+ hash
626
+ end
627
+
628
+ def extract_linked_element_ref_key(att_hash, att_meta)
629
+ (
630
+ att_hash["element_reference_ref"] ||
631
+ att_hash["element_attachment_ref"] ||
632
+ att_hash["client_ref"] ||
633
+ att_meta["element_attachment_ref"] ||
634
+ att_meta["client_ref"]
635
+ ).to_s.strip.presence
636
+ end
637
+
121
638
  def extract_feedback_payload
122
639
  raw = params[:feedback].is_a?(ActionController::Parameters) || params[:feedback].is_a?(Hash) ? params[:feedback] : params
123
640
  hash = stringify_hash(raw)
@@ -198,11 +715,13 @@ module ContextDrop
198
715
  raise ContextDrop::UploadValidator::ValidationError, "Invalid base64 attachment data"
199
716
  end
200
717
 
201
- def invoke_on_feedback_created(feedback)
202
- callback = ContextDrop.config.on_feedback_created
718
+ def invoke_lifecycle_callback(hook_name, feedback)
719
+ return unless ContextDrop.config.respond_to?(hook_name)
720
+
721
+ callback = ContextDrop.config.public_send(hook_name)
203
722
  callback.call(feedback) if callback.respond_to?(:call)
204
723
  rescue StandardError => e
205
- Rails.logger.error("[ContextDrop] on_feedback_created callback error: #{e.class}: #{e.message}") if defined?(Rails.logger) && Rails.logger
724
+ Rails.logger.error("[ContextDrop] #{hook_name} callback error: #{e.class}: #{e.message}") if defined?(Rails.logger) && Rails.logger
206
725
  end
207
726
 
208
727
  def stringify_hash(obj)
@@ -47,6 +47,10 @@ module ContextDrop
47
47
  def cd_icon_user(size: 13)
48
48
  %(<svg width="#{size}" height="#{size}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"></path><circle cx="12" cy="7" r="4"></circle></svg>).html_safe
49
49
  end
50
+
51
+ def cd_icon_comment(size: 13)
52
+ %(<svg width="#{size}" height="#{size}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"></path></svg>).html_safe
53
+ end
50
54
  end
51
55
  end
52
56