Skip to content

Commit c40ac8a

Browse files
authored
Small cleanups: bin/update, litestream test, delivery partials, support convert pair (#270)
Four small cleanups from the weekly health check, one commit each. - **bin/update**: drop the separate `bundle update brakeman --cooldown 0` step and its comment. The `--ensure-latest` flag it referred to was removed from `bin/brakeman` in 3cafedf, so brakeman now follows the Gemfile cooldown through `bundle update --all`. - **Litestream test**: the old "server task" test asserted Kamal's own behaviour. It now invokes `litestream:config:server` with the SSH session stubbed and asserts the host the task connects to. - **Delivery partials**: qualify the two remaining short partial renders in `members/deliveries` as `members/deliveries/...`. Views only, the controller is untouched. - **Support ticket**: move the convert-pair lookup from `SupportHelper` onto `Support::Ticket#convert_pair`, behaviour unchanged, with model tests for the pair, the 30 second gap, the RFC message id and the non-support reply cases. `bin/ci` is green (5280 runs, 0 failures; RuboCop and Archspec clean).
2 parents 9798ddf + 24cb0b0 commit c40ac8a

7 files changed

Lines changed: 109 additions & 29 deletions

File tree

‎app/helpers/support_helper.rb‎

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# frozen_string_literal: true
22

33
module SupportHelper
4-
CONVERT_PAIR_GAP = 30.seconds
5-
64
def ticket_admins_collection(relation = nil)
75
collection = Admin.order_by_name
86
if relation
@@ -39,7 +37,7 @@ def support_message_html(message)
3937

4038
def support_message_attachments(message)
4139
images, files = split_support_attachments(message.attachments)
42-
opening, reply = convert_pair(message)
40+
opening, reply = message.ticket&.convert_pair
4341
if opening && reply && message.id == opening.id
4442
files += split_support_attachments(reply.attachments).last
4543
elsif opening && reply && message.id == reply.id
@@ -77,19 +75,4 @@ def rendered_action_text(message)
7775
def split_support_attachments(attachments)
7876
attachments.partition { |attachment| support_previewable_image?(attachment.file) }
7977
end
80-
81-
# Pre-fix converts stored the original email's files on the support reply
82-
# that was inserted with the opening admin message.
83-
def convert_pair(message)
84-
ticket = message.ticket
85-
return unless ticket
86-
87-
opening, reply = ticket.messages.to_a.select(&:persisted?).sort_by(&:id).first(2)
88-
return unless opening&.author_admin? && reply&.author_support?
89-
return unless opening.rfc_message_id.blank?
90-
return unless opening.created_at && reply.created_at
91-
return unless (reply.created_at - opening.created_at).abs <= CONVERT_PAIR_GAP
92-
93-
[ opening, reply ]
94-
end
9578
end

‎app/models/support/ticket.rb‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class Ticket < ApplicationRecord
66

77
PRIORITY_ICONS = { medium: "❗️", high: "‼️" }
88
INBOUND_DOMAIN = "support.csa-admin.org"
9+
CONVERT_PAIR_GAP = 30.seconds
910

1011
include HasAttachments
1112
has_rich_text :html
@@ -138,6 +139,18 @@ def wrap_emails_for(message)
138139
Array(message.hop_email&.downcase)
139140
end
140141

142+
# Pre-fix converts stored the original email's files on the support reply
143+
# that was inserted with the opening admin message.
144+
def convert_pair
145+
opening, reply = messages.to_a.select(&:persisted?).sort_by(&:id).first(2)
146+
return unless opening&.author_admin? && reply&.author_support?
147+
return unless opening.rfc_message_id.blank?
148+
return unless opening.created_at && reply.created_at
149+
return unless (reply.created_at - opening.created_at).abs <= CONVERT_PAIR_GAP
150+
151+
[ opening, reply ]
152+
end
153+
141154
def ping_address
142155
Rails.app.creds.option(:support_email).presence
143156
end

‎app/views/members/deliveries/_basket_contents.html.erb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@
3030
<% end %>
3131
</ul>
3232

33-
<%= render "basket_contents_note" if note %>
33+
<%= render "members/deliveries/basket_contents_note" if note %>

‎app/views/members/deliveries/index.html.erb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
</div>
5252
<% end %>
5353

54-
<%= render "basket_contents", basket_contents: @basket_contents, basket_size: @next_basket.basket_size if @basket_contents&.any? %>
54+
<%= render "members/deliveries/basket_contents", basket_contents: @basket_contents, basket_size: @next_basket.basket_size if @basket_contents&.any? %>
5555
</section>
5656
<% end %>
5757

‎bin/update‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,6 @@ FileUtils.chdir APP_ROOT do
224224
puts "\n== Updating gems (cooldown: #{cooldown} days) =="
225225
system! "bundle", "update", "--all", "--cooldown", cooldown
226226

227-
puts "\n== Updating brakeman (cooldown: 0) =="
228-
# bin/brakeman passes --ensure-latest, which ignores the Gemfile cooldown.
229-
# Last, so the cooldown on the update above cannot retract this version.
230-
system! "bundle", "update", "brakeman", "--cooldown", "0"
231-
232227
puts "\n== Updating JS packages =="
233228
system! "bin/aube", "update"
234229

‎test/lib/tasks/litestream_rake_test.rb‎

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class LitestreamRakeTest < ActiveSupport::TestCase
1111
Rails.application.load_tasks unless Rake::Task.task_defined?("litestream:restore")
1212
Rake::Task["litestream:restore"].reenable
1313
Rake::Task["litestream:config:local"].reenable if Rake::Task.task_defined?("litestream:config:local")
14+
Rake::Task["litestream:config:server"].reenable if Rake::Task.task_defined?("litestream:config:server")
1415
end
1516

1617
test "restore reports all tenants without listing them" do
@@ -62,11 +63,16 @@ class LitestreamRakeTest < ActiveSupport::TestCase
6263
end
6364
end
6465

65-
test "server task uses Kamal primary_host, not the raw servers.web hash key" do
66-
kamal = Kamal::Configuration.create_from(config_file: Rails.root.join("config/deploy.yml"))
66+
test "server task connects to the deploy host" do
67+
hostnames = capture_server_hosts do
68+
with_rails_env("development") do
69+
with_litestream_credentials do
70+
Rake::Task["litestream:config:server"].invoke
71+
end
72+
end
73+
end
6774

68-
assert_equal [ "hosts", [ "isle.thibaud.gg" ] ], kamal.raw_config.dig("servers", "web").first
69-
assert_equal "isle.thibaud.gg", kamal.primary_host
75+
assert_equal [ "isle.thibaud.gg" ], hostnames
7076
end
7177

7278
test "server config builder is litestream 0.5 shaped" do
@@ -196,6 +202,29 @@ class LitestreamRakeTest < ActiveSupport::TestCase
196202

197203
private
198204

205+
def capture_server_hosts
206+
hostnames = []
207+
each = SSHKit::Coordinator.instance_method(:each)
208+
SSHKit::Coordinator.define_method(:each) do |*|
209+
hostnames.replace(hosts.map(&:hostname))
210+
end
211+
yield
212+
hostnames
213+
ensure
214+
SSHKit::Coordinator.define_method(:each, each) if each
215+
end
216+
217+
def with_litestream_credentials
218+
credentials = Rails.application.credentials
219+
credentials.define_singleton_method(:litestream) do
220+
Data.define(:access_key_id, :secret_access_key, :endpoint).new(
221+
"key", "secret", "s3.example.com")
222+
end
223+
yield
224+
ensure
225+
credentials.singleton_class.remove_method(:litestream)
226+
end
227+
199228
def run_restore(tenant: nil, s3_layout: false)
200229
Dir.mktmpdir do |dir|
201230
%w[acme demo].each do |name|

‎test/models/support/ticket_test.rb‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,4 +296,64 @@ class Support::TicketTest < ActiveSupport::TestCase
296296

297297
assert_equal [ admins(:external).email ], ticket.wrap_emails_for(jane)
298298
end
299+
300+
test "convert_pair is the opening and the support reply inserted together" do
301+
ticket, opening, reply = convert_pair_messages
302+
now = Time.current.change(usec: 0)
303+
opening.update_columns(created_at: now, rfc_message_id: nil)
304+
reply.update_columns(created_at: now + 30.seconds)
305+
306+
assert_equal [ opening, reply ], ticket.convert_pair
307+
end
308+
309+
test "convert_pair ignores a support reply outside the gap" do
310+
ticket, opening, reply = convert_pair_messages
311+
now = Time.current.change(usec: 0)
312+
opening.update_columns(created_at: now, rfc_message_id: nil)
313+
reply.update_columns(created_at: now + 31.seconds)
314+
315+
assert_nil ticket.convert_pair
316+
end
317+
318+
test "convert_pair ignores an opening that already has an rfc message id" do
319+
ticket, opening, reply = convert_pair_messages
320+
now = Time.current.change(usec: 0)
321+
opening.update_columns(created_at: now, rfc_message_id: "<already@example.test>")
322+
reply.update_columns(created_at: now + 1.second)
323+
324+
assert_nil ticket.convert_pair
325+
end
326+
327+
test "convert_pair ignores a second message that is not from support" do
328+
ticket = Support::Ticket.new(
329+
priority: :normal, subject: "Wait", content: "Hello",
330+
admin: admins(:external), skip_opening_message: true)
331+
ticket.save!
332+
now = Time.current.change(usec: 0)
333+
opening = quiet_ticket_message(ticket, author: "admin", admin: admins(:external), body: "Hello")
334+
follow_up = quiet_ticket_message(ticket, author: "admin", admin: admins(:external), body: "Again")
335+
opening.update_columns(created_at: now, rfc_message_id: nil)
336+
follow_up.update_columns(created_at: now + 1.second)
337+
338+
assert_nil ticket.convert_pair
339+
end
340+
341+
private
342+
343+
def convert_pair_messages
344+
ticket = Support::Ticket.new(
345+
priority: :normal, subject: "Wait", content: "Hello",
346+
admin: admins(:external), skip_opening_message: true)
347+
ticket.save!
348+
opening = quiet_ticket_message(ticket, author: "admin", admin: admins(:external), body: "Hello")
349+
reply = quiet_ticket_message(ticket, author: "support", body: "Answer")
350+
[ ticket, opening, reply ]
351+
end
352+
353+
def quiet_ticket_message(ticket, **attrs)
354+
message = ticket.messages.build(**attrs)
355+
message.skip_notify = true
356+
message.save!
357+
message
358+
end
299359
end

0 commit comments

Comments
 (0)