Skip to content

Commit

Permalink
Add Admin::EmailDomainBlock methods (#400)
Browse files Browse the repository at this point in the history
* Add Admin::EmailDomainBlock entity

* Add Admin::EmailDomainBlock methods

* Add unit tests

* Update documentation
  • Loading branch information
PattaFeuFeu authored Dec 26, 2023
1 parent 6fd3ac5 commit df7742b
Show file tree
Hide file tree
Showing 11 changed files with 479 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package social.bigbone.rx.admin

import io.reactivex.rxjava3.core.Completable
import io.reactivex.rxjava3.core.Single
import social.bigbone.MastodonClient
import social.bigbone.api.Pageable
import social.bigbone.api.Range
import social.bigbone.api.entity.admin.AdminEmailDomainBlock
import social.bigbone.api.method.admin.AdminEmailDomainBlockMethods

/**
* Reactive implementation of [AdminEmailDomainBlockMethods].
*
* Disallow certain email domains from signing up.
*
* @see <a href="https://docs.joinmastodon.org/methods/admin/email_domain_blocks/">Mastodon admin/email_domain_blocks API methods</a>
*/
class RxAdminEmailDomainBlockMethods(client: MastodonClient) {

private val adminEmailDomainBlockMethods = AdminEmailDomainBlockMethods(client)

/**
* Show information about all email domains blocked from signing up.
*
* @param range optional Range for the pageable return value
*
* @see <a href="https://docs.joinmastodon.org/methods/admin/email_domain_blocks/#get">Mastodon API documentation: admin/email_domain_blocks/#get</a>
*/
@JvmOverloads
fun getAllEmailDomainBlocks(range: Range = Range()): Single<Pageable<AdminEmailDomainBlock>> = Single.fromCallable {
adminEmailDomainBlockMethods.getAllEmailDomainBlocks(range).execute()
}

/**
* Show information about a single email domain that is blocked from signups.
*
* @param id The ID of the EmailDomainBlock in the database.
* @see <a href="https://docs.joinmastodon.org/methods/admin/email_domain_blocks/#get-one">
* Mastodon API documentation: admin/email_domain_blocks/#get-one</a>
*/
fun getEmailDomainBlock(id: String): Single<AdminEmailDomainBlock> = Single.fromCallable {
adminEmailDomainBlockMethods.getEmailDomainBlock(id).execute()
}

/**
* Add a domain to the list of email domains blocked from signups.
*
* @param domain The domain to block federation with.
* @see <a href="https://docs.joinmastodon.org/methods/admin/email_domain_blocks/#create">Mastodon API documentation: admin/email_domain_blocks/#create</a>
*/
fun blockEmailDomain(domain: String): Single<AdminEmailDomainBlock> = Single.fromCallable {
adminEmailDomainBlockMethods.blockEmailDomain(domain).execute()
}

/**
* Lift a block against an email domain.
*
* @param id The ID of the EmailDomainBlock in the database.
*
* @see <a href="https://docs.joinmastodon.org/methods/admin/email_domain_blocks/#delete">Mastodon API documentation: admin/email_domain_blocks/#delete</a>
*/
fun removeEmailDomainBlock(id: String) = Completable.fromAction {
adminEmailDomainBlockMethods.removeEmailDomainBlock(id)
}
}
12 changes: 10 additions & 2 deletions bigbone/src/main/kotlin/social/bigbone/MastodonClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import social.bigbone.api.method.TimelineMethods
import social.bigbone.api.method.TrendMethods
import social.bigbone.api.method.admin.AdminDimensionMethods
import social.bigbone.api.method.admin.AdminDomainBlockMethods
import social.bigbone.api.method.admin.AdminEmailDomainBlockMethods
import social.bigbone.api.method.admin.AdminIpBlockMethods
import social.bigbone.api.method.admin.AdminMeasureMethods
import social.bigbone.api.method.admin.AdminRetentionMethods
Expand Down Expand Up @@ -117,8 +118,15 @@ private constructor(
* Access API methods under the "admin/domain_blocks" endpoint.
*/
@Suppress("unused") // public API
@get:JvmName("adminDomainBlock")
val adminDomainBlock: AdminDomainBlockMethods by lazy { AdminDomainBlockMethods(this) }
@get:JvmName("adminDomainBlocks")
val adminDomainBlocks: AdminDomainBlockMethods by lazy { AdminDomainBlockMethods(this) }

/**
* Access API methods under the "admin/email_domain_blocks" endpoint.
*/
@Suppress("unused") // public API
@get:JvmName("adminEmailDomainBlocks")
val adminEmailDomainBlocks: AdminEmailDomainBlockMethods by lazy { AdminEmailDomainBlockMethods(this) }

/**
* Access API methods under the "admin/ip_blocks" endpoint.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package social.bigbone.api.entity.admin

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import social.bigbone.DateTimeSerializer
import social.bigbone.PrecisionDateTime
import social.bigbone.api.entity.data.History

/**
* Represents an email domain that cannot be used to sign up.
*
* @see <a href="https://docs.joinmastodon.org/entities/Admin_EmailDomainBlock/">Mastodon API Admin::EmailDomainBlock documentation</a>
*/
@Serializable
data class AdminEmailDomainBlock(
/**
* The ID of the EmailDomainBlock in the database.
* String cast from an Integer, but not guaranteed to be a number.
*/
@SerialName("id")
val id: String,

/**
* The email domain that is not allowed to be used for signups.
*/
@SerialName("domain")
val domain: String,

/**
* The timestamp of the notification.
*/
@SerialName("created_at")
@Serializable(with = DateTimeSerializer::class)
val createdAt: PrecisionDateTime = PrecisionDateTime.InvalidPrecisionDateTime.Unavailable,

/**
* Usage statistics for given days (typically the past week).
*/
@SerialName("history")
val history: List<History>
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import kotlinx.serialization.Serializable
* Usage statistics for given days (typically the past week).
* @see <a href="https://docs.joinmastodon.org/entities/Tag/#history">Mastodon API Tag history</a>
* @see <a href="https://docs.joinmastodon.org/entities/PreviewCard/#history">Mastodon API PreviewCard history
* @see <a href="https://docs.joinmastodon.org/entities/Admin_EmailDomainBlock/#history">Mastodon API Admin::EmailDomainBlock history
*/
@Serializable
data class History(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package social.bigbone.api.method.admin

import social.bigbone.MastodonClient
import social.bigbone.MastodonRequest
import social.bigbone.Parameters
import social.bigbone.api.Pageable
import social.bigbone.api.Range
import social.bigbone.api.entity.admin.AdminEmailDomainBlock

/**
* Disallow certain email domains from signing up.
*
* @see <a href="https://docs.joinmastodon.org/methods/admin/email_domain_blocks/">Mastodon admin/email_domain_blocks API methods</a>
*/
class AdminEmailDomainBlockMethods(private val client: MastodonClient) {

private val adminEmailDomainBlockEndpoint = "api/v1/admin/email_domain_blocks"

/**
* Show information about all email domains blocked from signing up.
*
* @param range optional Range for the pageable return value
*
* @see <a href="https://docs.joinmastodon.org/methods/admin/email_domain_blocks/#get">Mastodon API documentation: admin/email_domain_blocks/#get</a>
*/
@JvmOverloads
fun getAllEmailDomainBlocks(range: Range = Range()): MastodonRequest<Pageable<AdminEmailDomainBlock>> {
return client.getPageableMastodonRequest(
endpoint = adminEmailDomainBlockEndpoint,
method = MastodonClient.Method.GET,
parameters = range.toParameters()
)
}

/**
* Show information about a single email domain that is blocked from signups.
*
* @param id The ID of the EmailDomainBlock in the database.
* @see <a href="https://docs.joinmastodon.org/methods/admin/email_domain_blocks/#get-one">
* Mastodon API documentation: admin/email_domain_blocks/#get-one</a>
*/
fun getEmailDomainBlock(id: String): MastodonRequest<AdminEmailDomainBlock> {
return client.getMastodonRequest(
endpoint = "$adminEmailDomainBlockEndpoint/$id",
method = MastodonClient.Method.GET
)
}

/**
* Add a domain to the list of email domains blocked from signups.
*
* @param domain The domain to block federation with.
* @see <a href="https://docs.joinmastodon.org/methods/admin/email_domain_blocks/#create">Mastodon API documentation: admin/email_domain_blocks/#create</a>
*/
fun blockEmailDomain(domain: String): MastodonRequest<AdminEmailDomainBlock> {
return client.getMastodonRequest(
endpoint = adminEmailDomainBlockEndpoint,
method = MastodonClient.Method.POST,
parameters = Parameters().append("domain", domain)
)
}

/**
* Lift a block against an email domain.
*
* @param id The ID of the EmailDomainBlock in the database.
*
* @see <a href="https://docs.joinmastodon.org/methods/admin/email_domain_blocks/#delete">Mastodon API documentation: admin/email_domain_blocks/#delete</a>
*/
fun removeEmailDomainBlock(id: String) {
client.performAction(
endpoint = "$adminEmailDomainBlockEndpoint/$id",
method = MastodonClient.Method.DELETE
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[
{
"id": "1",
"domain": "foo",
"created_at": "2022-11-16T06:09:36.176Z",
"history": [
{
"day": "1668556800",
"accounts": "0",
"uses": "0"
},
{
"day": "1668470400",
"accounts": "0",
"uses": "0"
},
{
"day": "1668384000",
"accounts": "0",
"uses": "0"
},
{
"day": "1668297600",
"accounts": "0",
"uses": "0"
},
{
"day": "1668211200",
"accounts": "0",
"uses": "0"
},
{
"day": "1668124800",
"accounts": "0",
"uses": "0"
},
{
"day": "1668038400",
"accounts": "0",
"uses": "0"
}
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"id": "1",
"domain": "foo",
"created_at": "2022-11-16T06:09:36.176Z",
"history": [
{
"day": "1668556800",
"accounts": "0",
"uses": "0"
},
{
"day": "1668470400",
"accounts": "0",
"uses": "0"
},
{
"day": "1668384000",
"accounts": "0",
"uses": "0"
},
{
"day": "1668297600",
"accounts": "0",
"uses": "0"
},
{
"day": "1668211200",
"accounts": "0",
"uses": "0"
},
{
"day": "1668124800",
"accounts": "0",
"uses": "0"
},
{
"day": "1668038400",
"accounts": "0",
"uses": "0"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"id": "1",
"domain": "foo",
"created_at": "2022-11-16T06:09:36.176Z",
"history": [
{
"day": "1668556800",
"accounts": "0",
"uses": "0"
},
{
"day": "1668470400",
"accounts": "0",
"uses": "0"
},
{
"day": "1668384000",
"accounts": "0",
"uses": "0"
},
{
"day": "1668297600",
"accounts": "0",
"uses": "0"
},
{
"day": "1668211200",
"accounts": "0",
"uses": "0"
},
{
"day": "1668124800",
"accounts": "0",
"uses": "0"
},
{
"day": "1668038400",
"accounts": "0",
"uses": "0"
}
]
}
Loading

0 comments on commit df7742b

Please sign in to comment.