Import group-text fix from prod

Stephen Paul Weber created

Change summary

sgx_jmp.rb | 27 +++++++++++++++++++++++++--
1 file changed, 25 insertions(+), 2 deletions(-)

Detailed changes

sgx_jmp.rb 🔗

@@ -168,9 +168,32 @@ before nil, to: /\Acustomer_/, from: /(\A|@)#{CONFIG[:sgx]}(\/|\Z)/ do |s|
 	halt
 end
 
-# Ignore messages to component
+ADDRESSES_NS = "http://jabber.org/protocol/address"
+message(
+	to: /\A#{CONFIG[:component][:jid]}\Z/,
+	from: /(\A|@)#{CONFIG[:sgx]}(\/|\Z)/
+) do |m|
+	StatsD.increment("inbound_group_text")
+	sentry_hub = new_sentry_hub(m, name: "message")
+
+	address = m.find("ns:addresses", ns: ADDRESSES_NS).first
+		&.find("ns:address", ns: ADDRESSES_NS)
+		&.find { |el| el["jid"].to_s.start_with?("customer_") }
+	pass unless address
+
+	Customer.for_customer_id(
+		Blather::JID.new(address["jid"].to_s).node.delete_prefix("customer_")
+	).then(&:jid).then { |customer_jid|
+		m.from = m.from.with(domain: CONFIG[:component][:jid])
+		m.to = m.to.with(domain: customer_jid.domain)
+		address["jid"] = customer_jid.to_s
+		BLATHER << m
+	}.catch { |e| panic(e, sentry_hub) }
+end
+
+# Ignore groupchat messages
 # Especially if we have the component join MUC for notifications
-message(to: /\A#{CONFIG[:component][:jid]}\Z/) { true }
+message(type: :groupchat) { true }
 
 def billable_message(m)
 	(m.body && !m.body.empty?) || m.find("ns:x", ns: OOB.registered_ns).first