ideasbugs 1.1.0 → 1.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0f39a47c15692a574b7777e0c2fa49e6553de46115a5c971d0094710a5e64d3a
4
- data.tar.gz: aea8f55dc68b7a7a0cca8f683f7bbec1911a5fdc30ff8343ef82c87adc4a2dd6
3
+ metadata.gz: f1e288c1f22144db48200a796fddc805d6a7e420d5f9b1c7a88cd74be51b800c
4
+ data.tar.gz: beb0741c4e1c81dd46bd91f776525a3cabdac8028927e99ecb3f650129310127
5
5
  SHA512:
6
- metadata.gz: 96b2d648ca7b806b311af9cdf04b10066e6e4550a5ed0618c997534247367927275b49a644d4f6c7398ecd562db8061b96bbb7c76aeb6cf3e88306ed10631b4b
7
- data.tar.gz: 9478ed6b52f47b699d2c9431f5ba412b02b08490cb3833d8cb681f0ca5ed257591d49aa3f3a47e428f35900c5e89fbfbf848061402032c86c018536bd9cb923a
6
+ metadata.gz: 7e65c2eaa8171bdb2c67c5737503c353f2c90d755ce17334b0c18b2670e1cf77e04f90dd8c753f46417c0e910901219b2fde794ea4b5a4bcc49e0ff666905b31
7
+ data.tar.gz: d5c9b2d8960368ef45cb1ecdf0d3c24a91c951fef728ad6067bea3ffac0e66a1443f196040e434510723c9103ec4d0b189847a3b38f89cbb484f020a726b72a1
data/CHANGELOG.md CHANGED
@@ -2,6 +2,17 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [1.2.0] - 2026-11-09
6
+
7
+ - Add `Ideasbugs::Feedback.submit(attributes, author:, tenant:, user_agent:,
8
+ screenshots:)`, the widget's submission rules as one call: page URL privacy
9
+ filtering, author attribution, screenshot limits and the `on_submit` hook.
10
+ A host exposing feedback through its own API calls it instead of
11
+ re-implementing them. Only `kind`, `section`, `message` and `page_url` are
12
+ taken from attributes. The widget endpoint now uses the same method; its
13
+ responses and error messages are unchanged. No migration or initializer
14
+ change.
15
+
5
16
  ## [1.1.0] - 2026-10-18
6
17
 
7
18
  - Add Rails edge CI and experimental Ruby 4.0+ Ractor request support. The host
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'uri'
4
-
5
3
  module Ideasbugs
6
4
  # Who is asking, which tenant they are in, and the gates that answer both.
7
5
  #
@@ -49,23 +47,5 @@ module Ideasbugs
49
47
  def tenant_scope
50
48
  Feedback.for_tenant(current_tenant)
51
49
  end
52
-
53
- # Browser URLs can carry password-reset tokens, signed ids, and campaign
54
- # details in their query or fragment. Store only a bounded HTTP(S) location
55
- # without credentials before it reaches the feedback table.
56
- def clean_page_url(value)
57
- raw = value.to_s
58
- return if raw.blank? || raw.length > 2_048
59
-
60
- uri = URI.parse(raw)
61
- return unless %w[http https].include?(uri.scheme&.downcase)
62
- return if uri.host.blank? || uri.userinfo.present?
63
-
64
- uri.query = nil
65
- uri.fragment = nil
66
- uri.to_s.first(255)
67
- rescue URI::InvalidURIError
68
- nil
69
- end
70
50
  end
71
51
  end
@@ -22,17 +22,15 @@ module Ideasbugs
22
22
  end
23
23
 
24
24
  def create
