Skip to content

Commit d53bb48

Browse files
G10xyandregasser
andauthored
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 <[email protected]>
1 parent 1e8bf81 commit d53bb48

File tree

7 files changed

+489
-1
lines changed

7 files changed

+489
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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+
}

bigbone/src/main/kotlin/social/bigbone/MastodonClient.kt

+8
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import social.bigbone.api.method.OAuthMethods
3535
import social.bigbone.api.method.OEmbedMethods
3636
import social.bigbone.api.method.PollMethods
3737
import social.bigbone.api.method.PreferenceMethods
38+
import social.bigbone.api.method.PushNotificationMethods
3839
import social.bigbone.api.method.ReportMethods
3940
import social.bigbone.api.method.SearchMethods
4041
import social.bigbone.api.method.StatusMethods
@@ -284,6 +285,13 @@ private constructor(
284285
@get:JvmName("timelines")
285286
val timelines: TimelineMethods by lazy { TimelineMethods(this) }
286287

288+
/**
289+
* Access API methods under "push" endpoint.
290+
*/
291+
@Suppress("unused") // public API
292+
@get:JvmName("pushNotifications")
293+
val pushNotifications: PushNotificationMethods by lazy { PushNotificationMethods(this) }
294+
287295
/**
288296
* Specifies the HTTP methods / HTTP verb that can be used by this class.
289297
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package social.bigbone.api.entity
2+
3+
import kotlinx.serialization.SerialName
4+
import kotlinx.serialization.Serializable
5+
6+
/**
7+
* Represents a subscription to the push streaming server.
8+
* @see <a href="https://docs.joinmastodon.org/entities/WebPushSubscription/">Mastodon API Push</a>
9+
*/
10+
@Serializable
11+
data class WebPushSubscription(
12+
13+
/**
14+
* The ID of the Web Push subscription in the database.
15+
*/
16+
@SerialName("id")
17+
val id: String = "",
18+
19+
/**
20+
* Where push alerts will be sent to.
21+
*/
22+
@SerialName("endpoint")
23+
val endpoint: String = "",
24+
25+
/**
26+
* The streaming server’s VAPID key.
27+
*/
28+
@SerialName("server_key")
29+
val serverKey: String = "",
30+
31+
/**
32+
* Which alerts should be delivered to the endpoint.
33+
*/
34+
@SerialName("alerts")
35+
val alerts: Alerts
36+
)
37+
38+
/**
39+
* Which alerts should be delivered to the endpoint.
40+
* @see <a href="https://docs.joinmastodon.org/entities/WebPushSubscription/">Mastodon API Push</a>
41+
*/
42+
@Serializable
43+
data class Alerts(
44+
/**
45+
* Receive a push notification when someone else has mentioned you in a status?
46+
*/
47+
@SerialName("mention")
48+
val mention: Boolean? = false,
49+
50+
/**
51+
* Receive a push notification when a subscribed account posts a status?
52+
*/
53+
@SerialName("status")
54+
val status: Boolean? = false,
55+
56+
/**
57+
* Receive a push notification when a status you created has been boosted by someone else?
58+
*/
59+
@SerialName("reblog")
60+
val reblog: Boolean? = false,
61+
62+
/**
63+
* Receive a push notification when someone has followed you?
64+
*/
65+
@SerialName("follow")
66+
val follow: Boolean? = false,
67+
68+
/**
69+
* Receive a push notification when someone has requested to followed you?
70+
*/
71+
@SerialName("follow_request")
72+
val followRequest: Boolean? = false,
73+
74+
/**
75+
* Receive a push notification when a status you created has been favourited by someone else?
76+
*/
77+
@SerialName("favourite")
78+
val favourite: Boolean? = false,
79+
80+
/**
81+
* Receive a push notification when a poll you voted in or created has ended?
82+
*/
83+
@SerialName("poll")
84+
val poll: Boolean? = false,
85+
86+
/**
87+
* Receive a push notification when a status you interacted with has been edited?
88+
*/
89+
@SerialName("update")
90+
val update: Boolean? = false,
91+
92+
/**
93+
* Receive a push notification when a new user has signed up?
94+
*/
95+
@SerialName("admin.sign_up")
96+
val adminSignUp: Boolean? = false,
97+
98+
/**
99+
* Receive a push notification when a new report has been filed?
100+
*/
101+
@SerialName("admin.report")
102+
val adminReport: Boolean? = false
103+
)

0 commit comments

Comments
 (0)