diff --git a/README.md b/README.md index e43162f..38ce246 100644 --- a/README.md +++ b/README.md @@ -19,16 +19,14 @@ A pretty complete and straightforward implementation of the [Automattic's Akisme ```kotlin val akismet = Akismet(apiKey = "YOUR_API_KEY", blog = "YOUR_BLOG_URL") -val comment = AkismetComment(userIp = "127.0.0.1", userAgent = "curl/7.29.0") - -with(comment) { +val comment = AkismetComment(userIp = "127.0.0.1", userAgent = "curl/7.29.0").apply { referrer = "https://www.google.com" type = AkismetComment.TYPE_COMMENT author = "admin" authorEmail = "test@test.com" authorUrl = "https://www.CheckOutMyCoolSite.com" dateGmt = Akismet.dateToGmt(Date()) - content = "It means a lot that you would take the time to review our software." + content = "Thanks for reviewing our software." } // ... @@ -44,15 +42,17 @@ if (isSpam) { ```java final Akismet akismet = new Akismet("YOUR_API_KEY", "YOUR_BLOG_URL"); -final AkismetComment comment = new AkismetComment("127.0.0.1", "curl/7.29.0"); - -comment.setReferrer("https://www.google.com"); -comment.setType(AkismetComment.TYPE_COMMENT); -comment.setAuthor("admin"); -comment.setAuthorEmail("test@test.com"); -comment.setAuthorUrl("https://www.CheckOutMyCoolSite.com"); -comment.setDateGmt(Akismet.dateToGmt(new Date())); -comment.setContent("It means a lot that you would take the time to review our software."); +final AkismetComment comment = new AkismetComment( + new CommentConfig.Builder("127.0.0.1", "curl/7.29.0") + .referrer("https://www.google.com") + .type(Akismet.TYPE_COMMENT) + .author("admin") + .authorEmail("test@test.com") + .authorUrl("https://www.CheckOutMyCoolSite.com") + .dateGmt(Akismet.dateToGmt(new Date())) + .content("Thanks for reviewing our software.") + .build +); //... final boolean isSpam = akismet.checkComment(comment); diff --git a/config/detekt/baseline.xml b/config/detekt/baseline.xml index 935e447..a4794c3 100644 --- a/config/detekt/baseline.xml +++ b/config/detekt/baseline.xml @@ -5,12 +5,14 @@ CyclomaticComplexMethod:Akismet.kt$Akismet$private fun buildFormBody(comment: AkismetComment): FormBody CyclomaticComplexMethod:AkismetComment.kt$AkismetComment$@Suppress("DuplicatedCode") override fun equals(other: Any?): Boolean LongParameterList:AkismetServlet.kt$AkismetServlet$( id: String, name: String?, email: String?, date: String?, comment: String?, json: String, isSpam: Boolean ) + LongParameterList:CommentConfig.kt$CommentConfig$( var userIp: String, var userAgent: String, var referrer: String = "", var permalink: String = "", var type: String = "", var author: String = "", var authorEmail: String = "", var authorUrl: String = "", var content: String = "", var dateGmt: String = "", var postModifiedGmt: String = "", var blogLang: String = "", var blogCharset: String = "", var userRole: String = "", var isTest: Boolean = false, var recheckReason: String = "", var serverEnv: Map<String, String> = emptyMap() ) MagicNumber:Akismet.kt$Akismet$12 MagicNumber:Akismet.kt$Akismet$8 NestedBlockDepth:Akismet.kt$Akismet$@JvmOverloads fun executeMethod(apiUrl: HttpUrl, formBody: FormBody, trueOnError: Boolean = false): Boolean NestedBlockDepth:AkismetExample.kt$fun main(args: Array<String>) NestedBlockDepth:AkismetTest.kt$fun getKey(key: String): String ReturnCount:Akismet.kt$Akismet$@JvmOverloads fun executeMethod(apiUrl: HttpUrl, formBody: FormBody, trueOnError: Boolean = false): Boolean + TooManyFunctions:CommentConfig.kt$CommentConfig$Builder WildcardImport:AkismetTest.kt$import assertk.assertions.* diff --git a/examples/bld/README.md b/examples/bld/README.md index 52ef7f3..9786305 100644 --- a/examples/bld/README.md +++ b/examples/bld/README.md @@ -8,7 +8,6 @@ To compile & run the Kotlin example: ``` ## Java Example -cd To compile & run the Java example: ```console diff --git a/examples/bld/src/main/java/com/example/AkismetSample.java b/examples/bld/src/main/java/com/example/AkismetSample.java index f70698f..6d6ced5 100644 --- a/examples/bld/src/main/java/com/example/AkismetSample.java +++ b/examples/bld/src/main/java/com/example/AkismetSample.java @@ -2,6 +2,7 @@ package com.example; import net.thauvin.erik.akismet.Akismet; import net.thauvin.erik.akismet.AkismetComment; +import net.thauvin.erik.akismet.CommentConfig; import java.util.Date; @@ -9,25 +10,26 @@ public class AkismetSample { public static void main(String... args) { if (args.length == 1 && !args[0].isBlank()) { final Akismet akismet = new Akismet(args[0], "https://yourblogdomainname.com/blog/"); - final AkismetComment comment = new AkismetComment("127.0.0.1", "curl/7.29.0"); + final AkismetComment comment = new AkismetComment( + new CommentConfig.Builder("127.0.0.1", "curl/7.29.0") + .isTest(true) + .referrer("https://www.google.com") + .permalink(akismet.getBlog() + "post=1") + .type(AkismetComment.TYPE_COMMENT) + .author("admin") + .authorEmail("test@test.com") + .authorUrl("http://www.CheckOutMyCoolSite.com") + .dateGmt(Akismet.dateToGmt(new Date())) +// .userRole(AkismetComment.ADMIN_ROLE) + .content("It means a lot that you would take the time to review our software. Thanks again.") + .build() + ); - comment.setTest(true); - - comment.setReferrer("https://www.google.com"); - comment.setPermalink(akismet.getBlog() + "post=1"); - comment.setType(AkismetComment.TYPE_COMMENT); - comment.setAuthor("admin"); - comment.setAuthorEmail("test@test.com"); - comment.setAuthorUrl("http://www.CheckOutMyCoolSite.com"); - comment.setDateGmt(Akismet.dateToGmt(new Date())); - // comment.setUserRole(AkismetComment.ADMIN_ROLE); - comment.setContent("It means a lot that you would take the time to review our software. Thanks again."); - - // final ConsoleHandler consoleHandler = new ConsoleHandler(); - // consoleHandler.setLevel(Level.FINE); - // final Logger logger = akismet.getLogger(); - // logger.addHandler(consoleHandler); - // logger.setLevel(Level.FINE); +// final ConsoleHandler consoleHandler = new ConsoleHandler(); +// consoleHandler.setLevel(Level.FINE); +// final Logger logger = akismet.getLogger(); +// logger.addHandler(consoleHandler); +// logger.setLevel(Level.FINE); if (akismet.verifyKey()) { final boolean isSpam = akismet.checkComment(comment); diff --git a/examples/bld/src/main/kotlin/com/example/AkismetExample.kt b/examples/bld/src/main/kotlin/com/example/AkismetExample.kt index 149555f..013e633 100644 --- a/examples/bld/src/main/kotlin/com/example/AkismetExample.kt +++ b/examples/bld/src/main/kotlin/com/example/AkismetExample.kt @@ -2,18 +2,14 @@ package com.example import net.thauvin.erik.akismet.Akismet import net.thauvin.erik.akismet.AkismetComment -import java.util.Date +import java.util.* import kotlin.system.exitProcess fun main(args: Array) { - if (args.size == 1 && args[0].isNotEmpty()) { val akismet = Akismet(apiKey = args[0], blog = "https://yourblogdomainname.com/blog/") - val comment = AkismetComment(userIp = "127.0.0.1", userAgent = "curl/7.29.0") - - with(comment) { + val comment = AkismetComment(userIp = "127.0.0.1", userAgent = "curl/7.29.0").apply { isTest = true - referrer = "https://www.google.com" permalink = "${akismet.blog}post=1" type = AkismetComment.TYPE_COMMENT @@ -21,7 +17,7 @@ fun main(args: Array) { authorEmail = "test@test.com" authorUrl = "https://www.CheckOutMyCoolSite.com" dateGmt = Akismet.dateToGmt(Date()) -// userRole = AkismetComment.ADMIN_ROLE +// userRole = AkismetComment.ADMIN_ROLE content = "It means a lot that you would take the time to review our software. Thanks again." } diff --git a/examples/bld/src/main/kotlin/com/example/AkismetServlet.kt b/examples/bld/src/main/kotlin/com/example/AkismetServlet.kt index 4b245c8..3e776ed 100644 --- a/examples/bld/src/main/kotlin/com/example/AkismetServlet.kt +++ b/examples/bld/src/main/kotlin/com/example/AkismetServlet.kt @@ -20,8 +20,7 @@ class AkismetServlet : HttpServlet() { akismet.appUserAgent = request.servletContext.serverInfo - val comment = AkismetComment(request) - with(comment) { + val comment = AkismetComment(request).apply { permalink = "${akismet.blog}/comment/$id" type = AkismetComment.TYPE_COMMENT author = request.getParameter("name") diff --git a/examples/gradle/.idea/.name b/examples/gradle/.idea/.name index bdb1d33..ac5e64e 100644 --- a/examples/gradle/.idea/.name +++ b/examples/gradle/.idea/.name @@ -1 +1 @@ -akismet-examples \ No newline at end of file +akismet-examples-gradle \ No newline at end of file diff --git a/examples/gradle/.idea/kotlinc.xml b/examples/gradle/.idea/kotlinc.xml index e805548..6d0ee1c 100644 --- a/examples/gradle/.idea/kotlinc.xml +++ b/examples/gradle/.idea/kotlinc.xml @@ -1,6 +1,6 @@ - \ No newline at end of file diff --git a/examples/gradle/.idea/misc.xml b/examples/gradle/.idea/misc.xml index 7940811..72d1c06 100644 --- a/examples/gradle/.idea/misc.xml +++ b/examples/gradle/.idea/misc.xml @@ -6,11 +6,8 @@ - - - + - \ No newline at end of file + diff --git a/examples/gradle/build.gradle.kts b/examples/gradle/build.gradle.kts index b9c5436..db1e416 100644 --- a/examples/gradle/build.gradle.kts +++ b/examples/gradle/build.gradle.kts @@ -1,5 +1,3 @@ -import org.jetbrains.kotlin.gradle.tasks.KotlinCompile - plugins { id("application") id("com.github.ben-manes.versions") version "0.51.0" @@ -15,7 +13,7 @@ repositories { dependencies { implementation("jakarta.servlet:jakarta.servlet-api:6.0.0") - implementation("net.thauvin.erik:akismet-kotlin:1.0.0") + implementation("net.thauvin.erik:akismet-kotlin:1.0.1-SNAPSHOT") } java { @@ -27,11 +25,11 @@ application { mainClass.set("com.example.AkismetExampleKt") } -tasks { - withType().configureEach { - kotlinOptions.jvmTarget = java.targetCompatibility.toString() - } +kotlin { + compilerOptions.jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_11) +} +tasks { register("runJava", JavaExec::class) { group = "application" mainClass.set("com.example.AkismetSample") diff --git a/examples/gradle/src/main/java/com/example/AkismetSample.java b/examples/gradle/src/main/java/com/example/AkismetSample.java index f70698f..6d6ced5 100644 --- a/examples/gradle/src/main/java/com/example/AkismetSample.java +++ b/examples/gradle/src/main/java/com/example/AkismetSample.java @@ -2,6 +2,7 @@ package com.example; import net.thauvin.erik.akismet.Akismet; import net.thauvin.erik.akismet.AkismetComment; +import net.thauvin.erik.akismet.CommentConfig; import java.util.Date; @@ -9,25 +10,26 @@ public class AkismetSample { public static void main(String... args) { if (args.length == 1 && !args[0].isBlank()) { final Akismet akismet = new Akismet(args[0], "https://yourblogdomainname.com/blog/"); - final AkismetComment comment = new AkismetComment("127.0.0.1", "curl/7.29.0"); + final AkismetComment comment = new AkismetComment( + new CommentConfig.Builder("127.0.0.1", "curl/7.29.0") + .isTest(true) + .referrer("https://www.google.com") + .permalink(akismet.getBlog() + "post=1") + .type(AkismetComment.TYPE_COMMENT) + .author("admin") + .authorEmail("test@test.com") + .authorUrl("http://www.CheckOutMyCoolSite.com") + .dateGmt(Akismet.dateToGmt(new Date())) +// .userRole(AkismetComment.ADMIN_ROLE) + .content("It means a lot that you would take the time to review our software. Thanks again.") + .build() + ); - comment.setTest(true); - - comment.setReferrer("https://www.google.com"); - comment.setPermalink(akismet.getBlog() + "post=1"); - comment.setType(AkismetComment.TYPE_COMMENT); - comment.setAuthor("admin"); - comment.setAuthorEmail("test@test.com"); - comment.setAuthorUrl("http://www.CheckOutMyCoolSite.com"); - comment.setDateGmt(Akismet.dateToGmt(new Date())); - // comment.setUserRole(AkismetComment.ADMIN_ROLE); - comment.setContent("It means a lot that you would take the time to review our software. Thanks again."); - - // final ConsoleHandler consoleHandler = new ConsoleHandler(); - // consoleHandler.setLevel(Level.FINE); - // final Logger logger = akismet.getLogger(); - // logger.addHandler(consoleHandler); - // logger.setLevel(Level.FINE); +// final ConsoleHandler consoleHandler = new ConsoleHandler(); +// consoleHandler.setLevel(Level.FINE); +// final Logger logger = akismet.getLogger(); +// logger.addHandler(consoleHandler); +// logger.setLevel(Level.FINE); if (akismet.verifyKey()) { final boolean isSpam = akismet.checkComment(comment); diff --git a/examples/gradle/src/main/kotlin/com/example/AkismetExample.kt b/examples/gradle/src/main/kotlin/com/example/AkismetExample.kt index 149555f..013e633 100644 --- a/examples/gradle/src/main/kotlin/com/example/AkismetExample.kt +++ b/examples/gradle/src/main/kotlin/com/example/AkismetExample.kt @@ -2,18 +2,14 @@ package com.example import net.thauvin.erik.akismet.Akismet import net.thauvin.erik.akismet.AkismetComment -import java.util.Date +import java.util.* import kotlin.system.exitProcess fun main(args: Array) { - if (args.size == 1 && args[0].isNotEmpty()) { val akismet = Akismet(apiKey = args[0], blog = "https://yourblogdomainname.com/blog/") - val comment = AkismetComment(userIp = "127.0.0.1", userAgent = "curl/7.29.0") - - with(comment) { + val comment = AkismetComment(userIp = "127.0.0.1", userAgent = "curl/7.29.0").apply { isTest = true - referrer = "https://www.google.com" permalink = "${akismet.blog}post=1" type = AkismetComment.TYPE_COMMENT @@ -21,7 +17,7 @@ fun main(args: Array) { authorEmail = "test@test.com" authorUrl = "https://www.CheckOutMyCoolSite.com" dateGmt = Akismet.dateToGmt(Date()) -// userRole = AkismetComment.ADMIN_ROLE +// userRole = AkismetComment.ADMIN_ROLE content = "It means a lot that you would take the time to review our software. Thanks again." } diff --git a/examples/gradle/src/main/kotlin/com/example/AkismetServlet.kt b/examples/gradle/src/main/kotlin/com/example/AkismetServlet.kt index 4b245c8..3e776ed 100644 --- a/examples/gradle/src/main/kotlin/com/example/AkismetServlet.kt +++ b/examples/gradle/src/main/kotlin/com/example/AkismetServlet.kt @@ -20,8 +20,7 @@ class AkismetServlet : HttpServlet() { akismet.appUserAgent = request.servletContext.serverInfo - val comment = AkismetComment(request) - with(comment) { + val comment = AkismetComment(request).apply { permalink = "${akismet.blog}/comment/$id" type = AkismetComment.TYPE_COMMENT author = request.getParameter("name") diff --git a/lib/bld/bld-wrapper.properties b/lib/bld/bld-wrapper.properties index 1267563..d83a407 100644 --- a/lib/bld/bld-wrapper.properties +++ b/lib/bld/bld-wrapper.properties @@ -1,9 +1,9 @@ bld.downloadExtensionJavadoc=false bld.downloadExtensionSources=true -bld.extensions-detekt=com.uwyn.rife2:bld-detekt:0.9.2 bld.extensions=com.uwyn.rife2:bld-generated-version:0.9.5 bld.extension-jacoco=com.uwyn.rife2:bld-jacoco-report:0.9.5 -bld.extensions-kotlin=com.uwyn.rife2:bld-kotlin:0.9.7 +bld.extensions-kotlin=com.uwyn.rife2:bld-kotlin:0.9.5 +bld.extensions-detekt=com.uwyn.rife2:bld-detekt:0.9.4 bld.repositories=MAVEN_LOCAL,MAVEN_CENTRAL,RIFE2_SNAPSHOTS,RIFE2_RELEASES bld.downloadLocation= bld.sourceDirectories= diff --git a/src/main/kotlin/net/thauvin/erik/akismet/AkismetComment.kt b/src/main/kotlin/net/thauvin/erik/akismet/AkismetComment.kt index dbfcc1c..dc9c163 100644 --- a/src/main/kotlin/net/thauvin/erik/akismet/AkismetComment.kt +++ b/src/main/kotlin/net/thauvin/erik/akismet/AkismetComment.kt @@ -243,6 +243,24 @@ open class AkismetComment(val userIp: String, val userAgent: String) { serverEnv = buildServerEnv(request) } + constructor(config: CommentConfig) : this(config.userIp, config.userAgent) { + referrer = config.referrer + permalink = config.permalink + type = config.type + author = config.author + authorEmail = config.authorEmail + authorUrl = config.authorUrl + content = config.content + dateGmt = config.dateGmt + postModifiedGmt = config.postModifiedGmt + blogLang = config.blogLang + blogCharset = config.blogCharset + userRole = config.userRole + isTest = config.isTest + recheckReason = config.recheckReason + serverEnv = config.serverEnv + } + /** * Returns a JSON representation of the comment. * diff --git a/src/main/kotlin/net/thauvin/erik/akismet/CommentConfig.kt b/src/main/kotlin/net/thauvin/erik/akismet/CommentConfig.kt new file mode 100644 index 0000000..01b0659 --- /dev/null +++ b/src/main/kotlin/net/thauvin/erik/akismet/CommentConfig.kt @@ -0,0 +1,237 @@ +/* + * CommentConfig.kt + * + * Copyright 2019-2024 Erik C. Thauvin (erik@thauvin.net) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * Neither the name of this project nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package net.thauvin.erik.akismet + +import net.thauvin.erik.akismet.AkismetComment.Companion.ADMIN_ROLE +import net.thauvin.erik.akismet.AkismetComment.Companion.TYPE_BLOG_POST +import net.thauvin.erik.akismet.AkismetComment.Companion.TYPE_COMMENT +import net.thauvin.erik.akismet.AkismetComment.Companion.TYPE_CONTACT_FORM +import net.thauvin.erik.akismet.AkismetComment.Companion.TYPE_FORUM_POST +import net.thauvin.erik.akismet.AkismetComment.Companion.TYPE_MESSAGE +import net.thauvin.erik.akismet.AkismetComment.Companion.TYPE_PINGBACK +import net.thauvin.erik.akismet.AkismetComment.Companion.TYPE_REPLY +import net.thauvin.erik.akismet.AkismetComment.Companion.TYPE_SIGNUP +import net.thauvin.erik.akismet.AkismetComment.Companion.TYPE_TRACKBACK +import net.thauvin.erik.akismet.AkismetComment.Companion.TYPE_TWEET + +/** + * Provides a comment configuration. + * + * @param userIp IP address of the comment submitter. + * @param userAgent User agent string of the web browser submitting the comment. + */ +class CommentConfig @JvmOverloads constructor( + var userIp: String, + var userAgent: String, + var referrer: String = "", + var permalink: String = "", + var type: String = "", + var author: String = "", + var authorEmail: String = "", + var authorUrl: String = "", + var content: String = "", + var dateGmt: String = "", + var postModifiedGmt: String = "", + var blogLang: String = "", + var blogCharset: String = "", + var userRole: String = "", + var isTest: Boolean = false, + var recheckReason: String = "", + var serverEnv: Map = emptyMap() + +) { + constructor(builder: Builder) : this(builder.userIp, builder.userAgent) { + referrer = builder.referrer + permalink = builder.permalink + type = builder.type + author = builder.author + authorEmail = builder.authorEmail + authorUrl = builder.authorUrl + content = builder.content + dateGmt = builder.dateGmt + postModifiedGmt = builder.postModifiedGmt + blogLang = builder.blogLang + blogCharset = builder.blogCharset + userRole = builder.userRole + isTest = builder.isTest + recheckReason = builder.recheckReason + serverEnv = builder.serverEnv + } + + /** + * Provides a configuration builder. + * + * @param userIp IP address of the comment submitter. + * @param userAgent User agent string of the web browser submitting the comment. + */ + data class Builder(var userIp: String, var userAgent: String) { + var referrer = "" + var permalink = "" + var type = "" + var author = "" + var authorEmail = "" + var authorUrl = "" + var content = "" + var dateGmt = "" + var postModifiedGmt = "" + var blogLang = "" + var blogCharset = "" + var userRole = "" + var isTest = false + var recheckReason = "" + var serverEnv: Map = emptyMap() + + /** + * Sets the IP address of the comment submitter. + */ + fun userIp(userIp: String) : Builder = apply { this.userIp = userIp } + + /** + * Sets the user agent string of the web browser submitting the comment. + */ + fun userAgent(userAgent: String) : Builder = apply { this.userAgent = userAgent } + + /** + * Sets the content of the referrer header. + */ + fun referrer(referrer: String): Builder = apply { this.referrer = referrer } + + /** + * Sets the full permanent URL of the entry the comment was submitted to. + */ + fun permalink(permalink: String): Builder = apply { this.permalink = permalink } + + /** + * Sets a string that describes the type of content being sent, such as: + * + * - [TYPE_COMMENT] + * - [TYPE_FORUM_POST] + * - [TYPE_REPLY] + * - [TYPE_BLOG_POST] + * - [TYPE_CONTACT_FORM] + * - [TYPE_SIGNUP] + * - [TYPE_MESSAGE] + * - [TYPE_PINGBACK] + * - [TYPE_TRACKBACK] + * - [TYPE_TWEET] + * + * You may send a value not listed above if none of them accurately describe your content. + * + * This is further explained [here](http://blog.akismet.com/2012/06/19/pro-tip-tell-us-your-comment_type/). + */ + fun type(type: String): Builder = apply { this.type = type } + + /** + * Sets the mame submitted with the comment. + */ + fun author(author: String): Builder = apply { this.author = author } + + /** + * Sets the email address submitted with the comment. + */ + fun authorEmail(authorEmail: String): Builder = apply { this.authorEmail = authorEmail } + + /** + * Sets the URL submitted with comment. + */ + fun authorUrl(authorUrl: String): Builder = apply { this.authorUrl = authorUrl } + + /** + * Sets the content that was submitted. + */ + fun content(content: String): Builder = apply { this.content = content } + + /** + * Sets the UTC timestamp of the creation of the comment, in ISO 8601 format. + * + * May be omitted if the comment is sent to the API at the time it is created. + * + * @see [Akismet.dateToGmt] + */ + fun dateGmt(dateGmt: String): Builder = apply { this.dateGmt = dateGmt } + + /** + * Sets the UTC timestamp of the publication time for the post, page or thread on which the comment was posted. + * + * @see [Akismet.dateToGmt] + */ + fun postModifiedGmt(postModifiedGmt: String) = apply { this.postModifiedGmt = postModifiedGmt } + + /** + * Indicates the language(s) in use on the blog or site, in ISO 639-1 format, comma-separated. + * + * A site with articles in English and French might use: `en, fr_ca` + */ + fun blogLang(blogLang: String): Builder = apply { this.blogLang = blogLang } + + /** + * Sets the character encoding for the form values included in comment parameters, such as UTF-8 or ISO-8859-1 + */ + fun blogCharset(blogCharset: String): Builder = apply { this.blogCharset = blogCharset } + + /** + * Set the user role of the user who submitted the comment. This is an optional parameter. + * + * If you set it to [ADMIN_ROLE], Akismet will always return false. + */ + fun userRole(userRole: String): Builder = apply { this.userRole = userRole } + + /** + * This is optional. You can set it when submitting test queries to Akismet. + */ + fun isTest(isTest: Boolean): Builder = apply { this.isTest = isTest } + + /** + * If you are sending content to Akismet to be rechecked, such as a post that has been edited or old pending + * comments that you'd like to recheck, include this parameter with a string describing why the content is + * being rechecked. + * + * For example: `edit` + */ + fun recheckReason(checkReason: String): Builder = apply { this.recheckReason = checkReason } + + /** + * In PHP, there is an array of environmental variables called `$_SERVER` that contains information about the + * Web server itself as well as a key/value for every HTTP header sent with the request. This data is highly + * useful to Akismet. + * + * How the submitted content interacts with the server can be very telling, so please include as much of it as + * possible. + */ + fun serverEnv(serverEnv: Map): Builder = apply { this.serverEnv = serverEnv } + + /** + * Builds a new comment configuration. + */ + fun build(): CommentConfig = CommentConfig(this) + } +} diff --git a/src/main/kotlin/net/thauvin/erik/akismet/GeneratedVersion.kt b/src/main/kotlin/net/thauvin/erik/akismet/GeneratedVersion.kt index bc2ee49..8f63ec2 100644 --- a/src/main/kotlin/net/thauvin/erik/akismet/GeneratedVersion.kt +++ b/src/main/kotlin/net/thauvin/erik/akismet/GeneratedVersion.kt @@ -1,34 +1,3 @@ -/* - * GeneratedVersion.kt - * - * Copyright 2019-2024 Erik C. Thauvin (erik@thauvin.net) - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * Neither the name of this project nor the names of its contributors may be - * used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - /* * This file is automatically generated. * Do not modify! -- ALL CHANGES WILL BE ERASED! diff --git a/src/test/kotlin/net/thauvin/erik/akismet/AkismetTest.kt b/src/test/kotlin/net/thauvin/erik/akismet/AkismetTest.kt index 31f3376..3074992 100644 --- a/src/test/kotlin/net/thauvin/erik/akismet/AkismetTest.kt +++ b/src/test/kotlin/net/thauvin/erik/akismet/AkismetTest.kt @@ -118,6 +118,11 @@ class AkismetTest { assertThat(akismet::blog).isEqualTo(blog) } + @Test + fun validateConfigTest() { + assertThat(AkismetComment(config) == comment).isTrue() + } + @Test fun verifyKeyTest() { assertThat(akismet, "akismet").all { @@ -367,6 +372,7 @@ class AkismetTest { } companion object { + private const val REFERER = "http://www.google.com" private val apiKey = getKey("AKISMET_API_KEY") private val blog = getKey("AKISMET_BLOG") private val akismet = Akismet(apiKey, blog) @@ -375,24 +381,40 @@ class AkismetTest { userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6" ) private val date = Date() + private val config = CommentConfig.Builder(comment.userIp, comment.userAgent) + .referrer(REFERER) + .permalink("http://yourblogdomainname.com/blog/post=1") + .type(AkismetComment.TYPE_COMMENT) + .author("admin") + .authorEmail("test@test.com") + .authorUrl("http://www.CheckOutMyCoolSite.com") + .content("It means a lot that you would take the time to review our software. Thanks again.") + .dateGmt(Akismet.dateToGmt(date)) + .postModifiedGmt(Akismet.dateToGmt(date)) + .blogLang("en") + .blogCharset("UTF-8") + .userRole(AkismetComment.ADMIN_ROLE) + .recheckReason("edit") + .isTest(true) + .build() private val mockComment: AkismetComment = AkismetComment(request = getMockRequest()) - private const val REFERER = "http://www.google.com" init { with(comment) { - referrer = REFERER - permalink = "http://yourblogdomainname.com/blog/post=1" - type = AkismetComment.TYPE_COMMENT - author = "admin" - authorEmail = "test@test.com" - authorUrl = "http://www.CheckOutMyCoolSite.com" - content = "It means a lot that you would take the time to review our software. Thanks again." - dateGmt = Akismet.dateToGmt(date) - postModifiedGmt = dateGmt - blogLang = "en" - blogCharset = "UTF-8" - userRole = AkismetComment.ADMIN_ROLE - isTest = true + referrer = config.referrer + permalink = config.permalink + type = config.type + author = config.author + authorEmail = config.authorEmail + authorUrl = config.authorUrl + content = config.content + dateGmt = config.dateGmt + postModifiedGmt = config.postModifiedGmt + blogLang = config.blogLang + blogCharset = config.blogCharset + userRole = config.userRole + recheckReason = config.recheckReason + isTest = config.isTest } with(mockComment) { @@ -407,7 +429,7 @@ class AkismetTest { blogLang = comment.blogLang blogCharset = comment.blogCharset userRole = comment.userRole - recheckReason = "edit" + recheckReason = comment.recheckReason isTest = true } }