diff --git a/app/src/main/java/com/nextcloud/talk/api/NcApi.java b/app/src/main/java/com/nextcloud/talk/api/NcApi.java index 38476a29a3..78605a7fc4 100644 --- a/app/src/main/java/com/nextcloud/talk/api/NcApi.java +++ b/app/src/main/java/com/nextcloud/talk/api/NcApi.java @@ -25,7 +25,6 @@ import com.nextcloud.talk.models.json.opengraph.OpenGraphOverall; import com.nextcloud.talk.models.json.participants.AddParticipantOverall; import com.nextcloud.talk.models.json.participants.ParticipantsOverall; -import com.nextcloud.talk.models.json.participants.TalkBan; import com.nextcloud.talk.models.json.participants.TalkBanOverall; import com.nextcloud.talk.models.json.push.PushRegistrationOverall; import com.nextcloud.talk.models.json.reactions.ReactionsOverall; @@ -650,20 +649,4 @@ Observable acceptInvitation(@Header("Authorization") String auth @DELETE Observable rejectInvitation(@Header("Authorization") String authorization, @Url String url); - - @GET - Observable listBans(@Header("Authorization") String authorization, - @Url String url); - - @FormUrlEncoded - @POST - Observable banActor(@Header("Authorization") String authorization, - @Url String url, - @Field("actorType") String actorType, - @Field("actorId") String actorId, - @Field("internalNote") String internalNote); - - @DELETE - Observable unbanActor(@Header("Authorization") String authorization, - @Url String url); } \ No newline at end of file diff --git a/app/src/main/java/com/nextcloud/talk/api/NcApiCoroutines.kt b/app/src/main/java/com/nextcloud/talk/api/NcApiCoroutines.kt index 1970d2eeda..3bbdf0f901 100644 --- a/app/src/main/java/com/nextcloud/talk/api/NcApiCoroutines.kt +++ b/app/src/main/java/com/nextcloud/talk/api/NcApiCoroutines.kt @@ -11,6 +11,8 @@ import com.nextcloud.talk.models.json.autocomplete.AutocompleteOverall import com.nextcloud.talk.models.json.conversations.RoomOverall import com.nextcloud.talk.models.json.generic.GenericOverall import com.nextcloud.talk.models.json.participants.AddParticipantOverall +import com.nextcloud.talk.models.json.participants.TalkBan +import com.nextcloud.talk.models.json.participants.TalkBanOverall import okhttp3.MultipartBody import okhttp3.RequestBody import retrofit2.http.Body @@ -119,6 +121,22 @@ interface NcApiCoroutines { @DELETE suspend fun unarchiveConversation(@Header("Authorization") authorization: String, @Url url: String): GenericOverall + @FormUrlEncoded + @POST + suspend fun banActor( + @Header("Authorization") authorization: String, + @Url url: String, + @Field("actorType") actorType: String, + @Field("actorId") actorId: String, + @Field("internalNote") internalNote: String + ): TalkBan + + @GET + suspend fun listBans(@Header("Authorization") authorization: String, @Url url: String): TalkBanOverall + + @DELETE + suspend fun unbanActor(@Header("Authorization") authorization: String, @Url url: String): GenericOverall + @POST suspend fun addConversationToFavorites( @Header("Authorization") authorization: String, diff --git a/app/src/main/java/com/nextcloud/talk/chat/data/network/ChatNetworkDataSource.kt b/app/src/main/java/com/nextcloud/talk/chat/data/network/ChatNetworkDataSource.kt index f182929174..3b52c0a41e 100644 --- a/app/src/main/java/com/nextcloud/talk/chat/data/network/ChatNetworkDataSource.kt +++ b/app/src/main/java/com/nextcloud/talk/chat/data/network/ChatNetworkDataSource.kt @@ -13,7 +13,6 @@ import com.nextcloud.talk.models.json.chat.ChatOverallSingleMessage import com.nextcloud.talk.models.json.conversations.RoomOverall import com.nextcloud.talk.models.json.conversations.RoomsOverall import com.nextcloud.talk.models.json.generic.GenericOverall -import com.nextcloud.talk.models.json.participants.TalkBan import com.nextcloud.talk.models.json.reminder.Reminder import io.reactivex.Observable import retrofit2.Response @@ -64,14 +63,4 @@ interface ChatNetworkDataSource { fun createRoom(credentials: String, url: String, map: Map): Observable fun setChatReadMarker(credentials: String, url: String, previousMessageId: Int): Observable fun editChatMessage(credentials: String, url: String, text: String): Observable - fun listBans(credentials: String, url: String): Observable> - fun banActor( - credentials: String, - url: String, - actorType: String, - actorId: String, - internalNote: String - ): Observable - - fun unbanActor(credentials: String, url: String): Observable } diff --git a/app/src/main/java/com/nextcloud/talk/chat/data/network/RetrofitChatNetwork.kt b/app/src/main/java/com/nextcloud/talk/chat/data/network/RetrofitChatNetwork.kt index 774a6d423f..080b0706f6 100644 --- a/app/src/main/java/com/nextcloud/talk/chat/data/network/RetrofitChatNetwork.kt +++ b/app/src/main/java/com/nextcloud/talk/chat/data/network/RetrofitChatNetwork.kt @@ -14,7 +14,6 @@ import com.nextcloud.talk.models.json.chat.ChatOverallSingleMessage import com.nextcloud.talk.models.json.conversations.RoomOverall import com.nextcloud.talk.models.json.conversations.RoomsOverall import com.nextcloud.talk.models.json.generic.GenericOverall -import com.nextcloud.talk.models.json.participants.TalkBan import com.nextcloud.talk.models.json.reminder.Reminder import com.nextcloud.talk.utils.ApiUtils import io.reactivex.Observable @@ -179,22 +178,4 @@ class RetrofitChatNetwork(private val ncApi: NcApi) : ChatNetworkDataSource { override fun editChatMessage(credentials: String, url: String, text: String): Observable { return ncApi.editChatMessage(credentials, url, text).map { it } } - - override fun listBans(credentials: String, url: String): Observable> { - return ncApi.listBans(credentials, url).map { it.ocs?.data } - } - - override fun banActor( - credentials: String, - url: String, - actorType: String, - actorId: String, - internalNote: String - ): Observable { - return ncApi.banActor(credentials, url, actorType, actorId, internalNote) - } - - override fun unbanActor(credentials: String, url: String): Observable { - return ncApi.unbanActor(credentials, url) - } } diff --git a/app/src/main/java/com/nextcloud/talk/conversationinfo/viewmodel/ConversationInfoViewModel.kt b/app/src/main/java/com/nextcloud/talk/conversationinfo/viewmodel/ConversationInfoViewModel.kt index b6b4ef2885..a9ed147e7f 100644 --- a/app/src/main/java/com/nextcloud/talk/conversationinfo/viewmodel/ConversationInfoViewModel.kt +++ b/app/src/main/java/com/nextcloud/talk/conversationinfo/viewmodel/ConversationInfoViewModel.kt @@ -153,50 +153,34 @@ class ConversationInfoViewModel @Inject constructor( fun listBans(user: User, token: String) { val url = ApiUtils.getUrlForBans(user.baseUrl!!, token) - chatNetworkDataSource.listBans(user.getCredentials(), url) - .subscribeOn(Schedulers.io()) - ?.observeOn(AndroidSchedulers.mainThread()) - ?.subscribe(object : Observer> { - override fun onSubscribe(p0: Disposable) { - // unused atm - } - - override fun onError(e: Throwable) { - _getTalkBanState.value = ListBansErrorState - } - - override fun onComplete() { - // unused atm - } - - override fun onNext(talkBans: List) { - _getTalkBanState.value = ListBansSuccessState(talkBans) - } - }) + viewModelScope.launch { + try { + val listBans = conversationsRepository.listBans(user.getCredentials(), url) + _getTalkBanState.value = ListBansSuccessState(listBans) + } catch (exception: Exception) { + _getTalkBanState.value = ListBansErrorState + Log.e(TAG, "Error while getting list of banned participants", exception) + } + } } fun banActor(user: User, token: String, actorType: String, actorId: String, internalNote: String) { val url = ApiUtils.getUrlForBans(user.baseUrl!!, token) - chatNetworkDataSource.banActor(user.getCredentials(), url, actorType, actorId, internalNote) - .subscribeOn(Schedulers.io()) - ?.observeOn(AndroidSchedulers.mainThread()) - ?.subscribe(object : Observer { - override fun onSubscribe(p0: Disposable) { - // unused atm - } - - override fun onError(e: Throwable) { - _getBanActorState.value = BanActorErrorState - } - - override fun onComplete() { - // unused atm - } - - override fun onNext(talkBan: TalkBan) { - _getBanActorState.value = BanActorSuccessState(talkBan) - } - }) + viewModelScope.launch { + try { + val talkBan = conversationsRepository.banActor( + user.getCredentials(), + url, + actorType, + actorId, + internalNote + ) + _getBanActorState.value = BanActorSuccessState(talkBan) + } catch (exception: Exception) { + _getBanActorState.value = BanActorErrorState + Log.e(TAG, "Error banning a participant", exception) + } + } } fun setConversationReadOnly(roomToken: String, state: Int) { @@ -212,26 +196,15 @@ class ConversationInfoViewModel @Inject constructor( fun unbanActor(user: User, token: String, banId: Int) { val url = ApiUtils.getUrlForUnban(user.baseUrl!!, token, banId) - chatNetworkDataSource.unbanActor(user.getCredentials(), url) - .subscribeOn(Schedulers.io()) - ?.observeOn(AndroidSchedulers.mainThread()) - ?.subscribe(object : Observer { - override fun onSubscribe(p0: Disposable) { - // unused atm - } - - override fun onError(p0: Throwable) { - _getUnBanActorState.value = UnBanActorErrorState - } - - override fun onComplete() { - // unused atm - } - - override fun onNext(p0: GenericOverall) { - _getUnBanActorState.value = UnBanActorSuccessState - } - }) + viewModelScope.launch { + try { + conversationsRepository.unbanActor(user.getCredentials(), url) + _getUnBanActorState.value = UnBanActorSuccessState + } catch (exception: Exception) { + _getUnBanActorState.value = UnBanActorErrorState + Log.e(TAG, "Error while unbanning a participant", exception) + } + } } fun allowGuests(token: String, allow: Boolean) { diff --git a/app/src/main/java/com/nextcloud/talk/repositories/conversations/ConversationsRepository.kt b/app/src/main/java/com/nextcloud/talk/repositories/conversations/ConversationsRepository.kt index 75d373dd69..df43505046 100644 --- a/app/src/main/java/com/nextcloud/talk/repositories/conversations/ConversationsRepository.kt +++ b/app/src/main/java/com/nextcloud/talk/repositories/conversations/ConversationsRepository.kt @@ -8,6 +8,7 @@ package com.nextcloud.talk.repositories.conversations import com.nextcloud.talk.models.json.generic.GenericOverall +import com.nextcloud.talk.models.json.participants.TalkBan import io.reactivex.Observable interface ConversationsRepository { @@ -23,6 +24,19 @@ interface ConversationsRepository { suspend fun unarchiveConversation(credentials: String, url: String): GenericOverall + fun setConversationReadOnly(credentials: String, url: String, state: Int): Observable + + suspend fun banActor( + credentials: String, + url: String, + actorType: String, + actorId: String, + internalNote: String + ): TalkBan + + suspend fun listBans(credentials: String, url: String): List + suspend fun unbanActor(credentials: String, url: String): GenericOverall + suspend fun setPassword(password: String, token: String): GenericOverall suspend fun setConversationReadOnly(roomToken: String, state: Int): GenericOverall diff --git a/app/src/main/java/com/nextcloud/talk/repositories/conversations/ConversationsRepositoryImpl.kt b/app/src/main/java/com/nextcloud/talk/repositories/conversations/ConversationsRepositoryImpl.kt index 2d7fa0609e..5732b07cda 100644 --- a/app/src/main/java/com/nextcloud/talk/repositories/conversations/ConversationsRepositoryImpl.kt +++ b/app/src/main/java/com/nextcloud/talk/repositories/conversations/ConversationsRepositoryImpl.kt @@ -11,6 +11,7 @@ import com.nextcloud.talk.api.NcApi import com.nextcloud.talk.api.NcApiCoroutines import com.nextcloud.talk.data.user.model.User import com.nextcloud.talk.models.json.generic.GenericOverall +import com.nextcloud.talk.models.json.participants.TalkBan import com.nextcloud.talk.repositories.conversations.ConversationsRepository.ResendInvitationsResult import com.nextcloud.talk.utils.ApiUtils import com.nextcloud.talk.utils.database.user.CurrentUserProviderNew @@ -74,6 +75,10 @@ class ConversationsRepositoryImpl( return coroutineApi.unarchiveConversation(credentials, url) } + override fun setConversationReadOnly(credentials: String, url: String, state: Int): Observable { + return api.setConversationReadOnly(credentials, url, state) + } + override suspend fun setConversationReadOnly(roomToken: String, state: Int): GenericOverall { val apiVersion = ApiUtils.getConversationApiVersion(user, intArrayOf(ApiUtils.API_V4, ApiUtils.API_V1)) val url = ApiUtils.getUrlForConversationReadOnly(apiVersion, user.baseUrl!!, roomToken) @@ -100,6 +105,25 @@ class ConversationsRepositoryImpl( ) } + override suspend fun banActor( + credentials: String, + url: String, + actorType: String, + actorId: String, + internalNote: String + ): TalkBan { + return coroutineApi.banActor(credentials, url, actorType, actorId, internalNote) + } + + override suspend fun listBans(credentials: String, url: String): List { + val talkBanOverall = coroutineApi.listBans(credentials, url) + return talkBanOverall.ocs?.data!! + } + + override suspend fun unbanActor(credentials: String, url: String): GenericOverall { + return coroutineApi.unbanActor(credentials, url) + } + private fun apiVersion(): Int { return ApiUtils.getConversationApiVersion(user, intArrayOf(ApiUtils.API_V4)) }