Skip to content

Commit

Permalink
Merge pull request #4464 from nextcloud/coroutine_5
Browse files Browse the repository at this point in the history
coroutine_5
  • Loading branch information
mahibi authored Nov 21, 2024
2 parents 8fa3c7b + e8cf3ee commit c9d84c2
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 107 deletions.
17 changes: 0 additions & 17 deletions app/src/main/java/com/nextcloud/talk/api/NcApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -650,20 +649,4 @@ Observable<GenericOverall> acceptInvitation(@Header("Authorization") String auth
@DELETE
Observable<GenericOverall> rejectInvitation(@Header("Authorization") String authorization,
@Url String url);

@GET
Observable<TalkBanOverall> listBans(@Header("Authorization") String authorization,
@Url String url);

@FormUrlEncoded
@POST
Observable<TalkBan> banActor(@Header("Authorization") String authorization,
@Url String url,
@Field("actorType") String actorType,
@Field("actorId") String actorId,
@Field("internalNote") String internalNote);

@DELETE
Observable<GenericOverall> unbanActor(@Header("Authorization") String authorization,
@Url String url);
}
18 changes: 18 additions & 0 deletions app/src/main/java/com/nextcloud/talk/api/NcApiCoroutines.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -64,14 +63,4 @@ interface ChatNetworkDataSource {
fun createRoom(credentials: String, url: String, map: Map<String, String>): Observable<RoomOverall>
fun setChatReadMarker(credentials: String, url: String, previousMessageId: Int): Observable<GenericOverall>
fun editChatMessage(credentials: String, url: String, text: String): Observable<ChatOverallSingleMessage>
fun listBans(credentials: String, url: String): Observable<List<TalkBan>>
fun banActor(
credentials: String,
url: String,
actorType: String,
actorId: String,
internalNote: String
): Observable<TalkBan>

fun unbanActor(credentials: String, url: String): Observable<GenericOverall>
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -179,22 +178,4 @@ class RetrofitChatNetwork(private val ncApi: NcApi) : ChatNetworkDataSource {
override fun editChatMessage(credentials: String, url: String, text: String): Observable<ChatOverallSingleMessage> {
return ncApi.editChatMessage(credentials, url, text).map { it }
}

override fun listBans(credentials: String, url: String): Observable<List<TalkBan>> {
return ncApi.listBans(credentials, url).map { it.ocs?.data }
}

override fun banActor(
credentials: String,
url: String,
actorType: String,
actorId: String,
internalNote: String
): Observable<TalkBan> {
return ncApi.banActor(credentials, url, actorType, actorId, internalNote)
}

override fun unbanActor(credentials: String, url: String): Observable<GenericOverall> {
return ncApi.unbanActor(credentials, url)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<TalkBan>> {
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<TalkBan>) {
_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<TalkBan> {
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) {
Expand All @@ -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<GenericOverall> {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -23,6 +24,19 @@ interface ConversationsRepository {

suspend fun unarchiveConversation(credentials: String, url: String): GenericOverall

fun setConversationReadOnly(credentials: String, url: String, state: Int): Observable<GenericOverall>

suspend fun banActor(
credentials: String,
url: String,
actorType: String,
actorId: String,
internalNote: String
): TalkBan

suspend fun listBans(credentials: String, url: String): List<TalkBan>
suspend fun unbanActor(credentials: String, url: String): GenericOverall

suspend fun setPassword(password: String, token: String): GenericOverall

suspend fun setConversationReadOnly(roomToken: String, state: Int): GenericOverall
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -74,6 +75,10 @@ class ConversationsRepositoryImpl(
return coroutineApi.unarchiveConversation(credentials, url)
}

override fun setConversationReadOnly(credentials: String, url: String, state: Int): Observable<GenericOverall> {
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)
Expand All @@ -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<TalkBan> {
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))
}
Expand Down

0 comments on commit c9d84c2

Please sign in to comment.