-
-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4360 from nextcloud/last_moderator_should_delete_…
…room Last moderator should not leave the room
- Loading branch information
Showing
5 changed files
with
150 additions
and
125 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
115 changes: 0 additions & 115 deletions
115
app/src/main/java/com/nextcloud/talk/jobs/LeaveConversationWorker.java
This file was deleted.
Oops, something went wrong.
102 changes: 102 additions & 0 deletions
102
app/src/main/java/com/nextcloud/talk/jobs/LeaveConversationWorker.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,102 @@ | ||
/* | ||
* Nextcloud Talk - Android Client | ||
* | ||
* SPDX-FileCopyrightText: 2022 Andy Scherzinger <info@andy-scherzinger.de> | ||
* SPDX-FileCopyrightText: 2017-2018 Mario Danic <mario@lovelyhq.com> | ||
* SPDX-License-Identifier: GPL-3.0-or-later | ||
*/ | ||
package com.nextcloud.talk.jobs | ||
|
||
import android.annotation.SuppressLint | ||
import android.content.Context | ||
import android.util.Log | ||
import androidx.work.Data | ||
import androidx.work.ListenableWorker | ||
import androidx.work.WorkerParameters | ||
import androidx.work.impl.utils.futures.SettableFuture | ||
import autodagger.AutoInjector | ||
import com.google.common.util.concurrent.ListenableFuture | ||
import com.nextcloud.talk.api.NcApi | ||
import com.nextcloud.talk.application.NextcloudTalkApplication | ||
import com.nextcloud.talk.models.json.generic.GenericOverall | ||
import com.nextcloud.talk.users.UserManager | ||
import com.nextcloud.talk.utils.ApiUtils | ||
import com.nextcloud.talk.utils.ApiUtils.getConversationApiVersion | ||
import com.nextcloud.talk.utils.ApiUtils.getCredentials | ||
import com.nextcloud.talk.utils.ApiUtils.getUrlForParticipantsSelf | ||
import com.nextcloud.talk.utils.bundle.BundleKeys | ||
import io.reactivex.Observer | ||
import io.reactivex.android.schedulers.AndroidSchedulers | ||
import io.reactivex.disposables.Disposable | ||
import io.reactivex.schedulers.Schedulers | ||
import retrofit2.HttpException | ||
import javax.inject.Inject | ||
|
||
@SuppressLint("RestrictedApi") | ||
@AutoInjector(NextcloudTalkApplication::class) | ||
class LeaveConversationWorker(context: Context, workerParams: WorkerParameters) : | ||
ListenableWorker(context, workerParams) { | ||
|
||
@Inject | ||
lateinit var ncApi: NcApi | ||
|
||
@Inject | ||
lateinit var userManager: UserManager | ||
|
||
private val result = SettableFuture.create<Result>() | ||
|
||
override fun startWork(): ListenableFuture<Result> { | ||
NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this) | ||
val conversationToken = inputData.getString(BundleKeys.KEY_ROOM_TOKEN) | ||
val currentUser = userManager.currentUser.blockingGet() | ||
|
||
if (currentUser != null && conversationToken != null) { | ||
val credentials = getCredentials(currentUser.username, currentUser.token) | ||
val apiVersion = getConversationApiVersion(currentUser, intArrayOf(ApiUtils.API_V4, 1)) | ||
|
||
ncApi.removeSelfFromRoom( | ||
credentials, | ||
getUrlForParticipantsSelf(apiVersion, currentUser.baseUrl, conversationToken) | ||
) | ||
.subscribeOn(Schedulers.io()) | ||
.observeOn(AndroidSchedulers.mainThread()) | ||
.subscribe(object : Observer<GenericOverall?> { | ||
override fun onSubscribe(d: Disposable) { | ||
} | ||
|
||
override fun onNext(p0: GenericOverall) { | ||
} | ||
|
||
override fun onError(e: Throwable) { | ||
Log.e(TAG, "Failed to remove self from room", e) | ||
val httpException = e as? HttpException | ||
val errorData = if (httpException?.code() == HTTP_ERROR_CODE_400) { | ||
Data.Builder() | ||
.putString("error_type", ERROR_NO_OTHER_MODERATORS_OR_OWNERS_LEFT) | ||
.build() | ||
} else { | ||
Data.Builder() | ||
.putString("error_type", ERROR_OTHER) | ||
.build() | ||
} | ||
result.set(Result.failure(errorData)) | ||
} | ||
|
||
override fun onComplete() { | ||
result.set(Result.success()) | ||
} | ||
}) | ||
} else { | ||
result.set(Result.failure()) | ||
} | ||
|
||
return result | ||
} | ||
|
||
companion object { | ||
private const val TAG = "LeaveConversationWorker" | ||
const val ERROR_NO_OTHER_MODERATORS_OR_OWNERS_LEFT = "NO_OTHER_MODERATORS_OR_OWNERS_LEFT" | ||
const val ERROR_OTHER = "ERROR_OTHER" | ||
const val HTTP_ERROR_CODE_400 = 400 | ||
} | ||
} |
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