Skip to content

Commit

Permalink
Feat new push notifications methods (#316)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
G10xy and andregasser authored Nov 3, 2023
1 parent 1e8bf81 commit d53bb48
Show file tree
Hide file tree
Showing 7 changed files with 489 additions and 1 deletion.
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() }
}
8 changes: 8 additions & 0 deletions bigbone/src/main/kotlin/social/bigbone/MastodonClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import social.bigbone.api.method.OAuthMethods
import social.bigbone.api.method.OEmbedMethods
import social.bigbone.api.method.PollMethods
import social.bigbone.api.method.PreferenceMethods
import social.bigbone.api.method.PushNotificationMethods
import social.bigbone.api.method.ReportMethods
import social.bigbone.api.method.SearchMethods
import social.bigbone.api.method.StatusMethods
Expand Down Expand Up @@ -284,6 +285,13 @@ private constructor(
@get:JvmName("timelines")
val timelines: TimelineMethods by lazy { TimelineMethods(this) }

/**
* Access API methods under "push" endpoint.
*/
@Suppress("unused") // public API
@get:JvmName("pushNotifications")
val pushNotifications: PushNotificationMethods by lazy { PushNotificationMethods(this) }

/**
* Specifies the HTTP methods / HTTP verb that can be used by this class.
*/
Expand Down
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
)
Loading

0 comments on commit d53bb48

Please sign in to comment.