From e6d869d431988ca725157797bfbd8540ac25874e Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 4 May 2021 07:13:04 +0200 Subject: [PATCH] Allow to inject the version into the chat api Signed-off-by: Joas Schilling --- .../talk/controllers/ChatController.kt | 35 ++++++++++++++++--- .../MentionAutocompletePresenter.java | 16 +++++++-- .../com/nextcloud/talk/utils/ApiUtils.java | 34 ++++++++++-------- 3 files changed, 65 insertions(+), 20 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 df3f53d2b..40bcf9627 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt +++ b/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt @@ -1142,9 +1142,16 @@ class ChatController(args: Bundle) : private fun sendMessage(message: CharSequence, replyTo: Int?) { if (conversationUser != null) { + val apiVersion = ApiUtils.getChatApiVersion(conversationUser, intArrayOf(1)) + + if (apiVersion == null) { + Log.e(TAG, "No supported API version found") + return + } + ncApi!!.sendChatMessage( credentials, - ApiUtils.getUrlForChat(conversationUser.baseUrl, roomToken), + ApiUtils.getUrlForChat(apiVersion, conversationUser.baseUrl, roomToken), message, conversationUser.displayName, replyTo @@ -1244,11 +1251,23 @@ class ChatController(args: Bundle) : } if (!wasDetached) { + var apiVersion: Int? + // FIXME this is a best guess, guests would need to get the capabilities themselves + apiVersion = 1 + if (conversationUser != null) { + apiVersion = ApiUtils.getChatApiVersion(conversationUser, intArrayOf(1)) + + if (apiVersion == null) { + Log.e(TAG, "No supported API version found") + return + } + } + if (lookIntoFuture > 0) { val finalTimeout = timeout ncApi?.pullChatMessages( credentials, - ApiUtils.getUrlForChat(conversationUser?.baseUrl, roomToken), fieldMap + ApiUtils.getUrlForChat(apiVersion, conversationUser?.baseUrl, roomToken), fieldMap ) ?.subscribeOn(Schedulers.io()) ?.observeOn(AndroidSchedulers.mainThread()) @@ -1277,7 +1296,7 @@ class ChatController(args: Bundle) : } else { ncApi?.pullChatMessages( credentials, - ApiUtils.getUrlForChat(conversationUser?.baseUrl, roomToken), fieldMap + ApiUtils.getUrlForChat(apiVersion, conversationUser?.baseUrl, roomToken), fieldMap ) ?.subscribeOn(Schedulers.io()) ?.observeOn(AndroidSchedulers.mainThread()) @@ -1667,9 +1686,17 @@ class ChatController(args: Bundle) : true } R.id.action_delete_message -> { + val apiVersion = ApiUtils.getChatApiVersion(conversationUser, intArrayOf(1)) + + if (apiVersion == null) { + Log.e(TAG, "No supported API version found") + } + + ncApi?.deleteChatMessage( credentials, - ApiUtils.getUrlForMessageDeletion(conversationUser?.baseUrl, roomToken, message?.id) + ApiUtils.getUrlForChatMessage(apiVersion, conversationUser?.baseUrl, roomToken, + message?.id) )?.subscribeOn(Schedulers.io()) ?.observeOn(AndroidSchedulers.mainThread()) ?.subscribe(object : Observer { diff --git a/app/src/main/java/com/nextcloud/talk/presenters/MentionAutocompletePresenter.java b/app/src/main/java/com/nextcloud/talk/presenters/MentionAutocompletePresenter.java index 22d18e705..1662d5d49 100644 --- a/app/src/main/java/com/nextcloud/talk/presenters/MentionAutocompletePresenter.java +++ b/app/src/main/java/com/nextcloud/talk/presenters/MentionAutocompletePresenter.java @@ -20,7 +20,9 @@ package com.nextcloud.talk.presenters; +import android.annotation.SuppressLint; import android.content.Context; +import android.util.Log; import android.view.View; import androidx.annotation.Nullable; import androidx.recyclerview.widget.RecyclerView; @@ -47,6 +49,7 @@ import java.util.List; @AutoInjector(NextcloudTalkApplication.class) public class MentionAutocompletePresenter extends RecyclerViewPresenter implements FlexibleAdapter.OnItemClickListener { + private static final String TAG = "MentionAutocompletePresenter"; @Inject NcApi ncApi; @Inject @@ -81,6 +84,7 @@ public class MentionAutocompletePresenter extends RecyclerViewPresenter return adapter; } + @SuppressLint("LongLogTag") @Override protected void onQuery(@Nullable CharSequence query) { @@ -91,9 +95,17 @@ public class MentionAutocompletePresenter extends RecyclerViewPresenter queryString = ""; } + Integer apiVersion = ApiUtils.getChatApiVersion(currentUser, new int[] {1}); + + if (apiVersion == null) { + Log.e(TAG, "No supported API version found", new Exception("No supported API version found")); + return; + } + adapter.setFilter(queryString); - ncApi.getMentionAutocompleteSuggestions(ApiUtils.getCredentials(currentUser.getUsername(), currentUser - .getToken()), ApiUtils.getUrlForMentionSuggestions(currentUser.getBaseUrl(), roomToken), + ncApi.getMentionAutocompleteSuggestions( + ApiUtils.getCredentials(currentUser.getUsername(), currentUser.getToken()), + ApiUtils.getUrlForMentionSuggestions(apiVersion, currentUser.getBaseUrl(), roomToken), queryString, 5) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) 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 5846b7fd7..9ad27bcdd 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/ApiUtils.java +++ b/app/src/main/java/com/nextcloud/talk/utils/ApiUtils.java @@ -154,6 +154,16 @@ public class ApiUtils { return null; } + public static Integer getChatApiVersion(UserEntity capabilities, int[] versions) { + for (int version : versions) { + if (version == 1 && capabilities.hasSpreedFeatureCapability("chat-v2")) { + // Do not question that chat-v2 capability shows the availability of api/v1/ endpoint *see no evil* + return version; + } + } + return null; + } + protected static String getUrlForApi(int version, String baseUrl) { return baseUrl + spreedApiBase + version; } @@ -213,6 +223,16 @@ public class ApiUtils { public static String getUrlForCall(int version, String baseUrl, String token) { return getUrlForApi(version, baseUrl) + "/call/" + token; } + public static String getUrlForChat(int version, String baseUrl, String token) { + return getUrlForApi(version, baseUrl) + "/chat/" + token; + } + + public static String getUrlForMentionSuggestions(int version, String baseUrl, String token) { + return getUrlForChat(version, baseUrl, token) + "/mentions"; + } + public static String getUrlForChatMessage(int version, String baseUrl, String token, String messageId) { + return getUrlForChat(version, baseUrl, token) + "/" + messageId; + } public static String getUrlForSignaling(int version, String baseUrl) { return getUrlForApi(version, baseUrl) + "/signaling"; @@ -285,16 +305,6 @@ public class ApiUtils { return getUrlForCall(1, baseUrl, token) + "/ping"; } - public static String getUrlForChat(String baseUrl, String token) { - // FIXME Introduce API version - return baseUrl + ocsApiVersion + spreedApiVersion + "/chat/" + token; - } - - public static String getUrlForMentionSuggestions(String baseUrl, String token) { - // FIXME Introduce API version - return getUrlForChat(baseUrl, token) + "/mentions"; - } - public static String getUrlForUserProfile(String baseUrl) { return baseUrl + ocsApiVersion + "/cloud/user"; } @@ -363,10 +373,6 @@ public class ApiUtils { return baseUrl + "/remote.php/dav/files/" + user + "/" + remotePath; } - public static String getUrlForMessageDeletion(String baseUrl, String token, String messageId) { - return getUrlForChat(baseUrl, token) + "/" + messageId; - } - public static String getUrlForTempAvatar(String baseUrl) { return baseUrl + ocsApiVersion + "/apps/spreed/temp-user-avatar"; }