added template for spam posts, added spam filter, added new js to redirect
This commit is contained in:
@@ -27,14 +27,22 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type CommentDto struct {
|
||||
Url string
|
||||
}
|
||||
|
||||
var (
|
||||
db *gorm.DB
|
||||
apiKey string
|
||||
recentPosters map[string]uint
|
||||
)
|
||||
|
||||
func RegisterBlogGroup(r *gin.RouterGroup) {
|
||||
// Initialize recent posters cache
|
||||
recentPosters = make(map[string]uint)
|
||||
// Load dsn and initialize database
|
||||
dsn := utils.LoadDSN("dsn")
|
||||
// Load and initialize api key
|
||||
apiKey = utils.LoadApiKey("api_key.pem")
|
||||
var err error
|
||||
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
|
||||
@@ -46,6 +54,8 @@ func RegisterBlogGroup(r *gin.RouterGroup) {
|
||||
r.GET("/post/:url", RenderIndividualBlogPost)
|
||||
r.POST("/post", CreateBlogPost)
|
||||
r.POST("/post/comment", CreateComment)
|
||||
// Clear the commenters cache every 15 minutes
|
||||
go utils.TimedClearMap(&recentPosters, 180000*5)
|
||||
}
|
||||
|
||||
func RenderBlogPage(c *gin.Context) {
|
||||
@@ -103,6 +113,10 @@ func CreateComment(c *gin.Context) {
|
||||
a := []string{c.Request.Form.Get("author"),
|
||||
c.Request.Form.Get("content"),
|
||||
c.Request.Form.Get("url")}
|
||||
if GetNumberOfRecentPosts(c) > 3 {
|
||||
c.HTML(http.StatusOK, "comment_post_spam.tmpl", CommentDto{Url: a[2]})
|
||||
return
|
||||
}
|
||||
for _, v := range a {
|
||||
if len(v) == 0 {
|
||||
c.Status(http.StatusNotAcceptable)
|
||||
@@ -115,8 +129,13 @@ func CreateComment(c *gin.Context) {
|
||||
AssociatedPost: a[2],
|
||||
}
|
||||
db.Create(&comment)
|
||||
recentPosters[c.ClientIP()]++
|
||||
// Pass the associated post to the template to add to the href
|
||||
c.HTML(http.StatusOK, "comment_post.tmpl", struct{ Url string }{Url: comment.AssociatedPost})
|
||||
c.HTML(http.StatusOK, "comment_post.tmpl", CommentDto{Url: comment.AssociatedPost})
|
||||
}
|
||||
|
||||
func GetNumberOfRecentPosts(c *gin.Context) uint {
|
||||
return recentPosters[c.ClientIP()]
|
||||
}
|
||||
|
||||
func GetAllBlogPosts() *[]models.BlogPost {
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
{{ template "header.tmpl" . }}
|
||||
<h2>Your comment is being posted...</h2>
|
||||
<p>Click <a href="/blog/post/{{ .Url }}">here</a> to return to the blog post.</p>
|
||||
<p>You will be redirected or, click <a href="/blog/post/{{ .Url }}">here</a> to return to the blog post.</p>
|
||||
<input value="{{ .Url }}" readonly style="display: none;" id="url"/>
|
||||
<script>
|
||||
setTimeout(() => {
|
||||
window.location.replace("/blog/post/" + document.getElementById("url").value)
|
||||
}, 1500)
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
5
templates/comment_post_spam.tmpl
Normal file
5
templates/comment_post_spam.tmpl
Normal file
@@ -0,0 +1,5 @@
|
||||
{{ template "header.tmpl" . }}
|
||||
<h2>You've commented too many times, too quickly. Your comment will <u>not</u> be posted.</h2>
|
||||
<p>Click <a href="/blog/post/{{ .Url }}">here</a> to return to the post.<p>
|
||||
</body>
|
||||
</html>
|
||||
19
utils/timed_clear.go
Normal file
19
utils/timed_clear.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TimedClearMap(m *map[string]uint, timer uint) {
|
||||
for {
|
||||
t, err := time.ParseDuration(strconv.FormatUint(uint64(timer), 10) + "ms")
|
||||
if err != nil {
|
||||
panic("Something went wrong initializing time!!")
|
||||
}
|
||||
time.Sleep(t)
|
||||
for key := range *m {
|
||||
delete(*m, key)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user