events: send new event when user got new chat message

main
Alibek Omarov 4 years ago
parent c87971ca25
commit 61c70586ec
  1. 1
      app/src/main/java/com/keylesspalace/tusky/appstore/Events.kt
  2. 22
      app/src/main/java/com/keylesspalace/tusky/service/StreamingService.kt

@ -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

@ -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,13 +179,8 @@ class StreamingService: Service(), Injectable {
} }
} }
class StreamingListener( private fun makeStreamingListener(tag: String, account: AccountEntity) : WebSocketListener {
val tag: String, return object : WebSocketListener() {
val context: Context,
val gson: Gson,
val accountManager: AccountManager,
val account: AccountEntity
) : WebSocketListener() {
override fun onOpen(webSocket: WebSocket, response: Response) { override fun onOpen(webSocket: WebSocket, response: Response) {
Log.d(TAG, "Stream connected to: $tag") Log.d(TAG, "Stream connected to: $tag")
} }
@ -205,7 +198,11 @@ class StreamingService: Service(), Injectable {
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(notification.type == Notification.Type.CHAT_MESSAGE) {
eventHub.dispatch(ChatMessageReceivedEvent(notification.chatMessage!!))
}
if(account.lastNotificationId.isLessThan(notification.id)) { if(account.lastNotificationId.isLessThan(notification.id)) {
account.lastNotificationId = notification.id account.lastNotificationId = notification.id
@ -218,4 +215,5 @@ class StreamingService: Service(), Injectable {
} }
} }
} }
}
} }
Loading…
Cancel
Save