Skip to content

Commit 062cf47

Browse files
authored
Merge pull request element-hq#3645 from vector-im/rav/refactor_rageshake
Factor out rageshake upload to a separate file
2 parents fa132d3 + 3f291aa commit 062cf47

4 files changed

Lines changed: 105 additions & 73 deletions

File tree

‎src/components/views/dialogs/BugReportDialog.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ limitations under the License.
1616

1717
import React from 'react';
1818
import sdk from 'matrix-react-sdk';
19-
import rageshake from '../../../vector/rageshake';
19+
import submit_rageshake from '../../../vector/submit-rageshake';
20+
import SdkConfig from 'matrix-react-sdk/lib/SdkConfig';
2021

2122
export default class BugReportDialog extends React.Component {
2223
constructor(props, context) {
@@ -47,7 +48,10 @@ export default class BugReportDialog extends React.Component {
4748
return;
4849
}
4950
this.setState({ busy: true, err: null });
50-
rageshake.sendBugReport(userText, sendLogs).then(() => {
51+
submit_rageshake(SdkConfig.get().bug_report_endpoint_url, {
52+
userText: userText,
53+
sendLogs: sendLogs,
54+
}).then(() => {
5155
this.setState({ busy: false });
5256
this.props.onFinished(false);
5357
}, (err) => {

‎src/vector/index.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,6 @@ async function loadApp() {
259259
let configError;
260260
try {
261261
configJson = await getConfig();
262-
rageshake.setBugReportEndpoint(configJson.bug_report_endpoint_url);
263262
} catch (e) {
264263
configError = e;
265264
}

‎src/vector/rageshake.js‎

Lines changed: 15 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
import PlatformPeg from 'matrix-react-sdk/lib/PlatformPeg';
18-
import request from "browser-request";
1917
import q from "q";
2018

2119
// This module contains all the code needed to log the console, persist it to
@@ -396,7 +394,6 @@ function selectQuery(store, keyRange, resultMapper) {
396394
let store = null;
397395
let logger = null;
398396
let initPromise = null;
399-
let bugReportEndpoint = null;
400397
module.exports = {
401398

402399
/**
@@ -430,81 +427,29 @@ module.exports = {
430427
await store.consume();
431428
},
432429

433-
setBugReportEndpoint: function(url) {
434-
bugReportEndpoint = url;
435-
},
436-
437430
/**
438-
* Send a bug report.
439-
* @param {string} userText Any additional user input.
440-
* @param {boolean} sendLogs True to send logs
441-
* @return {Promise} Resolved when the bug report is sent.
431+
* Get a recent snapshot of the logs, ready for attaching to a bug report
432+
*
433+
* @return {Array<{lines: string, id, string}>} list of log data
442434
*/
443-
sendBugReport: async function(userText, sendLogs) {
435+
getLogsForReport: async function() {
444436
if (!logger) {
445437
throw new Error(
446438
"No console logger, did you forget to call init()?"
447439
);
448440
}
449-
if (!bugReportEndpoint) {
450-
throw new Error("No bug report endpoint has been set.");
451-
}
452-
453-
let version = "UNKNOWN";
454-
try {
455-
version = await PlatformPeg.get().getAppVersion();
456-
}
457-
catch (err) {} // PlatformPeg already logs this.
458-
459-
let userAgent = "UNKNOWN";
460-
if (window.navigator && window.navigator.userAgent) {
461-
userAgent = window.navigator.userAgent;
462-
}
463-
464441
// If in incognito mode, store is null, but we still want bug report
465442
// sending to work going off the in-memory console logs.
466-
console.log("Sending bug report.");
467-
let logs = [];
468-
if (sendLogs) {
469-
if (store) {
470-
// flush most recent logs
471-
await store.flush();
472-
logs = await store.consume();
473-
}
474-
else {
475-
logs.push({
476-
lines: logger.flush(true),
477-
id: "-",
478-
});
479-
}
443+
if (store) {
444+
// flush most recent logs
445+
await store.flush();
446+
return await store.consume();
480447
}
481-
482-
await q.Promise((resolve, reject) => {
483-
request({
484-
method: "POST",
485-
url: bugReportEndpoint,
486-
body: {
487-
logs: logs,
488-
text: (
489-
userText || "User did not supply any additional text."
490-
),
491-
app: 'riot-web',
492-
version: version,
493-
user_agent: userAgent,
494-
},
495-
json: true,
496-
timeout: 5 * 60 * 1000,
497-
}, (err, res) => {
498-
if (err) {
499-
reject(err);
500-
return;
501-
}
502-
if (res.status < 200 || res.status >= 400) {
503-
reject(new Error(`HTTP ${res.status}`));
504-
return;
505-
}
506-
resolve();
507-
})
508-
});
509-
}
448+
else {
449+
return [{
450+
lines: logger.flush(true),
451+
id: "-",
452+
}];
453+
}
454+
},
510455
};

‎src/vector/submit-rageshake.js‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
Copyright 2017 OpenMarket Ltd
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import q from "q";
18+
import request from "browser-request";
19+
20+
import PlatformPeg from 'matrix-react-sdk/lib/PlatformPeg';
21+
22+
import rageshake from './rageshake'
23+
24+
/**
25+
* Send a bug report.
26+
* @param {string} bugReportEndpoint HTTP url to send the report to
27+
* @param {object} opts optional dictionary of options
28+
* @param {string} opts.userText Any additional user input.
29+
* @param {boolean} opts.sendLogs True to send logs
30+
* @return {Promise} Resolved when the bug report is sent.
31+
*/
32+
export default async function sendBugReport(bugReportEndpoint, opts) {
33+
if (!bugReportEndpoint) {
34+
throw new Error("No bug report endpoint has been set.");
35+
}
36+
37+
opts = opts || {};
38+
39+
let version = "UNKNOWN";
40+
try {
41+
version = await PlatformPeg.get().getAppVersion();
42+
}
43+
catch (err) {} // PlatformPeg already logs this.
44+
45+
let userAgent = "UNKNOWN";
46+
if (window.navigator && window.navigator.userAgent) {
47+
userAgent = window.navigator.userAgent;
48+
}
49+
50+
console.log("Sending bug report.");
51+
52+
let logs = [];
53+
if (opts.sendLogs) {
54+
logs = await rageshake.getLogsForReport();
55+
}
56+
57+
await q.Promise((resolve, reject) => {
58+
request({
59+
method: "POST",
60+
url: bugReportEndpoint,
61+
body: {
62+
logs: logs,
63+
text: (
64+
opts.userText || "User did not supply any additional text."
65+
),
66+
app: 'riot-web',
67+
version: version,
68+
user_agent: userAgent,
69+
},
70+
json: true,
71+
timeout: 5 * 60 * 1000,
72+
}, (err, res) => {
73+
if (err) {
74+
reject(err);
75+
return;
76+
}
77+
if (res.status < 200 || res.status >= 400) {
78+
reject(new Error(`HTTP ${res.status}`));
79+
return;
80+
}
81+
resolve();
82+
})
83+
});
84+
}

0 commit comments

Comments
 (0)