Benoit Marty
2 years ago
committed by
Benoit Marty
12 changed files with 269 additions and 12 deletions
@ -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() |
||||||
|
} |
@ -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 |
||||||
|
} |
@ -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/" |
||||||
|
} |
@ -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 |
||||||
|
) |
@ -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> |
||||||
|
) |
@ -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 |
||||||
|
) |
@ -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 |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -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…
Reference in new issue