events: send new event when user got new chat message

This commit is contained in:
Alibek Omarov 2020-10-04 17:53:03 +03:00
parent 75d7ff1595
commit 8223f0223e
2 changed files with 31 additions and 32 deletions

View File

@ -24,3 +24,4 @@ data class MainTabsChangedEvent(val newTabs: List<TabData>) : Dispatchable
data class PollVoteEvent(val statusId: String, val poll: Poll) : Dispatchable data class PollVoteEvent(val statusId: String, val poll: Poll) : Dispatchable
data class DomainMuteEvent(val instance: String): Dispatchable data class DomainMuteEvent(val instance: String): Dispatchable
data class ChatMessageDeliveredEvent(val chatMsg: ChatMessage) : Dispatchable data class ChatMessageDeliveredEvent(val chatMsg: ChatMessage) : Dispatchable
data class ChatMessageReceivedEvent(val chatMsg: ChatMessage) : Dispatchable

View File

@ -13,6 +13,7 @@ import androidx.core.app.ServiceCompat
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import com.google.gson.Gson import com.google.gson.Gson
import com.keylesspalace.tusky.R import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.appstore.ChatMessageReceivedEvent
import com.keylesspalace.tusky.appstore.EventHub import com.keylesspalace.tusky.appstore.EventHub
import com.keylesspalace.tusky.components.notifications.NotificationHelper import com.keylesspalace.tusky.components.notifications.NotificationHelper
import com.keylesspalace.tusky.db.AccountEntity import com.keylesspalace.tusky.db.AccountEntity
@ -105,11 +106,8 @@ class StreamingService: Service(), Injectable {
sockets[account.id] = client.newWebSocket( sockets[account.id] = client.newWebSocket(
request, request,
StreamingListener( makeStreamingListener(
"${account.fullName}/user:notification", "${account.fullName}/user:notification",
this,
gson,
accountManager,
account account
) )
) )
@ -181,39 +179,39 @@ class StreamingService: Service(), Injectable {
} }
} }
class StreamingListener( private fun makeStreamingListener(tag: String, account: AccountEntity) : WebSocketListener {
val tag: String, return object : WebSocketListener() {
val context: Context, override fun onOpen(webSocket: WebSocket, response: Response) {
val gson: Gson, Log.d(TAG, "Stream connected to: $tag")
val accountManager: AccountManager, }
val account: AccountEntity
) : WebSocketListener() {
override fun onOpen(webSocket: WebSocket, response: Response) {
Log.d(TAG, "Stream connected to: $tag")
}
override fun onClosed(webSocket: WebSocket, code: Int, reason: String) { override fun onClosed(webSocket: WebSocket, code: Int, reason: String) {
Log.d(TAG, "Stream closed for: $tag") Log.d(TAG, "Stream closed for: $tag")
} }
override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) { override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
Log.d(TAG, "Stream failed for $tag", t) Log.d(TAG, "Stream failed for $tag", t)
} }
override fun onMessage(webSocket: WebSocket, text: String) { override fun onMessage(webSocket: WebSocket, text: String) {
val event = gson.fromJson(text, StreamEvent::class.java) val event = gson.fromJson(text, StreamEvent::class.java)
when(event.event) { when(event.event) {
StreamEvent.EventType.NOTIFICATION -> { StreamEvent.EventType.NOTIFICATION -> {
val notification = gson.fromJson(event.payload, Notification::class.java) val notification = gson.fromJson(event.payload, Notification::class.java)
NotificationHelper.make(context, notification, account, true) NotificationHelper.make(this@StreamingService, notification, account, true)
if(account.lastNotificationId.isLessThan(notification.id)) { if(notification.type == Notification.Type.CHAT_MESSAGE) {
account.lastNotificationId = notification.id eventHub.dispatch(ChatMessageReceivedEvent(notification.chatMessage!!))
accountManager.saveAccount(account) }
if(account.lastNotificationId.isLessThan(notification.id)) {
account.lastNotificationId = notification.id
accountManager.saveAccount(account)
}
}
else -> {
Log.d(TAG, "Unknown event type: ${event.event}")
} }
}
else -> {
Log.d(TAG, "Unknown event type: ${event.event}")
} }
} }
} }