Skip to content

Commit

Permalink
feat: scope the webhook controllers finder (#1997)
Browse files Browse the repository at this point in the history
  • Loading branch information
soey authored Aug 30, 2024
1 parent 04c369a commit 9327d90
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 15 deletions.
2 changes: 1 addition & 1 deletion app/adapters/threema_adapter/inbound.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def consume(threema_message)
return unless @sender

if delivery_receipt?(decrypted_message)
trigger(HANDLE_DELIVERY_RECEIPT, decrypted_message)
trigger(HANDLE_DELIVERY_RECEIPT, decrypted_message, @organization)
return
end

Expand Down
8 changes: 4 additions & 4 deletions app/controllers/threema/webhook_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def handle_callbacks
ErrorNotifier.report(exception)
end

adapter.on(ThreemaAdapter::HANDLE_DELIVERY_RECEIPT) do |delivery_receipt|
handle_delivery_receipt(delivery_receipt)
adapter.on(ThreemaAdapter::HANDLE_DELIVERY_RECEIPT) do |delivery_receipt, organization|
handle_delivery_receipt(delivery_receipt, organization)
end

adapter.on(ThreemaAdapter::UNSUBSCRIBE_CONTRIBUTOR) do |contributor, organization|
Expand All @@ -55,10 +55,10 @@ def handle_callbacks
end
end

def handle_delivery_receipt(delivery_receipt)
def handle_delivery_receipt(delivery_receipt, organization)
return if delivery_receipt.message_ids.blank?

messages = Message.where(external_id: delivery_receipt.message_ids)
messages = organization.messages.where(external_id: delivery_receipt.message_ids)
messages.each do |message|
delivery_receipt.message_ids.each do |message_id|
next unless message.external_id.eql?(message_id)
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/whats_app/webhook_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def send_requested_message(contributor, twilio_message_sid, organization)
message_text = fetch_message_from_twilio(twilio_message_sid, organization)

request_title = message_text.scan(/„[^"]*“/).first&.gsub('„', '')&.gsub('“', '')
request = Request.find_by(title: request_title)
request = organization.requests.find_by(title: request_title)

request&.messages&.where(recipient_id: contributor.id)&.first
end
Expand All @@ -149,7 +149,7 @@ def fetch_message_from_twilio(twilio_message_sid, organization)

def handle_freeform_message_not_allowed_error(contributor, twilio_message_sid)
message_text = fetch_message_from_twilio(twilio_message_sid, @organization)
message = Message.find_by(text: message_text)
message = @organization.messages.find_by(text: message_text)
return unless message

WhatsAppAdapter::TwilioOutbound.send_message_template!(contributor, message)
Expand All @@ -174,7 +174,7 @@ def handle_unsuccessful_delivery
def handle_successful_delivery
return unless @contributor

message = Message.where(external_id: status_params['MessageSid']).first
message = @organization.messages.where(external_id: status_params['MessageSid']).first
return unless message

delivered_status = SUCCESSFUL_DELIVERY.first
Expand Down
13 changes: 9 additions & 4 deletions spec/requests/threema/webhook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
Threema::Receive::DeliveryReceipt, content: 'x\00x\\0', message_ids: message_ids, status: status, timestamp: timestamp
)
end
let(:messages) { [create(:message, external_id: SecureRandom.alphanumeric(16))] }
let(:messages) { [create(:message, external_id: SecureRandom.alphanumeric(16), organization: organization)] }
let(:message_ids) { messages.pluck(:external_id) }
let(:status) { :received }
let(:timestamp) { Time.current.to_i }
Expand Down Expand Up @@ -99,15 +99,20 @@
end

context 'given multiple message_ids' do
let(:messages) { create_list(:message, 3, external_id: SecureRandom.alphanumeric(16)) }
let(:message_ids) { messages.pluck(:external_id) }
let(:messages) { create_list(:message, 3, external_id: SecureRandom.alphanumeric(16), organization: organization) }
let(:other_message) { create(:message, external_id: SecureRandom.alphanumeric(16)) }
let(:message_ids) { messages.pluck(:external_id) << other_message.id }
let(:status) { :read }

it 'updates all messages' do
it 'updates all messages belonging to the organization' do
expect { subject }.to change { messages.first.reload.read_at }.from(nil).to(kind_of(ActiveSupport::TimeWithZone)).and \
change { messages.second.reload.read_at }.from(nil).to(kind_of(ActiveSupport::TimeWithZone)).and \
change { messages.third.reload.read_at }.from(nil).to(kind_of(ActiveSupport::TimeWithZone))
end

it 'doesn\'t update the other message' do
expect { subject }.not_to(change { other_message.reload.read_at })
end
end
end

Expand Down
7 changes: 4 additions & 3 deletions spec/requests/whats_app/webhook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,8 @@

context 'due to a freeform message not allowed error' do
let!(:request) do
create(:request, title: 'I failed to send', text: 'Hey {{FIRST_NAME}}, because it was sent outside the allowed window')
create(:request, title: 'I failed to send', text: 'Hey {{FIRST_NAME}}, because it was sent outside the allowed window',
organization: organization)
end
let(:valid_account_sid) { 'VALID_ACCOUNT_SID' }
let(:valid_api_key_sid) { 'VALID_API_KEY_SID' }
Expand Down Expand Up @@ -426,7 +427,7 @@
end

context 'given a message with the twilio message sid as external_id' do
let!(:message) { create(:message, external_id: twilio_message_sid) }
let!(:message) { create(:message, external_id: twilio_message_sid, organization: organization) }

it 'is expected to mark the message as received' do
expect { subject.call }.to change { message.reload.received_at }.from(nil).to(kind_of(ActiveSupport::TimeWithZone))
Expand All @@ -438,7 +439,7 @@
before { params['MessageStatus'] = 'read' }

context 'given a message with the twilio message sid as external_id' do
let!(:message) { create(:message, external_id: twilio_message_sid, received_at: 1.hour.ago) }
let!(:message) { create(:message, external_id: twilio_message_sid, received_at: 1.hour.ago, organization: organization) }

it 'is expected to mark the message as read' do
expect { subject.call }.to change { message.reload.read_at }.from(nil).to(kind_of(ActiveSupport::TimeWithZone))
Expand Down

0 comments on commit 9327d90

Please sign in to comment.