25
- feedback = Feedback.new(feedback_params)
26
- feedback.page_url = clean_page_url(feedback.page_url)
27
- feedback.user_agent = request.user_agent
28
- feedback.tenant = current_tenant
29
- attribute_author(feedback)
30
-
31
- error = attach_screenshots(feedback)
32
- return render json: { errors: [error] }, status: :unprocessable_entity if error
33
-
34
- if feedback.save
35
- notify_host(feedback)
25
+ feedback = Feedback.submit(
26
+ params.require(:feedback).permit(*Feedback::SUBMITTED_ATTRIBUTES),
27
+ author: current_author,
28
+ tenant: current_tenant,
29
+ user_agent: request.user_agent,
30
+ screenshots: params.dig(:feedback, :screenshots)
31
+ )
32
+
33
+ if feedback.persisted?
36
34
  head :created
37
35
  else
38
36
  render json: { errors: feedback.errors.full_messages }, status: :unprocessable_entity
@@ -41,61 +39,8 @@ module Ideasbugs
41
39
 
42
40
  private
43
41
 
44
- # The host's hook must never turn a saved submission into a 500 — the
45
- # feedback is in the database; notification failures are the host's logs'
46
- # problem.
47
- def notify_host(feedback)
48
- Ideasbugs.config.on_submit.call(feedback)
49
- rescue StandardError => e
50
- Rails.logger.error("ideasbugs: on_submit hook raised #{e.class}: #{e.message}")
51
- end
52
-
53
- def feedback_params
54
- params.require(:feedback).permit(:kind, :section, :message, :page_url)
55
- end
56
-
57
- def attribute_author(feedback)
58
- author = current_author
59
- return unless author
60
-
61
- feedback.author_id = author.id.to_s if author.respond_to?(:id)
62
- feedback.author_label = Ideasbugs.config.author_label.call(author)
63
- end
64
-
65
- # Validates and attaches uploads. Returns an error message, or nil when
66
- # everything (including "no screenshots at all") is fine.
67
- def attach_screenshots(feedback)
68
- files = Array(params.dig(:feedback, :screenshots)).reject(&:blank?)
69
- return nil if files.empty?
70
- return t_error(:error_save) unless Ideasbugs.config.screenshots_enabled?
71
- return t_error(:error_too_many, count: Ideasbugs.config.max_screenshots) if too_many?(files)
72
- return t_error(:error_too_large, size: max_size_mb) if files.any? { |f| f.size > max_size }
73
- return t_error(:error_save) unless files.all? { |f| f.content_type.to_s.start_with?('image/') }
74
-
75
- feedback.screenshots.attach(files)
76
- nil
77
- end
78
-
79
- def too_many?(files)
80
- files.size > Ideasbugs.config.max_screenshots
81
- end
82
-
83
- def max_size_mb
84
- max_size / (1024 * 1024)
85
- end
86
-
87
- def max_size
88
- Ideasbugs.config.max_screenshot_size
89
- end
90
-
91
- def t_error(key, **args)
92
- defaults = {
93
- error_save: 'Could not send feedback. Please try again.',
94
- error_too_many: 'Too many screenshots (max %{count}).',
95
- error_too_large: 'A screenshot is too large (max %{size} MB).',
96
- error_rate_limited: 'Too many submissions. Please wait a moment and try again.'
97
- }
98
- I18n.t(key, scope: :ideasbugs, default: defaults[key], **args)
42
+ def t_error(key, **)
43
+ Feedback.submission_error(key, **)
99
44
  end
100
45
  end
101
46
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'uri'
4
+
3
5
  module Ideasbugs
4
6
  # One piece of user feedback. Author attribution is optional and stored as
5
7
  # loose fields (no foreign key to the host's user table) so the model is
@@ -32,6 +34,55 @@ module Ideasbugs
32
34
  scope status, -> { where(status:) }
33
35
  end
34
36
 
