Merge branch 'develop' into aleksandrs/6786_inactive_sessions_screen

# Conflicts:
#	RiotSwiftUI/Modules/UserSessions/UserSessionDetails/UserSessionDetailsViewModel.swift
This commit is contained in:
Aleksandrs Proskurins
2022-10-05 11:51:08 +03:00
56 changed files with 712 additions and 216 deletions
@@ -125,18 +125,28 @@ enum UserAgentParser {
// Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) ElementNightly/2022091301 Chrome/104.0.5112.102 Electron/20.1.1 Safari/537.36
private static func parseDesktop(_ userAgent: String) -> UserAgent {
var deviceOS: String?
let browserName = browserName(for: userAgent)
let browserInfo = browserInfo(for: userAgent)
if let deviceInfo = findFirstDeviceInfo(in: userAgent) {
let deviceInfoComponents = deviceInfo.components(separatedBy: "; ")
deviceOS = deviceInfoComponents[safe: 1]?.hasPrefix("Android") == true ? deviceInfoComponents[safe: 1] : deviceInfoComponents.first
if deviceInfoComponents[safe: 1]?.hasPrefix("Android") == true {
deviceOS = deviceInfoComponents[safe: 1]
} else if deviceInfoComponents.first == "Macintosh" {
var osFull = deviceInfoComponents[safe: 1]
osFull = osFull?.replacingOccurrences(of: "Intel ", with: "")
osFull = osFull?.replacingOccurrences(of: "Mac OS X", with: "macOS")
osFull = osFull?.replacingOccurrences(of: "_", with: ".")
deviceOS = osFull
} else {
deviceOS = deviceInfoComponents.first
}
}
return UserAgent(deviceType: .desktop,
deviceModel: browserName,
deviceModel: nil,
deviceOS: deviceOS,
clientName: nil,
clientVersion: nil)
clientName: browserInfo.name,
clientVersion: browserInfo.version)
}
// Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36
@@ -164,20 +174,30 @@ enum UserAgentParser {
return nil
}
private static func browserName(for userAgent: String) -> String? {
private static func browserInfo(for userAgent: String) -> (name: String?, version: String?) {
let components = userAgent.components(separatedBy: " ")
if components.last?.hasPrefix("Firefox") == true {
return "Firefox"
} else if components.last?.hasPrefix("Safari") == true
&& components[safe:components.count - 2]?.hasPrefix("Mobile") == true {
let version = components.last?.components(separatedBy: "/").last
return ("Firefox", version)
} else if components.last?.hasPrefix("Safari") == true,
components[safe: components.count - 2]?.hasPrefix("Mobile") == true {
// mobile browser
let possibleBrowserName = components[safe:components.count - 3]?.components(separatedBy: "/").first
return possibleBrowserName == "Version" ? "Safari" : possibleBrowserName
} else if components.last?.hasPrefix("Safari") == true && components[safe:components.count - 2]?.hasPrefix("Version") == true {
return "Safari"
let possibleBrowserComponents = components[safe: components.count - 3]
if possibleBrowserComponents?.hasPrefix("Version") == true {
let version = possibleBrowserComponents?.components(separatedBy: "/").last
return ("Safari", version)
} else {
let components = possibleBrowserComponents?.components(separatedBy: "/")
return (components?.first, components?.last)
}
} else if components.last?.hasPrefix("Safari") == true, components[safe: components.count - 2]?.hasPrefix("Version") == true {
let version = components[safe: components.count - 2]?.components(separatedBy: "/").last
return ("Safari", version)
} else {
// regular browser
return components[safe:components.count - 2]?.components(separatedBy: "/").first
let browserComponent = components[safe: components.count - 2]
let components = browserComponent?.components(separatedBy: "/")
return (components?.first, components?[safe: 1])
}
}
}
@@ -58,8 +58,11 @@ struct UserSessionInfo: Identifiable {
/// Last seen IP location
let lastSeenIPLocation: String?
/// Device name
let deviceName: String?
/// Client name
let clientName: String?
/// Client version
let clientVersion: String?
/// True to indicate that session has been used under `inactiveSessionDurationTreshold` value
let isActive: Bool
@@ -150,7 +150,8 @@ struct UserSessionCardViewPreview: View {
deviceModel: nil,
deviceOS: "iOS 15.5",
lastSeenIPLocation: nil,
deviceName: "My iPhone",
clientName: "Element",
clientVersion: "1.0.0",
isActive: true,
isCurrent: isCurrent)
viewData = UserSessionCardViewData(sessionInfo: sessionInfo)
@@ -53,7 +53,8 @@ enum MockUserSessionDetailsScreenState: MockScreenState, CaseIterable {
deviceModel: nil,
deviceOS: "iOS 15.5",
lastSeenIPLocation: nil,
deviceName: "My iPhone",
clientName: "Element",
clientVersion: "1.0.0",
isActive: true,
isCurrent: true)
case .sessionSectionOnly:
@@ -69,7 +70,8 @@ enum MockUserSessionDetailsScreenState: MockScreenState, CaseIterable {
deviceModel: nil,
deviceOS: "Android 4.0",
lastSeenIPLocation: nil,
deviceName: "My Phone",
clientName: "Element",
clientVersion: "1.0.0",
isActive: true,
isCurrent: false)
}
@@ -113,7 +113,8 @@ class UserSessionDetailsViewModelTests: XCTestCase {
deviceModel: String? = nil,
deviceOS: String? = nil,
lastSeenIPLocation: String? = nil,
deviceName: String? = nil,
clientName: String? = nil,
clientVersion: String? = nil,
isActive: Bool = true,
isCurrent: Bool = true) -> UserSessionInfo {
UserSessionInfo(id: id,
@@ -128,7 +129,8 @@ class UserSessionDetailsViewModelTests: XCTestCase {
deviceModel: deviceModel,
deviceOS: deviceOS,
lastSeenIPLocation: lastSeenIPLocation,
deviceName: deviceName,
clientName: clientName,
clientVersion: clientVersion,
isActive: isActive,
isCurrent: isCurrent)
}
@@ -93,6 +93,12 @@ class UserSessionDetailsViewModel: UserSessionDetailsViewModelType, UserSessionD
deviceSectionItems.append(.init(title: VectorL10n.userSessionDetailsDeviceModel,
value: model))
}
if sessionInfo.deviceType == .web,
let clientName = sessionInfo.clientName,
let clientVersion = sessionInfo.clientVersion {
deviceSectionItems.append(.init(title: VectorL10n.userSessionDetailsDeviceBrowser,
value: "\(clientName) \(clientVersion)"))
}
if let deviceOS = sessionInfo.deviceOS {
deviceSectionItems.append(.init(title: VectorL10n.userSessionDetailsDeviceOs,
value: deviceOS))
@@ -60,7 +60,8 @@ enum MockUserSessionOverviewScreenState: MockScreenState, CaseIterable {
deviceModel: nil,
deviceOS: "iOS 15.5",
lastSeenIPLocation: nil,
deviceName: "My iPhone",
clientName: "Element",
clientVersion: "1.0.0",
isActive: true,
isCurrent: true)
service = MockUserSessionOverviewService()
@@ -77,7 +78,8 @@ enum MockUserSessionOverviewScreenState: MockScreenState, CaseIterable {
deviceModel: nil,
deviceOS: "macOS 12.5.1",
lastSeenIPLocation: nil,
deviceName: "My Mac",
clientName: "Electron",
clientVersion: "20.1.1",
isActive: false,
isCurrent: false)
service = MockUserSessionOverviewService()
@@ -94,7 +96,8 @@ enum MockUserSessionOverviewScreenState: MockScreenState, CaseIterable {
deviceModel: nil,
deviceOS: "macOS 12.5.1",
lastSeenIPLocation: nil,
deviceName: "My Mac",
clientName: "My Mac",
clientVersion: "1.0.0",
isActive: false,
isCurrent: false)
service = MockUserSessionOverviewService(pusherEnabled: enabled)
@@ -111,7 +114,8 @@ enum MockUserSessionOverviewScreenState: MockScreenState, CaseIterable {
deviceModel: nil,
deviceOS: "macOS 12.5.1",
lastSeenIPLocation: nil,
deviceName: "My Mac",
clientName: "My Mac",
clientVersion: "1.0.0",
isActive: false,
isCurrent: false)
service = MockUserSessionOverviewService(pusherEnabled: true, remotelyTogglingPushersAvailable: false)
@@ -29,6 +29,7 @@ class UserSessionOverviewService: UserSessionOverviewServiceProtocol {
private let session: MXSession
private let sessionInfo: UserSessionInfo
private var pusher: MXPusher?
private var localNotificationSettings: [String: Any]?
// MARK: - Setup
@@ -37,19 +38,43 @@ class UserSessionOverviewService: UserSessionOverviewServiceProtocol {
self.sessionInfo = sessionInfo
self.pusherEnabledSubject = CurrentValueSubject(nil)
self.remotelyTogglingPushersAvailableSubject = CurrentValueSubject(false)
checkServerVersions { [weak self] in
self?.checkPusher()
self.localNotificationSettings = session.accountData.localNotificationSettingsForDevice(withId: sessionInfo.id)
if let localNotificationSettings = localNotificationSettings, let isSilenced = localNotificationSettings[kMXAccountDataIsSilencedKey] as? Bool {
remotelyTogglingPushersAvailableSubject.send(true)
pusherEnabledSubject.send(!isSilenced)
}
checkPusher { [weak self] in
guard self?.pusher != nil else {
return
}
self?.checkServerVersions()
}
}
// MARK: - UserSessionOverviewServiceProtocol
func togglePushNotifications() {
guard let pusher = pusher, let enabled = pusher.enabled?.boolValue, self.remotelyTogglingPushersAvailableSubject.value else {
guard let pusher = pusher, let enabled = pusher.enabled?.boolValue else {
updateLocalNotification()
return
}
toggle(pusher, enabled: !enabled)
}
// MARK: - Private
private func toggle(_ pusher: MXPusher, enabled: Bool) {
guard self.remotelyTogglingPushersAvailableSubject.value else {
MXLog.warning("[UserSessionOverviewService] toggle pusher canceled: remotely toggling pushers not available")
return
}
MXLog.debug("[UserSessionOverviewService] remotely toggling pusher")
let data = pusher.data.jsonDictionary() as? [String: Any] ?? [:]
self.session.matrixRestClient.setPusher(pushKey: pusher.pushkey,
@@ -61,22 +86,36 @@ class UserSessionOverviewService: UserSessionOverviewServiceProtocol {
lang: pusher.lang,
data: data,
append: false,
enabled: !enabled) { [weak self] response in
enabled: enabled) { [weak self] response in
guard let self = self else { return }
switch response {
case .success:
self.checkPusher()
case .failure(let error):
MXLog.warning("[UserSessionOverviewService] togglePushNotifications failed due to error: \(error)")
self.pusherEnabledSubject.send(enabled)
MXLog.warning("[UserSessionOverviewService] togglePusher failed due to error: \(error)")
self.pusherEnabledSubject.send(!enabled)
}
}
}
// MARK: - Private
private func checkServerVersions(_ completion: @escaping () -> Void) {
private func updateLocalNotification() {
guard var localNotificationSettings = localNotificationSettings, let isSilenced = localNotificationSettings[kMXAccountDataIsSilencedKey] as? Bool else {
MXLog.warning("[UserSessionOverviewService] updateLocalNotification canceled: \"\(kMXAccountDataIsSilencedKey)\" notification property not found")
return
}
localNotificationSettings[kMXAccountDataIsSilencedKey] = !isSilenced
session.setAccountData(localNotificationSettings, forType: MXAccountData.localNotificationSettingsKeyForDevice(withId: sessionInfo.id)) { [weak self] in
self?.localNotificationSettings = localNotificationSettings
self?.pusherEnabledSubject.send(isSilenced)
} failure: { [weak self] error in
MXLog.warning("[UserSessionOverviewService] updateLocalNotification failed due to error: \(String(describing: error))")
self?.pusherEnabledSubject.send(!isSilenced)
}
}
private func checkServerVersions() {
session.supportedMatrixVersions { [weak self] response in
switch response {
case .success(let versions):
@@ -84,11 +123,10 @@ class UserSessionOverviewService: UserSessionOverviewServiceProtocol {
case .failure(let error):
MXLog.warning("[UserSessionOverviewService] checkServerVersions failed due to error: \(error)")
}
completion()
}
}
private func checkPusher() {
private func checkPusher(_ completion: (() -> Void)? = nil) {
session.matrixRestClient.pushers { [weak self] response in
switch response {
case .success(let pushers):
@@ -96,6 +134,7 @@ class UserSessionOverviewService: UserSessionOverviewServiceProtocol {
case .failure(let error):
MXLog.warning("[UserSessionOverviewService] checkPusher failed due to error: \(error)")
}
completion?()
}
}
@@ -92,13 +92,14 @@ class UserSessionOverviewViewModelTests: XCTestCase {
isVerified: false,
lastSeenIP: "10.0.0.10",
lastSeenTimestamp: Date().timeIntervalSince1970 - 100,
applicationName: "Element",
applicationName: "Element iOS",
applicationVersion: "1.9.7",
applicationURL: nil,
deviceModel: "iPhone XS",
deviceOS: "iOS 15.5",
lastSeenIPLocation: nil,
deviceName: "Mobile",
clientName: "Element",
clientVersion: "1.9.7",
isActive: true,
isCurrent: true)
}
@@ -1,4 +1,4 @@
//
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
@@ -44,7 +44,7 @@ class UserSessionsDataProvider: UserSessionsDataProviderProtocol {
session.crypto.device(withDeviceId: deviceId, ofUser: userId)
}
func accountData(for eventType: String) -> [AnyHashable : Any]? {
func accountData(for eventType: String) -> [AnyHashable: Any]? {
session.accountData.accountData(forEventType: eventType)
}
}
@@ -1,4 +1,4 @@
//
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
@@ -143,7 +143,8 @@ extension UserSessionInfo {
deviceModel: userAgent?.deviceModel,
deviceOS: userAgent?.deviceOS,
lastSeenIPLocation: nil,
deviceName: userAgent?.clientName,
clientName: userAgent?.clientName,
clientVersion: userAgent?.clientVersion,
isActive: isActive,
isCurrent: isCurrent)
}
@@ -89,7 +89,8 @@ class MockUserSessionsOverviewService: UserSessionsOverviewServiceProtocol {
deviceModel: nil,
deviceOS: "iOS 15.5",
lastSeenIPLocation: nil,
deviceName: "My iPhone",
clientName: "Element",
clientVersion: "1.0.0",
isActive: true,
isCurrent: true)
}
@@ -107,7 +108,8 @@ class MockUserSessionsOverviewService: UserSessionsOverviewServiceProtocol {
deviceModel: nil,
deviceOS: "macOS 12.5.1",
lastSeenIPLocation: nil,
deviceName: "My Mac",
clientName: "Electron",
clientVersion: "20.0.0",
isActive: active,
isCurrent: false),
UserSessionInfo(id: "2 verified: \(verified) active: \(active)",
@@ -122,7 +124,8 @@ class MockUserSessionsOverviewService: UserSessionsOverviewServiceProtocol {
deviceModel: nil,
deviceOS: "Windows 10",
lastSeenIPLocation: nil,
deviceName: "My Windows",
clientName: "Firefox",
clientVersion: "39.0",
isActive: active,
isCurrent: false),
UserSessionInfo(id: "3 verified: \(verified) active: \(active)",
@@ -137,7 +140,8 @@ class MockUserSessionsOverviewService: UserSessionsOverviewServiceProtocol {
deviceModel: nil,
deviceOS: "Android 4.0",
lastSeenIPLocation: nil,
deviceName: "My Phone",
clientName: "Element",
clientVersion: "1.0.0",
isActive: active,
isCurrent: false)]
}