Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SP4: Space Settings #1385

Merged
merged 6 commits into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions MatrixSDK/JSONModels/MXEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,12 @@ FOUNDATION_EXPORT NSString *const kMXMessageContentKeyExtensibleAssetMSC3488;
FOUNDATION_EXPORT NSString *const kMXMessageContentKeyExtensibleAssetType;
FOUNDATION_EXPORT NSString *const kMXMessageContentKeyExtensibleAssetTypeUser;

// Join Rules

FOUNDATION_EXPORT NSString *const kMXJoinRulesContentKeyAllow;
FOUNDATION_EXPORT NSString *const kMXJoinRulesContentKeyType;
FOUNDATION_EXPORT NSString *const kMXJoinRulesContentKeyRoomId;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 👍


/**
The internal event state used to handle the different steps of the event sending.
*/
Expand Down
6 changes: 6 additions & 0 deletions MatrixSDK/JSONModels/MXEvent.m
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,12 @@
NSString *const kMXMessageContentKeyExtensibleAssetType = @"type";
NSString *const kMXMessageContentKeyExtensibleAssetTypeUser = @"m.self";

// Join Rules

NSString *const kMXJoinRulesContentKeyAllow = @"allow";
NSString *const kMXJoinRulesContentKeyType = @"type";
NSString *const kMXJoinRulesContentKeyRoomId = @"room_id";

#pragma mark - MXEvent
@interface MXEvent ()
{
Expand Down
32 changes: 25 additions & 7 deletions MatrixSDK/Space/MXRoomAliasAvailabilityChecker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class MXRoomAliasAvailabilityChecker {
return nil
}

let fullAlias = aliasLocalPart.fullLocalAlias(with: session)
let fullAlias = MXTools.fullLocalAlias(from: aliasLocalPart, with: session)
guard aliasLocalPart.filter({ !validAliasCharacters.contains($0) }).count == 0 else {
completion(.invalid)
return nil
Expand All @@ -66,22 +66,40 @@ public class MXRoomAliasAvailabilityChecker {
}
}

public extension String {
public extension MXTools {
/// Generates a full local alias String (e.g. "#my_alias:example.org" for the string "my_alias")
/// - Parameters:
/// - string: based string
/// - session: session used to retrieve the homeserver suffix
/// - Returns:the full local alias String without checking the validity of the alias local part
func fullLocalAlias(with session: MXSession) -> String {
static func fullLocalAlias(from string: String, with session: MXSession) -> String {
guard let homeserverSuffix = session.matrixRestClient.homeserverSuffix else {
return self
return string
}

return "#\(self)\(homeserverSuffix)"
return "#\(string)\(homeserverSuffix)"
}

/// Generates a valid local alias part String by replacing unauthorised characters
/// - Parameters:
/// - string: based string
/// - Returns:a valid local alias part.
func toValidAliasLocalPart() -> String {
return lowercased().replacingOccurrences(of: " ", with: "-").filter { MXRoomAliasAvailabilityChecker.validAliasCharacters.contains($0) }
static func validAliasLocalPart(from string: String) -> String {
return string.lowercased().replacingOccurrences(of: " ", with: "-").filter { MXRoomAliasAvailabilityChecker.validAliasCharacters.contains($0) }
}

/// Extract the valid local alias part String of the string ((e.g. "my_alias" for the string "#my_alias:example.org")
/// - Parameters:
/// - string: based string
/// - Returns:the valid local alias part extracted from the string.
static func extractLocalAliasPart(from string: String) -> String {
var aliasPart = string
while aliasPart.starts(with: "#") {
aliasPart.removeFirst()
}
if let index = aliasPart.firstIndex(of: ":") {
aliasPart.removeSubrange(index ..< aliasPart.endIndex)
}
return aliasPart
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//

import XCTest
import MatrixSDK

class MXRoomAliasAvailabilityCheckerResultTests: XCTestCase {

Expand Down Expand Up @@ -59,19 +60,19 @@ class MXRoomAliasAvailabilityCheckerResultTests: XCTestCase {
}

let aliasLocalPart = UUID().uuidString
var validAliasLocalPart = aliasLocalPart.toValidAliasLocalPart()
var validAliasLocalPart = MXTools.validAliasLocalPart(from: aliasLocalPart)

XCTAssertEqual(aliasLocalPart.lowercased(), validAliasLocalPart)

var fullAlias = validAliasLocalPart.fullLocalAlias(with: session)
var fullAlias = MXTools.fullLocalAlias(from: validAliasLocalPart, with: session)
XCTAssertEqual(fullAlias, "#\(validAliasLocalPart)\(session.matrixRestClient.homeserverSuffix!)")

let invalidAliasLocalPart = "Some Invalid al;i{a|s"
validAliasLocalPart = invalidAliasLocalPart.toValidAliasLocalPart()
validAliasLocalPart = MXTools.validAliasLocalPart(from: invalidAliasLocalPart)

XCTAssertEqual(validAliasLocalPart, "some-invalid-alias")

fullAlias = validAliasLocalPart.fullLocalAlias(with: session)
fullAlias = MXTools.fullLocalAlias(from: validAliasLocalPart, with: session)
XCTAssertEqual(fullAlias, "#\(validAliasLocalPart)\(session.matrixRestClient.homeserverSuffix!)")

expectation?.fulfill()
Expand Down
2 changes: 1 addition & 1 deletion MatrixSDKTests/MXSpaceChildContentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ class MXSpaceChildContentTests: XCTestCase {
}

// Create space with default parameters
bobSession.spaceService.createSpace(withName: spaceName, topic: topic, isPublic: true, aliasLocalPart: "\(spaceName.toValidAliasLocalPart())-\(NSUUID().uuidString)", inviteArray: [aliceSession.myUserId]) { response in
bobSession.spaceService.createSpace(withName: spaceName, topic: topic, isPublic: true, aliasLocalPart: "\(MXTools.validAliasLocalPart(from: spaceName))-\(NSUUID().uuidString)", inviteArray: [aliceSession.myUserId]) { response in
switch response {
case .success(let space):
guard space.room != nil else {
Expand Down
4 changes: 2 additions & 2 deletions MatrixSDKTests/MXSpaceServiceTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class MXSpaceServiceTest: XCTestCase {
for spaceName in spaceNames {

dispatchGroup.enter()
let alias = "\(spaceName.toValidAliasLocalPart())-\(NSUUID().uuidString)"
let alias = "\(MXTools.validAliasLocalPart(from: spaceName))-\(NSUUID().uuidString)"

spaceService.createSpace(withName: spaceName, topic: nil, isPublic: true, aliasLocalPart: alias, inviteArray: nil) { (response) in

Expand Down Expand Up @@ -176,7 +176,7 @@ class MXSpaceServiceTest: XCTestCase {

let expectedSpaceName = "mxSpace \(NSUUID().uuidString)"
let expectedSpaceTopic = "Space topic"
let alias = expectedSpaceName.toValidAliasLocalPart()
let alias = MXTools.validAliasLocalPart(from: expectedSpaceName)

// Create a public space
spaceService.createSpace(withName: expectedSpaceName, topic: expectedSpaceTopic, isPublic: true, aliasLocalPart: alias, inviteArray: nil) { (response) in
Expand Down
1 change: 1 addition & 0 deletions changelog.d/5233.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MXRoomAliasAvailabilityChecker: added extractLocalAliasPart()