From b8f008d21f4ec6e2cb59b8bd2f01e1862922c3cd Mon Sep 17 00:00:00 2001 From: Andreas Date: Mon, 31 Jul 2023 11:46:53 +0200 Subject: [PATCH] Make idempotency key value optional when posting (#245) Users can disable the automated generation and use of an idempotency key value when posting, using the new parameter addIdempotencyKey=false. If this value is used (=default behaviour), it prevents the accidental duplicate submission of statuses. Fixes #118. --- .../bigbone/api/method/StatusMethods.kt | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/bigbone/src/main/kotlin/social/bigbone/api/method/StatusMethods.kt b/bigbone/src/main/kotlin/social/bigbone/api/method/StatusMethods.kt index d8e5e2ec7..9a74c2989 100644 --- a/bigbone/src/main/kotlin/social/bigbone/api/method/StatusMethods.kt +++ b/bigbone/src/main/kotlin/social/bigbone/api/method/StatusMethods.kt @@ -107,6 +107,9 @@ class StatusMethods(private val client: MastodonClient) { * @param sensitive set this to mark the media of the status as NSFW * @param spoilerText text to be shown as a warning before the actual content * @param language ISO 639 language code for this status. + * @param addIdempotencyKey If true, this will generate a unique hash value from all given parameters and add it to + * the request as its idempotency key value. To avoid duplicate submissions of the same status, if this + * value is reused within a short timeframe, another status will not be created. Defaults to true. * @see Mastodon API documentation: methods/statuses/#create */ @JvmOverloads @@ -118,7 +121,8 @@ class StatusMethods(private val client: MastodonClient) { mediaIds: List? = null, sensitive: Boolean = false, spoilerText: String? = null, - language: String? = null + language: String? = null, + addIdempotencyKey: Boolean = true ): MastodonRequest { return client.getMastodonRequest( endpoint = "api/v1/statuses", @@ -132,7 +136,7 @@ class StatusMethods(private val client: MastodonClient) { spoilerText?.let { append("spoiler_text", it) } language?.let { append("language", it) } }, - addIdempotencyKey = true + addIdempotencyKey = addIdempotencyKey ) } @@ -145,6 +149,9 @@ class StatusMethods(private val client: MastodonClient) { * @param sensitive set this to mark the media of the status as NSFW * @param spoilerText text to be shown as a warning before the actual content * @param language ISO 639 language code for this status. + * @param addIdempotencyKey If true, this will generate a unique hash value from all given parameters and add it to + * the request as its idempotency key value. To avoid duplicate submissions of the same status, if this + * value is reused within a short timeframe, another status will not be created. Defaults to true. * @see Mastodon API documentation: methods/statuses/#create */ @JvmOverloads @@ -156,7 +163,8 @@ class StatusMethods(private val client: MastodonClient) { inReplyToId: String? = null, sensitive: Boolean = false, spoilerText: String? = null, - language: String? = null + language: String? = null, + addIdempotencyKey: Boolean = true ): MastodonRequest { return client.getMastodonRequest( endpoint = "api/v1/statuses", @@ -173,7 +181,7 @@ class StatusMethods(private val client: MastodonClient) { spoilerText?.let { append("spoiler_text", it) } language?.let { append("language", it) } }, - addIdempotencyKey = true + addIdempotencyKey = addIdempotencyKey ) } @@ -188,6 +196,9 @@ class StatusMethods(private val client: MastodonClient) { * @param sensitive set this to mark the media of the status as NSFW * @param spoilerText text to be shown as a warning before the actual content * @param language ISO 639 language code for this status. + * @param addIdempotencyKey If true, this will generate a unique hash value from all given parameters and add it to + * the request as its idempotency key value. To avoid duplicate submissions of the same status, if this + * value is reused within a short timeframe, another status will not be created. Defaults to true. * @see Mastodon API documentation: methods/statuses/#create */ @JvmOverloads @@ -200,7 +211,8 @@ class StatusMethods(private val client: MastodonClient) { mediaIds: List? = null, sensitive: Boolean = false, spoilerText: String? = null, - language: String? = null + language: String? = null, + addIdempotencyKey: Boolean = true ): MastodonRequest { return client.getMastodonRequest( endpoint = "api/v1/statuses", @@ -215,7 +227,7 @@ class StatusMethods(private val client: MastodonClient) { spoilerText?.let { append("spoiler_text", it) } language?.let { append("language", it) } }, - addIdempotencyKey = true + addIdempotencyKey = addIdempotencyKey ) } @@ -229,6 +241,9 @@ class StatusMethods(private val client: MastodonClient) { * @param sensitive set this to mark the media of the status as NSFW * @param spoilerText text to be shown as a warning before the actual content * @param language ISO 639 language code for this status. + * @param addIdempotencyKey If true, this will generate a unique hash value from all given parameters and add it to + * the request as its idempotency key value. To avoid duplicate submissions of the same status, if this + * value is reused within a short timeframe, another status will not be created. Defaults to true. * @see Mastodon API documentation: methods/statuses/#create */ @JvmOverloads @@ -241,7 +256,8 @@ class StatusMethods(private val client: MastodonClient) { inReplyToId: String? = null, sensitive: Boolean = false, spoilerText: String? = null, - language: String? = null + language: String? = null, + addIdempotencyKey: Boolean = true ): MastodonRequest { return client.getMastodonRequest( endpoint = "api/v1/statuses", @@ -259,7 +275,7 @@ class StatusMethods(private val client: MastodonClient) { spoilerText?.let { append("spoiler_text", it) } language?.let { append("language", it) } }, - addIdempotencyKey = true + addIdempotencyKey = addIdempotencyKey ) }