Use session id returned from join room & more

1)
Use session id returned from join room
= introduce sessionIdAfterRoomJoined to make clear this is the session to use instead of currentConversation?.sessionId

See https://nextcloud-talk.readthedocs.io/en/latest/conversation/#get-user-s-conversations :
"'0' if not connected, otherwise an up to 512 character long string that is the identifier of the user's session making the request. Should only be used to pre-check if the user joined already with this session, but this might be outdated by the time of usage, so better check via Get list of participants in a conversation"

2)
Also, trigger getRoomInfo() or handleFromNotification() in onAttach() instead of in onViewBound.
onViewBound is not called when returning back from an other view (e.g. conversation infos) so after this, new messages were not handled.

Furthermore, getRoomInfo()/joinRoomWithPassword() in onViewBound and onAttach were sometimes both called, because the handling of validSessionId was buggy. This resulted in duplicated messages.

3)
Use ApplicationWideCurrentRoomHolder to set sessionId from call and check in ChatController if there is already a session for the room. If yes, use this instead to joinRoom again.
This is necessary for PictureInPicture mode. Otherwise, call would be left whenever ChatController joins room again.

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
Marcel Hibbe 2023-03-01 14:29:00 +01:00
parent 5d129ba03e
commit d75e235ba2
No known key found for this signature in database
GPG key ID: C793F8B59F43CE7B
2 changed files with 26 additions and 30 deletions

View file

