Skip to content

Commit a538031

Browse files
committed
feat: report spam
1 parent dcee5a7 commit a538031

File tree

3 files changed

+93
-4
lines changed

3 files changed

+93
-4
lines changed

resource/theme/admin/comments.html

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@ <h1>
3838
</td>
3939
<td>{{.Version}}</td>
4040
<td>{{tf .CreatedAt ($.Tr.T "date_format")}}</td>
41-
<td><button onclick="deleteComment('{{.Article.ID}}','{{.ID}}','{{.ReplyTo}}')"
42-
class="btn btn-warning btn-xs">{{$.Tr.T "delete"}}</button></td>
41+
<td><div class="btn-group btn-group-xs">
42+
<button onclick="deleteComment('{{.Article.ID}}','{{.ID}}')" class="btn btn-danger btn-xs">{{$.Tr.T
43+
"delete"}}</button>
44+
<button onclick="reportSpam('{{.Article.ID}}','{{.ID}}')" type="button" class="btn btn-warning">SPAM</button>
45+
</div></td>
4346
</tr>
4447
{{end}}
4548
</tbody>
@@ -56,12 +59,12 @@ <h1>
5659
</section>
5760
</div>
5861
<script>
59-
function deleteComment(aid, commendId, rpl) {
62+
function deleteComment(aid, commendId) {
6063
if (!confirm("Confirm delete?")) {
6164
return
6265
}
6366
$.ajax({
64-
url: "/admin/comments?aid=" + aid + '&id=' + commendId + '&rpl=' + (rpl == '\x3cnil\x3e' ? '' : rpl),
67+
url: "/admin/comments?aid=" + aid + '&id=' + commendId,
6568
type: 'DELETE',
6669
success: () => {
6770
window.location.reload()
@@ -71,6 +74,22 @@ <h1>
7174
},
7275
})
7376
}
77+
78+
function reportSpam(aid, commendId) {
79+
if (!confirm("Confirm report SPAM?")) {
80+
return
81+
}
82+
$.ajax({
83+
url: "/admin/report-spam?aid=" + aid + '&id=' + commendId,
84+
type: 'POST',
85+
success: () => {
86+
window.location.reload()
87+
},
88+
error: (e) => {
89+
alert(e.responseText)
90+
},
91+
})
92+
}
7493
</script>
7594
{{template "admin/footer" .}}
7695
{{end}}

router/manage_comment.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/http"
66
"strconv"
77

8+
"github.com/adtac/go-akismet/akismet"
89
"github.com/biezhi/gorm-paginator/pagination"
910
"github.com/gofiber/fiber/v2"
1011
"github.com/jinzhu/gorm"
@@ -45,6 +46,74 @@ func deleteComment(c *fiber.Ctx) error {
4546
tx.Rollback()
4647
return err
4748
}
49+
if err := tx.Model(model.Comment{}).Where("reply_to = ?", id).Update("reply_to", nil).Error; err != nil {
50+
tx.Rollback()
51+
return err
52+
}
53+
if err := tx.Model(model.Article{}).Where("id = ?", articleID).
54+
UpdateColumn("comment_num", gorm.Expr("comment_num - ?", 1)).Error; err != nil {
55+
tx.Rollback()
56+
return err
57+
}
58+
if err := tx.Commit().Error; err != nil {
59+
return err
60+
}
61+
return nil
62+
}
63+
64+
func reportSpam(c *fiber.Ctx) error {
65+
id := c.Query("id")
66+
articleID := c.Query("aid")
67+
68+
if len(id) < 10 || len(articleID) < 10 {
69+
return errors.New("error id")
70+
}
71+
72+
var cm model.Comment
73+
if err := solitudes.System.DB.Take(&cm, "id = ?", id).Error; err != nil {
74+
return err
75+
}
76+
77+
var article model.Article
78+
if err := solitudes.System.DB.Take(&article, "id = ?", articleID).Error; err != nil {
79+
return err
80+
}
81+
82+
cmType, _, err := getCommentType(&commentForm{
83+
Nickname: cm.Nickname,
84+
Email: cm.Email,
85+
Website: cm.Website,
86+
Content: cm.Content,
87+
ReplyTo: cm.ReplyTo,
88+
})
89+
if err != nil {
90+
return err
91+
}
92+
93+
if err := akismet.SubmitSpam(&akismet.Comment{
94+
Blog: "https://" + solitudes.System.Config.Site.Domain, // required
95+
UserIP: cm.IP, // required
96+
UserAgent: cm.UserAgent, // required
97+
CommentType: cmType,
98+
Referrer: string(c.Request().Header.Referer()),
99+
Permalink: "https://" + solitudes.System.Config.Site.Domain + "/" + article.Slug,
100+
CommentAuthor: cm.Nickname,
101+
CommentAuthorEmail: cm.Email,
102+
CommentAuthorURL: cm.Website,
103+
CommentContent: cm.Content,
104+
}, solitudes.System.Config.Akismet); err != nil {
105+
return err
106+
}
107+
108+
tx := solitudes.System.DB.Begin()
109+
if err := tx.Delete(&model.Comment{}, "id =?", id).Error; err != nil {
110+
tx.Rollback()
111+
return err
112+
}
113+
if err := tx.Model(model.Comment{}).Where("reply_to = ?", id).Update("reply_to", nil).Error; err != nil {
114+
tx.Rollback()
115+
return err
116+
}
48117
if err := tx.Model(model.Article{}).Where("id = ?", articleID).
49118
UpdateColumn("comment_num", gorm.Expr("comment_num - ?", 1)).Error; err != nil {
50119
tx.Rollback()

router/router.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ func Serve() {
115115
admin.Post("/fetch", fetch)
116116
admin.Get("/comments", comments)
117117
admin.Delete("/comments", deleteComment)
118+
admin.Post("/report-spam", reportSpam)
118119
admin.Get("/articles", manageArticle)
119120
admin.Delete("/articles", deleteArticle)
120121
admin.Get("/media", media)

0 commit comments

Comments
 (0)