Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

Approve and verify DONE

Matt Roberts7292e7089944
diff --git a/app/cmd/routes.go b/app/cmd/routes.go
index 16879520d..b199b56e6 100644
--- a/app/cmd/routes.go
+++ b/app/cmd/routes.go
@@ -262,9 +262,11 @@ func routes(r *web.Engine) *web.Engine {
adminApi.Put("/api/v1/tags/:slug", apiv1.CreateEditTag())
adminApi.Delete("/api/v1/tags/:slug", apiv1.DeleteTag())
+ adminApi.Post("/api/v1/admin/moderation/posts/:id/approve-and-verify", apiv1.ApprovePostAndVerify())
+ adminApi.Post("/api/v1/admin/moderation/posts/:id/decline-and-block", apiv1.DeclinePostAndBlock())
adminApi.Post("/api/v1/admin/moderation/posts/:id/approve", apiv1.ApprovePost())
adminApi.Post("/api/v1/admin/moderation/posts/:id/decline", apiv1.DeclinePost())
- adminApi.Post("/api/v1/admin/moderation/posts/:id/decline-and-block", apiv1.DeclinePostAndBlock())
+ adminApi.Post("/api/v1/admin/moderation/comments/:id/approve-and-verify", apiv1.ApproveCommentAndVerify())
adminApi.Post("/api/v1/admin/moderation/comments/:id/decline-and-block", apiv1.DeclineCommentAndBlock())
adminApi.Post("/api/v1/admin/moderation/comments/:id/approve", apiv1.ApproveComment())
adminApi.Post("/api/v1/admin/moderation/comments/:id/decline", apiv1.DeclineComment())
diff --git a/app/handlers/apiv1/moderation.go b/app/handlers/apiv1/moderation.go
index ae92f2661..b6734172a 100644
--- a/app/handlers/apiv1/moderation.go
+++ b/app/handlers/apiv1/moderation.go
@@ -6,6 +6,7 @@ import (
"github.com/getfider/fider/app/models/cmd"
"github.com/getfider/fider/app/models/query"
"github.com/getfider/fider/app/pkg/bus"
+ "github.com/getfider/fider/app/pkg/log"
"github.com/getfider/fider/app/pkg/web"
)
@@ -133,3 +134,60 @@ func DeclineCommentAndBlock() web.HandlerFunc {
return c.Ok(web.Map{})
}
}
+
+// ApprovePostAndVerify approves a post and verifies the user
+func ApprovePostAndVerify() web.HandlerFunc {
+ return func(c *web.Context) error {
+ postID, err := strconv.Atoi(c.Param("id"))
+ if err != nil {
+ return c.BadRequest(web.Map{"error": "Invalid post ID"})
+ }
+
+ // Get the post that is being approved
+ getPost := &query.GetPostByID{PostID: postID}
+ if err := bus.Dispatch(c, getPost); err != nil {
+ return c.Failure(err)
+ }
+
+ // First approve the post
+ if err := bus.Dispatch(c, &cmd.ApprovePost{PostID: postID}); err != nil {
+ return c.Failure(err)
+ }
+
+ // Then verify the user
+ if err := bus.Dispatch(c, &cmd.VerifyUser{UserID: getPost.Result.User.ID}); err != nil {
+ return c.Failure(err)
+ }
+
+ return c.Ok(web.Map{})
+ }
+}
+
+// ApproveCommentAndVerify approves a comment and verifies the user
+func ApproveCommentAndVerify() web.HandlerFunc {
+ return func(c *web.Context) error {
+ log.Info(c, "Approving comment and verifying user")
+ commentID, err := strconv.Atoi(c.Param("id"))
+ if err != nil {
+ return c.BadRequest(web.Map{"error": "Invalid comment ID"})
+ }
+
+ // Get the comment that is being approved
+ getComment := &query.GetCommentByID{CommentID: commentID}
+ if err := bus.Dispatch(c, getComment); err != nil {
+ return c.Failure(err)
+ }
+
+ // First approve the comment
+ if err := bus.Dispatch(c, &cmd.ApproveComment{CommentID: commentID}); err != nil {
+ return c.Failure(err)
+ }
+
+ // Then verify the user
+ if err := bus.Dispatch(c, &cmd.VerifyUser{UserID: getComment.Result.User.ID}); err != nil {
+ return c.Failure(err)
+ }
+
+ return c.Ok(web.Map{})
+ }
+}
diff --git a/app/handlers/apiv1/moderation_test.go b/app/handlers/apiv1/moderation_test.go
index 8e5c0555c..3f4c7238d 100644
--- a/app/handlers/apiv1/moderation_test.go
+++ b/app/handlers/apiv1/moderation_test.go
@@ -217,8 +217,8 @@ func TestDeclinePostAndBlock_Success(t *testing.T) {
User: mock.AryaStark,
}
- bus.AddHandler(func(ctx context.Context, q *query.GetPostByNumber) error {
- if q.Number == 5 {
+ bus.AddHandler(func(ctx context.Context, q *query.GetPostByID) error {
+ if q.PostID == 5 {
q.Result = post
return nil
}
@@ -264,7 +264,7 @@ func TestDeclinePostAndBlock_InvalidID(t *testing.T) {
func TestDeclinePostAndBlock_PostNotFound(t *testing.T) {
RegisterT(t)
- bus.AddHandler(func(ctx context.Context, q *query.GetPostByNumber) error {
+ bus.AddHandler(func(ctx context.Context, q *query.GetPostByID) error {
return app.ErrNotFound
})
@@ -287,8 +287,8 @@ func TestDeclinePostAndBlock_BlockUserFailure(t *testing.T) {
User: mock.AryaStark,
}
- bus.AddHandler(func(ctx context.Context, q *query.GetPostByNumber) error {
- if q.Number == 5 {
+ bus.AddHandler(func(ctx context.Context, q *query.GetPostByID) error {
+ if q.PostID == 5 {
q.Result = post
return nil
}
@@ -406,3 +406,272 @@ func TestDeclineCommentAndBlock_BlockUserFailure(t *testing.T) {
Expect(code).Equals(http.StatusNotFound)
}
+
+func TestApprovePostAndVerify_Success(t *testing.T) {
+ RegisterT(t)
+
+ post := &entity.Post{
+ ID: 1,
+ Number: 5,
+ Title: "Test Post",
+ User: mock.AryaStark,
+ }
+
+ bus.AddHandler(func(ctx context.Context, q *query.GetPostByID) error {
+ if q.PostID == 5 {
+ q.Result = post
+ return nil
+ }
+ return app.ErrNotFound
+ })
+
+ var approvePost *cmd.ApprovePost
+ bus.AddHandler(func(ctx context.Context, c *cmd.ApprovePost) error {
+ approvePost = c
+ return nil
+ })
+
+ var verifyUser *cmd.VerifyUser
+ bus.AddHandler(func(ctx context.Context, c *cmd.VerifyUser) error {
+ verifyUser = c
+ return nil
+ })
+
+ code, _ := mock.NewServer().
+ OnTenant(mock.DemoTenant).
+ AsUser(mock.JonSnow).
+ AddParam("id", "5").
+ ExecutePost(apiv1.ApprovePostAndVerify(), `{}`)
+
+ Expect(code).Equals(http.StatusOK)
+ Expect(approvePost.PostID).Equals(5)
+ Expect(verifyUser.UserID).Equals(mock.AryaStark.ID)
+}
+
+func TestApprovePostAndVerify_InvalidID(t *testing.T) {
+ RegisterT(t)
+
+ code, response := mock.NewServer().
+ OnTenant(mock.DemoTenant).
+ AsUser(mock.JonSnow).
+ AddParam("id", "invalid").
+ ExecuteAsJSON(apiv1.ApprovePostAndVerify())
+
+ Expect(code).Equals(http.StatusBadRequest)
+ Expect(response.String("error")).Equals("Invalid post ID")
+}
+
+func TestApprovePostAndVerify_PostNotFound(t *testing.T) {
+ RegisterT(t)
+
+ bus.AddHandler(func(ctx context.Context, q *query.GetPostByID) error {
+ return app.ErrNotFound
+ })
+
+ code, _ := mock.NewServer().
+ OnTenant(mock.DemoTenant).
+ AsUser(mock.JonSnow).
+ AddParam("id", "999").
+ ExecutePost(apiv1.ApprovePostAndVerify(), `{}`)
+
+ Expect(code).Equals(http.StatusNotFound)
+}
+
+func TestApprovePostAndVerify_ApproveFailure(t *testing.T) {
+ RegisterT(t)
+
+ post := &entity.Post{
+ ID: 1,
+ Number: 5,
+ Title: "Test Post",
+ User: mock.AryaStark,
+ }
+
+ bus.AddHandler(func(ctx context.Context, q *query.GetPostByID) error {
+ if q.PostID == 5 {
+ q.Result = post
+ return nil
+ }
+ return app.ErrNotFound
+ })
+
+ bus.AddHandler(func(ctx context.Context, c *cmd.ApprovePost) error {
+ return app.ErrNotFound
+ })
+
+ code, _ := mock.NewServer().
+ OnTenant(mock.DemoTenant).
+ AsUser(mock.JonSnow).
+ AddParam("id", "5").
+ ExecutePost(apiv1.ApprovePostAndVerify(), `{}`)
+
+ Expect(code).Equals(http.StatusNotFound)
+}
+
+func TestApprovePostAndVerify_VerifyUserFailure(t *testing.T) {
+ RegisterT(t)
+
+ post := &entity.Post{
+ ID: 1,
+ Number: 5,
+ Title: "Test Post",
+ User: mock.AryaStark,
+ }
+
+ bus.AddHandler(func(ctx context.Context, q *query.GetPostByID) error {
+ if q.PostID == 5 {
+ q.Result = post
+ return nil
+ }
+ return app.ErrNotFound
+ })
+
+ bus.AddHandler(func(ctx context.Context, c *cmd.ApprovePost) error {
+ return nil
+ })
+
+ bus.AddHandler(func(ctx context.Context, c *cmd.VerifyUser) error {
+ return app.ErrNotFound
+ })
+
+ code, _ := mock.NewServer().
+ OnTenant(mock.DemoTenant).
+ AsUser(mock.JonSnow).
+ AddParam("id", "5").
+ ExecutePost(apiv1.ApprovePostAndVerify(), `{}`)
+
+ Expect(code).Equals(http.StatusNotFound)
+}
+
+func TestApproveCommentAndVerify_Success(t *testing.T) {
+ RegisterT(t)
+
+ comment := &entity.Comment{
+ ID: 25,
+ Content: "Test Comment",
+ User: mock.AryaStark,
+ }
+
+ bus.AddHandler(func(ctx context.Context, q *query.GetCommentByID) error {
+ if q.CommentID == 25 {
+ q.Result = comment
+ return nil
+ }
+ return app.ErrNotFound
+ })
+
+ var approveComment *cmd.ApproveComment
+ bus.AddHandler(func(ctx context.Context, c *cmd.ApproveComment) error {
+ approveComment = c
+ return nil
+ })
+
+ var verifyUser *cmd.VerifyUser
+ bus.AddHandler(func(ctx context.Context, c *cmd.VerifyUser) error {
+ verifyUser = c
+ return nil
+ })
+
+ code, _ := mock.NewServer().
+ OnTenant(mock.DemoTenant).
+ AsUser(mock.JonSnow).
+ AddParam("id", "25").
+ ExecutePost(apiv1.ApproveCommentAndVerify(), `{}`)
+
+ Expect(code).Equals(http.StatusOK)
+ Expect(approveComment.CommentID).Equals(25)
+ Expect(verifyUser.UserID).Equals(mock.AryaStark.ID)
+}
+
+func TestApproveCommentAndVerify_InvalidID(t *testing.T) {
+ RegisterT(t)
+
+ code, response := mock.NewServer().
+ OnTenant(mock.DemoTenant).
+ AsUser(mock.JonSnow).
+ AddParam("id", "invalid").
+ ExecuteAsJSON(apiv1.ApproveCommentAndVerify())
+
+ Expect(code).Equals(http.StatusBadRequest)
+ Expect(response.String("error")).Equals("Invalid comment ID")
+}
+
+func TestApproveCommentAndVerify_CommentNotFound(t *testing.T) {
+ RegisterT(t)
+
+ bus.AddHandler(func(ctx context.Context, q *query.GetCommentByID) error {
+ return app.ErrNotFound
+ })
+
+ code, _ := mock.NewServer().
+ OnTenant(mock.DemoTenant).
+ AsUser(mock.JonSnow).
+ AddParam("id", "999").
+ ExecutePost(apiv1.ApproveCommentAndVerify(), `{}`)
+
+ Expect(code).Equals(http.StatusNotFound)
+}
+
+func TestApproveCommentAndVerify_ApproveFailure(t *testing.T) {
+ RegisterT(t)
+
+ comment := &entity.Comment{
+ ID: 25,
+ Content: "Test Comment",
+ User: mock.AryaStark,
+ }
+
+ bus.AddHandler(func(ctx context.Context, q *query.GetCommentByID) error {
+ if q.CommentID == 25 {
+ q.Result = comment
+ return nil
+ }
+ return app.ErrNotFound
+ })
+
+ bus.AddHandler(func(ctx context.Context, c *cmd.ApproveComment) error {
+ return app.ErrNotFound
+ })
+
+ code, _ := mock.NewServer().
+ OnTenant(mock.DemoTenant).
+ AsUser(mock.JonSnow).
+ AddParam("id", "25").
+ ExecutePost(apiv1.ApproveCommentAndVerify(), `{}`)
+
+ Expect(code).Equals(http.StatusNotFound)
+}
+
+func TestApproveCommentAndVerify_VerifyUserFailure(t *testing.T) {
+ RegisterT(t)
+
+ comment := &entity.Comment{
+ ID: 25,
+ Content: "Test Comment",
+ User: mock.AryaStark,
+ }
+
+ bus.AddHandler(func(ctx context.Context, q *query.GetCommentByID) error {
+ if q.CommentID == 25 {
+ q.Result = comment
+ return nil
+ }
+ return app.ErrNotFound
+ })
+
+ bus.AddHandler(func(ctx context.Context, c *cmd.ApproveComment) error {
+ return nil
+ })
+
+ bus.AddHandler(func(ctx context.Context, c *cmd.VerifyUser) error {
+ return app.ErrNotFound
+ })
+
+ code, _ := mock.NewServer().
+ OnTenant(mock.DemoTenant).
+ AsUser(mock.JonSnow).
+ AddParam("id", "25").
+ ExecutePost(apiv1.ApproveCommentAndVerify(), `{}`)
+
+ Expect(code).Equals(http.StatusNotFound)
+}
diff --git a/app/services/sqlstore/postgres/moderation.go b/app/services/sqlstore/postgres/moderation.go
index e4493f3d7..6d761b2e5 100644
--- a/app/services/sqlstore/postgres/moderation.go
+++ b/app/services/sqlstore/postgres/moderation.go
@@ -220,6 +220,7 @@ func getModerationItems(ctx context.Context, q *query.GetModerationItems) error
INNER JOIN users u ON u.id = c.user_id AND u.tenant_id = c.tenant_id
INNER JOIN posts p ON p.id = c.post_id AND p.tenant_id = c.tenant_id
WHERE c.tenant_id = $1 AND c.is_approved = false and p.status <> $2
+ AND c.deleted_at IS NULL
ORDER BY c.created_at DESC`, tenant.ID, enum.PostDeleted)
if err != nil {
return errors.Wrap(err, "failed to get unmoderated comments")
@@ -255,7 +256,7 @@ func getModerationCount(ctx context.Context, q *query.GetModerationCount) error
err := trx.Get(&count, `
SELECT
(SELECT COUNT(*) FROM posts WHERE tenant_id = $1 AND is_approved = false and status <> $2) +
- (SELECT COUNT(*) FROM comments c JOIN posts p on c.post_id = p.id WHERE p.tenant_id = $1 AND c.is_approved = false AND p.status <> $2)
+ (SELECT COUNT(*) FROM comments c JOIN posts p on c.post_id = p.id WHERE p.tenant_id = $1 AND c.is_approved = false AND p.status <> $2 AND c.deleted_at IS NULL)
`, tenant.ID, enum.PostDeleted)
if err != nil {