Преглед на файлове

Fix #584

Signed-off-by: Mario Danic <mario@lovelyhq.com>
Mario Danic преди 5 години
родител
ревизия
d225e2ac9f

+ 52 - 6
app/src/main/java/com/nextcloud/talk/controllers/ChatController.java

@@ -37,7 +37,9 @@ import android.text.TextUtils;
 import android.text.TextWatcher;
 import android.util.Log;
 import android.view.*;
-import android.widget.*;
+import android.widget.AbsListView;
+import android.widget.ImageButton;
+import android.widget.ProgressBar;
 import androidx.annotation.NonNull;
 import androidx.recyclerview.widget.LinearLayoutManager;
 import androidx.recyclerview.widget.RecyclerView;
@@ -62,6 +64,7 @@ import com.nextcloud.talk.callbacks.MentionAutocompleteCallback;
 import com.nextcloud.talk.components.filebrowser.controllers.BrowserController;
 import com.nextcloud.talk.controllers.base.BaseController;
 import com.nextcloud.talk.events.UserMentionClickEvent;
+import com.nextcloud.talk.events.WebSocketCommunicationEvent;
 import com.nextcloud.talk.models.RetrofitBucket;
 import com.nextcloud.talk.models.database.UserEntity;
 import com.nextcloud.talk.models.json.call.Call;
@@ -80,6 +83,8 @@ import com.nextcloud.talk.utils.database.user.UserUtils;
 import com.nextcloud.talk.utils.preferences.AppPreferences;
 import com.nextcloud.talk.utils.singletons.ApplicationWideCurrentRoomHolder;
 import com.nextcloud.talk.utils.text.Spans;
+import com.nextcloud.talk.webrtc.MagicWebSocketInstance;
+import com.nextcloud.talk.webrtc.WebSocketConnectionHelper;
 import com.otaliastudios.autocomplete.Autocomplete;
 import com.otaliastudios.autocomplete.AutocompleteCallback;
 import com.otaliastudios.autocomplete.AutocompletePresenter;
@@ -162,7 +167,6 @@ public class ChatController extends BaseController implements MessagesListAdapte
     private String roomId;
     private boolean voiceOnly;
     private boolean isFirstMessagesProcessing = true;
-    private boolean isHelloClicked;
     private boolean isLeavingForConversation;
     private boolean isLinkPreviewAllowed;
     private boolean wasDetached;
@@ -176,6 +180,8 @@ public class ChatController extends BaseController implements MessagesListAdapte
 
     private boolean readOnlyCheckPerformed;
 
+    private MagicWebSocketInstance magicWebSocketInstance;
+
     public ChatController(Bundle args) {
         super(args);
         setHasOptionsMenu(true);
@@ -437,6 +443,7 @@ public class ChatController extends BaseController implements MessagesListAdapte
         }
     }
 
