From a76b01727d2896f2db56ad5434d933ce87701075 Mon Sep 17 00:00:00 2001
From: Felipe Cecagno <fcecagno@gmail.com>
Date: Wed, 18 Jul 2018 00:42:02 -0300
Subject: [PATCH] fix issue when config.hooks.getRaw=true but the callback
 getRaw=false

---
 bbb-webhooks/config.js               |  2 +-
 bbb-webhooks/config_local.js.example |  8 ++++----
 bbb-webhooks/hook.js                 |  6 +++---
 bbb-webhooks/test/test.js            | 10 +++++-----
 bbb-webhooks/web_hooks.js            |  4 +---
 bbb-webhooks/web_server.js           |  7 ++++---
 6 files changed, 18 insertions(+), 19 deletions(-)

diff --git a/bbb-webhooks/config.js b/bbb-webhooks/config.js
index 80845f1fc9..34155b3a9d 100644
--- a/bbb-webhooks/config.js
+++ b/bbb-webhooks/config.js
@@ -29,7 +29,7 @@ if (!config.hooks.permanentURLs) { config.hooks.permanentURLs = []; }
 // How many messages will be enqueued to be processed at the same time
 if (config.hooks.queueSize  == null) { config.hooks.queueSize = 10000; }
 // Allow permanent hooks to receive raw message, which is the message straight from BBB
-if (config.hooks.getRaw  == null) { config.hooks.getRaw = true; }
+if (config.hooks.defaultGetRaw  == null) { config.hooks.defaultGetRaw = false; }
 // If set to higher than 1, will send events on the format:
 // "event=[{event1},{event2}],timestamp=000" or "[{event1},{event2}]" (based on using auth2_0 or not)
 // when there are more than 1 event on the queue at the moment of processing the queue.
diff --git a/bbb-webhooks/config_local.js.example b/bbb-webhooks/config_local.js.example
index 9771197385..f3274bb5e7 100644
--- a/bbb-webhooks/config_local.js.example
+++ b/bbb-webhooks/config_local.js.example
@@ -6,7 +6,7 @@ const config = {};
 config.bbb = {};
 config.bbb.sharedSecret = "mysharedsecret";
 // Whether to use Auth2.0 or not, Auth2.0 sends the sharedSecret whithin an Authorization header as a bearer
-config.bbb.auth2_0 = false
+config.bbb.auth2_0 = false;
 
 // The port in which the API server will run.
 config.server = {};
@@ -21,9 +21,9 @@ config.server.port = 3005;
 //}
 
 // IP where permanent hook will post data (more than 1 URL means more than 1 permanent hook)
-//config.hooks.permanentURLs = ["request.catcher.url", "another.request.catcher.url"]
+//config.hooks.permanentURLs = [ { url: "request.catcher.url", getRaw: false }, { url: "another.request.catcher.url", getRaw: true } ]
 
-// Allow global hook to receive all events with raw data
-//config.hooks.getRaw = false;
+// Determine default behaviour when getRaw isn't defined while registering the hook
+//config.hooks.defaultGetRaw = false;
 
 module.exports = config;
