~singpolyma/sgx-jmp

245f3843e9a1cbb63c1d033b72c63d4d7d0d614a — Christopher Vollick 1 year, 3 months ago 1145cc7
Don't Count Stanzas Without Body as Reached

We get messages without bodies from time to time as things like delivery
receipts.

These are great, and we don't want to forward them to the end-user for
sure if we're dealing with a reachable number, but we also don't want to
count it as a message that reached us.

It's a receipt for a thing I did, not a thing the tester did.
2 files changed, 22 insertions(+), 6 deletions(-)

M lib/reachability_repo.rb
M sgx_jmp.rb
M lib/reachability_repo.rb => lib/reachability_repo.rb +17 -2
@@ 39,13 39,28 @@ class ReachabilityRepo
		end
	end

	# This one is for things that don't have a body.
	# I still don't want to deliver them to the user, but I don't want to count
	# them as a message either
	class Ignore
		def filter(if_yes: ->(_) {}, **)
			EMPromise.resolve(if_yes.call(nil))
		end
	end

	ACCEPTABLE_STANZA = Blather::Stanza::Message.new(nil, "I'm cool").freeze

	# The customer is who is being contacted
	# The initiator is the phone number trying to reach them
	def find(customer, initiator)
	def find(customer, initiator, stanza: ACCEPTABLE_STANZA)
		return EMPromise.resolve(NotTest.new) unless potential?(initiator)

		testing?(customer).then do |active|
			active ? Test.new(redis, key(customer)) : NotTest.new
			if active
				stanza.body ? Test.new(redis, key(customer)) : Ignore.new
			else
				NotTest.new
			end
		end
	end


M sgx_jmp.rb => sgx_jmp.rb +5 -4
@@ 282,11 282,12 @@ before nil, to: /\Acustomer_/, from: /(\A|@)#{CONFIG[:sgx]}(\/|\Z)/ do |s|
	CustomerRepo.new(set_user: Sentry.method(:set_user)).find(
		s.to.node.delete_prefix("customer_")
	).then do |customer|
		ReachabilityRepo::SMS.new.find(customer, s.from.node).then do |reach|
			reach.filter do
				customer.stanza_to(s)
		ReachabilityRepo::SMS.new
			.find(customer, s.from.node, stanza: s).then do |reach|
				reach.filter do
					customer.stanza_to(s)
				end
			end
		end
	end

	halt