Skip to content

Commit

Permalink
Merge pull request #2854 from nextcloud/backport/2852/stable-16.0
Browse files Browse the repository at this point in the history
[stable-16.0] Fix to get websocketInstance
  • Loading branch information
mahibi authored Mar 11, 2023
2 parents 6623c6b + 6ec2a9c commit a0d02db
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import com.nextcloud.talk.dagger.modules.ViewModelModule
import com.nextcloud.talk.jobs.AccountRemovalWorker
import com.nextcloud.talk.jobs.CapabilitiesWorker
import com.nextcloud.talk.jobs.SignalingSettingsWorker
import com.nextcloud.talk.jobs.WebsocketConnectionsWorker
import com.nextcloud.talk.ui.theme.ThemeModule
import com.nextcloud.talk.utils.ClosedInterfaceImpl
import com.nextcloud.talk.utils.DeviceUtils
Expand Down Expand Up @@ -176,31 +177,40 @@ class NextcloudTalkApplication : MultiDexApplication(), LifecycleObserver {
ClosedInterfaceImpl().providerInstallerInstallIfNeededAsync()
DeviceUtils.ignoreSpecialBatteryFeatures()

initWorkers()

val config = BundledEmojiCompatConfig(this)
config.setReplaceAll(true)
val emojiCompat = EmojiCompat.init(config)

EmojiManager.install(GoogleEmojiProvider())

NotificationUtils.registerNotificationChannels(applicationContext, appPreferences)
}

private fun initWorkers() {
val accountRemovalWork = OneTimeWorkRequest.Builder(AccountRemovalWorker::class.java).build()
val capabilitiesUpdateWork = OneTimeWorkRequest.Builder(CapabilitiesWorker::class.java).build()
val signalingSettingsWork = OneTimeWorkRequest.Builder(SignalingSettingsWorker::class.java).build()
val websocketConnectionsWorker = OneTimeWorkRequest.Builder(WebsocketConnectionsWorker::class.java).build()

WorkManager.getInstance(applicationContext)
.beginWith(accountRemovalWork)
.then(capabilitiesUpdateWork)
.then(signalingSettingsWork)
.then(websocketConnectionsWorker)
.enqueue()

val periodicCapabilitiesUpdateWork = PeriodicWorkRequest.Builder(
CapabilitiesWorker::class.java,
HALF_DAY,
TimeUnit.HOURS
).build()
val capabilitiesUpdateWork = OneTimeWorkRequest.Builder(CapabilitiesWorker::class.java).build()
val signalingSettingsWork = OneTimeWorkRequest.Builder(SignalingSettingsWorker::class.java).build()

WorkManager.getInstance(applicationContext).enqueue(accountRemovalWork)
WorkManager.getInstance(applicationContext).enqueue(capabilitiesUpdateWork)
WorkManager.getInstance(applicationContext).enqueue(signalingSettingsWork)
WorkManager.getInstance(applicationContext).enqueueUniquePeriodicWork(
"DailyCapabilitiesUpdateWork",
ExistingPeriodicWorkPolicy.REPLACE,
periodicCapabilitiesUpdateWork
)

val config = BundledEmojiCompatConfig(this)
config.setReplaceAll(true)
val emojiCompat = EmojiCompat.init(config)

EmojiManager.install(GoogleEmojiProvider())

NotificationUtils.registerNotificationChannels(applicationContext, appPreferences)
}

