-
Notifications
You must be signed in to change notification settings - Fork 767
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
262 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/alias/RoomAliasError.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright (c) 2020 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. | ||
*/ | ||
|
||
package org.matrix.android.sdk.api.session.room.alias | ||
|
||
sealed class RoomAliasError : Throwable() { | ||
object AliasEmpty : RoomAliasError() | ||
object AliasNotAvailable : RoomAliasError() | ||
object AliasInvalid : RoomAliasError() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...n/java/org/matrix/android/sdk/internal/session/room/alias/RoomAliasAvailabilityChecker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright (c) 2020 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. | ||
*/ | ||
|
||
package org.matrix.android.sdk.internal.session.room.alias | ||
|
||
import org.greenrobot.eventbus.EventBus | ||
import org.matrix.android.sdk.api.failure.Failure | ||
import org.matrix.android.sdk.api.session.room.alias.RoomAliasError | ||
import org.matrix.android.sdk.internal.di.UserId | ||
import org.matrix.android.sdk.internal.network.executeRequest | ||
import org.matrix.android.sdk.internal.session.directory.DirectoryAPI | ||
import javax.inject.Inject | ||
|
||
internal class RoomAliasAvailabilityChecker @Inject constructor( | ||
@UserId private val userId: String, | ||
private val directoryAPI: DirectoryAPI, | ||
private val eventBus: EventBus | ||
) { | ||
@Throws(RoomAliasError::class) | ||
suspend fun check(aliasLocalPart: String?) { | ||
if (aliasLocalPart.isNullOrEmpty()) { | ||
throw RoomAliasError.AliasEmpty | ||
} | ||
// Check alias availability | ||
val fullAlias = aliasLocalPart.toFullAlias(userId) | ||
try { | ||
executeRequest<RoomAliasDescription>(eventBus) { | ||
apiCall = directoryAPI.getRoomIdByAlias(fullAlias) | ||
} | ||
} catch (throwable: Throwable) { | ||
if (throwable is Failure.ServerError && throwable.httpCode == 404) { | ||
// This is a 404, so the alias is available: nominal case | ||
null | ||
} else { | ||
// Other error, propagate it | ||
throw throwable | ||
} | ||
} | ||
?.let { | ||
// Alias already exists: error case | ||
throw RoomAliasError.AliasNotAvailable | ||
} | ||
} | ||
|
||
companion object { | ||
internal fun String.toFullAlias(userId: String) = "#" + this + ":" + userId.substringAfter(":") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
.../src/main/java/im/vector/app/features/roomdirectory/createroom/RoomAliasErrorFormatter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (c) 2020 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. | ||
*/ | ||
|
||
package im.vector.app.features.roomdirectory.createroom | ||
|
||
import im.vector.app.R | ||
import im.vector.app.core.resources.StringProvider | ||
import org.matrix.android.sdk.api.session.room.alias.RoomAliasError | ||
import javax.inject.Inject | ||
|
||
class RoomAliasErrorFormatter @Inject constructor( | ||
private val stringProvider: StringProvider | ||
) { | ||
fun format(roomAliasError: RoomAliasError?): String? { | ||
return when (roomAliasError) { | ||
is RoomAliasError.AliasEmpty -> R.string.create_room_alias_empty | ||
is RoomAliasError.AliasNotAvailable -> R.string.create_room_alias_already_in_use | ||
is RoomAliasError.AliasInvalid -> R.string.create_room_alias_invalid | ||
else -> null | ||
} | ||
?.let { stringProvider.getString(it) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.