mirror of
https://github.com/element-hq/element-android
synced 2024-11-24 18:35:40 +03:00
Rework: create a MediaModule
This commit is contained in:
parent
2cc5d46cd3
commit
cafe86e675
6 changed files with 84 additions and 17 deletions
|
@ -40,6 +40,7 @@ import org.matrix.android.sdk.internal.session.group.GroupModule
|
|||
import org.matrix.android.sdk.internal.session.homeserver.HomeServerCapabilitiesModule
|
||||
import org.matrix.android.sdk.internal.session.identity.IdentityModule
|
||||
import org.matrix.android.sdk.internal.session.integrationmanager.IntegrationManagerModule
|
||||
import org.matrix.android.sdk.internal.session.media.MediaModule
|
||||
import org.matrix.android.sdk.internal.session.openid.OpenIdModule
|
||||
import org.matrix.android.sdk.internal.session.profile.ProfileModule
|
||||
import org.matrix.android.sdk.internal.session.pushers.AddHttpPusherWorker
|
||||
|
@ -75,6 +76,7 @@ import org.matrix.android.sdk.internal.util.MatrixCoroutineDispatchers
|
|||
GroupModule::class,
|
||||
ContentModule::class,
|
||||
CacheModule::class,
|
||||
MediaModule::class,
|
||||
CryptoModule::class,
|
||||
PushersModule::class,
|
||||
OpenIdModule::class,
|
||||
|
|
|
@ -22,19 +22,12 @@ import retrofit2.Call
|
|||
import retrofit2.http.GET
|
||||
|
||||
internal interface CapabilitiesAPI {
|
||||
|
||||
/**
|
||||
* Request the homeserver capabilities
|
||||
*/
|
||||
@GET(NetworkConstants.URI_API_PREFIX_PATH_R0 + "capabilities")
|
||||
fun getCapabilities(): Call<GetCapabilitiesResult>
|
||||
|
||||
/**
|
||||
* Request the upload capabilities
|
||||
*/
|
||||
@GET(NetworkConstants.URI_API_MEDIA_PREFIX_PATH_R0 + "config")
|
||||
fun getUploadCapabilities(): Call<GetUploadCapabilitiesResult>
|
||||
|
||||
/**
|
||||
* Request the versions
|
||||
*/
|
||||
|
|
|
@ -29,6 +29,8 @@ import org.matrix.android.sdk.internal.di.SessionDatabase
|
|||
import org.matrix.android.sdk.internal.di.UserId
|
||||
import org.matrix.android.sdk.internal.network.executeRequest
|
||||
import org.matrix.android.sdk.internal.session.integrationmanager.IntegrationManagerConfigExtractor
|
||||
import org.matrix.android.sdk.internal.session.media.GetMediaConfigResult
|
||||
import org.matrix.android.sdk.internal.session.media.MediaAPI
|
||||
import org.matrix.android.sdk.internal.task.Task
|
||||
import org.matrix.android.sdk.internal.util.awaitTransaction
|
||||
import org.matrix.android.sdk.internal.wellknown.GetWellknownTask
|
||||
|
@ -40,6 +42,7 @@ internal interface GetHomeServerCapabilitiesTask : Task<Unit, Unit>
|
|||
|
||||
internal class DefaultGetHomeServerCapabilitiesTask @Inject constructor(
|
||||
private val capabilitiesAPI: CapabilitiesAPI,
|
||||
private val mediaAPI: MediaAPI,
|
||||
@SessionDatabase private val monarchy: Monarchy,
|
||||
private val eventBus: EventBus,
|
||||
private val getWellknownTask: GetWellknownTask,
|
||||
|
@ -67,9 +70,9 @@ internal class DefaultGetHomeServerCapabilitiesTask @Inject constructor(
|
|||
}
|
||||
}.getOrNull()
|
||||
|
||||
val uploadCapabilities = runCatching {
|
||||
executeRequest<GetUploadCapabilitiesResult>(eventBus) {
|
||||
apiCall = capabilitiesAPI.getUploadCapabilities()
|
||||
val mediaConfig = runCatching {
|
||||
executeRequest<GetMediaConfigResult>(eventBus) {
|
||||
apiCall = mediaAPI.getMediaConfig()
|
||||
}
|
||||
}.getOrNull()
|
||||
|
||||
|
@ -83,11 +86,11 @@ internal class DefaultGetHomeServerCapabilitiesTask @Inject constructor(
|
|||
getWellknownTask.execute(GetWellknownTask.Params(userId, homeServerConnectionConfig))
|
||||
}.getOrNull()
|
||||
|
||||
insertInDb(capabilities, uploadCapabilities, versions, wellknownResult)
|
||||
insertInDb(capabilities, mediaConfig, versions, wellknownResult)
|
||||
}
|
||||
|
||||
private suspend fun insertInDb(getCapabilitiesResult: GetCapabilitiesResult?,
|
||||
getUploadCapabilitiesResult: GetUploadCapabilitiesResult?,
|
||||
getMediaConfigResult: GetMediaConfigResult?,
|
||||
getVersionResult: Versions?,
|
||||
getWellknownResult: WellknownResult?) {
|
||||
monarchy.awaitTransaction { realm ->
|
||||
|
@ -97,8 +100,8 @@ internal class DefaultGetHomeServerCapabilitiesTask @Inject constructor(
|
|||
homeServerCapabilitiesEntity.canChangePassword = getCapabilitiesResult.canChangePassword()
|
||||
}
|
||||
|
||||
if (getUploadCapabilitiesResult != null) {
|
||||
homeServerCapabilitiesEntity.maxUploadFileSize = getUploadCapabilitiesResult.maxUploadSize
|
||||
if (getMediaConfigResult != null) {
|
||||
homeServerCapabilitiesEntity.maxUploadFileSize = getMediaConfigResult.maxUploadSize
|
||||
?: HomeServerCapabilities.MAX_UPLOAD_FILE_SIZE_UNKNOWN
|
||||
}
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
* 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
|
||||
* 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,
|
||||
|
@ -14,13 +14,13 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.matrix.android.sdk.internal.session.homeserver
|
||||
package org.matrix.android.sdk.internal.session.media
|
||||
|
||||
import com.squareup.moshi.Json
|
||||
import com.squareup.moshi.JsonClass
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
internal data class GetUploadCapabilitiesResult(
|
||||
internal data class GetMediaConfigResult(
|
||||
/**
|
||||
* The maximum size an upload can be in bytes. Clients SHOULD use this as a guide when uploading content.
|
||||
* If not listed or null, the size limit should be treated as unknown.
|
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
* Copyright 2020 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.session.media
|
||||
|
||||
import org.matrix.android.sdk.internal.network.NetworkConstants
|
||||
import retrofit2.Call
|
||||
import retrofit2.http.GET
|
||||
|
||||
internal interface MediaAPI {
|
||||
/**
|
||||
* Retrieve the configuration of the content repository
|
||||
* Ref: https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-media-r0-config
|
||||
*/
|
||||
@GET(NetworkConstants.URI_API_MEDIA_PREFIX_PATH_R0 + "config")
|
||||
fun getMediaConfig(): Call<GetMediaConfigResult>
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
* Copyright 2020 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.session.media
|
||||
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import org.matrix.android.sdk.internal.session.SessionScope
|
||||
import retrofit2.Retrofit
|
||||
|
||||
@Module
|
||||
internal abstract class MediaModule {
|
||||
|
||||
@Module
|
||||
companion object {
|
||||
@Provides
|
||||
@JvmStatic
|
||||
@SessionScope
|
||||
fun providesMediaAPI(retrofit: Retrofit): MediaAPI {
|
||||
return retrofit.create(MediaAPI::class.java)
|
||||
}
|
||||
}
|
||||
|
||||
// @Binds
|
||||
// abstract fun bindGetHomeServerCapabilitiesTask(task: DefaultGetHomeServerCapabilitiesTask): GetHomeServerCapabilitiesTask
|
||||
}
|
Loading…
Reference in a new issue