Retrieve notification - WIP

This commit is contained in:
Benoit Marty 2023-03-29 18:04:32 +02:00 committed by Benoit Marty
parent 2247639a89
commit d41f4fc954
16 changed files with 228 additions and 22 deletions

View file

@ -18,6 +18,7 @@ package io.element.android.libraries.push.impl
import com.squareup.anvil.annotations.ContributesBinding
import io.element.android.libraries.di.AppScope
import io.element.android.libraries.matrix.api.core.UserId
import io.element.android.libraries.push.api.PushService
import io.element.android.libraries.push.impl.notifications.NotificationDrawerManager
import javax.inject.Inject
@ -39,6 +40,10 @@ class DefaultPushService @Inject constructor(
notificationDrawerManager.notificationStyleChanged()
}
override suspend fun registerPusher(userId: UserId) {
pusherManager.registerPusher(userId)
}
override suspend fun testPush() {
pusherManager.testPush()
}

View file

@ -18,6 +18,7 @@ package io.element.android.libraries.push.impl
import io.element.android.libraries.matrix.api.auth.MatrixAuthenticationService
import io.element.android.libraries.matrix.api.core.SessionId
import io.element.android.libraries.matrix.api.core.UserId
import io.element.android.libraries.matrix.api.pusher.SetHttpPusherData
import io.element.android.libraries.push.impl.clientsecret.PushClientSecret
import io.element.android.libraries.push.impl.config.PushConfig
@ -39,6 +40,7 @@ class PushersManager @Inject constructor(
private val pushClientSecret: PushClientSecret,
private val sessionStore: SessionStore,
private val matrixAuthenticationService: MatrixAuthenticationService,
private val fcmHelper: FcmHelper,
) {
suspend fun testPush() {
pushGatewayNotifyRequest.execute(
@ -55,6 +57,7 @@ class PushersManager @Inject constructor(
return enqueueRegisterPusher(pushKey, PushConfig.pusher_http_url)
}
// TODO Rename
suspend fun enqueueRegisterPusher(
pushKey: String,
gateway: String
@ -68,6 +71,14 @@ class PushersManager @Inject constructor(
}
}
suspend fun registerPusher(userId: UserId) {
val pushKey = fcmHelper.getFcmToken() ?: return
// Register the pusher for the session
val client = matrixAuthenticationService.restoreSession(userId).getOrNull() ?: return
client.pushersService().setHttpPusher(createHttpPusher(pushKey, PushConfig.pusher_http_url, userId.value))
// Close sessions?
}
private suspend fun createHttpPusher(
pushKey: String,
gateway: String,

View file

@ -27,6 +27,8 @@ import io.element.android.libraries.androidutils.network.WifiDetector
import io.element.android.libraries.core.log.logger.LoggerTag
import io.element.android.libraries.core.meta.BuildMeta
import io.element.android.libraries.di.ApplicationContext
import io.element.android.libraries.matrix.api.auth.MatrixAuthenticationService
import io.element.android.libraries.matrix.api.core.SessionId
import io.element.android.libraries.push.api.store.PushDataStore
import io.element.android.libraries.push.impl.clientsecret.PushClientSecret
import io.element.android.libraries.push.impl.model.PushData
@ -34,11 +36,7 @@ import io.element.android.libraries.push.impl.notifications.NotifiableEventResol
import io.element.android.libraries.push.impl.notifications.NotificationActionIds
import io.element.android.libraries.push.impl.notifications.NotificationDrawerManager
import io.element.android.libraries.push.impl.store.DefaultPushDataStore
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.*
import timber.log.Timber
import javax.inject.Inject
@ -53,7 +51,8 @@ class VectorPushHandler @Inject constructor(
private val pushClientSecret: PushClientSecret,
private val actionIds: NotificationActionIds,
@ApplicationContext private val context: Context,
private val buildMeta: BuildMeta
private val buildMeta: BuildMeta,
private val matrixAuthenticationService: MatrixAuthenticationService,
) {
private val coroutineScope = CoroutineScope(SupervisorJob())
@ -115,9 +114,38 @@ class VectorPushHandler @Inject constructor(
Timber.tag(loggerTag.value).d("## handleInternal()")
}
pushData.roomId ?: return
pushData.eventId ?: return
val clientSecret = pushData.clientSecret
val userId = if (clientSecret == null) {
// Should not happen. In this case, restore default session
null
} else {
// Get userId from client secret
pushClientSecret.getUserIdFromSecret(clientSecret)
} ?: run {
matrixAuthenticationService.getLatestSessionId()?.value
}
if (userId == null) {
Timber.w("Unable to get a session")
return
}
// Restore session
val session = matrixAuthenticationService.restoreSession(SessionId(userId)).getOrNull() ?: return
// TODO EAx, no need for a session?
val notificationData = session.notificationService().getNotification(
userId = userId,
roomId = pushData.roomId,
eventId = pushData.eventId,
)
Timber.w("Notification: $notificationData")
// TODO Display notification
/* TODO EAx
- Retrieve secret and use pushClientSecret
- Open matching session
- get the event
- display the notif

View file

@ -27,6 +27,5 @@ data class PushData(
val eventId: String?,
val roomId: String?,
val unread: Int?,
// TODO EAx Client secret
val clientSecret: String?,
)

View file

@ -25,19 +25,22 @@ import io.element.android.libraries.matrix.api.core.MatrixPatterns
* "event_id":"$anEventId",
* "room_id":"!aRoomId",
* "unread":"1",
* "prio":"high"
* "prio":"high",
* "cs":"<client_secret>"
* }
* </pre>
* .
*/
data class PushDataFcm(
val eventId: String?,
val roomId: String?,
var unread: Int?,
val eventId: String?,
val roomId: String?,
var unread: Int?,
val clientSecret: String?
)
fun PushDataFcm.toPushData() = PushData(
eventId = eventId?.takeIf { MatrixPatterns.isEventId(it) },
roomId = roomId?.takeIf { MatrixPatterns.isRoomId(it) },
unread = unread
eventId = eventId?.takeIf { MatrixPatterns.isEventId(it) },
roomId = roomId?.takeIf { MatrixPatterns.isRoomId(it) },
unread = unread,
clientSecret = clientSecret,
)

View file

@ -38,7 +38,7 @@ import kotlinx.serialization.Serializable
*/
@Serializable
data class PushDataUnifiedPush(
val notification: PushDataUnifiedPushNotification?
val notification: PushDataUnifiedPushNotification?
)
@Serializable
@ -50,11 +50,12 @@ data class PushDataUnifiedPushNotification(
@Serializable
data class PushDataUnifiedPushCounts(
@SerialName("unread") val unread: Int?
@SerialName("unread") val unread: Int?
)
fun PushDataUnifiedPush.toPushData() = PushData(
eventId = notification?.eventId?.takeIf { MatrixPatterns.isEventId(it) },
roomId = notification?.roomId?.takeIf { MatrixPatterns.isRoomId(it) },
unread = notification?.counts?.unread
eventId = notification?.eventId?.takeIf { MatrixPatterns.isEventId(it) },
roomId = notification?.roomId?.takeIf { MatrixPatterns.isRoomId(it) },
unread = notification?.counts?.unread,
clientSecret = null // TODO EAx check how client secret will be sent through UnifiedPush
)

View file

@ -50,6 +50,7 @@ class PushParser @Inject constructor() {
eventId = message["event_id"],
roomId = message["room_id"],
unread = message["unread"]?.let { tryOrNull { Integer.parseInt(it) } },
clientSecret = message["cs"],
)
return pushDataFcm.toPushData()
}