diff --git a/bbb-webhooks/hook.js b/bbb-webhooks/hook.js
index f959ca1635..ce2a53fc19 100644
--- a/bbb-webhooks/hook.js
+++ b/bbb-webhooks/hook.js
@@ -168,11 +168,11 @@ module.exports = class Hook {
       hook.callbackURL = callbackURL;
       hook.externalMeetingID = meetingID;
       hook.getRaw = getRaw;
-      hook.permanent = config.hooks.permanentURLs.some( url => {
-        return url === callbackURL
+      hook.permanent = config.hooks.permanentURLs.some( obj => {
+        return obj.url === callbackURL
       });
       if (hook.permanent) {
-        hook.id = config.hooks.permanentURLs.indexOf(callbackURL) + 1;
+        hook.id = config.hooks.permanentURLs.map(obj => obj.url).indexOf(callbackURL) + 1;
         nextID = config.hooks.permanentURLs.length + 1;
       } else {
         hook.id = nextID++;
diff --git a/bbb-webhooks/test/test.js b/bbb-webhooks/test/test.js
index 6394a3ba89..51b2d0ab20 100644
--- a/bbb-webhooks/test/test.js
+++ b/bbb-webhooks/test/test.js
@@ -14,7 +14,7 @@ Logger.remove(winston.transports.Console);
 describe('bbb-webhooks tests', () => {
   before( (done) => {
     config.hooks.queueSize = 10;
-    config.hooks.permanentURLs = ["http://wh.requestcatcher.com"];
+    config.hooks.permanentURLs = [ { url: "http://wh.requestcatcher.com", getRaw: true } ];
     application = new Application();
     application.start( () => {
       done();
@@ -110,7 +110,7 @@ describe('bbb-webhooks tests', () => {
       .expect('Content-Type', /text\/xml/)
       .expect(200, (res) => {
         const hooks = Hook.allGlobalSync();
-        if (hooks && hooks[0].callbackURL == config.hooks.permanentURLs[0]) {
+        if (hooks && hooks[0].callbackURL == config.hooks.permanentURLs[0].url) {
           done();
         }
         else {
@@ -221,7 +221,7 @@ describe('bbb-webhooks tests', () => {
       const hooks = Hook.allGlobalSync();
       const hook = hooks[0];
 
-      const getpost = nock(config.hooks.permanentURLs[0])
+      const getpost = nock(config.hooks.permanentURLs[0].url)
                       .filteringRequestBody( (body) => {
                         let parsed = JSON.parse(body)
                         return parsed[0].data.id ? "mapped" : "not mapped";
@@ -262,7 +262,7 @@ describe('bbb-webhooks tests', () => {
                       .reply(200, () => {
                         done();
                       });
-      const permanent = nock(config.hooks.permanentURLs[0])
+      const permanent = nock(config.hooks.permanentURLs[0].url)
                         .post("/")
                         .reply(200)
       config.redis.client.publish("test-channel", JSON.stringify(Helpers.rawMessage));
@@ -280,7 +280,7 @@ describe('bbb-webhooks tests', () => {
       const hooks = Hook.allGlobalSync();
       const hook = hooks[0];
       hook.enqueue("multiMessage2")
-      const getpost = nock(config.hooks.permanentURLs[0])
+      const getpost = nock(config.hooks.permanentURLs[0].url)
                       .filteringPath( (path) => {
                         return path.split('?')[0];
                       })
diff --git a/bbb-webhooks/web_hooks.js b/bbb-webhooks/web_hooks.js
index e30cc96e0a..2b1f50605d 100644
--- a/bbb-webhooks/web_hooks.js
+++ b/bbb-webhooks/web_hooks.js
@@ -39,7 +39,7 @@ module.exports = class WebHooks {
         let messageMapped = new MessageMapping();
         messageMapped.mapMessage(JSON.parse(message));
         message = messageMapped.mappedObject;
-        if (!_.isEmpty(message) && !config.hooks.getRaw) {
+        if (!_.isEmpty(message)) {
           const intId = message.data.attributes.meeting["internal-meeting-id"];
           IDMapping.reportActivity(intId);
 
@@ -67,8 +67,6 @@ module.exports = class WebHooks {
             default:
               processMessage();
           }
-        } else {
-          this._processRaw(raw);
         }
       } catch (e) {
         Logger.error("[WebHooks] error processing the message:", JSON.stringify(raw), ":", e);
diff --git a/bbb-webhooks/web_server.js b/bbb-webhooks/web_server.js
index 937690183a..072a2ad8e3 100644
--- a/bbb-webhooks/web_server.js
+++ b/bbb-webhooks/web_server.js
@@ -49,10 +49,11 @@ module.exports = class WebServer {
     const callbackURL = urlObj.query["callbackURL"];
     const meetingID = urlObj.query["meetingID"];
     let getRaw = urlObj.query["getRaw"];
-    if(getRaw){
+    if (getRaw){
       getRaw = JSON.parse(getRaw.toLowerCase());
+    } else {
+      getRaw = config.hooks.defaultGetRaw;
     }
-    else getRaw = false
 
     if (callbackURL == null) {
       respondWithXML(res, config.api.responses.missingParamCallbackURL);
@@ -73,7 +74,7 @@ module.exports = class WebServer {
   // Create a permanent hook. Permanent hooks can't be deleted via API and will try to emit a message until it succeed
   createPermanents(callback) {
     for (let i = 0; i < config.hooks.permanentURLs.length; i++) {
-      Hook.addSubscription(config.hooks.permanentURLs[i], null, config.hooks.getRaw, function(error, hook) {
+      Hook.addSubscription(config.hooks.permanentURLs[i].url, null, config.hooks.permanentURLs[i].getRaw, function(error, hook) {
         if (error != null) { // there probably won't be any errors here
           Logger.info("[WebServer] duplicated permanent hook", error);
         } else if (hook != null) {
-- 
GitLab