Skip to content

Commit

Permalink
Set a proper user agent
Browse files Browse the repository at this point in the history
Fixes: #225
  • Loading branch information
Johennes committed Oct 6, 2022
1 parent e2af796 commit 10bb1c9
Show file tree
Hide file tree
Showing 15 changed files with 291 additions and 55 deletions.
153 changes: 99 additions & 54 deletions ElementX.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
"version" : "1.5.0"
}
},
{
"identity" : "devicekit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/devicekit/DeviceKit",
"state" : {
"revision" : "20e0991f3975916ab0f6d58db84d8bc64f883537",
"version" : "4.7.0"
}
},
{
"identity" : "dtcoretext",
"kind" : "remoteSourceControl",
Expand Down
1 change: 1 addition & 0 deletions ElementX/Sources/Generated/InfoPlist.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ internal enum ElementInfoPlist {
private static let _document = PlistDocument(path: "Info.plist")

internal static let cfBundleDevelopmentRegion: String = _document["CFBundleDevelopmentRegion"]
internal static let cfBundleDisplayName: String = _document["CFBundleDisplayName"]
internal static let cfBundleExecutable: String = _document["CFBundleExecutable"]
internal static let cfBundleIdentifier: String = _document["CFBundleIdentifier"]
internal static let cfBundleInfoDictionaryVersion: String = _document["CFBundleInfoDictionaryVersion"]
Expand Down
14 changes: 14 additions & 0 deletions ElementX/Sources/Other/Extensions/Bundle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@
import Foundation

public extension Bundle {
/// Returns the real app bundle.
/// Can also be used in app extensions.
static let app: Bundle = {
let bundle = main
if bundle.bundleURL.pathExtension == "appex" {
// Peel off two directory levels - MY_APP.app/PlugIns/MY_APP_EXTENSION.appex
let url = bundle.bundleURL.deletingLastPathComponent().deletingLastPathComponent()
if let otherBundle = Bundle(url: url) {
return otherBundle
}
}
return bundle
}()

/// Get an lproj language bundle from the receiver bundle.
/// - Parameter language: The language to try to load.
/// - Returns: The lproj bundle if found otherwise nil.
Expand Down
11 changes: 11 additions & 0 deletions ElementX/Sources/Other/Extensions/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,15 @@ extension String {
}
return abs(hash)
}

func asciified() -> String {
guard !canBeConverted(to: .ascii) else {
return self
}
let mutableString = NSMutableString(string: self)
guard CFStringTransform(mutableString, nil, "Any-Latin; Latin-ASCII; [:^ASCII:] Remove" as CFString, false) else {
return self
}
return mutableString as String
}
}
71 changes: 71 additions & 0 deletions ElementX/Sources/Other/UserAgentFactory.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//
// Copyright 2022 New Vector 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.
//

import Foundation
import UIKit

#if !os(OSX)
import DeviceKit
#endif

final class UserAgentFactory {
class func makeASCIIUserAgent() -> String? {
makeUserAgent()?.asciified()
}

private class func makeUserAgent() -> String? {
let clientName = ElementInfoPlist.cfBundleDisplayName
let clientVersion = ElementInfoPlist.cfBundleShortVersionString

#if os(iOS)
return String(
format: "%@/%@ (%@; iOS %@; Scale/%0.2f)",
clientName,
clientVersion,
Device.current.safeDescription,
UIDevice.current.systemVersion,
UIScreen.main.scale
)
#elseif os(tvOS)
return String(
format: "%@/%@ (%@; tvOS %@; Scale/%0.2f)",
clientName,
clientVersion,
Device.current.safeDescription,
UIDevice.current.systemVersion,
UIScreen.main.scale
)
#elseif os(watchOS)
return String(
format: "%@/%@ (%@; watchOS %@; Scale/%0.2f)",
clientName,
clientVersion,
Device.current.safeDescription,
WKInterfaceDevice.current.systemVersion,
WKInterfaceDevice.currentDevice.screenScale
)
#elseif os(OSX)
return String(
format: "%@/%@ (Mac; Mac OS X %@)",
clientName,
clientVersion,
NSProcessInfo.processInfo.operatingSystemVersionString
)
#else
return nil
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ class UserSessionStore: UserSessionStoreProtocol {
let builder = ClientBuilder()
.basePath(path: baseDirectory.path)
.username(username: credentials.userID)

.userAgent(userAgent: UserAgentFactory.makeASCIIUserAgent())

do {
let client: Client = try await Task.dispatch(on: .global()) {
let client = try builder.build()
Expand Down
2 changes: 2 additions & 0 deletions ElementX/SupportingFiles/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleDisplayName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
Expand Down
2 changes: 2 additions & 0 deletions ElementX/SupportingFiles/target.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ targets:
path: ../SupportingFiles/Info.plist
properties:
UILaunchStoryboardName: LaunchScreen
CFBundleDisplayName: $(PRODUCT_NAME)
CFBundleShortVersionString: $(MARKETING_VERSION)
CFBundleVersion: $(CURRENT_PROJECT_VERSION)
UISupportedInterfaceOrientations: [
Expand Down Expand Up @@ -97,6 +98,7 @@ targets:
- package: DesignKit
- package: AnalyticsEvents
- package: AppAuth
- package: DeviceKit
- package: DTCoreText
- package: KeychainAccess
- package: Kingfisher
Expand Down
2 changes: 2 additions & 0 deletions IntegrationTests/SupportingFiles/target.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ targets:
- target: ElementX
- package: MatrixRustSDK
linkType: static
- package: DeviceKit
linkType: static
- package: DTCoreText
linkType: static
- package: KeychainAccess
Expand Down
2 changes: 2 additions & 0 deletions UITests/SupportingFiles/target.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ targets:
linkType: static
- package: AppAuth
linkType: static
- package: DeviceKit
linkType: static
- package: DTCoreText
linkType: static
- package: KeychainAccess
Expand Down
37 changes: 37 additions & 0 deletions UnitTests/Sources/StringTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// Copyright 2022 New Vector 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.
//

import XCTest

@testable import ElementX

class StringTests: XCTestCase {
func testAsciiStaysAscii() {
XCTAssertEqual("johnny".asciified(), "johnny")
}

func testÖBecomesO() {
XCTAssertEqual("jöhnny".asciified(), "johnny")
}

func testÅBecomesA() {
XCTAssertEqual("jåhnny".asciified(), "jahnny")
}

func test1️⃣2️⃣3️⃣Becomes123() {
XCTAssertEqual("1️⃣2️⃣3️⃣".asciified(), "123")
}
}
35 changes: 35 additions & 0 deletions UnitTests/Sources/UserAgentFactoryTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// Copyright 2022 New Vector 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.
//

import XCTest

@testable import ElementX

class UserAgentFactoryTests: XCTestCase {
func testIsNotNil() {
XCTAssertNotNil(UserAgentFactory.makeASCIIUserAgent())
}

func testContainsClientName() {
let userAgent = UserAgentFactory.makeASCIIUserAgent()
XCTAssert(userAgent?.contains(ElementInfoPlist.cfBundleDisplayName) == true, "\(userAgent ?? "nil") does not contain client name")
}

func testContainsClientVersion() {
let userAgent = UserAgentFactory.makeASCIIUserAgent()
XCTAssert(userAgent?.contains(ElementInfoPlist.cfBundleShortVersionString) == true, "\(userAgent ?? "nil") does not contain client version")
}
}
1 change: 1 addition & 0 deletions changelog.d/225.change
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Set a proper user agent
3 changes: 3 additions & 0 deletions project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ packages:
AppAuth:
url: https://github.com/openid/AppAuth-iOS
majorVersion: 1.5.0
DeviceKit:
url: https://github.com/devicekit/DeviceKit
majorVersion: 4.7.0
DTCoreText:
url: https://github.com/Cocoanetics/DTCoreText
majorVersion: 1.6.26
Expand Down

0 comments on commit 10bb1c9

Please sign in to comment.