Skip to content

Commit 81ef9b2

Browse files
committed
handle loading of initial messages depending on HPB existence
Signed-off-by: Marcel Hibbe <[email protected]>
1 parent 4ca77d7 commit 81ef9b2

File tree

4 files changed

+34
-15
lines changed

4 files changed

+34
-15
lines changed

app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,9 @@ class ChatActivity :
830830

831831
chatViewModel.loadMessages(
832832
withCredentials = credentials!!,
833-
withUrl = urlForChatting
833+
withUrl = urlForChatting,
834+
hasHighPerformanceBackend =
835+
WebSocketConnectionHelper.getWebSocketInstanceForUser(conversationUser) != null
834836
)
835837
} else {
836838
Log.w(
@@ -1042,7 +1044,6 @@ class ChatActivity :
10421044
}
10431045
.launchIn(lifecycleScope)
10441046

1045-
10461047
this.lifecycleScope.launch {
10471048
chatViewModel.getRemoveMessageFlow
10481049
.onEach {
@@ -2764,6 +2765,10 @@ class ChatActivity :
27642765
if (mentionAutocomplete != null && mentionAutocomplete!!.isPopupShowing) {
27652766
mentionAutocomplete?.dismissPopup()
27662767
}
2768+
2769+
// TODO: when updating remote last read message in onPause, there is a race condition with loading conversations
2770+
// for conversation list. It may or may not include info about the sent last read message...
2771+
// -> save this field offline in conversation?
27672772
updateRemoteLastReadMessageIfNeeded()
27682773

27692774
adapter = null
@@ -2920,6 +2925,7 @@ class ChatActivity :
29202925

29212926
private fun setupWebsocket() {
29222927
if (currentConversation == null || conversationUser == null) {
2928+
Log.e(TAG, "setupWebsocket: currentConversation or conversationUser is null")
29232929
return
29242930
}
29252931

app/src/main/java/com/nextcloud/talk/chat/data/ChatMessageRepository.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ interface ChatMessageRepository : LifecycleAwareManager {
5050

5151
fun updateConversation(conversationModel: ConversationModel)
5252

53-
fun initScopeAndLoadInitialMessages(withNetworkParams: Bundle)
53+
fun initScopeAndLoadInitialMessages(withNetworkParams: Bundle, hasHighPerformanceBackend: Boolean)
5454

5555
/**
5656
* Loads messages from local storage. If the messages are not found, then it

app/src/main/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepository.kt

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,15 @@ class OfflineFirstChatRepository @Inject constructor(
140140
this.conversationModel = conversationModel
141141
}
142142

143-
override fun initScopeAndLoadInitialMessages(withNetworkParams: Bundle) {
143+
override fun initScopeAndLoadInitialMessages(withNetworkParams: Bundle, hasHighPerformanceBackend: Boolean) {
144144
scope = CoroutineScope(Dispatchers.IO)
145-
loadInitialMessages(withNetworkParams)
145+
loadInitialMessages(
146+
withNetworkParams,
147+
hasHighPerformanceBackend
148+
)
146149
}
147150

148-
private fun loadInitialMessages(withNetworkParams: Bundle): Job =
151+
private fun loadInitialMessages(withNetworkParams: Bundle, hasHighPerformanceBackend: Boolean): Job =
149152
scope.launch {
150153
Log.d(TAG, "---- loadInitialMessages ------------")
151154
newXChatLastCommonRead = conversationModel.lastCommonReadMessage
@@ -161,19 +164,27 @@ class OfflineFirstChatRepository @Inject constructor(
161164
val weHaveAtLeastTheLastReadMessage = newestMessageIdFromDb >= conversationModel.lastReadMessage.toLong()
162165
Log.d(TAG, "weAlreadyHaveSomeOfflineMessages:$weAlreadyHaveSomeOfflineMessages")
163166
Log.d(TAG, "weHaveAtLeastTheLastReadMessage:$weHaveAtLeastTheLastReadMessage")
167+
Log.d(TAG, "hasHighPerformanceBackend:$hasHighPerformanceBackend")
164168

165-
if (weAlreadyHaveSomeOfflineMessages && weHaveAtLeastTheLastReadMessage) {
169+
if (weAlreadyHaveSomeOfflineMessages && weHaveAtLeastTheLastReadMessage && !hasHighPerformanceBackend) {
166170
Log.d(
167171
TAG,
168172
"Initial online request is skipped because offline messages are up to date" +
169173
" until lastReadMessage"
170174
)
171175

172-
// This is a problem! No long polling should be done when we have the HPB. How to initially get the
173-
// messages newer than TheLastReadMessage?
174-
Log.d(TAG, "For messages newer than lastRead, lookIntoFuture will load them.")
176+
// For messages newer than lastRead, lookIntoFuture will load them.
177+
// We must only end up here when NO HPB is used!
178+
// If a HPB is used, longPolling is not available to handle loading of newer messages.
179+
// When a HPB is used the initial request must be made.
175180
} else {
176-
if (!weAlreadyHaveSomeOfflineMessages) {
181+
if (hasHighPerformanceBackend) {
182+
Log.d(
183+
TAG,
184+
"An online request for newest 100 messages is made because HPB is used (No long " +
185+
"polling available to catch up with messages newer than last read.)"
186+
)
187+
} else if (!weAlreadyHaveSomeOfflineMessages) {
177188
Log.d(TAG, "An online request for newest 100 messages is made because offline chat is empty")
178189
if (networkMonitor.isOnline.value.not()) {
179190
_generalUIFlow.emit(ChatActivity.NO_OFFLINE_MESSAGES_FOUND)
@@ -208,8 +219,9 @@ class OfflineFirstChatRepository @Inject constructor(
208219

209220
handleMessagesFromDb(newestMessageIdFromDb)
210221

211-
// temp disabled to test only signaling
212-
// initMessagePolling(newestMessageIdFromDb)
222+
if (!hasHighPerformanceBackend) {
223+
initMessagePolling(newestMessageIdFromDb)
224+
}
213225
}
214226

215227
private suspend fun handleMessagesFromDb(newestMessageIdFromDb: Long) {

app/src/main/java/com/nextcloud/talk/chat/viewmodels/ChatViewModel.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -508,12 +508,13 @@ class ChatViewModel @Inject constructor(
508508
}
509509
}
510510

511-
fun loadMessages(withCredentials: String, withUrl: String) {
511+
fun loadMessages(withCredentials: String, withUrl: String, hasHighPerformanceBackend: Boolean) {
512512
val bundle = Bundle()
513513
bundle.putString(BundleKeys.KEY_CHAT_URL, withUrl)
514514
bundle.putString(BundleKeys.KEY_CREDENTIALS, withCredentials)
515515
chatRepository.initScopeAndLoadInitialMessages(
516-
withNetworkParams = bundle
516+
withNetworkParams = bundle,
517+
hasHighPerformanceBackend = hasHighPerformanceBackend
517518
)
518519
}
519520

0 commit comments

Comments
 (0)