override fun onTerminate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import com.nextcloud.talk.events.EventStatus
import com.nextcloud.talk.jobs.CapabilitiesWorker
import com.nextcloud.talk.jobs.PushRegistrationWorker
import com.nextcloud.talk.jobs.SignalingSettingsWorker
import com.nextcloud.talk.jobs.WebsocketConnectionsWorker
import com.nextcloud.talk.models.json.capabilities.Capabilities
import com.nextcloud.talk.models.json.capabilities.CapabilitiesOverall
import com.nextcloud.talk.models.json.generic.Status
Expand Down Expand Up @@ -434,11 +435,15 @@ class AccountVerificationController(args: Bundle? = null) :
Data.Builder()
.putLong(KEY_INTERNAL_USER_ID, internalAccountId)
.build()
val signalingSettings =
OneTimeWorkRequest.Builder(SignalingSettingsWorker::class.java)
.setInputData(userData)
.build()
WorkManager.getInstance().enqueue(signalingSettings)
val signalingSettings = OneTimeWorkRequest.Builder(SignalingSettingsWorker::class.java)
.setInputData(userData)
.build()
val websocketConnectionsWorker = OneTimeWorkRequest.Builder(WebsocketConnectionsWorker::class.java).build()

WorkManager.getInstance(applicationContext!!)
.beginWith(signalingSettings)
.then(websocketConnectionsWorker)
.enqueue()
}