37
+ SUBMITTED_ATTRIBUTES = %i[kind section message page_url].freeze
38
+
39
+ # Files feedback the way the widget does: privacy-filtered page URL, author
40
+ # attribution, screenshot limits, then the host's on_submit hook. A host
41
+ # with its own API calls this rather than re-implementing those rules.
42
+ # Returns the feedback; check persisted? and errors.
43
+ def self.submit(attributes, author: nil, tenant: nil, user_agent: nil, screenshots: nil)
44
+ feedback = new(attributes.to_h.symbolize_keys.slice(*SUBMITTED_ATTRIBUTES))
45
+ feedback.page_url = clean_page_url(feedback.page_url)
46
+ feedback.user_agent = user_agent
47
+ feedback.tenant = tenant.presence&.to_s
48
+ feedback.attribute_to(author)
49
+
50
+ if (error = feedback.attach_screenshots(Array(screenshots).compact_blank))
51
+ feedback.errors.add(:base, error)
52
+ elsif feedback.save
53
+ feedback.notify_host
54
+ end
55
+ feedback
56
+ end
57
+
58
+ # Browser URLs can carry password-reset tokens, signed ids, and campaign
59
+ # details in their query or fragment. Store only a bounded HTTP(S) location
60
+ # without credentials before it reaches the feedback table.
61
+ def self.clean_page_url(value)
62
+ raw = value.to_s
63
+ return if raw.blank? || raw.length > 2_048
64
+
65
+ uri = URI.parse(raw)
66
+ return unless %w[http https].include?(uri.scheme&.downcase)
67
+ return if uri.host.blank? || uri.userinfo.present?
68
+
69
+ uri.query = nil
70
+ uri.fragment = nil
71
+ uri.to_s.first(255)
72
+ rescue URI::InvalidURIError
73
+ nil
74
+ end
75
+
76
+ def self.submission_error(key, **args)
77
+ defaults = {
78
+ error_save: 'Could not send feedback. Please try again.',
79
+ error_too_many: 'Too many screenshots (max %{count}).',
80
+ error_too_large: 'A screenshot is too large (max %{size} MB).',
81
+ error_rate_limited: 'Too many submissions. Please wait a moment and try again.'
82
+ }
83
+ I18n.t(key, scope: :ideasbugs, default: defaults[key], **args)
84
+ end
85
+
35
86
  def open? = status == 'open'
36
87
  def in_review? = status == 'in_review'
37
88
  def resolved? = status == 'resolved'
@@ -39,5 +90,46 @@ module Ideasbugs
39
90
  def screenshots?
40
91
  respond_to?(:screenshots) && screenshots.attached?
41
92
  end
93
+
94
+ def attribute_to(author)
95
+ return unless author
96
+
97
+ self.author_id = author.id.to_s if author.respond_to?(:id)
98
+ self.author_label = Ideasbugs.config.author_label.call(author)
99
+ end
100
+
101
+ # Attaches uploads, or returns the error that rejects them. No screenshots
102
+ # at all is fine.
103
+ def attach_screenshots(files)
104
+ return if files.empty?
105
+
106
+ key, args = screenshot_error(files)
107
+ return self.class.submission_error(key, **args.to_h) if key
108
+
109
+ screenshots.attach(files)
110
+ nil
111
+ end
112
+
113
+ # The host's hook must never turn a saved submission into a failure: the
114
+ # feedback is in the database; notification failures are the host's logs'
115
+ # problem.
116
+ def notify_host
117
+ Ideasbugs.config.on_submit.call(self)
118
+ rescue StandardError => e
119
+ Rails.logger.error("ideasbugs: on_submit hook raised #{e.class}: #{e.message}")
120
+ end
121
+
122
+ private
123
+
124
+ def screenshot_error(files)
125
+ config = Ideasbugs.config
126
+ return [:error_save] unless config.screenshots_enabled?
127
+ return [:error_too_many, { count: config.max_screenshots }] if files.size > config.max_screenshots
128
+
129
+ max_size = config.max_screenshot_size
130
+ return [:error_too_large, { size: max_size / (1024 * 1024) }] if files.any? { |f| f.size > max_size }
131
+
132
+ [:error_save] unless files.all? { |f| f.content_type.to_s.start_with?('image/') }
133
+ end
42
134
  end
43
135
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ideasbugs
4
- VERSION = '1.1.0'
4
+ VERSION = '1.2.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ideasbugs
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yaroslav Shmarov