Browse Source

Push: be able to test Push

Create `:libraries:network`
test/jme/compound-poc
Benoit Marty 2 years ago committed by Benoit Marty
parent
commit
1aa699f522
  1. 2
      libraries/push/api/src/main/kotlin/io/element/android/libraries/push/api/PushService.kt
  2. 21
      libraries/push/api/src/main/kotlin/io/element/android/libraries/push/api/gateway/PushGatewayFailure.kt
  3. 4
      libraries/push/impl/build.gradle.kts
  4. 5
      libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/DefaultPushService.kt
  5. 20
      libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/PushersManager.kt
  6. 30
      libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/pushgateway/PushGatewayAPI.kt
  7. 22
      libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/pushgateway/PushGatewayConfig.kt
  8. 34
      libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/pushgateway/PushGatewayDevice.kt
  9. 32
      libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/pushgateway/PushGatewayNotification.kt
  10. 29
      libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/pushgateway/PushGatewayNotifyBody.kt
  11. 56
      libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/pushgateway/PushGatewayNotifyRequest.kt
  12. 26
      libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/pushgateway/PushGatewayNotifyResponse.kt

2
libraries/push/api/src/main/kotlin/io/element/android/libraries/push/api/PushService.kt

@ -20,4 +20,6 @@ interface PushService { @@ -20,4 +20,6 @@ interface PushService {
fun setCurrentRoom(roomId: String?)
fun setCurrentThread(threadId: String?)
fun notificationStyleChanged()
suspend fun testPush()
}

21
libraries/push/api/src/main/kotlin/io/element/android/libraries/push/api/gateway/PushGatewayFailure.kt

@ -0,0 +1,21 @@ @@ -0,0 +1,21 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.libraries.push.api.gateway
sealed class PushGatewayFailure : Throwable(cause = null) {
object PusherRejected : PushGatewayFailure()
}

4
libraries/push/impl/build.gradle.kts

@ -33,6 +33,7 @@ dependencies { @@ -33,6 +33,7 @@ dependencies {
implementation(libs.androidx.corektx)
implementation(libs.androidx.datastore.preferences)
implementation(libs.androidx.lifecycle.process)
implementation(libs.network.retrofit)
implementation(libs.serialization.json)
implementation(projects.libraries.architecture)
@ -41,16 +42,17 @@ dependencies { @@ -41,16 +42,17 @@ dependencies {
implementation(projects.libraries.core)
implementation(projects.libraries.di)
implementation(projects.libraries.androidutils)
implementation(projects.libraries.network)
implementation(projects.libraries.matrix.api)
implementation(projects.libraries.push.api)
implementation(projects.services.toolbox.api)
api("me.gujun.android:span:1.7") {
exclude(group = "com.android.support", module = "support-annotations")
}
implementation(platform(libs.google.firebase.bom))
implementation("com.google.firebase:firebase-messaging-ktx")

5
libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/DefaultPushService.kt

@ -25,6 +25,7 @@ import javax.inject.Inject @@ -25,6 +25,7 @@ import javax.inject.Inject
@ContributesBinding(AppScope::class)
class DefaultPushService @Inject constructor(
private val notificationDrawerManager: NotificationDrawerManager,
private val pusherManager: PushersManager,
) : PushService {
override fun setCurrentRoom(roomId: String?) {
notificationDrawerManager.setCurrentRoom(roomId)
@ -37,4 +38,8 @@ class DefaultPushService @Inject constructor( @@ -37,4 +38,8 @@ class DefaultPushService @Inject constructor(
override fun notificationStyleChanged() {
notificationDrawerManager.notificationStyleChanged()
}
override suspend fun testPush() {
pusherManager.testPush()
}
}

20
libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/PushersManager.kt

@ -17,6 +17,7 @@ @@ -17,6 +17,7 @@
package io.element.android.libraries.push.impl
import io.element.android.libraries.push.impl.config.PushConfig
import io.element.android.libraries.push.impl.pushgateway.PushGatewayNotifyRequest
import io.element.android.libraries.toolbox.api.appname.AppNameProvider
import java.util.UUID
import javax.inject.Inject
@ -30,19 +31,17 @@ class PushersManager @Inject constructor( @@ -30,19 +31,17 @@ class PushersManager @Inject constructor(
// private val localeProvider: LocaleProvider,
private val appNameProvider: AppNameProvider,
// private val getDeviceInfoUseCase: GetDeviceInfoUseCase,
private val pushGatewayNotifyRequest: PushGatewayNotifyRequest,
) {
suspend fun testPush() {
/*
val currentSession = activeSessionHolder.getActiveSession()
currentSession.pushersService().testPush(
unifiedPushHelper.getPushGateway() ?: return,
PushConfig.pusher_app_id,
unifiedPushHelper.getEndpointOrToken().orEmpty(),
TEST_EVENT_ID
pushGatewayNotifyRequest.execute(
PushGatewayNotifyRequest.Params(
url = unifiedPushHelper.getPushGateway() ?: return,
appId = PushConfig.pusher_app_id,
pushKey = unifiedPushHelper.getEndpointOrToken().orEmpty(),
eventId = TEST_EVENT_ID
)
)
*/
}
fun enqueueRegisterPusherWithFcmKey(pushKey: String): UUID {
@ -107,7 +106,6 @@ class PushersManager @Inject constructor( @@ -107,7 +106,6 @@ class PushersManager @Inject constructor(
}
*/
suspend fun unregisterEmailPusher(email: String) {
// val currentSession = activeSessionHolder.getSafeActiveSession() ?: return
// currentSession.pushersService().removeEmailPusher(email)

30
libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/pushgateway/PushGatewayAPI.kt

@ -0,0 +1,30 @@ @@ -0,0 +1,30 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.libraries.push.impl.pushgateway
import retrofit2.http.Body
import retrofit2.http.POST
internal interface PushGatewayAPI {
/**
* Ask the Push Gateway to send a push to the current device.
*
* Ref: https://matrix.org/docs/spec/push_gateway/r0.1.1#post-matrix-push-v1-notify
*/
@POST(PushGatewayConfig.URI_PUSH_GATEWAY_PREFIX_PATH + "notify")
suspend fun notify(@Body body: PushGatewayNotifyBody): PushGatewayNotifyResponse
}

22
libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/pushgateway/PushGatewayConfig.kt

@ -0,0 +1,22 @@ @@ -0,0 +1,22 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.libraries.push.impl.pushgateway
object PushGatewayConfig {
// Push Gateway
const val URI_PUSH_GATEWAY_PREFIX_PATH = "_matrix/push/v1/"
}

34
libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/pushgateway/PushGatewayDevice.kt

@ -0,0 +1,34 @@ @@ -0,0 +1,34 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.libraries.push.impl.pushgateway
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
internal data class PushGatewayDevice(
/**
* Required. The app_id given when the pusher was created.
*/
@SerialName("app_id")
val appId: String,
/**
* Required. The pushkey given when the pusher was created.
*/
@SerialName("pushkey")
val pushKey: String
)

32
libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/pushgateway/PushGatewayNotification.kt

@ -0,0 +1,32 @@ @@ -0,0 +1,32 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.libraries.push.impl.pushgateway
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
internal data class PushGatewayNotification(
@SerialName("event_id")
val eventId: String,
/**
* Required. This is an array of devices that the notification should be sent to.
*/
@SerialName("devices")
val devices: List<PushGatewayDevice>
)

29
libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/pushgateway/PushGatewayNotifyBody.kt

@ -0,0 +1,29 @@ @@ -0,0 +1,29 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.libraries.push.impl.pushgateway
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
internal data class PushGatewayNotifyBody(
/**
* Required. Information about the push notification
*/
@SerialName("notification")
val notification: PushGatewayNotification
)

56
libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/pushgateway/PushGatewayNotifyRequest.kt

@ -0,0 +1,56 @@ @@ -0,0 +1,56 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.libraries.push.impl.pushgateway
import io.element.android.libraries.network.RetrofitFactory
import io.element.android.libraries.push.api.gateway.PushGatewayFailure
import javax.inject.Inject
class PushGatewayNotifyRequest @Inject constructor(
private val retrofitFactory: RetrofitFactory,
) {
data class Params(
val url: String,
val appId: String,
val pushKey: String,
val eventId: String
)
suspend fun execute(params: Params) {
val sygnalApi = retrofitFactory.create(
params.url.substringBefore(PushGatewayConfig.URI_PUSH_GATEWAY_PREFIX_PATH)
)
.create(PushGatewayAPI::class.java)
val response = sygnalApi.notify(
PushGatewayNotifyBody(
PushGatewayNotification(
eventId = params.eventId,
devices = listOf(
PushGatewayDevice(
params.appId,
params.pushKey
)
)
)
)
)
if (response.rejectedPushKeys.contains(params.pushKey)) {
throw PushGatewayFailure.PusherRejected
}
}
}

26
libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/pushgateway/PushGatewayNotifyResponse.kt

@ -0,0 +1,26 @@ @@ -0,0 +1,26 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.libraries.push.impl.pushgateway
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
internal data class PushGatewayNotifyResponse(
@SerialName("rejected")
val rejectedPushKeys: List<String>
)
Loading…
Cancel
Save