From cbfa4dd1abc6f8081db32ca3b1b4941d8c9c41b3 Mon Sep 17 00:00:00 2001
From: Richard van der Hoff <richard@matrix.org>
Date: Tue, 11 Apr 2017 18:46:48 +0100
Subject: [PATCH 1/4] Get rageshake endpoint from SdkConfig instead of storing
 in rageshake

- in preparation for factoring out the sending of the rageshake
---
 src/components/views/dialogs/BugReportDialog.js | 5 ++++-
 src/vector/index.js                             | 1 -
 src/vector/rageshake.js                         | 8 ++------
 3 files changed, 6 insertions(+), 8 deletions(-)

diff --git a/src/components/views/dialogs/BugReportDialog.js b/src/components/views/dialogs/BugReportDialog.js
index dcc0850e..c03c9b43 100644
--- a/src/components/views/dialogs/BugReportDialog.js
+++ b/src/components/views/dialogs/BugReportDialog.js
@@ -17,6 +17,7 @@ limitations under the License.
 import React from 'react';
 import sdk from 'matrix-react-sdk';
 import rageshake from '../../../vector/rageshake';
+import SdkConfig from 'matrix-react-sdk/lib/SdkConfig';
 
 export default class BugReportDialog extends React.Component {
     constructor(props, context) {
@@ -47,7 +48,9 @@ export default class BugReportDialog extends React.Component {
             return;
         }
         this.setState({ busy: true, err: null });
-        rageshake.sendBugReport(userText, sendLogs).then(() => {
+        rageshake.sendBugReport(
+            SdkConfig.get().bug_report_endpoint_url, userText, sendLogs,
+        ).then(() => {
             this.setState({ busy: false });
             this.props.onFinished(false);
         }, (err) => {
diff --git a/src/vector/index.js b/src/vector/index.js
index e08b7174..42a60e45 100644
--- a/src/vector/index.js
+++ b/src/vector/index.js
@@ -259,7 +259,6 @@ async function loadApp() {
     let configError;
     try {
         configJson = await getConfig();
-        rageshake.setBugReportEndpoint(configJson.bug_report_endpoint_url);
     } catch (e) {
         configError = e;
     }
diff --git a/src/vector/rageshake.js b/src/vector/rageshake.js
index 0e5b5dac..c519f8b3 100644
--- a/src/vector/rageshake.js
+++ b/src/vector/rageshake.js
@@ -396,7 +396,6 @@ function selectQuery(store, keyRange, resultMapper) {
 let store = null;
 let logger = null;
 let initPromise = null;
-let bugReportEndpoint = null;
 module.exports = {
 
     /**
@@ -430,17 +429,14 @@ module.exports = {
         await store.consume();
     },
 
-    setBugReportEndpoint: function(url) {
-        bugReportEndpoint = url;
-    },
-
     /**
      * Send a bug report.
+     * @param {string} bugReportEndpoint HTTP url to send the report to
      * @param {string} userText Any additional user input.
      * @param {boolean} sendLogs True to send logs
      * @return {Promise} Resolved when the bug report is sent.
      */
-    sendBugReport: async function(userText, sendLogs) {
+    sendBugReport: async function(bugReportEndpoint, userText, sendLogs) {
         if (!logger) {
             throw new Error(
                 "No console logger, did you forget to call init()?"

From 4efb2b6750c62646f427f204d6c67c54502db8c9 Mon Sep 17 00:00:00 2001
From: Richard van der Hoff <richard@matrix.org>
Date: Tue, 11 Apr 2017 18:47:55 +0100
Subject: [PATCH 2/4] Rageshake: Factor out `getLogsForReport`

... in preparation for factoring out sending the report
---
 src/vector/rageshake.js | 46 +++++++++++++++++++++++++----------------
 1 file changed, 28 insertions(+), 18 deletions(-)

diff --git a/src/vector/rageshake.js b/src/vector/rageshake.js
index c519f8b3..711cc298 100644
--- a/src/vector/rageshake.js
+++ b/src/vector/rageshake.js
@@ -429,6 +429,32 @@ module.exports = {
         await store.consume();
     },
 
+    /**
+     * Get a recent snapshot of the logs, ready for attaching to a bug report
+     *
+     * @return {Array<{lines: string, id, string}>}  list of log data
+     */
+    getLogsForReport: async function() {
+        if (!logger) {
+            throw new Error(
+                "No console logger, did you forget to call init()?"
+            );
+        }
+        // If in incognito mode, store is null, but we still want bug report
+        // sending to work going off the in-memory console logs.
+        if (store) {
+            // flush most recent logs
+            await store.flush();
+            return await store.consume();
+        }
+        else {
+            return [{
+                lines: logger.flush(true),
+                id: "-",
+            }];
+        }
+    },
+
     /**
      * Send a bug report.
      * @param {string} bugReportEndpoint HTTP url to send the report to
@@ -437,11 +463,6 @@ module.exports = {
      * @return {Promise} Resolved when the bug report is sent.
      */
     sendBugReport: async function(bugReportEndpoint, userText, sendLogs) {
-        if (!logger) {
-            throw new Error(
-                "No console logger, did you forget to call init()?"
-            );
-        }
         if (!bugReportEndpoint) {
             throw new Error("No bug report endpoint has been set.");
         }
@@ -457,22 +478,11 @@ module.exports = {
             userAgent = window.navigator.userAgent;
         }
 
-        // If in incognito mode, store is null, but we still want bug report
-        // sending to work going off the in-memory console logs.
         console.log("Sending bug report.");
+
         let logs = [];
         if (sendLogs) {
-            if (store) {
-                // flush most recent logs
-                await store.flush();
-                logs = await store.consume();
-            }
-            else {
-                logs.push({
-                    lines: logger.flush(true),
-                    id: "-",
-                });
-            }
+            logs = await this.getLogsForReport();
         }
 
         await q.Promise((resolve, reject) => {

From 6423f7ce03b68fd219fbd226def68a809e1c5494 Mon Sep 17 00:00:00 2001
From: Richard van der Hoff <richard@matrix.org>
Date: Tue, 11 Apr 2017 18:59:22 +0100
Subject: [PATCH 3/4] rageshake: factor out submission to a separate file

This will mean we can load it asyncronously in future, if we want.
---
 .../views/dialogs/BugReportDialog.js          |  4 +-
 src/vector/rageshake.js                       | 61 --------------
 src/vector/submit-rageshake.js                | 81 +++++++++++++++++++
 3 files changed, 83 insertions(+), 63 deletions(-)
 create mode 100644 src/vector/submit-rageshake.js

diff --git a/src/components/views/dialogs/BugReportDialog.js b/src/components/views/dialogs/BugReportDialog.js
index c03c9b43..62424cb1 100644
--- a/src/components/views/dialogs/BugReportDialog.js
+++ b/src/components/views/dialogs/BugReportDialog.js
@@ -16,7 +16,7 @@ limitations under the License.
 
 import React from 'react';
 import sdk from 'matrix-react-sdk';
-import rageshake from '../../../vector/rageshake';
+import submit_rageshake from '../../../vector/submit-rageshake';
 import SdkConfig from 'matrix-react-sdk/lib/SdkConfig';
 
 export default class BugReportDialog extends React.Component {
@@ -48,7 +48,7 @@ export default class BugReportDialog extends React.Component {
             return;
         }
         this.setState({ busy: true, err: null });
-        rageshake.sendBugReport(
+        submit_rageshake(
             SdkConfig.get().bug_report_endpoint_url, userText, sendLogs,
         ).then(() => {
             this.setState({ busy: false });
diff --git a/src/vector/rageshake.js b/src/vector/rageshake.js
index 711cc298..feaec26d 100644
--- a/src/vector/rageshake.js
+++ b/src/vector/rageshake.js
@@ -14,8 +14,6 @@ See the License for the specific language governing permissions and
 limitations under the License.
 */
 
-import PlatformPeg from 'matrix-react-sdk/lib/PlatformPeg';
-import request from "browser-request";
 import q from "q";
 
 // This module contains all the code needed to log the console, persist it to
@@ -454,63 +452,4 @@ module.exports = {
             }];
         }
     },
-
-    /**
-     * Send a bug report.
-     * @param {string} bugReportEndpoint HTTP url to send the report to
-     * @param {string} userText Any additional user input.
-     * @param {boolean} sendLogs True to send logs
-     * @return {Promise} Resolved when the bug report is sent.
-     */
-    sendBugReport: async function(bugReportEndpoint, userText, sendLogs) {
-        if (!bugReportEndpoint) {
-            throw new Error("No bug report endpoint has been set.");
-        }
-
-        let version = "UNKNOWN";
-        try {
-            version = await PlatformPeg.get().getAppVersion();
-        }
-        catch (err) {} // PlatformPeg already logs this.
-
-        let userAgent = "UNKNOWN";
-        if (window.navigator && window.navigator.userAgent) {
-            userAgent = window.navigator.userAgent;
-        }
-
-        console.log("Sending bug report.");
-
-        let logs = [];
-        if (sendLogs) {
-            logs = await this.getLogsForReport();
-        }
-
-        await q.Promise((resolve, reject) => {
-            request({
-                method: "POST",
-                url: bugReportEndpoint,
-                body: {
-                    logs: logs,
-                    text: (
-                        userText || "User did not supply any additional text."
-                    ),
-                    app: 'riot-web',
-                    version: version,
-                    user_agent: userAgent,
-                },
-                json: true,
-                timeout: 5 * 60 * 1000,
-            }, (err, res) => {
-                if (err) {
-                    reject(err);
-                    return;
-                }
-                if (res.status < 200 || res.status >= 400) {
-                    reject(new Error(`HTTP ${res.status}`));
-                    return;
-                }
-                resolve();
-            })
-        });
-    }
 };
diff --git a/src/vector/submit-rageshake.js b/src/vector/submit-rageshake.js
new file mode 100644
index 00000000..87188821
--- /dev/null
+++ b/src/vector/submit-rageshake.js
@@ -0,0 +1,81 @@
+/*
+Copyright 2017 OpenMarket Ltd
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+import q from "q";
+import request from "browser-request";
+
+import PlatformPeg from 'matrix-react-sdk/lib/PlatformPeg';
+
+import rageshake from './rageshake'
+
+/**
+ * Send a bug report.
+ * @param {string} bugReportEndpoint HTTP url to send the report to
+ * @param {string} userText Any additional user input.
+ * @param {boolean} sendLogs True to send logs
+ * @return {Promise} Resolved when the bug report is sent.
+ */
+export default async function sendBugReport(bugReportEndpoint, userText, sendLogs) {
+    if (!bugReportEndpoint) {
+        throw new Error("No bug report endpoint has been set.");
+    }
+
+    let version = "UNKNOWN";
+    try {
+        version = await PlatformPeg.get().getAppVersion();
+    }
+    catch (err) {} // PlatformPeg already logs this.
+
+    let userAgent = "UNKNOWN";
+    if (window.navigator && window.navigator.userAgent) {
+        userAgent = window.navigator.userAgent;
+    }
+
+    console.log("Sending bug report.");
+
+    let logs = [];
+    if (sendLogs) {
+        logs = await rageshake.getLogsForReport();
+    }
+
+    await q.Promise((resolve, reject) => {
+        request({
+            method: "POST",
+            url: bugReportEndpoint,
+            body: {
+                logs: logs,
+                text: (
+                    userText || "User did not supply any additional text."
+                ),
+                app: 'riot-web',
+                version: version,
+                user_agent: userAgent,
+            },
+            json: true,
+            timeout: 5 * 60 * 1000,
+        }, (err, res) => {
+            if (err) {
+                reject(err);
+                return;
+            }
+            if (res.status < 200 || res.status >= 400) {
+                reject(new Error(`HTTP ${res.status}`));
+                return;
+            }
+            resolve();
+        })
+    });
+}

From 3f291aae5b01fe842757e1cce33dcc67025c3f68 Mon Sep 17 00:00:00 2001
From: Richard van der Hoff <richard@matrix.org>
Date: Wed, 12 Apr 2017 11:26:53 +0100
Subject: [PATCH 4/4] Use an opts arg for submit-rageshake

---
 src/components/views/dialogs/BugReportDialog.js |  7 ++++---
 src/vector/submit-rageshake.js                  | 13 ++++++++-----
 2 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/src/components/views/dialogs/BugReportDialog.js b/src/components/views/dialogs/BugReportDialog.js
index 62424cb1..badc994b 100644
--- a/src/components/views/dialogs/BugReportDialog.js
+++ b/src/components/views/dialogs/BugReportDialog.js
@@ -48,9 +48,10 @@ export default class BugReportDialog extends React.Component {
             return;
         }
         this.setState({ busy: true, err: null });
-        submit_rageshake(
-            SdkConfig.get().bug_report_endpoint_url, userText, sendLogs,
-        ).then(() => {
+        submit_rageshake(SdkConfig.get().bug_report_endpoint_url, {
+            userText: userText,
+            sendLogs: sendLogs,
+        }).then(() => {
             this.setState({ busy: false });
             this.props.onFinished(false);
         }, (err) => {
diff --git a/src/vector/submit-rageshake.js b/src/vector/submit-rageshake.js
index 87188821..8c070076 100644
--- a/src/vector/submit-rageshake.js
+++ b/src/vector/submit-rageshake.js
@@ -24,15 +24,18 @@ import rageshake from './rageshake'
 /**
  * Send a bug report.
  * @param {string} bugReportEndpoint HTTP url to send the report to
- * @param {string} userText Any additional user input.
- * @param {boolean} sendLogs True to send logs
+ * @param {object} opts optional dictionary of options
+ * @param {string} opts.userText Any additional user input.
+ * @param {boolean} opts.sendLogs True to send logs
  * @return {Promise} Resolved when the bug report is sent.
  */
-export default async function sendBugReport(bugReportEndpoint, userText, sendLogs) {
+export default async function sendBugReport(bugReportEndpoint, opts) {
     if (!bugReportEndpoint) {
         throw new Error("No bug report endpoint has been set.");
     }
 
+    opts = opts || {};
+
     let version = "UNKNOWN";
     try {
         version = await PlatformPeg.get().getAppVersion();
@@ -47,7 +50,7 @@ export default async function sendBugReport(bugReportEndpoint, userText, sendLog
     console.log("Sending bug report.");
 
     let logs = [];
-    if (sendLogs) {
+    if (opts.sendLogs) {
         logs = await rageshake.getLogsForReport();
     }
 
@@ -58,7 +61,7 @@ export default async function sendBugReport(bugReportEndpoint, userText, sendLog
             body: {
                 logs: logs,
                 text: (
-                    userText || "User did not supply any additional text."
+                    opts.userText || "User did not supply any additional text."
                 ),
                 app: 'riot-web',
                 version: version,