-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat new push notifications methods (#316)
* add data classes for PushMethods * update PushNotificationMethods params default * fix string error * remvoed useless dto * fix string error * add Rx version of PushNotificationSubscription and add dto model for the response * update WebPushNotification inner object of Alerts * feat PushNotificationMethods to subscribe to push api * add PushNotificationMethods to MastodonClient * fix dependencies import * update tests * update documentations within PushNotificationMethods * refactor PushNotificationMethods * fix pushNotification naming variable * update PushNotificationMethods method docs * update WebPushSubscription with default values of attributes * add method docs within RxPushNotificationMethods * fix ktlint * update serialization naming of WebPushSubscription attributes * update kdoc of every attributes of Alerts nested object * fix double instantiation --------- Co-authored-by: André Gasser <andre.gasser@protonmail.com>
- Loading branch information
1 parent
1e8bf81
commit d53bb48
Showing
7 changed files
with
489 additions
and
1 deletion.
There are no files selected for viewing
130 changes: 130 additions & 0 deletions
130
bigbone-rx/src/main/kotlin/social/bigbone/rx/RxPushNotificationMethods.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package social.bigbone.rx | ||
|
||
import io.reactivex.rxjava3.core.Completable | ||
import io.reactivex.rxjava3.core.Single | ||
import social.bigbone.MastodonClient | ||
import social.bigbone.api.entity.WebPushSubscription | ||
import social.bigbone.api.method.PushNotificationMethods | ||
|
||
/** | ||
* Reactive implementation of [PushNotificationMethods]. | ||
* Allows access to API methods with endpoints having an "api/vX/push" prefix. | ||
* @see <a href="https://docs.joinmastodon.org/methods/push/">Mastodon push notification API methods</a> | ||
*/ | ||
class RxPushNotificationMethods(client: MastodonClient) { | ||
|
||
private val pushNotificationMethods = PushNotificationMethods(client) | ||
|
||
/** | ||
* Add a Web Push API subscription to receive notifications. | ||
* Each access token can have one push subscription. If you create a new subscription, the old subscription is deleted. | ||
* @param endpoint The endpoint URL that is called when a notification event occurs. | ||
* @param userPublicKey User agent public key. Base64 encoded string of a public key from a ECDH keypair using the prime256v1 curve. | ||
* @param userAuthSecret Auth secret, Base64 encoded string of 16 bytes of random data. | ||
* @param mention Receive mention notifications? | ||
* @param status Receive new subscribed account notifications? | ||
* @param reblog Receive reblog notifications? | ||
* @param follow Receive follow notifications? | ||
* @param followRequest Receive follow request notifications? | ||
* @param favourite Receive favourite notifications? | ||
* @param poll Receive poll notifications? | ||
* @param update Receive status edited notifications? | ||
* @param adminSignUp Receive new user signup notifications? Defaults to false. Must have a role with the appropriate permissions. | ||
* @param adminReport Receive new report notifications? Defaults to false. Must have a role with the appropriate permissions. | ||
* @param policy Specify which to receive push notifications from. | ||
* @see <a href="https://docs.joinmastodon.org/methods/push/#create">Mastodon API documentation: methods/push/#create</a> | ||
*/ | ||
@JvmOverloads | ||
fun subscribePushNotification( | ||
endpoint: String, | ||
userPublicKey: String, | ||
userAuthSecret: String, | ||
mention: Boolean? = false, | ||
status: Boolean? = false, | ||
reblog: Boolean? = false, | ||
follow: Boolean? = false, | ||
followRequest: Boolean? = false, | ||
favourite: Boolean? = false, | ||
poll: Boolean? = false, | ||
update: Boolean? = false, | ||
adminSignUp: Boolean? = false, | ||
adminReport: Boolean? = false, | ||
policy: PushNotificationMethods.PushDataPolicy? = null | ||
): Single<WebPushSubscription> = | ||
Single.fromCallable { | ||
pushNotificationMethods.subscribePushNotification( | ||
endpoint, | ||
userPublicKey, | ||
userAuthSecret, | ||
mention, | ||
status, | ||
reblog, | ||
follow, | ||
followRequest, | ||
favourite, | ||
poll, | ||
update, | ||
adminSignUp, | ||
adminReport, | ||
policy | ||
).execute() | ||
} | ||
|
||
/** | ||
* Updates the current push subscription. Only the data part can be updated. | ||
* To change fundamentals, a new subscription must be created instead. | ||
* @param mention Receive mention notifications? | ||
* @param status Receive new subscribed account notifications? | ||
* @param reblog Receive reblog notifications? | ||
* @param follow Receive follow notifications? | ||
* @param followRequest Receive follow request notifications? | ||
* @param favourite Receive favourite notifications? | ||
* @param poll Receive poll notifications? | ||
* @param update Receive status edited notifications? | ||
* @param adminSignUp Receive new user signup notifications? Defaults to false. Must have a role with the appropriate permissions. | ||
* @param adminReport Receive new report notifications? Defaults to false. Must have a role with the appropriate permissions. | ||
* @param policy Specify which to receive push notifications from. | ||
* @see <a href="https://docs.joinmastodon.org/methods/push/#update">Mastodon API documentation: methods/push/#update</a> | ||
*/ | ||
@JvmOverloads | ||
fun updatePushSubscription( | ||
mention: Boolean? = false, | ||
status: Boolean? = false, | ||
reblog: Boolean? = false, | ||
follow: Boolean? = false, | ||
followRequest: Boolean? = false, | ||
favourite: Boolean? = false, | ||
poll: Boolean? = false, | ||
update: Boolean? = false, | ||
adminSignUp: Boolean? = false, | ||
adminReport: Boolean? = false, | ||
policy: PushNotificationMethods.PushDataPolicy? = null | ||
): Single<WebPushSubscription> = | ||
Single.fromCallable { | ||
pushNotificationMethods.updatePushSubscription( | ||
mention, | ||
status, | ||
reblog, | ||
follow, | ||
followRequest, | ||
favourite, | ||
poll, | ||
update, | ||
adminSignUp, | ||
adminReport, | ||
policy | ||
).execute() | ||
} | ||
|
||
/** | ||
* View the PushSubscription currently associated with this access token. | ||
* @see <a href="https://docs.joinmastodon.org/methods/push/#get">Mastodon API documentation: methods/push/#get</a> | ||
*/ | ||
fun getPushNotification(): Single<WebPushSubscription> = Single.fromCallable { pushNotificationMethods.getPushNotification().execute() } | ||
|
||
/** | ||
* Removes the current Web Push API subscription. | ||
* @see <a href="https://docs.joinmastodon.org/methods/push/#delete">Mastodon API documentation: methods/push/#delete</a> | ||
*/ | ||
fun removePushSubscription(): Completable = Completable.fromAction { pushNotificationMethods.removePushSubscription() } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
bigbone/src/main/kotlin/social/bigbone/api/entity/WebPushSubscription.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package social.bigbone.api.entity | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
/** | ||
* Represents a subscription to the push streaming server. | ||
* @see <a href="https://docs.joinmastodon.org/entities/WebPushSubscription/">Mastodon API Push</a> | ||
*/ | ||
@Serializable | ||
data class WebPushSubscription( | ||
|
||
/** | ||
* The ID of the Web Push subscription in the database. | ||
*/ | ||
@SerialName("id") | ||
val id: String = "", | ||
|
||
/** | ||
* Where push alerts will be sent to. | ||
*/ | ||
@SerialName("endpoint") | ||
val endpoint: String = "", | ||
|
||
/** | ||
* The streaming server’s VAPID key. | ||
*/ | ||
@SerialName("server_key") | ||
val serverKey: String = "", | ||
|
||
/** | ||
* Which alerts should be delivered to the endpoint. | ||
*/ | ||
@SerialName("alerts") | ||
val alerts: Alerts | ||
) | ||
|
||
/** | ||
* Which alerts should be delivered to the endpoint. | ||
* @see <a href="https://docs.joinmastodon.org/entities/WebPushSubscription/">Mastodon API Push</a> | ||
*/ | ||
@Serializable | ||
data class Alerts( | ||
/** | ||
* Receive a push notification when someone else has mentioned you in a status? | ||
*/ | ||
@SerialName("mention") | ||
val mention: Boolean? = false, | ||
|
||
/** | ||
* Receive a push notification when a subscribed account posts a status? | ||
*/ | ||
@SerialName("status") | ||
val status: Boolean? = false, | ||
|
||
/** | ||
* Receive a push notification when a status you created has been boosted by someone else? | ||
*/ | ||
@SerialName("reblog") | ||
val reblog: Boolean? = false, | ||
|
||
/** | ||
* Receive a push notification when someone has followed you? | ||
*/ | ||
@SerialName("follow") | ||
val follow: Boolean? = false, | ||
|
||
/** | ||
* Receive a push notification when someone has requested to followed you? | ||
*/ | ||
@SerialName("follow_request") | ||
val followRequest: Boolean? = false, | ||
|
||
/** | ||
* Receive a push notification when a status you created has been favourited by someone else? | ||
*/ | ||
@SerialName("favourite") | ||
val favourite: Boolean? = false, | ||
|
||
/** | ||
* Receive a push notification when a poll you voted in or created has ended? | ||
*/ | ||
@SerialName("poll") | ||
val poll: Boolean? = false, | ||
|
||
/** | ||
* Receive a push notification when a status you interacted with has been edited? | ||
*/ | ||
@SerialName("update") | ||
val update: Boolean? = false, | ||
|
||
/** | ||
* Receive a push notification when a new user has signed up? | ||
*/ | ||
@SerialName("admin.sign_up") | ||
val adminSignUp: Boolean? = false, | ||
|
||
/** | ||
* Receive a push notification when a new report has been filed? | ||
*/ | ||
@SerialName("admin.report") | ||
val adminReport: Boolean? = false | ||
) |
Oops, something went wrong.