@ -1591,7 +1591,7 @@ public class CallActivity extends CallBaseActivity {
Log.d(TAG, " new callSession by joinRoom= " + callSession); Log.d(TAG, " new callSession by joinRoom= " + callSession);
ApplicationWideCurrentRoomHolder.getInstance().setSession(callSession); ApplicationWideCurrentRoomHolder.getInstance().setSession(callSession);
ApplicationWideCurrentRoomHolder.getInstance().setCurrentRoomId(roomId); ApplicationWideCurrentRoomHolder.getInstance().setCurrentRoomId(conversation.getRoomId());
ApplicationWideCurrentRoomHolder.getInstance().setCurrentRoomToken(roomToken); ApplicationWideCurrentRoomHolder.getInstance().setCurrentRoomToken(roomToken);
ApplicationWideCurrentRoomHolder.getInstance().setUserInRoom(conversationUser); ApplicationWideCurrentRoomHolder.getInstance().setUserInRoom(conversationUser);
callOrJoinRoomViaWebSocket(); callOrJoinRoomViaWebSocket();

View file

@ -251,6 +251,7 @@ class ChatController(args: Bundle) :
val disposables = DisposableSet() val disposables = DisposableSet()
var sessionIdAfterRoomJoined: String? = null
var roomToken: String? = null var roomToken: String? = null
val conversationUser: User? val conversationUser: User?
private val roomPassword: String private val roomPassword: String
@ -526,13 +527,10 @@ class ChatController(args: Bundle) :
override fun onViewBound(view: View) { override fun onViewBound(view: View) {
Log.d(TAG, "onViewBound: " + System.identityHashCode(this).toString()) Log.d(TAG, "onViewBound: " + System.identityHashCode(this).toString())
actionBar?.show() actionBar?.show()
var adapterWasNull = false
if (adapter == null) { if (adapter == null) {
binding?.progressBar?.visibility = View.VISIBLE binding?.progressBar?.visibility = View.VISIBLE
adapterWasNull = true
val messageHolders = MessageHolders() val messageHolders = MessageHolders()
val profileBottomSheet = ProfileBottomSheet(ncApi, conversationUser!!, router) val profileBottomSheet = ProfileBottomSheet(ncApi, conversationUser!!, router)
@ -917,14 +915,6 @@ class ChatController(args: Bundle) :
setTitle() setTitle()
} }
if (adapterWasNull) {
Log.d(TAG, "starting for the first time (because adapter was null)")
if (TextUtils.isEmpty(roomToken)) {
handleFromNotification()
} else {
getRoomInfo()
}
}
super.onViewBound(view) super.onViewBound(view)
} }
@ -1787,8 +1777,8 @@ class ChatController(args: Bundle) :
private fun validSessionId(): Boolean { private fun validSessionId(): Boolean {
return currentConversation != null && return currentConversation != null &&
!TextUtils.isEmpty(currentConversation?.sessionId) && sessionIdAfterRoomJoined?.isNotEmpty() == true &&
currentConversation?.sessionId != "0" sessionIdAfterRoomJoined != "0"
} }
@Suppress("Detekt.TooGenericExceptionCaught") @Suppress("Detekt.TooGenericExceptionCaught")
@ -1807,10 +1797,6 @@ class ChatController(args: Bundle) :
activity?.findViewById<View>(R.id.toolbar)?.setOnClickListener { v -> showConversationInfoScreen() } activity?.findViewById<View>(R.id.toolbar)?.setOnClickListener { v -> showConversationInfoScreen() }
} }
ApplicationWideCurrentRoomHolder.getInstance().currentRoomId = roomId
ApplicationWideCurrentRoomHolder.getInstance().currentRoomToken = roomToken
ApplicationWideCurrentRoomHolder.getInstance().userInRoom = conversationUser
val smileyButton = binding?.messageInputView?.findViewById<ImageButton>(R.id.smileyButton) val smileyButton = binding?.messageInputView?.findViewById<ImageButton>(R.id.smileyButton)
emojiPopup = binding?.messageInputView?.inputEditText?.let { emojiPopup = binding?.messageInputView?.inputEditText?.let {
@ -1850,9 +1836,10 @@ class ChatController(args: Bundle) :
cancelNotificationsForCurrentConversation() cancelNotificationsForCurrentConversation()
if (!validSessionId()) { if (TextUtils.isEmpty(roomToken)) {
Log.d(TAG, "execute joinRoomWithPassword in onAttach") handleFromNotification()
joinRoomWithPassword() } else {
getRoomInfo()
} }
} }
@ -1954,6 +1941,17 @@ class ChatController(args: Bundle) :
} }
private fun joinRoomWithPassword() { private fun joinRoomWithPassword() {
// if ApplicationWideCurrentRoomHolder contains a session (because a call is active), then keep the sessionId
if (ApplicationWideCurrentRoomHolder.getInstance().currentRoomId ==
currentConversation!!.roomId
) {
sessionIdAfterRoomJoined = ApplicationWideCurrentRoomHolder.getInstance().session
ApplicationWideCurrentRoomHolder.getInstance().currentRoomId = roomId
ApplicationWideCurrentRoomHolder.getInstance().currentRoomToken = roomToken
ApplicationWideCurrentRoomHolder.getInstance().userInRoom = conversationUser
}
if (!validSessionId()) { if (!validSessionId()) {
Log.d(TAG, "sessionID was not valid -> joinRoom") Log.d(TAG, "sessionID was not valid -> joinRoom")
var apiVersion = 1 var apiVersion = 1
@ -1980,12 +1978,11 @@ class ChatController(args: Bundle) :
@Suppress("Detekt.TooGenericExceptionCaught") @Suppress("Detekt.TooGenericExceptionCaught")
override fun onNext(roomOverall: RoomOverall) { override fun onNext(roomOverall: RoomOverall) {
Log.d(TAG, "joinRoomWithPassword - joinRoom - got response: $startNanoTime") Log.d(TAG, "joinRoomWithPassword - joinRoom - got response: $startNanoTime")
currentConversation?.sessionId = roomOverall.ocs!!.data!!.sessionId sessionIdAfterRoomJoined = roomOverall.ocs!!.data!!.sessionId
logConversationInfos("joinRoomWithPassword#onNext") logConversationInfos("joinRoomWithPassword#onNext")
ApplicationWideCurrentRoomHolder.getInstance().session = ApplicationWideCurrentRoomHolder.getInstance().session = sessionIdAfterRoomJoined
currentConversation?.sessionId
// FIXME The web socket should be set up in onAttach(). It is currently setup after joining the // 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 // room to "ensure" (rather, increase the chances) that the WebsocketConnectionsWorker job
@ -2007,7 +2004,7 @@ class ChatController(args: Bundle) :
if (webSocketInstance != null) { if (webSocketInstance != null) {
webSocketInstance?.joinRoomWithRoomTokenAndSession( webSocketInstance?.joinRoomWithRoomTokenAndSession(
roomToken!!, roomToken!!,
currentConversation?.sessionId sessionIdAfterRoomJoined
) )
} }
if (startCallFromNotification != null && startCallFromNotification ?: false) { if (startCallFromNotification != null && startCallFromNotification ?: false) {
@ -2031,11 +2028,10 @@ class ChatController(args: Bundle) :
} else { } else {
Log.d(TAG, "sessionID was valid -> skip joinRoom") Log.d(TAG, "sessionID was valid -> skip joinRoom")
ApplicationWideCurrentRoomHolder.getInstance().session = currentConversation?.sessionId
if (webSocketInstance != null) { if (webSocketInstance != null) {
webSocketInstance?.joinRoomWithRoomTokenAndSession( webSocketInstance?.joinRoomWithRoomTokenAndSession(
roomToken!!, roomToken!!,
currentConversation?.sessionId sessionIdAfterRoomJoined
) )
} }
checkLobbyState() checkLobbyState()
@ -2089,7 +2085,7 @@ class ChatController(args: Bundle) :
if (webSocketInstance != null && currentConversation != null) { if (webSocketInstance != null && currentConversation != null) {
webSocketInstance?.joinRoomWithRoomTokenAndSession( webSocketInstance?.joinRoomWithRoomTokenAndSession(
"", "",
currentConversation?.sessionId sessionIdAfterRoomJoined
) )
} else { } else {
Log.e(TAG, "magicWebSocketInstance or currentConversation were null! Failed to leave the room!") Log.e(TAG, "magicWebSocketInstance or currentConversation were null! Failed to leave the room!")
@ -2102,7 +2098,7 @@ class ChatController(args: Bundle) :
} }
} }
currentConversation?.sessionId = "0" sessionIdAfterRoomJoined = "0"
if (remapChatModel != null && funToCallWhenLeaveSuccessful != null) { if (remapChatModel != null && funToCallWhenLeaveSuccessful != null) {
Log.d(TAG, "a callback action was set and is now executed because room was left successfully") Log.d(TAG, "a callback action was set and is now executed because room was left successfully")
@ -3498,7 +3494,7 @@ class ChatController(args: Bundle) :
Log.d(TAG, " | ChatController: " + System.identityHashCode(this).toString()) Log.d(TAG, " | ChatController: " + System.identityHashCode(this).toString())
Log.d(TAG, " | roomToken: $roomToken") Log.d(TAG, " | roomToken: $roomToken")
Log.d(TAG, " | currentConversation?.displayName: ${currentConversation?.displayName}") Log.d(TAG, " | currentConversation?.displayName: ${currentConversation?.displayName}")
Log.d(TAG, " | currentConversation?.sessionId: ${currentConversation?.sessionId}") Log.d(TAG, " | sessionIdAfterRoomJoined: $sessionIdAfterRoomJoined")
Log.d(TAG, " |-----------------------------------------------") Log.d(TAG, " |-----------------------------------------------")
} }