2017-04-11 19:59:22 +02:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2017-04-18 18:23:34 +02:00
|
|
|
import pako from 'pako';
|
2017-04-11 19:59:22 +02:00
|
|
|
import q from "q";
|
|
|
|
|
2017-05-04 18:21:16 +02:00
|
|
|
import MatrixClientPeg from 'matrix-react-sdk/lib/MatrixClientPeg';
|
2017-04-11 19:59:22 +02:00
|
|
|
import PlatformPeg from 'matrix-react-sdk/lib/PlatformPeg';
|
|
|
|
|
|
|
|
import rageshake from './rageshake'
|
|
|
|
|
2017-04-18 17:47:50 +02:00
|
|
|
|
|
|
|
// polyfill textencoder if necessary
|
|
|
|
import * as TextEncodingUtf8 from 'text-encoding-utf-8';
|
|
|
|
let TextEncoder = window.TextEncoder;
|
|
|
|
if (!TextEncoder) {
|
|
|
|
TextEncoder = TextEncodingUtf8.TextEncoder;
|
|
|
|
}
|
|
|
|
|
2017-04-11 19:59:22 +02:00
|
|
|
/**
|
|
|
|
* Send a bug report.
|
2017-04-12 15:53:47 +02:00
|
|
|
*
|
2017-04-11 19:59:22 +02:00
|
|
|
* @param {string} bugReportEndpoint HTTP url to send the report to
|
2017-04-12 15:53:47 +02:00
|
|
|
*
|
2017-04-12 12:26:53 +02:00
|
|
|
* @param {object} opts optional dictionary of options
|
2017-04-12 15:53:47 +02:00
|
|
|
*
|
2017-04-12 12:26:53 +02:00
|
|
|
* @param {string} opts.userText Any additional user input.
|
2017-04-12 15:53:47 +02:00
|
|
|
*
|
2017-04-12 12:26:53 +02:00
|
|
|
* @param {boolean} opts.sendLogs True to send logs
|
2017-04-12 15:53:47 +02:00
|
|
|
*
|
|
|
|
* @param {function(string)} opts.progressCallback Callback to call with progress updates
|
|
|
|
*
|
2017-04-11 19:59:22 +02:00
|
|
|
* @return {Promise} Resolved when the bug report is sent.
|
|
|
|
*/
|
2017-04-12 12:26:53 +02:00
|
|
|
export default async function sendBugReport(bugReportEndpoint, opts) {
|
2017-04-11 19:59:22 +02:00
|
|
|
if (!bugReportEndpoint) {
|
|
|
|
throw new Error("No bug report endpoint has been set.");
|
|
|
|
}
|
|
|
|
|
2017-04-12 12:26:53 +02:00
|
|
|
opts = opts || {};
|
2017-04-12 15:53:47 +02:00
|
|
|
const progressCallback = opts.progressCallback || (() => {});
|
2017-04-12 12:26:53 +02:00
|
|
|
|
2017-04-12 15:53:47 +02:00
|
|
|
progressCallback("Collecting app version information");
|
2017-04-11 19:59:22 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-05-04 18:21:16 +02:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
|
2017-04-11 19:59:22 +02:00
|
|
|
console.log("Sending bug report.");
|
|
|
|
|
2017-04-18 17:47:50 +02:00
|
|
|
const body = new FormData();
|
|
|
|
body.append('text', opts.userText || "User did not supply any additional text.");
|
|
|
|
body.append('app', 'riot-web');
|
|
|
|
body.append('version', version);
|
|
|
|
body.append('user_agent', userAgent);
|
|
|
|
|
2017-05-04 18:21:16 +02:00
|
|
|
if (client) {
|
|
|
|
body.append('user_id', client.credentials.userId);
|
|
|
|
body.append('device_id', client.deviceId);
|
|
|
|
}
|
|
|
|
|
2017-04-12 12:26:53 +02:00
|
|
|
if (opts.sendLogs) {
|
2017-04-12 15:53:47 +02:00
|
|
|
progressCallback("Collecting logs");
|
2017-04-18 17:47:50 +02:00
|
|
|
const logs = await rageshake.getLogsForReport();
|
|
|
|
for (let entry of logs) {
|
|
|
|
// encode as UTF-8
|
|
|
|
const buf = new TextEncoder().encode(entry.lines);
|
|
|
|
|
2017-04-18 18:23:34 +02:00
|
|
|
// compress
|
|
|
|
const compressed = pako.gzip(buf);
|
|
|
|
|
|
|
|
body.append('compressed-log', new Blob([compressed]), entry.id);
|
2017-04-18 17:47:50 +02:00
|
|
|
}
|
2017-04-11 19:59:22 +02:00
|
|
|
}
|
|
|
|
|
2017-04-12 15:53:47 +02:00
|
|
|
progressCallback("Uploading report");
|
|
|
|
await _submitReport(bugReportEndpoint, body, progressCallback);
|
2017-04-18 17:47:50 +02:00
|
|
|
}
|
|
|
|
|
2017-04-12 15:53:47 +02:00
|
|
|
function _submitReport(endpoint, body, progressCallback) {
|
2017-04-18 17:47:50 +02:00
|
|
|
const deferred = q.defer();
|
|
|
|
|
|
|
|
const req = new XMLHttpRequest();
|
|
|
|
req.open("POST", endpoint);
|
|
|
|
req.timeout = 5 * 60 * 1000;
|
|
|
|
req.onreadystatechange = function() {
|
2017-04-12 15:53:47 +02:00
|
|
|
if (req.readyState === XMLHttpRequest.LOADING) {
|
|
|
|
progressCallback("Waiting for response from server");
|
|
|
|
} else if (req.readyState === XMLHttpRequest.DONE) {
|
2017-04-18 17:47:50 +02:00
|
|
|
on_done();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
req.send(body);
|
|
|
|
return deferred.promise;
|
|
|
|
|
|
|
|
function on_done() {
|
|
|
|
if (req.status < 200 || req.status >= 400) {
|
|
|
|
deferred.reject(new Error(`HTTP ${req.status}`));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
deferred.resolve();
|
|
|
|
}
|
2017-04-11 19:59:22 +02:00
|
|
|
}
|