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

Loading…
Cancel
Save