Selaa lähdekoodia

For `forbidden` errors, show error message from server

JC Brand 5 vuotta sitten
vanhempi
commit
161cbec0d8
2 muutettua tiedostoa jossa 25 lisäystä ja 5 poistoa
  1. 19 3
      spec/muc.js
  2. 6 2
      src/headless/utils/stanza.js

+ 19 - 3
spec/muc.js

@@ -5316,16 +5316,32 @@
                 view.onFormSubmitted(new Event('submit'));
                 await new Promise(resolve => view.once('messageInserted', resolve));
 
-                const stanza = u.toStanza(`
+                let stanza = u.toStanza(`
                     <message xmlns="jabber:client" type="error" to="troll@montague.lit/resource" from="trollbox@montague.lit">
                         <error type="auth"><forbidden xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error>
                     </message>`);
                 _converse.connection._dataRecv(test_utils.createRequest(stanza));
+                await new Promise(resolve => view.once('messageInserted', resolve));
+                expect(view.el.querySelector('.chat-error').textContent.trim()).toBe(
+                    "Your message was not delivered because you weren't allowed to send it.");
 
+                textarea.value = 'Hello again';
+                view.onFormSubmitted(new Event('submit'));
                 await new Promise(resolve => view.once('messageInserted', resolve));
 
-                expect(view.el.querySelector('.chat-error').textContent.trim()).toBe(
-                    "Your message was not delivered because you're not allowed to send messages in this groupchat.");
+                stanza = u.toStanza(`
+                    <message xmlns="jabber:client" type="error" to="troll@montague.lit/resource" from="trollbox@montague.lit">
+                        <error type="auth">
+                            <forbidden xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
+                            <text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas">Thou shalt not!</text>
+                        </error>
+                    </message>`);
+                _converse.connection._dataRecv(test_utils.createRequest(stanza));
+                await new Promise(resolve => view.once('messageInserted', resolve));
+
+                expect(view.el.querySelector('.message:last-child').textContent.trim()).toBe(
+                    'Your message was not delivered because you weren\'t allowed to send it. '+
+                    'The message from the server is: "Thou shalt not!"')
                 done();
             }));
 

+ 6 - 2
src/headless/utils/stanza.js

@@ -234,8 +234,12 @@ const stanza_utils = {
     getErrorMessage (stanza, is_muc, _converse) {
         const { __ } = _converse;
         if (is_muc) {
-            if (sizzle(`forbidden[xmlns="${Strophe.NS.STANZAS}"]`, stanza).length) {
-                return __("Your message was not delivered because you're not allowed to send messages in this groupchat.");
+            const forbidden = sizzle(`error forbidden[xmlns="${Strophe.NS.STANZAS}"]`, stanza).pop();
+            if (forbidden) {
+                const msg = __("Your message was not delivered because you weren't allowed to send it.");
+                const text = sizzle(`error text[xmlns="${Strophe.NS.STANZAS}"]`, stanza).pop();
+                const server_msg = text ? __('The message from the server is: "%1$s"', text.textContent) : '';
+                return server_msg ? `${msg} ${server_msg}` : msg;
             } else if (sizzle(`not-acceptable[xmlns="${Strophe.NS.STANZAS}"]`, stanza).length) {
                 return __("Your message was not delivered because you're not present in the groupchat.");
             }