Added comment config and builder
This commit is contained in:
parent
539bb6fbc0
commit
c5810c1986
18 changed files with 366 additions and 130 deletions
|
@ -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.
|
||||
*
|
||||
|
|
237
src/main/kotlin/net/thauvin/erik/akismet/CommentConfig.kt
Normal file
237
src/main/kotlin/net/thauvin/erik/akismet/CommentConfig.kt
Normal file
|
@ -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<String, String> = 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<String, String> = 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<String, String>): Builder = apply { this.serverEnv = serverEnv }
|
||||
|
||||
/**
|
||||
* Builds a new comment configuration.
|
||||
*/
|
||||
fun build(): CommentConfig = CommentConfig(this)
|
||||
}
|
||||
}
|
|
@ -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!
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue