Signed-off-by: Mario Danic <mario@lovelyhq.com>
This commit is contained in:
Mario Danic 2019-07-21 22:39:03 +02:00
parent ceac6efa39
commit d225e2ac9f
4 changed files with 102 additions and 49 deletions

View File

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

View File

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

View File

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

View File

@ -44,6 +44,14 @@ public class WebSocketConnectionHelper {
NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this); 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) { public static synchronized MagicWebSocketInstance getExternalSignalingInstanceForServer(String url, UserEntity userEntity, String webSocketTicket, boolean isGuest) {
String generatedURL = url.replace("https://", "wss://").replace("http://", "ws://"); String generatedURL = url.replace("https://", "wss://").replace("http://", "ws://");