mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-05 11:58:32 +00:00
Fixes vector-im/element-x-ios/issues/117 - Event permalink timeline action
* moved NSRegularExpression outside of the AttributedString builder into the MatrixEntityRegex * fixed eventId v3 regex * added permalink builders for users, room identifiers and aliases, and events * added timeline item permalink contextual menu actions and error alerts * added an app wide ServiceLocator and moved the top level userIndicatorPresenter to it. * added URL constructor that takes a StaticString and returns an non-optional * Include Unit and UI tests in the swiftlint search paths
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
//
|
||||
// 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
|
||||
|
||||
extension URL {
|
||||
init(staticString: StaticString) {
|
||||
guard let url = URL(string: "\(staticString)") else {
|
||||
fatalError("The static string used to create this URL is invalid")
|
||||
}
|
||||
|
||||
self = url
|
||||
}
|
||||
}
|
||||
@@ -22,24 +22,6 @@ struct AttributedStringBuilder: AttributedStringBuilderProtocol {
|
||||
private let temporaryCodeBlockMarkingColor = UIColor.cyan
|
||||
private let linkColor = UIColor.blue
|
||||
|
||||
private let userIdDetector: NSRegularExpression
|
||||
private let roomIdDetector: NSRegularExpression
|
||||
private let eventIdDetector: NSRegularExpression
|
||||
private let roomAliasDetector: NSRegularExpression
|
||||
private let linkDetector: NSDataDetector
|
||||
|
||||
init() {
|
||||
do {
|
||||
userIdDetector = try NSRegularExpression(pattern: MatrixEntityRegex.userId.rawValue, options: .caseInsensitive)
|
||||
roomIdDetector = try NSRegularExpression(pattern: MatrixEntityRegex.roomId.rawValue, options: .caseInsensitive)
|
||||
eventIdDetector = try NSRegularExpression(pattern: MatrixEntityRegex.eventId.rawValue, options: .caseInsensitive)
|
||||
roomAliasDetector = try NSRegularExpression(pattern: MatrixEntityRegex.roomAlias.rawValue, options: .caseInsensitive)
|
||||
linkDetector = try NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
|
||||
} catch {
|
||||
fatalError()
|
||||
}
|
||||
}
|
||||
|
||||
func fromPlain(_ string: String?) async -> AttributedString? {
|
||||
await Task.detached {
|
||||
fromPlain(string)
|
||||
@@ -181,11 +163,11 @@ struct AttributedStringBuilder: AttributedStringBuilderProtocol {
|
||||
let string = attributedString.string
|
||||
let range = NSRange(location: 0, length: attributedString.string.count)
|
||||
|
||||
var matches = userIdDetector.matches(in: string, options: [], range: range)
|
||||
matches.append(contentsOf: roomIdDetector.matches(in: string, options: [], range: range))
|
||||
matches.append(contentsOf: eventIdDetector.matches(in: string, options: [], range: range))
|
||||
matches.append(contentsOf: roomAliasDetector.matches(in: string, options: [], range: range))
|
||||
matches.append(contentsOf: linkDetector.matches(in: string, options: [], range: range))
|
||||
var matches = MatrixEntityRegex.userIdentifierRegex.matches(in: string, options: [], range: range)
|
||||
matches.append(contentsOf: MatrixEntityRegex.roomIdentifierRegex.matches(in: string, options: [], range: range))
|
||||
matches.append(contentsOf: MatrixEntityRegex.eventIdentifierRegex.matches(in: string, options: [], range: range))
|
||||
matches.append(contentsOf: MatrixEntityRegex.roomAliasRegex.matches(in: string, options: [], range: range))
|
||||
matches.append(contentsOf: MatrixEntityRegex.linkRegex.matches(in: string, options: [], range: range))
|
||||
|
||||
guard matches.count > 0 else {
|
||||
return
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
// https://spec.matrix.org/latest/appendices/#identifier-grammar
|
||||
enum MatrixEntityRegex: String {
|
||||
case homeserver
|
||||
case userId
|
||||
@@ -34,7 +35,56 @@ enum MatrixEntityRegex: String {
|
||||
case .roomId:
|
||||
return "![A-Z0-9]+:" + MatrixEntityRegex.homeserver.rawValue
|
||||
case .eventId:
|
||||
return "\\$[A-Z0-9]+:" + MatrixEntityRegex.homeserver.rawValue
|
||||
return "\\$[A-Z0-9\\/+]+"
|
||||
}
|
||||
}
|
||||
|
||||
// swiftlint:disable force_try
|
||||
static var homeserverRegex = try! NSRegularExpression(pattern: MatrixEntityRegex.homeserver.rawValue, options: .caseInsensitive)
|
||||
static var userIdentifierRegex = try! NSRegularExpression(pattern: MatrixEntityRegex.userId.rawValue, options: .caseInsensitive)
|
||||
static var roomAliasRegex = try! NSRegularExpression(pattern: MatrixEntityRegex.roomAlias.rawValue, options: .caseInsensitive)
|
||||
static var roomIdentifierRegex = try! NSRegularExpression(pattern: MatrixEntityRegex.roomId.rawValue, options: .caseInsensitive)
|
||||
static var eventIdentifierRegex = try! NSRegularExpression(pattern: MatrixEntityRegex.eventId.rawValue, options: .caseInsensitive)
|
||||
static var linkRegex = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
|
||||
// swiftlint:enable force_try
|
||||
|
||||
static func isMatrixHomeserver(_ homeserver: String) -> Bool {
|
||||
guard let match = userIdentifierRegex.firstMatch(in: homeserver, range: .init(location: 0, length: homeserver.count)) else {
|
||||
return false
|
||||
}
|
||||
|
||||
return match.range.length == homeserver.count
|
||||
}
|
||||
|
||||
static func isMatrixUserIdentifier(_ identifier: String) -> Bool {
|
||||
guard let match = userIdentifierRegex.firstMatch(in: identifier, range: .init(location: 0, length: identifier.count)) else {
|
||||
return false
|
||||
}
|
||||
|
||||
return match.range.length == identifier.count
|
||||
}
|
||||
|
||||
static func isMatrixRoomAlias(_ alias: String) -> Bool {
|
||||
guard let match = roomAliasRegex.firstMatch(in: alias, range: .init(location: 0, length: alias.count)) else {
|
||||
return false
|
||||
}
|
||||
|
||||
return match.range.length == alias.count
|
||||
}
|
||||
|
||||
static func isMatrixRoomIdentifier(_ identifier: String) -> Bool {
|
||||
guard let match = roomIdentifierRegex.firstMatch(in: identifier, range: .init(location: 0, length: identifier.count)) else {
|
||||
return false
|
||||
}
|
||||
|
||||
return match.range.length == identifier.count
|
||||
}
|
||||
|
||||
static func isMatrixEventIdentifier(_ identifier: String) -> Bool {
|
||||
guard let match = eventIdentifierRegex.firstMatch(in: identifier, range: .init(location: 0, length: identifier.count)) else {
|
||||
return false
|
||||
}
|
||||
|
||||
return match.range.length == identifier.count
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
//
|
||||
// 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
|
||||
|
||||
enum PermalinkBuilderError: Error {
|
||||
case invalidUserIdentifier
|
||||
case invalidRoomIdentifier
|
||||
case invalidRoomAlias
|
||||
case invalidEventIdentifier
|
||||
case failedConstructingURL
|
||||
case failedAddingPercentEncoding
|
||||
}
|
||||
|
||||
enum PermalinkBuilder {
|
||||
static var uriComponentCharacterSet: CharacterSet = {
|
||||
var charset = CharacterSet.alphanumerics
|
||||
charset.insert(charactersIn: "-_.!~*'()")
|
||||
return charset
|
||||
}()
|
||||
|
||||
static func permalinkTo(userIdentifier: String) throws -> URL {
|
||||
guard MatrixEntityRegex.isMatrixUserIdentifier(userIdentifier) else {
|
||||
throw PermalinkBuilderError.invalidUserIdentifier
|
||||
}
|
||||
|
||||
let urlString = "\(BuildSettings.permalinkBaseURL)/#/\(userIdentifier)"
|
||||
|
||||
guard let url = URL(string: urlString) else {
|
||||
throw PermalinkBuilderError.failedConstructingURL
|
||||
}
|
||||
|
||||
return url
|
||||
}
|
||||
|
||||
static func permalinkTo(roomIdentifier: String) throws -> URL {
|
||||
guard MatrixEntityRegex.isMatrixRoomIdentifier(roomIdentifier) else {
|
||||
throw PermalinkBuilderError.invalidRoomIdentifier
|
||||
}
|
||||
|
||||
return try permalinkTo(roomIdentifierOrAlias: roomIdentifier)
|
||||
}
|
||||
|
||||
static func permalinkTo(roomAlias: String) throws -> URL {
|
||||
guard MatrixEntityRegex.isMatrixRoomAlias(roomAlias) else {
|
||||
throw PermalinkBuilderError.invalidRoomAlias
|
||||
}
|
||||
|
||||
return try permalinkTo(roomIdentifierOrAlias: roomAlias)
|
||||
}
|
||||
|
||||
static func permalinkTo(eventIdentifier: String, roomIdentifier: String) throws -> URL {
|
||||
guard MatrixEntityRegex.isMatrixEventIdentifier(eventIdentifier) else {
|
||||
throw PermalinkBuilderError.invalidEventIdentifier
|
||||
}
|
||||
guard MatrixEntityRegex.isMatrixRoomIdentifier(roomIdentifier) else {
|
||||
throw PermalinkBuilderError.invalidRoomIdentifier
|
||||
}
|
||||
|
||||
guard let roomId = roomIdentifier.addingPercentEncoding(withAllowedCharacters: uriComponentCharacterSet),
|
||||
let eventId = eventIdentifier.addingPercentEncoding(withAllowedCharacters: uriComponentCharacterSet) else {
|
||||
throw PermalinkBuilderError.failedAddingPercentEncoding
|
||||
}
|
||||
|
||||
let urlString = "\(BuildSettings.permalinkBaseURL)/#/\(roomId)/\(eventId)"
|
||||
|
||||
guard let url = URL(string: urlString) else {
|
||||
throw PermalinkBuilderError.failedConstructingURL
|
||||
}
|
||||
|
||||
return url
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
private static func permalinkTo(roomIdentifierOrAlias: String) throws -> URL {
|
||||
guard let identifier = roomIdentifierOrAlias.addingPercentEncoding(withAllowedCharacters: uriComponentCharacterSet) else {
|
||||
throw PermalinkBuilderError.failedAddingPercentEncoding
|
||||
}
|
||||
|
||||
let urlString = "\(BuildSettings.permalinkBaseURL)/#/\(identifier)"
|
||||
|
||||
guard let url = URL(string: urlString) else {
|
||||
throw PermalinkBuilderError.failedConstructingURL
|
||||
}
|
||||
|
||||
return url
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user