Skip to content

Commit

Permalink
Add a bug report hook. (#2988)
Browse files Browse the repository at this point in the history
  • Loading branch information
pixlwave authored Jul 1, 2024
1 parent 1178561 commit 78b0c70
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 9 deletions.
7 changes: 4 additions & 3 deletions ElementX/Sources/Application/AppCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg

navigationRootCoordinator = NavigationRootCoordinator()

Self.setupServiceLocator(appSettings: appSettings)
Self.setupServiceLocator(appSettings: appSettings, appHooks: appHooks)

ServiceLocator.shared.analytics.startIfEnabled()

Expand Down Expand Up @@ -340,14 +340,15 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg

// MARK: - Private

private static func setupServiceLocator(appSettings: AppSettings) {
private static func setupServiceLocator(appSettings: AppSettings, appHooks: AppHooks) {
ServiceLocator.shared.register(userIndicatorController: UserIndicatorController())
ServiceLocator.shared.register(appSettings: appSettings)
ServiceLocator.shared.register(networkMonitor: NetworkMonitor())
ServiceLocator.shared.register(bugReportService: BugReportService(withBaseURL: appSettings.bugReportServiceBaseURL,
applicationId: appSettings.bugReportApplicationId,
sdkGitSHA: sdkGitSha(),
maxUploadSize: appSettings.bugReportMaxUploadSize))
maxUploadSize: appSettings.bugReportMaxUploadSize,
appHooks: appHooks))
let posthogAnalyticsClient = PostHogAnalyticsClient()
posthogAnalyticsClient.updateSuperProperties(AnalyticsEvent.SuperProperties(appPlatform: .EXI, cryptoSDK: .Rust, cryptoSDKVersion: sdkGitSha()))
ServiceLocator.shared.register(analytics: AnalyticsService(client: posthogAnalyticsClient,
Expand Down
16 changes: 15 additions & 1 deletion ElementX/Sources/Hooks/AppHooks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Foundation
// MARK: Registration

class AppHooks: AppHooksProtocol {
private(set) var appSettingsHook: AppSettingsHookProtocol?
private var appSettingsHook: AppSettingsHookProtocol?
func registerAppSettingsHook(_ hook: AppSettingsHookProtocol) {
appSettingsHook = hook
}
Expand All @@ -28,6 +28,16 @@ class AppHooks: AppHooksProtocol {
guard let appSettingsHook else { return appSettings }
return appSettingsHook.run(appSettings: appSettings)
}

private var bugReportHook: BugReportHookProtocol?
func registerBugReportHook(_ hook: BugReportHookProtocol) {
bugReportHook = hook
}

func runBugReportHook(_ bugReport: BugReport) -> BugReport {
guard let bugReportHook else { return bugReport }
return bugReportHook.run(bugReport: bugReport)
}
}

protocol AppHooksProtocol {
Expand All @@ -43,3 +53,7 @@ extension AppHooksProtocol {
protocol AppSettingsHookProtocol {
func run(appSettings: AppSettings) -> AppSettings
}

protocol BugReportHookProtocol {
func run(bugReport: BugReport) -> BugReport
}
9 changes: 8 additions & 1 deletion ElementX/Sources/Services/BugReport/BugReportService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class BugReportService: NSObject, BugReportServiceProtocol {
private let sdkGitSHA: String
private let maxUploadSize: Int
private let session: URLSession

private let appHooks: AppHooks

private let progressSubject = PassthroughSubject<Double, Never>()
private var cancellables = Set<AnyCancellable>()

Expand All @@ -35,12 +38,14 @@ class BugReportService: NSObject, BugReportServiceProtocol {
applicationId: String,
sdkGitSHA: String,
maxUploadSize: Int,
session: URLSession = .shared) {
session: URLSession = .shared,
appHooks: AppHooks) {
self.baseURL = baseURL
self.applicationId = applicationId
self.sdkGitSHA = sdkGitSHA
self.maxUploadSize = maxUploadSize
self.session = session
self.appHooks = appHooks
super.init()
}

Expand All @@ -53,6 +58,8 @@ class BugReportService: NSObject, BugReportServiceProtocol {
// swiftlint:disable:next cyclomatic_complexity
func submitBugReport(_ bugReport: BugReport,
progressListener: CurrentValueSubject<Double, Never>) async -> Result<SubmitBugReportResponse, BugReportServiceError> {
let bugReport = appHooks.runBugReportHook(bugReport)

var params = [
MultipartFormData(key: "text", type: .text(value: bugReport.text)),
MultipartFormData(key: "can_contact", type: .text(value: "\(bugReport.canContact)"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct BugReport: Equatable {
let text: String
let includeLogs: Bool
let canContact: Bool
let githubLabels: [String]
var githubLabels: [String]
let files: [URL]
}

Expand Down
2 changes: 1 addition & 1 deletion Enterprise
6 changes: 4 additions & 2 deletions UnitTests/Sources/BugReportServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ class BugReportServiceTests: XCTestCase {
applicationId: "mock_app_id",
sdkGitSHA: "1234",
maxUploadSize: ServiceLocator.shared.settings.bugReportMaxUploadSize,
session: .mock)
session: .mock,
appHooks: AppHooks())
XCTAssertFalse(service.crashedLastRun)
}

Expand All @@ -63,7 +64,8 @@ class BugReportServiceTests: XCTestCase {
applicationId: "mock_app_id",
sdkGitSHA: "1234",
maxUploadSize: ServiceLocator.shared.settings.bugReportMaxUploadSize,
session: .mock)
session: .mock,
appHooks: AppHooks())

let bugReport = BugReport(userID: "@mock:client.com",
deviceID: nil,
Expand Down

0 comments on commit 78b0c70

Please sign in to comment.