Support any <message> with a body

Stephen Paul Weber created

Empty and whitespace-only bodies are banned by catapult API, so return a
policy-violation error in those cases.

Closes #12

Change summary

sgx-catapult.rb | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)

Detailed changes

sgx-catapult.rb 🔗

@@ -215,6 +215,13 @@ module SGXcatapult
 	end
 
 	def self.to_catapult(s, murl, num_dest, user_id, token, secret, usern)
+		body = s.respond_to?(:body) ? s.body : ''
+		if murl.to_s.empty? && body.to_s.strip.empty?
+			return EMPromise.reject(
+				[:modify, 'policy-violation']
+			)
+		end
+
 		extra = if murl
 			{
 				media: murl
@@ -234,7 +241,7 @@ module SGXcatapult
 			JSON.dump(extra.merge(
 				from: usern,
 				to:   num_dest,
-				text: s.respond_to?(:body) ? s.body : '',
+				text: body,
 				tag:
 					# callbacks need id and resourcepart
 					WEBrick::HTTPUtils.escape(s.id.to_s) +
@@ -279,7 +286,7 @@ module SGXcatapult
 		}
 	end
 
-	message :chat?, :body do |m|
+	message :body do |m|
 		EMPromise.all([
 			validate_num(m.to.node),
 			fetch_catapult_cred_for(m.from)