|
| 1 | +package social.bigbone.rx |
| 2 | + |
| 3 | +import io.reactivex.rxjava3.core.Completable |
| 4 | +import io.reactivex.rxjava3.core.Single |
| 5 | +import social.bigbone.MastodonClient |
| 6 | +import social.bigbone.api.entity.WebPushSubscription |
| 7 | +import social.bigbone.api.method.PushNotificationMethods |
| 8 | + |
| 9 | +/** |
| 10 | + * Reactive implementation of [PushNotificationMethods]. |
| 11 | + * Allows access to API methods with endpoints having an "api/vX/push" prefix. |
| 12 | + * @see <a href="https://docs.joinmastodon.org/methods/push/">Mastodon push notification API methods</a> |
| 13 | + */ |
| 14 | +class RxPushNotificationMethods(client: MastodonClient) { |
| 15 | + |
| 16 | + private val pushNotificationMethods = PushNotificationMethods(client) |
| 17 | + |
| 18 | + /** |
| 19 | + * Add a Web Push API subscription to receive notifications. |
| 20 | + * Each access token can have one push subscription. If you create a new subscription, the old subscription is deleted. |
| 21 | + * @param endpoint The endpoint URL that is called when a notification event occurs. |
| 22 | + * @param userPublicKey User agent public key. Base64 encoded string of a public key from a ECDH keypair using the prime256v1 curve. |
| 23 | + * @param userAuthSecret Auth secret, Base64 encoded string of 16 bytes of random data. |
| 24 | + * @param mention Receive mention notifications? |
| 25 | + * @param status Receive new subscribed account notifications? |
| 26 | + * @param reblog Receive reblog notifications? |
| 27 | + * @param follow Receive follow notifications? |
| 28 | + * @param followRequest Receive follow request notifications? |
| 29 | + * @param favourite Receive favourite notifications? |
| 30 | + * @param poll Receive poll notifications? |
| 31 | + * @param update Receive status edited notifications? |
| 32 | + * @param adminSignUp Receive new user signup notifications? Defaults to false. Must have a role with the appropriate permissions. |
| 33 | + * @param adminReport Receive new report notifications? Defaults to false. Must have a role with the appropriate permissions. |
| 34 | + * @param policy Specify which to receive push notifications from. |
| 35 | + * @see <a href="https://docs.joinmastodon.org/methods/push/#create">Mastodon API documentation: methods/push/#create</a> |
| 36 | + */ |
| 37 | + @JvmOverloads |
| 38 | + fun subscribePushNotification( |
| 39 | + endpoint: String, |
| 40 | + userPublicKey: String, |
| 41 | + userAuthSecret: String, |
| 42 | + mention: Boolean? = false, |
| 43 | + status: Boolean? = false, |
| 44 | + reblog: Boolean? = false, |
| 45 | + follow: Boolean? = false, |
| 46 | + followRequest: Boolean? = false, |
| 47 | + favourite: Boolean? = false, |
| 48 | + poll: Boolean? = false, |
| 49 | + update: Boolean? = false, |
| 50 | + adminSignUp: Boolean? = false, |
| 51 | + adminReport: Boolean? = false, |
| 52 | + policy: PushNotificationMethods.PushDataPolicy? = null |
| 53 | + ): Single<WebPushSubscription> = |
| 54 | + Single.fromCallable { |
| 55 | + pushNotificationMethods.subscribePushNotification( |
| 56 | + endpoint, |
| 57 | + userPublicKey, |
| 58 | + userAuthSecret, |
| 59 | + mention, |
| 60 | + status, |
| 61 | + reblog, |
| 62 | + follow, |
| 63 | + followRequest, |
| 64 | + favourite, |
| 65 | + poll, |
| 66 | + update, |
| 67 | + adminSignUp, |
| 68 | + adminReport, |
| 69 | + policy |
| 70 | + ).execute() |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Updates the current push subscription. Only the data part can be updated. |
| 75 | + * To change fundamentals, a new subscription must be created instead. |
| 76 | + * @param mention Receive mention notifications? |
| 77 | + * @param status Receive new subscribed account notifications? |
| 78 | + * @param reblog Receive reblog notifications? |
| 79 | + * @param follow Receive follow notifications? |
| 80 | + * @param followRequest Receive follow request notifications? |
| 81 | + * @param favourite Receive favourite notifications? |
| 82 | + * @param poll Receive poll notifications? |
| 83 | + * @param update Receive status edited notifications? |
| 84 | + * @param adminSignUp Receive new user signup notifications? Defaults to false. Must have a role with the appropriate permissions. |
| 85 | + * @param adminReport Receive new report notifications? Defaults to false. Must have a role with the appropriate permissions. |
| 86 | + * @param policy Specify which to receive push notifications from. |
| 87 | + * @see <a href="https://docs.joinmastodon.org/methods/push/#update">Mastodon API documentation: methods/push/#update</a> |
| 88 | + */ |
| 89 | + @JvmOverloads |
| 90 | + fun updatePushSubscription( |
| 91 | + mention: Boolean? = false, |
| 92 | + status: Boolean? = false, |
| 93 | + reblog: Boolean? = false, |
| 94 | + follow: Boolean? = false, |
| 95 | + followRequest: Boolean? = false, |
| 96 | + favourite: Boolean? = false, |
| 97 | + poll: Boolean? = false, |
| 98 | + update: Boolean? = false, |
| 99 | + adminSignUp: Boolean? = false, |
| 100 | + adminReport: Boolean? = false, |
| 101 | + policy: PushNotificationMethods.PushDataPolicy? = null |
| 102 | + ): Single<WebPushSubscription> = |
| 103 | + Single.fromCallable { |
| 104 | + pushNotificationMethods.updatePushSubscription( |
| 105 | + mention, |
| 106 | + status, |
| 107 | + reblog, |
| 108 | + follow, |
| 109 | + followRequest, |
| 110 | + favourite, |
| 111 | + poll, |
| 112 | + update, |
| 113 | + adminSignUp, |
| 114 | + adminReport, |
| 115 | + policy |
| 116 | + ).execute() |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * View the PushSubscription currently associated with this access token. |
| 121 | + * @see <a href="https://docs.joinmastodon.org/methods/push/#get">Mastodon API documentation: methods/push/#get</a> |
| 122 | + */ |
| 123 | + fun getPushNotification(): Single<WebPushSubscription> = Single.fromCallable { pushNotificationMethods.getPushNotification().execute() } |
| 124 | + |
| 125 | + /** |
| 126 | + * Removes the current Web Push API subscription. |
| 127 | + * @see <a href="https://docs.joinmastodon.org/methods/push/#delete">Mastodon API documentation: methods/push/#delete</a> |
| 128 | + */ |
| 129 | + fun removePushSubscription(): Completable = Completable.fromAction { pushNotificationMethods.removePushSubscription() } |
| 130 | +} |
0 commit comments