From bd3499056ae358d1de1f172dc33e2d3f939a827d Mon Sep 17 00:00:00 2001 From: Bruno Windels <274386+bwindels@users.noreply.github.com> Date: Tue, 10 Jan 2023 12:05:30 +0100 Subject: [PATCH] provider higher-level rageshake fn for opened session Co-authored-by: R Midhun Suresh --- src/domain/rageshake.ts | 33 +++++++++++++++++-- .../session/settings/SettingsViewModel.js | 30 ++++------------- 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/src/domain/rageshake.ts b/src/domain/rageshake.ts index cb06e638..08e82d8c 100644 --- a/src/domain/rageshake.ts +++ b/src/domain/rageshake.ts @@ -16,11 +16,15 @@ limitations under the License. import type {BlobHandle} from "../platform/web/dom/BlobHandle"; import type {RequestFunction} from "../platform/types/types"; +import type {Platform} from "../platform/web/Platform"; +import type {ILogger} from "../logging/types"; +import type { IDBLogPersister } from "../logging/IDBLogPersister"; +import type { Session } from "../matrix/Session"; // see https://github.com/matrix-org/rageshake#readme type RageshakeData = { // A textual description of the problem. Included in the details.log.gz file. - text: string | undefined; + text?: string; // Application user-agent. Included in the details.log.gz file. userAgent: string; // Identifier for the application (eg 'riot-web'). Should correspond to a mapping configured in the configuration file for github issue reporting to work. @@ -28,7 +32,7 @@ type RageshakeData = { // Application version. Included in the details.log.gz file. version: string; // Label to attach to the github issue, and include in the details file. - label: string | undefined; + label?: string; }; export async function submitLogsToRageshakeServer(data: RageshakeData, logsBlob: BlobHandle, submitUrl: string, request: RequestFunction): Promise { @@ -63,3 +67,28 @@ export async function submitLogsToRageshakeServer(data: RageshakeData, logsBlob: // we don't bother with reading report_url from the body as the rageshake server doesn't always return it // and would have to have CORS setup properly for us to be able to read it. } + +/** @throws {Error} */ +export async function submitLogsFromSessionToDefaultServer(session: Session, platform: Platform): Promise { + const {bugReportEndpointUrl} = platform.config; + if (!bugReportEndpointUrl) { + throw new Error("no server configured to submit logs"); + } + const logReporters = (platform.logger as ILogger).reporters; + const exportReporter = logReporters.find(r => !!r["export"]) as IDBLogPersister | undefined; + if (!exportReporter) { + throw new Error("No logger that can export configured"); + } + const logExport = await exportReporter.export(); + await submitLogsToRageshakeServer( + { + app: "hydrogen", + userAgent: platform.description, + version: platform.version, + text: `Submit logs from settings for user ${session.userId} on device ${session.deviceId}`, + }, + logExport.asBlob(), + bugReportEndpointUrl, + platform.request + ); +} \ No newline at end of file diff --git a/src/domain/session/settings/SettingsViewModel.js b/src/domain/session/settings/SettingsViewModel.js index 0d61bcac..d60a6327 100644 --- a/src/domain/session/settings/SettingsViewModel.js +++ b/src/domain/session/settings/SettingsViewModel.js @@ -16,7 +16,7 @@ limitations under the License. import {ViewModel} from "../../ViewModel"; import {KeyBackupViewModel} from "./KeyBackupViewModel.js"; -import {submitLogsToRageshakeServer} from "../../../domain/rageshake"; +import {submitLogsFromSessionToDefaultServer} from "../../../domain/rageshake"; class PushNotificationStatus { constructor() { @@ -175,29 +175,13 @@ export class SettingsViewModel extends ViewModel { } async sendLogsToServer() { - const {bugReportEndpointUrl} = this.platform.config; - if (bugReportEndpointUrl) { - this._logsFeedbackMessage = this.i18n`Sending logs…`; + this._logsFeedbackMessage = this.i18n`Sending logs…`; + try { + await submitLogsFromSessionToDefaultServer(this._session, this.platform); + this._logsFeedbackMessage = this.i18n`Logs sent succesfully!`; + } catch (err) { + this._logsFeedbackMessage = err.message; this.emitChange(); - try { - const logExport = await this.logger.export(); - await submitLogsToRageshakeServer( - { - app: "hydrogen", - userAgent: this.platform.description, - version: DEFINE_VERSION, - text: `Submit logs from settings for user ${this._session.userId} on device ${this._session.deviceId}`, - }, - logExport.asBlob(), - bugReportEndpointUrl, - this.platform.request - ); - this._logsFeedbackMessage = this.i18n`Logs sent succesfully!`; - this.emitChange(); - } catch (err) { - this._logsFeedbackMessage = err.message; - this.emitChange(); - } } }