瀏覽代碼

Allow to inject the version into the chat api

Signed-off-by: Joas Schilling <coding@schilljs.com>
Joas Schilling 4 年之前
父節點
當前提交
e6d869d431

+ 31 - 4
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?) {
     private fun sendMessage(message: CharSequence, replyTo: Int?) {
 
 
         if (conversationUser != null) {
         if (conversationUser != null) {
+            val apiVersion = ApiUtils.getChatApiVersion(conversationUser, intArrayOf(1))
+
+            if (apiVersion == null) {
+                Log.e(TAG, "No supported API version found")
+                return
+            }
+
             ncApi!!.sendChatMessage(
             ncApi!!.sendChatMessage(
                 credentials,
                 credentials,
-                ApiUtils.getUrlForChat(conversationUser.baseUrl, roomToken),
+                ApiUtils.getUrlForChat(apiVersion, conversationUser.baseUrl, roomToken),
                 message,
                 message,
                 conversationUser.displayName,
                 conversationUser.displayName,
                 replyTo
                 replyTo
@@ -1244,11 +1251,23 @@ class ChatController(args: Bundle) :
         }
         }
 
 
         if (!wasDetached) {
         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) {
             if (lookIntoFuture > 0) {
                 val finalTimeout = timeout
                 val finalTimeout = timeout
                 ncApi?.pullChatMessages(
                 ncApi?.pullChatMessages(
                     credentials,
                     credentials,
-                    ApiUtils.getUrlForChat(conversationUser?.baseUrl, roomToken), fieldMap
+                    ApiUtils.getUrlForChat(apiVersion, conversationUser?.baseUrl, roomToken), fieldMap
                 )
                 )
                     ?.subscribeOn(Schedulers.io())
                     ?.subscribeOn(Schedulers.io())
                     ?.observeOn(AndroidSchedulers.mainThread())
                     ?.observeOn(AndroidSchedulers.mainThread())
@@ -1277,7 +1296,7 @@ class ChatController(args: Bundle) :
             } else {
             } else {
                 ncApi?.pullChatMessages(
                 ncApi?.pullChatMessages(
                     credentials,
                     credentials,
-                    ApiUtils.getUrlForChat(conversationUser?.baseUrl, roomToken), fieldMap
+                    ApiUtils.getUrlForChat(apiVersion, conversationUser?.baseUrl, roomToken), fieldMap
                 )
                 )
                     ?.subscribeOn(Schedulers.io())
                     ?.subscribeOn(Schedulers.io())
                     ?.observeOn(AndroidSchedulers.mainThread())
                     ?.observeOn(AndroidSchedulers.mainThread())
@@ -1667,9 +1686,17 @@ class ChatController(args: Bundle) :
                         true
                         true
                     }
                     }
                     R.id.action_delete_message -> {
                     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(
                         ncApi?.deleteChatMessage(
                             credentials,
                             credentials,
-                            ApiUtils.getUrlForMessageDeletion(conversationUser?.baseUrl, roomToken, message?.id)
+                            ApiUtils.getUrlForChatMessage(apiVersion, conversationUser?.baseUrl, roomToken,
+                                message?.id)
                         )?.subscribeOn(Schedulers.io())
                         )?.subscribeOn(Schedulers.io())
                             ?.observeOn(AndroidSchedulers.mainThread())
                             ?.observeOn(AndroidSchedulers.mainThread())
                             ?.subscribe(object : Observer<ChatOverallSingleMessage> {
                             ?.subscribe(object : Observer<ChatOverallSingleMessage> {

+ 14 - 2
app/src/main/java/com/nextcloud/talk/presenters/MentionAutocompletePresenter.java

@@ -20,7 +20,9 @@
 
 
 package com.nextcloud.talk.presenters;
 package com.nextcloud.talk.presenters;
 
 
+import android.annotation.SuppressLint;
 import android.content.Context;
 import android.content.Context;
+import android.util.Log;
 import android.view.View;
 import android.view.View;
 import androidx.annotation.Nullable;
 import androidx.annotation.Nullable;
 import androidx.recyclerview.widget.RecyclerView;
 import androidx.recyclerview.widget.RecyclerView;
@@ -47,6 +49,7 @@ import java.util.List;
 
 
 @AutoInjector(NextcloudTalkApplication.class)
 @AutoInjector(NextcloudTalkApplication.class)
 public class MentionAutocompletePresenter extends RecyclerViewPresenter<Mention> implements FlexibleAdapter.OnItemClickListener {
 public class MentionAutocompletePresenter extends RecyclerViewPresenter<Mention> implements FlexibleAdapter.OnItemClickListener {
+    private static final String TAG = "MentionAutocompletePresenter";
     @Inject
     @Inject
     NcApi ncApi;
     NcApi ncApi;
     @Inject
     @Inject
@@ -81,6 +84,7 @@ public class MentionAutocompletePresenter extends RecyclerViewPresenter<Mention>
         return adapter;
         return adapter;
     }
     }
 
 
+    @SuppressLint("LongLogTag")
     @Override
     @Override
     protected void onQuery(@Nullable CharSequence query) {
     protected void onQuery(@Nullable CharSequence query) {
 
 
@@ -91,9 +95,17 @@ public class MentionAutocompletePresenter extends RecyclerViewPresenter<Mention>
             queryString = "";
             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);
         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)
                 queryString, 5)
                 .subscribeOn(Schedulers.io())
                 .subscribeOn(Schedulers.io())
                 .observeOn(AndroidSchedulers.mainThread())
                 .observeOn(AndroidSchedulers.mainThread())

+ 20 - 14
app/src/main/java/com/nextcloud/talk/utils/ApiUtils.java

@@ -154,6 +154,16 @@ public class ApiUtils {
         return null;
         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) {
     protected static String getUrlForApi(int version, String baseUrl) {
         return baseUrl + spreedApiBase + version;
         return baseUrl + spreedApiBase + version;
     }
     }
@@ -213,6 +223,16 @@ public class ApiUtils {
     public static String getUrlForCall(int version, String baseUrl, String token) {
     public static String getUrlForCall(int version, String baseUrl, String token) {
         return getUrlForApi(version, baseUrl) + "/call/" + 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) {
     public static String getUrlForSignaling(int version, String baseUrl) {
         return getUrlForApi(version, baseUrl) + "/signaling";
         return getUrlForApi(version, baseUrl) + "/signaling";
@@ -285,16 +305,6 @@ public class ApiUtils {
         return getUrlForCall(1, baseUrl, token) + "/ping";
         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) {
     public static String getUrlForUserProfile(String baseUrl) {
         return baseUrl + ocsApiVersion + "/cloud/user";
         return baseUrl + ocsApiVersion + "/cloud/user";
     }
     }
@@ -363,10 +373,6 @@ public class ApiUtils {
         return baseUrl + "/remote.php/dav/files/" + user + "/" + remotePath;
         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) {
     public static String getUrlForTempAvatar(String baseUrl) {
         return baseUrl + ocsApiVersion + "/apps/spreed/temp-user-avatar";
         return baseUrl + ocsApiVersion + "/apps/spreed/temp-user-avatar";
     }
     }