From de085df3bc6587c917ab96970a7c554fa0cdb2ae Mon Sep 17 00:00:00 2001 From: Andy Scherzinger Date: Tue, 28 Dec 2021 09:53:59 +0100 Subject: [PATCH] add proper API version detection don't let switch fall through make api versions constants Signed-off-by: Andy Scherzinger --- .../nextcloud/talk/controllers/ChatController.kt | 2 +- .../bottomsheet/OperationsMenuController.java | 7 ++++--- .../java/com/nextcloud/talk/utils/ApiUtils.java | 16 +++++++++------- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt b/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt index 996da875c..9fcbc10dd 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt +++ b/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt @@ -2324,7 +2324,7 @@ class ChatController(args: Bundle) : ncApi!!.setChatReadMarker( credentials, ApiUtils.getUrlForSetChatReadMarker( - 1, + ApiUtils.getChatApiVersion(conversationUser, intArrayOf(ApiUtils.APIv1)), conversationUser?.baseUrl, roomToken ), diff --git a/app/src/main/java/com/nextcloud/talk/controllers/bottomsheet/OperationsMenuController.java b/app/src/main/java/com/nextcloud/talk/controllers/bottomsheet/OperationsMenuController.java index d08363354..3be1a2537 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/bottomsheet/OperationsMenuController.java +++ b/app/src/main/java/com/nextcloud/talk/controllers/bottomsheet/OperationsMenuController.java @@ -280,7 +280,8 @@ public class OperationsMenuController extends BaseController { } credentials = ApiUtils.getCredentials(currentUser.getUsername(), currentUser.getToken()); - int apiVersion = ApiUtils.getConversationApiVersion(currentUser, new int[] {ApiUtils.APIv4, 1}); + int apiVersion = ApiUtils.getConversationApiVersion(currentUser, new int[] {ApiUtils.APIv4, ApiUtils.APIv1}); + int chatApiVersion = ApiUtils.getChatApiVersion(currentUser, new int[] {ApiUtils.APIv1}); switch (operationCode) { case 2: @@ -483,9 +484,8 @@ public class OperationsMenuController extends BaseController { break; case 96: - // TODO: why does it break with v4? ncApi.setChatReadMarker(credentials, - ApiUtils.getUrlForSetChatReadMarker(1, + ApiUtils.getUrlForSetChatReadMarker(chatApiVersion, currentUser.getBaseUrl(), conversation.getToken()), conversation.lastMessage.jsonMessageId) @@ -493,6 +493,7 @@ public class OperationsMenuController extends BaseController { .observeOn(AndroidSchedulers.mainThread()) .retry(1) .subscribe(genericOperationsObserver); + break; case 97: case 98: if (operationCode == 97) { diff --git a/app/src/main/java/com/nextcloud/talk/utils/ApiUtils.java b/app/src/main/java/com/nextcloud/talk/utils/ApiUtils.java index 9d8921dbf..6b0be29d0 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/ApiUtils.java +++ b/app/src/main/java/com/nextcloud/talk/utils/ApiUtils.java @@ -40,6 +40,8 @@ import androidx.annotation.Nullable; import okhttp3.Credentials; public class ApiUtils { + public static final int APIv1 = 1; + public static final int APIv2 = 2; public static final int APIv3 = 3; public static final int APIv4 = 4; private static final String TAG = "ApiUtils"; @@ -59,7 +61,7 @@ public class ApiUtils { */ @Deprecated public static String getUrlForRemovingParticipantFromConversation(String baseUrl, String roomToken, boolean isGuest) { - String url = getUrlForParticipants(1, baseUrl, roomToken); + String url = getUrlForParticipants(APIv1, baseUrl, roomToken); if (isGuest) { url += "/guests"; @@ -121,7 +123,7 @@ public class ApiUtils { public static int getConversationApiVersion(UserEntity user, int[] versions) throws NoSupportedApiException { boolean hasApiV4 = false; for (int version : versions) { - hasApiV4 |= version == 4; + hasApiV4 |= version == APIv4; } if (!hasApiV4) { @@ -135,11 +137,11 @@ public class ApiUtils { } // Fallback for old API versions - if ((version == 1 || version == 2)) { + if ((version == APIv1 || version == APIv2)) { if (CapabilitiesUtil.hasSpreedFeatureCapability(user, "conversation-v2")) { return version; } - if (version == 1 && + if (version == APIv1 && CapabilitiesUtil.hasSpreedFeatureCapability(user, "mention-flag") && !CapabilitiesUtil.hasSpreedFeatureCapability(user, "conversation-v4")) { return version; @@ -155,13 +157,13 @@ public class ApiUtils { return version; } - if (version == 2 && + if (version == APIv2 && CapabilitiesUtil.hasSpreedFeatureCapability(user, "sip-support") && !CapabilitiesUtil.hasSpreedFeatureCapability(user, "signaling-v3")) { return version; } - if (version == 1 && + if (version == APIv1 && !CapabilitiesUtil.hasSpreedFeatureCapability(user, "signaling-v3")) { // Has no capability, we just assume it is always there when there is no v3 or later return version; @@ -172,7 +174,7 @@ public class ApiUtils { public static int getChatApiVersion(UserEntity user, int[] versions) throws NoSupportedApiException { for (int version : versions) { - if (version == 1 && CapabilitiesUtil.hasSpreedFeatureCapability(user, "chat-v2")) { + if (version == APIv1 && CapabilitiesUtil.hasSpreedFeatureCapability(user, "chat-v2")) { // Do not question that chat-v2 capability shows the availability of api/v1/ endpoint *see no evil* return version; }