From a8be5ed6b02b942e2059cdb3f3ce4e01ad26817c Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 16 Feb 2021 14:50:50 +0100 Subject: [PATCH] Create FederationModule --- .../sdk/api/federation/FederationService.kt | 24 +++++++++ .../sdk/api/federation/FederationVersion.kt | 31 ++++++++++++ .../matrix/android/sdk/api/session/Session.kt | 6 +++ .../federation/DefaultFederationService.kt | 29 +++++++++++ .../sdk/internal/federation/FederationAPI.kt | 27 ++++++++++ .../federation/FederationGetVersionResult.kt | 37 ++++++++++++++ .../internal/federation/FederationModule.kt | 49 +++++++++++++++++++ .../federation/GetFederationVersionTask.kt | 40 +++++++++++++++ .../sdk/internal/network/NetworkConstants.kt | 3 ++ .../sdk/internal/session/DefaultSession.kt | 4 ++ .../sdk/internal/session/SessionComponent.kt | 2 + 11 files changed, 252 insertions(+) create mode 100644 matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/federation/FederationService.kt create mode 100644 matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/federation/FederationVersion.kt create mode 100644 matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/federation/DefaultFederationService.kt create mode 100644 matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/federation/FederationAPI.kt create mode 100644 matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/federation/FederationGetVersionResult.kt create mode 100644 matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/federation/FederationModule.kt create mode 100644 matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/federation/GetFederationVersionTask.kt diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/federation/FederationService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/federation/FederationService.kt new file mode 100644 index 0000000000..0761ef8d21 --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/federation/FederationService.kt @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2021 The Matrix.org Foundation C.I.C. + * + * 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.federation + +interface FederationService { + /** + * Get information about the homeserver + */ + suspend fun getFederationVersion(): FederationVersion +} diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/federation/FederationVersion.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/federation/FederationVersion.kt new file mode 100644 index 0000000000..2ed3f848b6 --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/federation/FederationVersion.kt @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2021 The Matrix.org Foundation C.I.C. + * + * 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.federation + +/** + * Ref: https://matrix.org/docs/spec/server_server/latest#get-matrix-federation-v1-version + */ +data class FederationVersion( + /** + * Arbitrary name that identify this implementation. + */ + val name: String?, + /** + * Version of this implementation. The version format depends on the implementation. + */ + val version: String? +) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/Session.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/Session.kt index 039025e0df..86ac0056e2 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/Session.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/Session.kt @@ -21,6 +21,7 @@ import androidx.lifecycle.LiveData import okhttp3.OkHttpClient import org.matrix.android.sdk.api.auth.data.SessionParams import org.matrix.android.sdk.api.failure.GlobalError +import org.matrix.android.sdk.api.federation.FederationService import org.matrix.android.sdk.api.pushrules.PushRuleService import org.matrix.android.sdk.api.session.account.AccountService import org.matrix.android.sdk.api.session.accountdata.AccountDataService @@ -213,6 +214,11 @@ interface Session : */ fun searchService(): SearchService + /** + * Returns the federation service associated with the session + */ + fun federationService(): FederationService + /** * Returns the third party service associated with the session */ diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/federation/DefaultFederationService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/federation/DefaultFederationService.kt new file mode 100644 index 0000000000..862a4855cc --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/federation/DefaultFederationService.kt @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2021 The Matrix.org Foundation C.I.C. + * + * 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.federation + +import org.matrix.android.sdk.api.federation.FederationService +import org.matrix.android.sdk.api.federation.FederationVersion +import javax.inject.Inject + +internal class DefaultFederationService @Inject constructor( + private val getFederationVersionTask: GetFederationVersionTask +) : FederationService { + override suspend fun getFederationVersion(): FederationVersion { + return getFederationVersionTask.execute(Unit) + } +} diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/federation/FederationAPI.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/federation/FederationAPI.kt new file mode 100644 index 0000000000..1816616336 --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/federation/FederationAPI.kt @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2021 The Matrix.org Foundation C.I.C. + * + * 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.federation + +import org.matrix.android.sdk.internal.network.NetworkConstants +import retrofit2.Call +import retrofit2.http.GET + +internal interface FederationAPI { + @GET(NetworkConstants.URI_FEDERATION_PATH + "version") + fun getVersion(): Call +} diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/federation/FederationGetVersionResult.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/federation/FederationGetVersionResult.kt new file mode 100644 index 0000000000..4e436c0da2 --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/federation/FederationGetVersionResult.kt @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2021 The Matrix.org Foundation C.I.C. + * + * 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.federation + +import com.squareup.moshi.Json +import com.squareup.moshi.JsonClass + +/** + * Ref: https://matrix.org/docs/spec/server_server/latest#get-matrix-federation-v1-version + */ +@JsonClass(generateAdapter = true) +internal data class FederationGetVersionResult( + /** + * Arbitrary name that identify this implementation. + */ + @Json(name = "name") + val name: String?, + /** + * Version of this implementation. The version format depends on the implementation. + */ + @Json(name = "version") + val version: String? +) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/federation/FederationModule.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/federation/FederationModule.kt new file mode 100644 index 0000000000..320bf1d445 --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/federation/FederationModule.kt @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2021 The Matrix.org Foundation C.I.C. + * + * 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.federation + +import dagger.Binds +import dagger.Lazy +import dagger.Module +import dagger.Provides +import okhttp3.OkHttpClient +import org.matrix.android.sdk.api.auth.data.SessionParams +import org.matrix.android.sdk.api.federation.FederationService +import org.matrix.android.sdk.internal.di.Unauthenticated +import org.matrix.android.sdk.internal.network.RetrofitFactory + +@Module +internal abstract class FederationModule { + + @Module + companion object { + @Provides + @JvmStatic + fun providesFederationAPI(@Unauthenticated okHttpClient: Lazy, + sessionParams: SessionParams, + retrofitFactory: RetrofitFactory): FederationAPI { + return retrofitFactory.create(okHttpClient, sessionParams.homeServerUrl).create(FederationAPI::class.java) + } + } + + @Binds + abstract fun bindFederationService(service: DefaultFederationService): FederationService + + @Binds + abstract fun bindGetFederationVersionTask(task: DefaultGetFederationVersionTask): GetFederationVersionTask +} diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/federation/GetFederationVersionTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/federation/GetFederationVersionTask.kt new file mode 100644 index 0000000000..6000ff6d31 --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/federation/GetFederationVersionTask.kt @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2021 The Matrix.org Foundation C.I.C. + * + * 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.federation + +import org.matrix.android.sdk.api.federation.FederationVersion +import org.matrix.android.sdk.internal.network.executeRequest +import org.matrix.android.sdk.internal.task.Task +import javax.inject.Inject + +internal interface GetFederationVersionTask : Task + +internal class DefaultGetFederationVersionTask @Inject constructor( + private val federationAPI: FederationAPI +) : GetFederationVersionTask { + + override suspend fun execute(params: Unit): FederationVersion { + val result = executeRequest(null) { + apiCall = federationAPI.getVersion() + } + + return FederationVersion( + name = result.name, + version = result.version + ) + } +} diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/network/NetworkConstants.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/network/NetworkConstants.kt index a14c86efb6..99c12255cd 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/network/NetworkConstants.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/network/NetworkConstants.kt @@ -36,4 +36,7 @@ internal object NetworkConstants { // Integration const val URI_INTEGRATION_MANAGER_PATH = "_matrix/integrations/v1/" + + // Federation + const val URI_FEDERATION_PATH = "_matrix/federation/v1/" } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt index 890f3a6ac3..06bb4bd929 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt @@ -22,6 +22,7 @@ import io.realm.RealmConfiguration import okhttp3.OkHttpClient import org.matrix.android.sdk.api.auth.data.SessionParams import org.matrix.android.sdk.api.failure.GlobalError +import org.matrix.android.sdk.api.federation.FederationService import org.matrix.android.sdk.api.pushrules.PushRuleService import org.matrix.android.sdk.api.session.InitialSyncProgressService import org.matrix.android.sdk.api.session.Session @@ -88,6 +89,7 @@ internal class DefaultSession @Inject constructor( private val groupService: Lazy, private val userService: Lazy, private val filterService: Lazy, + private val federationService: Lazy, private val cacheService: Lazy, private val signOutService: Lazy, private val pushRuleService: Lazy, @@ -260,6 +262,8 @@ internal class DefaultSession @Inject constructor( override fun searchService(): SearchService = searchService.get() + override fun federationService(): FederationService = federationService.get() + override fun thirdPartyService(): ThirdPartyService = thirdPartyService.get() override fun getOkHttpClient(): OkHttpClient { diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionComponent.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionComponent.kt index 9279c5c97a..7e1e3d0f70 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionComponent.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionComponent.kt @@ -27,6 +27,7 @@ import org.matrix.android.sdk.internal.crypto.SendGossipWorker import org.matrix.android.sdk.internal.crypto.crosssigning.UpdateTrustWorker import org.matrix.android.sdk.internal.crypto.verification.SendVerificationMessageWorker import org.matrix.android.sdk.internal.di.MatrixComponent +import org.matrix.android.sdk.internal.federation.FederationModule import org.matrix.android.sdk.internal.network.NetworkConnectivityChecker import org.matrix.android.sdk.internal.session.account.AccountModule import org.matrix.android.sdk.internal.session.cache.CacheModule @@ -87,6 +88,7 @@ import org.matrix.android.sdk.internal.util.MatrixCoroutineDispatchers AccountDataModule::class, ProfileModule::class, AccountModule::class, + FederationModule::class, CallModule::class, SearchModule::class, ThirdPartyModule::class