Updated setPusher function (#684)

* updated set_pusher function from the SDK

* this fixes a crash and allows for the navigation to work by using the threadIdentifier of the notification

* adding NCE target

* project setup completed with xcodegen

* no need for those ugly storyboards

* code improvement

* removing unused outlet

* mocks generated with the comment instead of the marker protocol

* updated stencil

* fixed unit tests

* updated swiftformat

* pr comments
This commit is contained in:
Mauro
2023-03-16 16:39:10 +01:00
committed by GitHub
parent 701581f4a1
commit 7544619a55
16 changed files with 245 additions and 154 deletions
@@ -473,11 +473,12 @@ extension AppCoordinator: NotificationManagerDelegate {
func notificationTapped(_ service: NotificationManagerProtocol, content: UNNotificationContent) async {
MXLog.info("[AppCoordinator] tappedNotification")
guard let roomId = content.userInfo[NotificationConstants.UserInfoKey.roomIdentifier] as? String else {
// We store the room identifier into the thread identifier
guard !content.threadIdentifier.isEmpty else {
return
}
userSessionFlowCoordinator?.tryDisplayingRoomScreen(roomId: roomId)
userSessionFlowCoordinator?.tryDisplayingRoomScreen(roomId: content.threadIdentifier)
}
func handleInlineReply(_ service: NotificationManagerProtocol, content: UNNotificationContent, replyText: String) async {
@@ -2,7 +2,6 @@
// DO NOT EDIT
// swiftlint:disable all
import Combine
import MatrixRustSDK
class BugReportServiceMock: BugReportServiceProtocol {
@@ -11,18 +10,20 @@ class BugReportServiceMock: BugReportServiceProtocol {
set(value) { underlyingCrashedLastRun = value }
}
var underlyingCrashedLastRun: Bool!
// MARK: - crash
//MARK: - crash
var crashCallsCount = 0
var crashCalled: Bool {
return crashCallsCount > 0
}
var crashClosure: (() -> Void)?
func crash() {
crashCallsCount += 1
crashClosure?()
}
// MARK: - submitBugReport
//MARK: - submitBugReport
var submitBugReportProgressListenerThrowableError: Error?
var submitBugReportProgressListenerCallsCount = 0
@@ -33,6 +34,7 @@ class BugReportServiceMock: BugReportServiceProtocol {
var submitBugReportProgressListenerReceivedInvocations: [(bugReport: BugReport, progressListener: ProgressListener?)] = []
var submitBugReportProgressListenerReturnValue: SubmitBugReportResponse!
var submitBugReportProgressListenerClosure: ((BugReport, ProgressListener?) async throws -> SubmitBugReportResponse)?
func submitBugReport(_ bugReport: BugReport, progressListener: ProgressListener?) async throws -> SubmitBugReportResponse {
if let error = submitBugReportProgressListenerThrowableError {
throw error
@@ -58,7 +60,8 @@ class SessionVerificationControllerProxyMock: SessionVerificationControllerProxy
set(value) { underlyingIsVerified = value }
}
var underlyingIsVerified: Bool!
// MARK: - requestVerification
//MARK: - requestVerification
var requestVerificationCallsCount = 0
var requestVerificationCalled: Bool {
@@ -66,6 +69,7 @@ class SessionVerificationControllerProxyMock: SessionVerificationControllerProxy
}
var requestVerificationReturnValue: Result<Void, SessionVerificationControllerProxyError>!
var requestVerificationClosure: (() async -> Result<Void, SessionVerificationControllerProxyError>)?
func requestVerification() async -> Result<Void, SessionVerificationControllerProxyError> {
requestVerificationCallsCount += 1
if let requestVerificationClosure = requestVerificationClosure {
@@ -74,7 +78,7 @@ class SessionVerificationControllerProxyMock: SessionVerificationControllerProxy
return requestVerificationReturnValue
}
}
// MARK: - startSasVerification
//MARK: - startSasVerification
var startSasVerificationCallsCount = 0
var startSasVerificationCalled: Bool {
@@ -82,6 +86,7 @@ class SessionVerificationControllerProxyMock: SessionVerificationControllerProxy
}
var startSasVerificationReturnValue: Result<Void, SessionVerificationControllerProxyError>!
var startSasVerificationClosure: (() async -> Result<Void, SessionVerificationControllerProxyError>)?
func startSasVerification() async -> Result<Void, SessionVerificationControllerProxyError> {
startSasVerificationCallsCount += 1
if let startSasVerificationClosure = startSasVerificationClosure {
@@ -90,7 +95,7 @@ class SessionVerificationControllerProxyMock: SessionVerificationControllerProxy
return startSasVerificationReturnValue
}
}
// MARK: - approveVerification
//MARK: - approveVerification
var approveVerificationCallsCount = 0
var approveVerificationCalled: Bool {
@@ -98,6 +103,7 @@ class SessionVerificationControllerProxyMock: SessionVerificationControllerProxy
}
var approveVerificationReturnValue: Result<Void, SessionVerificationControllerProxyError>!
var approveVerificationClosure: (() async -> Result<Void, SessionVerificationControllerProxyError>)?
func approveVerification() async -> Result<Void, SessionVerificationControllerProxyError> {
approveVerificationCallsCount += 1
if let approveVerificationClosure = approveVerificationClosure {
@@ -106,7 +112,7 @@ class SessionVerificationControllerProxyMock: SessionVerificationControllerProxy
return approveVerificationReturnValue
}
}
// MARK: - declineVerification
//MARK: - declineVerification
var declineVerificationCallsCount = 0
var declineVerificationCalled: Bool {
@@ -114,6 +120,7 @@ class SessionVerificationControllerProxyMock: SessionVerificationControllerProxy
}
var declineVerificationReturnValue: Result<Void, SessionVerificationControllerProxyError>!
var declineVerificationClosure: (() async -> Result<Void, SessionVerificationControllerProxyError>)?
func declineVerification() async -> Result<Void, SessionVerificationControllerProxyError> {
declineVerificationCallsCount += 1
if let declineVerificationClosure = declineVerificationClosure {
@@ -122,7 +129,7 @@ class SessionVerificationControllerProxyMock: SessionVerificationControllerProxy
return declineVerificationReturnValue
}
}
// MARK: - cancelVerification
//MARK: - cancelVerification
var cancelVerificationCallsCount = 0
var cancelVerificationCalled: Bool {
@@ -130,6 +137,7 @@ class SessionVerificationControllerProxyMock: SessionVerificationControllerProxy
}
var cancelVerificationReturnValue: Result<Void, SessionVerificationControllerProxyError>!
var cancelVerificationClosure: (() async -> Result<Void, SessionVerificationControllerProxyError>)?
func cancelVerification() async -> Result<Void, SessionVerificationControllerProxyError> {
cancelVerificationCallsCount += 1
if let cancelVerificationClosure = cancelVerificationClosure {
@@ -139,5 +147,4 @@ class SessionVerificationControllerProxyMock: SessionVerificationControllerProxy
}
}
}
// swiftlint:enable all
@@ -14,6 +14,15 @@
// limitations under the License.
//
// This protocol is used only as a marker
// to mark protocols that can be auto-mocked by Sourcery
protocol AutoMockable { }
import Foundation
extension Dictionary {
var jsonString: String? {
guard let data = try? JSONSerialization.data(withJSONObject: self,
options: [.fragmentsAllowed, .sortedKeys]) else {
return nil
}
return String(data: data, encoding: .utf8)
}
}
@@ -31,7 +31,8 @@ struct SubmitBugReportResponse: Decodable {
var reportUrl: String
}
protocol BugReportServiceProtocol: AutoMockable {
// sourcery: AutoMockable
protocol BugReportServiceProtocol {
var crashedLastRun: Bool { get }
func crash()
@@ -212,30 +212,15 @@ class ClientProxy: ClientProxyProtocol {
}
}
// swiftlint:disable:next function_parameter_count
func setPusher(pushkey: String,
kind: PusherKind?,
appId: String,
appDisplayName: String,
deviceDisplayName: String,
profileTag: String?,
lang: String,
url: URL?,
format: PushFormat?,
defaultPayload: [AnyHashable: Any]?) async throws {
// let defaultPayloadString = jsonString(from: defaultPayload)
// try await Task.dispatch(on: .global()) {
// try self.client.setPusher(pushkey: pushkey,
// kind: kind?.rustValue,
// appId: appId,
// appDisplayName: appDisplayName,
// deviceDisplayName: deviceDisplayName,
// profileTag: profileTag,
// lang: lang,
// url: url,
// format: format?.rustValue,
// defaultPayload: defaultPayloadString)
// }
func setPusher(with configuration: PusherConfiguration) async throws {
try await Task.dispatch(on: .global()) {
try self.client.setPusher(identifiers: configuration.identifiers,
kind: configuration.kind,
appDisplayName: configuration.appDisplayName,
deviceDisplayName: configuration.deviceDisplayName,
profileTag: configuration.profileTag,
lang: configuration.lang)
}
}
// MARK: Private
@@ -399,17 +384,6 @@ class ClientProxy: ClientProxyProtocol {
fileprivate func didReceiveSlidingSyncUpdate(summary: UpdateSummary) {
callbacks.send(.receivedSyncUpdate)
}
/// Convenience method to get the json string of an Encodable
private func jsonString(from dictionary: [AnyHashable: Any]?) -> String? {
guard let dictionary,
let data = try? JSONSerialization.data(withJSONObject: dictionary,
options: [.fragmentsAllowed]) else {
return nil
}
return String(data: data, encoding: .utf8)
}
}
extension ClientProxy: MediaLoaderProtocol {
@@ -31,37 +31,24 @@ enum ClientProxyError: Error {
case failedLoadingMedia
}
enum PusherKind {
case http
case email
// var rustValue: MatrixRustSDK.PusherKind {
// switch self {
// case .http:
// return .http
// case .email:
// return .email
// }
// }
}
enum PushFormat {
case eventIdOnly
// var rustValue: MatrixRustSDK.PushFormat {
// switch self {
// case .eventIdOnly:
// return .eventIdOnly
// }
// }
}
enum SlidingSyncConstants {
static let initialTimelineLimit: UInt = 0
static let lastMessageTimelineLimit: UInt = 1
static let timelinePrecachingTimelineLimit: UInt = 20
}
/// This struct represents the configuration that we are using to register the application through Pusher to Sygnal
/// using the Matrix Rust SDK, more info here:
/// https://github.com/matrix-org/sygnal
struct PusherConfiguration {
let identifiers: PusherIdentifiers
let kind: PusherKind
let appDisplayName: String
let deviceDisplayName: String
let profileTag: String?
let lang: String
}
protocol ClientProxyProtocol: AnyObject, MediaLoaderProtocol {
var callbacks: PassthroughSubject<ClientProxyCallback, Never> { get }
@@ -97,15 +84,5 @@ protocol ClientProxyProtocol: AnyObject, MediaLoaderProtocol {
func logout() async
// swiftlint:disable:next function_parameter_count
func setPusher(pushkey: String,
kind: PusherKind?,
appId: String,
appDisplayName: String,
deviceDisplayName: String,
profileTag: String?,
lang: String,
url: URL?,
format: PushFormat?,
defaultPayload: [AnyHashable: Any]?) async throws
func setPusher(with configuration: PusherConfiguration) async throws
}
@@ -89,40 +89,13 @@ class MockClientProxy: ClientProxyProtocol {
// no-op
}
var setPusherCalled = false
var setPusherErrorToThrow: Error?
var setPusherPushkey: String?
var setPusherKind: PusherKind?
var setPusherAppId: String?
var setPusherAppDisplayName: String?
var setPusherDeviceDisplayName: String?
var setPusherProfileTag: String?
var setPusherLang: String?
var setPusherUrl: URL?
var setPusherFormat: PushFormat?
var setPusherDefaultPayload: [AnyHashable: Any]?
// swiftlint:disable:next function_parameter_count
func setPusher(pushkey: String,
kind: PusherKind?,
appId: String,
appDisplayName: String,
deviceDisplayName: String,
profileTag: String?,
lang: String,
url: URL?,
format: PushFormat?,
defaultPayload: [AnyHashable: Any]?) async throws {
var setPusherArgument: PusherConfiguration?
var setPusherCalled = false
func setPusher(with configuration: PusherConfiguration) async throws {
if let setPusherErrorToThrow { throw setPusherErrorToThrow }
setPusherCalled = true
setPusherPushkey = pushkey
setPusherKind = kind
setPusherAppId = appId
setPusherAppDisplayName = appDisplayName
setPusherDeviceDisplayName = deviceDisplayName
setPusherProfileTag = profileTag
setPusherLang = lang
setPusherUrl = url
setPusherFormat = format
setPusherDefaultPayload = defaultPayload
setPusherArgument = configuration
}
}
@@ -84,24 +84,25 @@ class NotificationManager: NSObject, NotificationManagerProtocol {
private func setPusher(with deviceToken: Data, clientProxy: ClientProxyProtocol) async -> Bool {
do {
try await clientProxy.setPusher(pushkey: deviceToken.base64EncodedString(),
kind: .http,
appId: ServiceLocator.shared.settings.pusherAppId,
appDisplayName: "\(InfoPlistReader.main.bundleDisplayName) (iOS)",
deviceDisplayName: UIDevice.current.name,
profileTag: pusherProfileTag(),
lang: Bundle.preferredLanguages.first ?? "en",
url: ServiceLocator.shared.settings.pushGatewayBaseURL,
format: .eventIdOnly,
defaultPayload: [
"aps": [
"mutable-content": 1,
"alert": [
"loc-key": "Notification",
"loc-args": []
]
]
])
let defaultPayload = [
"aps": [
"mutable-content": 1,
"alert": [
"loc-key": "Notification",
"loc-args": []
]
]
]
let configuration = await PusherConfiguration(identifiers: .init(pushkey: deviceToken.base64EncodedString(),
appId: ServiceLocator.shared.settings.pusherAppId),
kind: .http(data: .init(url: ServiceLocator.shared.settings.pushGatewayBaseURL.absoluteString,
format: .eventIdOnly,
defaultPayload: defaultPayload.jsonString)),
appDisplayName: "\(InfoPlistReader.main.bundleDisplayName) (iOS)",
deviceDisplayName: UIDevice.current.name,
profileTag: pusherProfileTag(),
lang: Bundle.preferredLanguages.first ?? "en")
try await clientProxy.setPusher(with: configuration)
MXLog.info("[NotificationManager] set pusher succeeded")
return true
} catch {
@@ -144,6 +145,7 @@ extension NotificationManager: UNUserNotificationCenterDelegate {
return [.badge, .sound, .list, .banner]
}
@MainActor
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse) async {
switch response.actionIdentifier {
@@ -39,7 +39,8 @@ struct SessionVerificationEmoji: Hashable {
let description: String
}
protocol SessionVerificationControllerProxyProtocol: AutoMockable {
// sourcery: AutoMockable
protocol SessionVerificationControllerProxyProtocol {
var callbacks: PassthroughSubject<SessionVerificationControllerProxyCallback, Never> { get }
var isVerified: Bool { get }