fix send_media() so it supports both V1 & V2 media

Denver Gingerich created

Ah, the idealism of 91ff289 could simply not be left unsullied.  We
though the V2 SGX would only need to deal with V2 media, but alas it
must deal with V1 media too, since we want it to work with the V1
jmp-fwdcalls.  In particular this means that send_media() must handle
the V1 media that is sent by jmp-fwdcalls (voicemail recording URLs).

This is roughly the counterpart of the fix we made at
https://gitlab.com/ossguy/sgx-catapult/commit/7dfe149683d31738076b075e9815643194d81684
- check the prefix and set the user accordingly (instead of checking
the user to set the prefix accordingly).

While we should probably document that deployment requires this extra
user, the warning in the code should serve the small number of people
who actually care well enough.  We hopefully won't need to run this
V1 + V2 monstrosity for very long.

With this fix we now fully support voicemails received by V2 users who
happen to still be using the V1 API for voice (which happens to be
everyone right now).  We thought this was all done as of
https://gitlab.com/ossguy/jmp-fwdcalls/commit/a4ed555e2dd16fb342f33ea7ee5675553baa39ec
but we needed the commit here to make voicemail recordings work.  I
suppose the jmp-fwdcalls commit message is still correct, though - no
additional work was needed in jmp-fwdcalls to fix the voicemail
recordings: only the fix here was required.

Change summary

sgx-bwmsgsv2.rb | 26 +++++++++++++++++++++++---
1 file changed, 23 insertions(+), 3 deletions(-)

Detailed changes

sgx-bwmsgsv2.rb 🔗

@@ -106,12 +106,32 @@ module SGXbwmsgsv2
 	end
 
 	def self.send_media(from, to, media_url, desc=nil, subject=nil, m=nil)
-		# we assume media_url is of the form (always the case so far):
+		# we assume media_url is one of these (always the case so far):
+		#  https://api.catapult.inetwork.com/v1/users/[uid]/media/[file]
 		#  https://messaging.bandwidth.com/api/v2/users/[u]/media/[path]
 
+		usr = to
+		pth = ''
+		if media_url.start_with?(
+			'https://api.catapult.inetwork.com/v1/users/')
+
+			# TODO: MUST fix this TERRIBLE hack
+			# there must be a catapult_cred-<usr> key with V1 creds
+			usr = 'v1'
+
+			pth = media_url.split('/', 8)[7]
+
+		elsif media_url.start_with?(
+			'https://messaging.bandwidth.com/api/v2/users/')
+
+			pth = media_url.split('/', 9)[8]
+		else
+			puts "ERROR2: unrecognized media_url: '#{media_url}'"
+			return
+		end
+
 		# the caller must guarantee that 'to' is a bare JID
-		proxy_url = ARGV[6] + WEBrick::HTTPUtils.escape(to) + '/' +
-			media_url.split('/', 9)[8]
+		proxy_url = ARGV[6] + WEBrick::HTTPUtils.escape(usr) + '/' + pth
 
 		puts 'ORIG_URL: ' + media_url
 		puts 'PROX_URL: ' + proxy_url