private fun proceedWithLogin() {
Expand Down
24 changes: 3 additions & 21 deletions app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1783,6 +1783,7 @@ class ChatController(args: Bundle) :

eventBus.register(this)

setupWebsocket()
webSocketInstance?.getSignalingMessageReceiver()?.addListener(localParticipantMessageListener)

if (conversationUser?.userId != "?" &&
Expand Down Expand Up @@ -2002,15 +2003,6 @@ class ChatController(args: Bundle) :

logConversationInfos("joinRoomWithPassword#onNext")

// FIXME The web socket should be set up in onAttach(). It is currently setup after joining the
// room to "ensure" (rather, increase the chances) that the WebsocketConnectionsWorker job
// was able to finish and, therefore, that the web socket instance can be got.
setupWebsocket()

// Ensure that the listener is added if the web socket instance was not set up yet when
// onAttach() was called.
webSocketInstance?.getSignalingMessageReceiver()?.addListener(localParticipantMessageListener)

if (isFirstMessagesProcessing) {
pullChatMessages(false)
} else {
Expand Down Expand Up @@ -2103,15 +2095,6 @@ class ChatController(args: Bundle) :
"",
sessionIdAfterRoomJoined
)
} else {
Log.e(TAG, "magicWebSocketInstance or currentConversation were null! Failed to leave the room!")
if (BuildConfig.DEBUG) {
Toast.makeText(
context,
"magicWebSocketInstance or currentConversation were null! Failed to leave the room!",
Toast.LENGTH_LONG
).show()
}
}

sessionIdAfterRoomJoined = "0"
Expand Down Expand Up @@ -2224,11 +2207,10 @@ class ChatController(args: Bundle) :
if (conversationUser == null) {
return
}

webSocketInstance = WebSocketConnectionHelper.getMagicWebSocketInstanceForUserId(conversationUser.id!!)
webSocketInstance = WebSocketConnectionHelper.getWebSocketInstanceForUserId(conversationUser.id!!)

if (webSocketInstance == null) {
Log.d(TAG, "magicWebSocketInstance became null")
Log.d(TAG, "webSocketInstance not set up. This should only happen when not using the HPB")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ public Result doWork() {
long internalUserId = data.getLong(BundleKeys.KEY_INTERNAL_USER_ID, -1);

List<User> userEntityObjectList = new ArrayList<>();
boolean userExists = userManager.getUserWithInternalId(internalUserId).isEmpty().blockingGet();
boolean userNotFound = userManager.getUserWithInternalId(internalUserId).isEmpty().blockingGet();

if (internalUserId == -1 || !userExists) {
if (internalUserId == -1 || userNotFound) {
userEntityObjectList = userManager.getUsers().blockingGet();
} else {
userEntityObjectList.add(userManager.getUserWithInternalId(internalUserId).blockingGet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@

import androidx.annotation.NonNull;
import androidx.work.Data;
import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkManager;
import androidx.work.Worker;
import androidx.work.WorkerParameters;
import autodagger.AutoInjector;
Expand Down Expand Up @@ -77,9 +75,9 @@ public Result doWork() {
long internalUserId = data.getLong(BundleKeys.KEY_INTERNAL_USER_ID, -1);

List<User> userEntityObjectList = new ArrayList<>();
boolean userExists = userManager.getUserWithInternalId(internalUserId).isEmpty().blockingGet();
boolean userNotFound = userManager.getUserWithInternalId(internalUserId).isEmpty().blockingGet();

if (internalUserId == -1 || !userExists) {
if (internalUserId == -1 || userNotFound) {
userEntityObjectList = userManager.getUsers().blockingGet();
} else {
userEntityObjectList.add(userManager.getUserWithInternalId(internalUserId).blockingGet());
Expand Down Expand Up @@ -115,6 +113,8 @@ public void onNext(SignalingSettingsOverall signalingSettingsOverall) {
.getExternalSignalingTicket());
}

user.setExternalSignalingServer(externalSignalingServer);

userManager.saveUser(user).subscribe(new SingleObserver<Integer>() {
@Override
public void onSubscribe(Disposable d) {
Expand Down Expand Up @@ -157,11 +157,6 @@ public void onComplete() {
});
}

OneTimeWorkRequest websocketConnectionsWorker = new OneTimeWorkRequest
.Builder(WebsocketConnectionsWorker.class)
.build();
WorkManager.getInstance().enqueue(websocketConnectionsWorker);

return Result.success();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
@AutoInjector(NextcloudTalkApplication.class)
public class WebSocketConnectionHelper {
public static final String TAG = "WebSocketConnectionHelper";
private static Map<Long, WebSocketInstance> magicWebSocketInstanceMap = new HashMap<>();
private static Map<Long, WebSocketInstance> webSocketInstanceMap = new HashMap<>();

@Inject
OkHttpClient okHttpClient;
Expand All @@ -59,11 +59,11 @@ public WebSocketConnectionHelper() {
}

@SuppressLint("LongLogTag")
public static synchronized WebSocketInstance getMagicWebSocketInstanceForUserId(long userId) {
WebSocketInstance webSocketInstance = magicWebSocketInstanceMap.get(userId);
public static synchronized WebSocketInstance getWebSocketInstanceForUserId(long userId) {
WebSocketInstance webSocketInstance = webSocketInstanceMap.get(userId);

if (webSocketInstance == null) {
Log.d(TAG, "No magicWebSocketInstance found for user " + userId);
Log.e(TAG, "No webSocketInstance found for user " + userId);
}

return webSocketInstance;
Expand All @@ -83,24 +83,24 @@ public static synchronized WebSocketInstance getExternalSignalingInstanceForServ
long userId = isGuest ? -1 : user.getId();

WebSocketInstance webSocketInstance;
if (userId != -1 && magicWebSocketInstanceMap.containsKey(user.getId()) && (webSocketInstance = magicWebSocketInstanceMap.get(user.getId())) != null) {
if (userId != -1 && webSocketInstanceMap.containsKey(user.getId()) && (webSocketInstance = webSocketInstanceMap.get(user.getId())) != null) {
return webSocketInstance;
} else {
if (userId == -1) {
deleteExternalSignalingInstanceForUserEntity(userId);
}
webSocketInstance = new WebSocketInstance(user, generatedURL, webSocketTicket);
magicWebSocketInstanceMap.put(user.getId(), webSocketInstance);
webSocketInstanceMap.put(user.getId(), webSocketInstance);
return webSocketInstance;
}
}

public static synchronized void deleteExternalSignalingInstanceForUserEntity(long id) {
WebSocketInstance webSocketInstance;
if ((webSocketInstance = magicWebSocketInstanceMap.get(id)) != null) {
if ((webSocketInstance = webSocketInstanceMap.get(id)) != null) {
if (webSocketInstance.isConnected()) {
webSocketInstance.sendBye();
magicWebSocketInstanceMap.remove(id);
webSocketInstanceMap.remove(id);
}
}
}
Expand Down

0 comments on commit a0d02db

Please sign in to comment.