+
     private void checkReadOnlyState() {
         if (currentConversation != null && !readOnlyCheckPerformed) {
 
@@ -561,6 +568,7 @@ public class ChatController extends BaseController implements MessagesListAdapte
 
         if (inChat) {
             if (wasDetached && conversationUser.hasSpreedFeatureCapability("no-ping")) {
+                currentCall = null;
                 wasDetached = false;
                 joinRoomWithPassword();
             }
@@ -570,7 +578,7 @@ public class ChatController extends BaseController implements MessagesListAdapte
     private void cancelNotificationsForCurrentConversation() {
         if (!conversationUser.hasSpreedFeatureCapability("no-ping") && !TextUtils.isEmpty(roomId)) {
             NotificationUtils.cancelExistingNotificationsForRoom(getApplicationContext(), conversationUser, roomId);
-        } else if (!TextUtils.isEmpty(roomToken)){
+        } else if (!TextUtils.isEmpty(roomToken)) {
             NotificationUtils.cancelExistingNotificationsForRoom(getApplicationContext(), conversationUser, roomToken);
         }
     }
@@ -679,6 +687,11 @@ public class ChatController extends BaseController implements MessagesListAdapte
                                 pullChatMessages(1);
                             }
 
+                            setupWebsocket();
+                            if (magicWebSocketInstance != null) {
+                                magicWebSocketInstance.joinRoomWithRoomTokenAndSession(roomToken,
+                                        currentCall.getSessionId());
+                            }
                             if (startCallFromNotification != null && startCallFromNotification) {
                                 startCallFromNotification = false;
                                 startACall(voiceOnly);
@@ -698,6 +711,10 @@ public class ChatController extends BaseController implements MessagesListAdapte
         } else {
             inChat = true;
             ApplicationWideCurrentRoomHolder.getInstance().setSession(currentCall.getSessionId());
+            if (magicWebSocketInstance != null) {
+                magicWebSocketInstance.joinRoomWithRoomTokenAndSession(roomToken,
+                        currentCall.getSessionId());
+            }
             startPing();
             if (isFirstMessagesProcessing) {
                 pullChatMessages(0);
@@ -813,21 +830,37 @@ public class ChatController extends BaseController implements MessagesListAdapte
                 });
     }
 
+    private void setupWebsocket() {
+        if (WebSocketConnectionHelper.getMagicWebSocketInstanceForUserId(conversationUser.getId()) != null) {
+            magicWebSocketInstance = WebSocketConnectionHelper.getMagicWebSocketInstanceForUserId(conversationUser.getId());
+        } else {
+            magicWebSocketInstance = null;
+        }
+    }
+
     private void pullChatMessages(int lookIntoFuture) {
         if (!inChat) {
             return;
         }
 
-        if (!lookingIntoFuture && lookIntoFuture == 1) {
+        if (lookIntoFuture == 1 && magicWebSocketInstance != null) {
+            return;
+        }
+
+        if (!lookingIntoFuture && lookIntoFuture > 0) {
             lookingIntoFuture = true;
         }
 
+        if (lookIntoFuture > 1) {
+            lookIntoFuture = 1;
+        }
+
         Map<String, Integer> fieldMap = new HashMap<>();
         fieldMap.put("lookIntoFuture", lookIntoFuture);
         fieldMap.put("limit", 25);
 
         int lastKnown;
-        if (lookIntoFuture == 1) {
+        if (lookIntoFuture > 0) {
             lastKnown = globalLastKnownFutureMessageId;
         } else {
             lastKnown = globalLastKnownPastMessageId;
@@ -838,7 +871,7 @@ public class ChatController extends BaseController implements MessagesListAdapte
         }
 
         if (!wasDetached) {
-            if (lookIntoFuture == 1) {
+            if (lookIntoFuture > 0) {
                 ncApi.pullChatMessages(credentials, ApiUtils.getUrlForChat(conversationUser.getBaseUrl(),
                         roomToken),
                         fieldMap)
@@ -1157,6 +1190,19 @@ public class ChatController extends BaseController implements MessagesListAdapte
         return false;
     }
 
+    @Subscribe(threadMode = ThreadMode.BACKGROUND)
+    public void onMessageEvent(WebSocketCommunicationEvent webSocketCommunicationEvent) {
+        switch (webSocketCommunicationEvent.getType()) {
+            case "refreshChat":
+                if (webSocketCommunicationEvent.getHashMap().get(BundleKeys.KEY_INTERNAL_USER_ID).equals(Long.toString(conversationUser.getId()))) {
+                    if (roomToken.equals(webSocketCommunicationEvent.getHashMap().get(BundleKeys.KEY_ROOM_TOKEN))) {
+                        pullChatMessages(2);
+                    }
+                }
+                break;
+            default:
+        }
+    }
 
     @Subscribe(threadMode = ThreadMode.BACKGROUND)
     public void onMessageEvent(UserMentionClickEvent userMentionClickEvent) {

+ 31 - 37
app/src/main/java/com/nextcloud/talk/jobs/SignalingSettingsWorker.java

@@ -21,7 +21,6 @@
 package com.nextcloud.talk.jobs;
 
 import android.content.Context;
-import android.text.TextUtils;
 import android.util.Log;
 import androidx.annotation.NonNull;
 import androidx.work.*;
@@ -93,43 +92,38 @@ public class SignalingSettingsWorker extends Worker {
                         @Override
                         public void onNext(SignalingSettingsOverall signalingSettingsOverall) {
                             ExternalSignalingServer externalSignalingServer;
-                            if (!TextUtils.isEmpty(signalingSettingsOverall.getOcs().getSettings().getExternalSignalingServer()) &&
-                                    !TextUtils.isEmpty(signalingSettingsOverall.getOcs().getSettings().getExternalSignalingTicket())) {
-                                externalSignalingServer = new ExternalSignalingServer();
-                                externalSignalingServer.setExternalSignalingServer(signalingSettingsOverall.getOcs().getSettings().getExternalSignalingServer());
-                                externalSignalingServer.setExternalSignalingTicket(signalingSettingsOverall.getOcs().getSettings().getExternalSignalingTicket());
-
-                                try {
-                                    userUtils.createOrUpdateUser(null, null, null, null, null,
-                                            null, null, finalUserEntity.getId(), null, null, LoganSquare.serialize(externalSignalingServer))
-                                            .subscribe(new Observer<UserEntity>() {
-                                                @Override
-                                                public void onSubscribe(Disposable d) {
-
-                                                }
-
-                                                @Override
-                                                public void onNext(UserEntity userEntity) {
-                                                    eventBus.post(new EventStatus(finalUserEntity.getId(), EventStatus.EventType.SIGNALING_SETTINGS, true));
-                                                }
-
-                                                @Override
-                                                public void onError(Throwable e) {
-                                                    eventBus.post(new EventStatus(finalUserEntity.getId(), EventStatus.EventType.SIGNALING_SETTINGS, false));
-                                                }
-
-                                                @Override
-                                                public void onComplete() {
-
-                                                }
-                                            });
-                                } catch (IOException e) {
-                                    Log.e(TAG, "Failed to serialize external signaling server");
-                                }
-                            } else {
-                                eventBus.post(new EventStatus(finalUserEntity.getId(), EventStatus.EventType.SIGNALING_SETTINGS, true));
+                            externalSignalingServer = new ExternalSignalingServer();
+                            externalSignalingServer.setExternalSignalingServer(signalingSettingsOverall.getOcs().getSettings().getExternalSignalingServer());
+                            externalSignalingServer.setExternalSignalingTicket(signalingSettingsOverall.getOcs().getSettings().getExternalSignalingTicket());
+
+                            Log.d("MARIO", "MARIO");
+                            try {
+                                userUtils.createOrUpdateUser(null, null, null, null, null,
+                                        null, null, finalUserEntity.getId(), null, null, LoganSquare.serialize(externalSignalingServer))
+                                        .subscribe(new Observer<UserEntity>() {
+                                            @Override
+                                            public void onSubscribe(Disposable d) {
+
+                                            }
+
+                                            @Override
+                                            public void onNext(UserEntity userEntity) {
+                                                eventBus.post(new EventStatus(finalUserEntity.getId(), EventStatus.EventType.SIGNALING_SETTINGS, true));
+                                            }
+
+                                            @Override
+                                            public void onError(Throwable e) {
+                                                eventBus.post(new EventStatus(finalUserEntity.getId(), EventStatus.EventType.SIGNALING_SETTINGS, false));
+                                            }
+
+                                            @Override
+                                            public void onComplete() {
+
+                                            }
+                                        });
+                            } catch (IOException e) {
+                                Log.e(TAG, "Failed to serialize external signaling server");
                             }
-
                         }
 
                         @Override

+ 17 - 12
app/src/main/java/com/nextcloud/talk/webrtc/MagicWebSocketInstance.java

@@ -36,6 +36,7 @@ import com.nextcloud.talk.models.json.signaling.NCSignalingMessage;
 import com.nextcloud.talk.models.json.websocket.*;
 import com.nextcloud.talk.utils.LoggingUtils;
 import com.nextcloud.talk.utils.MagicMap;
+import com.nextcloud.talk.utils.bundle.BundleKeys;
 import com.nextcloud.talk.utils.singletons.MerlinTheWizard;
 import okhttp3.*;
 import okio.ByteString;
@@ -207,19 +208,23 @@ public class MagicWebSocketInstance extends WebSocketListener {
                             switch (target) {
                                 case "room":
                                     if (eventOverallWebSocketMessage.getEventMap().get("type").equals("message")) {
-                                        if (eventOverallWebSocketMessage.getEventMap().containsKey("data")) {
-                                            Map<String, Object> dataHashMap = (Map<String, Object>) eventOverallWebSocketMessage.getEventMap().get("data");
-                                            if (dataHashMap.containsKey("chat")) {
-                                                boolean shouldRefreshChat;
-                                                Map<String, Object> chatMap = (Map<String, Object>) dataHashMap.get("chat");
-                                                if (chatMap.containsKey("refresh")) {
-                                                    shouldRefreshChat = (boolean) chatMap.get("refresh");
-                                                    if (shouldRefreshChat) {
-                                                        HashMap<String, String> refreshChatHashMap = new HashMap<>();
-                                                        refreshChatHashMap.put("roomToken", (String) eventOverallWebSocketMessage.getEventMap().get("roomid"));
-                                                        eventBus.post(new WebSocketCommunicationEvent("refreshChat", refreshChatHashMap));
+                                            Map<String, Object> messageHashMap =
+                                                    (Map<String, Object>) eventOverallWebSocketMessage.getEventMap().get("message");
+                                            if (messageHashMap.containsKey("data")) {
+                                                Map<String, Object> dataHashMap = (Map<String, Object>) messageHashMap.get(
+                                                        "data");
+                                                if (dataHashMap.containsKey("chat")) {
+                                                    boolean shouldRefreshChat;
+                                                    Map<String, Object> chatMap = (Map<String, Object>) dataHashMap.get("chat");
+                                                    if (chatMap.containsKey("refresh")) {
+                                                        shouldRefreshChat = (boolean) chatMap.get("refresh");
+                                                        if (shouldRefreshChat) {
+                                                            HashMap<String, String> refreshChatHashMap = new HashMap<>();
+                                                            refreshChatHashMap.put(BundleKeys.KEY_ROOM_TOKEN, (String) messageHashMap.get("roomid"));
+                                                            refreshChatHashMap.put(BundleKeys.KEY_INTERNAL_USER_ID, Long.toString(conversationUser.getId()));
+                                                            eventBus.post(new WebSocketCommunicationEvent("refreshChat", refreshChatHashMap));
+                                                        }
                                                     }
-                                                }
                                             }
                                         }
                                     } else if (eventOverallWebSocketMessage.getEventMap().get("type").equals("join")) {

+ 8 - 0
app/src/main/java/com/nextcloud/talk/webrtc/WebSocketConnectionHelper.java

@@ -44,6 +44,14 @@ public class WebSocketConnectionHelper {
         NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
     }
 
+    public static synchronized MagicWebSocketInstance getMagicWebSocketInstanceForUserId(long userId) {
+        if (userId != -1 && magicWebSocketInstanceMap.containsKey(userId)) {
+            return magicWebSocketInstanceMap.get(userId);
+        }
+
+        return null;
+    }
+
     public static synchronized MagicWebSocketInstance getExternalSignalingInstanceForServer(String url, UserEntity userEntity, String webSocketTicket, boolean isGuest) {
         String generatedURL = url.replace("https://", "wss://").replace("http://", "ws://");