Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

More work towards the commercial separation.

Matt Robertsbd9269a07514
diff --git a/commercial/components/ModerationIndicator.tsx b/commercial/components/ModerationIndicator.tsx
index 40c53f607..a568944dc 100644
--- a/commercial/components/ModerationIndicator.tsx
+++ b/commercial/components/ModerationIndicator.tsx
@@ -38,6 +38,8 @@ export const ModerationIndicator = () => {
}, [fider.session.user, fider.session.tenant.isModerationEnabled])
// Don't show the indicator if commercial license is not available
+ console.log("hasCommercialLicense:", hasCommercialLicense)
+ console.log("isModerationEnabled:", fider.session.tenant.isModerationEnabled)
if (!hasCommercialLicense) {
return null
}
diff --git a/commercial/handlers/apiv1/moderation.go b/commercial/handlers/apiv1/moderation.go
new file mode 100644
index 000000000..f8c047778
--- /dev/null
+++ b/commercial/handlers/apiv1/moderation.go
@@ -0,0 +1,193 @@
+package apiv1
+
+import (
+ "strconv"
+
+ "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"
+)
+
+// ApprovePost approves a post
+func ApprovePost() 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"})
+ }
+
+ if err := bus.Dispatch(c, &cmd.ApprovePost{PostID: postID}); err != nil {
+ return c.Failure(err)
+ }
+
+ return c.Ok(web.Map{})
+ }
+}
+
+// DeclinePost declines (deletes) a post
+func DeclinePost() 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"})
+ }
+
+ if err := bus.Dispatch(c, &cmd.DeclinePost{PostID: postID}); err != nil {
+ return c.Failure(err)
+ }
+
+ return c.Ok(web.Map{})
+ }
+}
+
+// ApproveComment approves a comment
+func ApproveComment() web.HandlerFunc {
+ return func(c *web.Context) error {
+ commentID, err := strconv.Atoi(c.Param("id"))
+ if err != nil {
+ return c.BadRequest(web.Map{"error": "Invalid comment ID"})
+ }
+
+ if err := bus.Dispatch(c, &cmd.ApproveComment{CommentID: commentID}); err != nil {
+ return c.Failure(err)
+ }
+
+ return c.Ok(web.Map{})
+ }
+}
+
+// DeclineComment declines (deletes) a comment
+func DeclineComment() web.HandlerFunc {
+ return func(c *web.Context) error {
+ commentID, err := strconv.Atoi(c.Param("id"))
+ if err != nil {
+ return c.BadRequest(web.Map{"error": "Invalid comment ID"})
+ }
+
+ if err := bus.Dispatch(c, &cmd.DeclineComment{CommentID: commentID}); err != nil {
+ return c.Failure(err)
+ }
+
+ return c.Ok(web.Map{})
+ }
+}
+
+// DeclinePostAndBlock declines (deletes) a post and blocks the user
+func DeclinePostAndBlock() web.HandlerFunc {
+ return func(c *web.Context) error {
+
+ // Get the post that is being declined
+ postID, err := strconv.Atoi(c.Param("id"))
+ if err != nil {
+ return c.BadRequest(web.Map{"error": "Invalid post ID"})
+ }
+
+ getPost := &query.GetPostByID{PostID: postID}
+ if err := bus.Dispatch(c, getPost); err != nil {
+ return c.Failure(err)
+ }
+
+ // Call the existing BlockUser command inside the DeclinePostAndBlock handler
+ err = bus.Dispatch(c, &cmd.BlockUser{UserID: getPost.Result.User.ID})
+ if err != nil {
+ return c.Failure(err)
+ }
+
+ // Finally call the existing DeclinePost command
+ err = bus.Dispatch(c, &cmd.DeclinePost{PostID: postID})
+ if err != nil {
+ return c.Failure(err)
+ }
+
+ return c.Ok(web.Map{})
+ }
+}
+
+// DeclineCommentAndBlock declines (deletes) a comment and blocks the user
+func DeclineCommentAndBlock() web.HandlerFunc {
+ return func(c *web.Context) error {
+ // Get the comment that is being declined
+ commentID, err := strconv.Atoi(c.Param("id"))
+ if err != nil {
+ return c.BadRequest(web.Map{"error": "Invalid comment ID"})
+ }
+
+ getComment := &query.GetCommentByID{CommentID: commentID}
+ if err := bus.Dispatch(c, getComment); err != nil {
+ return c.Failure(err)
+ }
+
+ // Call the existing BlockUser command inside the DeclinePostAndBlock handler
+ err = bus.Dispatch(c, &cmd.BlockUser{UserID: getComment.Result.User.ID})
+ if err != nil {
+ return c.Failure(err)
+ }
+
+ // Finally call the existing DeclinePost command
+ err = bus.Dispatch(c, &cmd.DeclineComment{CommentID: commentID})
+ if err != nil {
+ return c.Failure(err)
+ }
+
+ 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{})
+ }
+}
\ No newline at end of file
diff --git a/commercial/handlers/moderation.go b/commercial/handlers/moderation.go
new file mode 100644
index 000000000..a45ee7946
--- /dev/null
+++ b/commercial/handlers/moderation.go
@@ -0,0 +1,51 @@
+package handlers
+
+import (
+ "context"
+ "net/http"
+
+ "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"
+)
+
+// ModerationPage is the commercial moderation administration page
+func ModerationPage() web.HandlerFunc {
+ return func(c *web.Context) error {
+ return c.Page(http.StatusOK, web.Props{
+ Page: "Administration/pages/ContentModeration.page",
+ Title: "Moderation · Site Settings",
+ })
+ }
+}
+
+// GetModerationItems returns all unmoderated posts and comments
+func GetModerationItems() web.HandlerFunc {
+ return func(c *web.Context) error {
+ q := &query.GetModerationItems{}
+ if err := bus.Dispatch(c, q); err != nil {
+ return c.Failure(err)
+ }
+
+ return c.Ok(web.Map{
+ "items": q.Result,
+ })
+ }
+}
+
+// GetModerationCount returns the count of items awaiting moderation
+func GetModerationCount() web.HandlerFunc {
+ log.Info(context.Background(), "GetModerationCount outside Handler")
+ return func(c *web.Context) error {
+ log.Info(c, "GetModerationCount inside Handler")
+ q := &query.GetModerationCount{}
+ if err := bus.Dispatch(c, q); err != nil {
+ return c.Failure(err)
+ }
+
+ return c.Ok(web.Map{
+ "count": q.Result,
+ })
+ }
+}
diff --git a/commercial/init.go b/commercial/init.go
index eae560743..f8c88e8cf 100644
--- a/commercial/init.go
+++ b/commercial/init.go
@@ -1,26 +1,37 @@
package commercial
import (
+ "github.com/getfider/fider/app/handlers"
+ "github.com/getfider/fider/app/handlers/apiv1"
"github.com/getfider/fider/app/services"
+ commercialHandlers "github.com/getfider/fider/commercial/handlers"
+ commercialApiv1 "github.com/getfider/fider/commercial/handlers/apiv1"
)
-// This file will contain commercial license validation and service registration
-// It will be populated in later phases
-
func init() {
- // TODO: Phase 4 - Register commercial license service
- // TODO: Phase 4 - Register commercial route overrides
- // TODO: Phase 3 - Register commercial service handlers
+ // Register commercial license service
+ services.License = &commercialLicenseService{}
+
+ // Register commercial handlers
+ handlers.RegisterModerationHandlers(
+ commercialHandlers.ModerationPage,
+ commercialHandlers.GetModerationItems,
+ commercialHandlers.GetModerationCount,
+ )
- // Example (will be implemented later):
- // if isValidCommercialLicense() {
- // services.License = &commercialLicenseService{}
- // registerCommercialRoutes()
- // registerCommercialServices()
- // }
+ apiv1.RegisterModerationHandlers(
+ commercialApiv1.ApprovePost,
+ commercialApiv1.DeclinePost,
+ commercialApiv1.ApproveComment,
+ commercialApiv1.DeclineComment,
+ commercialApiv1.DeclinePostAndBlock,
+ commercialApiv1.DeclineCommentAndBlock,
+ commercialApiv1.ApprovePostAndVerify,
+ commercialApiv1.ApproveCommentAndVerify,
+ )
}
-// Placeholder for commercial license service (to be implemented)
+// Commercial license service implementation
type commercialLicenseService struct{}
func (s *commercialLicenseService) IsCommercialFeatureEnabled(feature string) bool {
diff --git a/commercial/routes.go b/commercial/routes.go
new file mode 100644
index 000000000..d4723bd4b
--- /dev/null
+++ b/commercial/routes.go
@@ -0,0 +1,81 @@
+package commercial
+
+import (
+ commercialHandlers "github.com/getfider/fider/commercial/handlers"
+ commercialApiv1 "github.com/getfider/fider/commercial/handlers/apiv1"
+ "github.com/getfider/fider/app/pkg/web"
+)
+
+// Override system for commercial routes
+
+var (
+ // Commercial handler overrides
+ CommercialModerationPage = commercialHandlers.ModerationPage
+ CommercialGetModerationItems = commercialHandlers.GetModerationItems
+ CommercialGetModerationCount = commercialHandlers.GetModerationCount
+
+ // Commercial API v1 handler overrides
+ CommercialApprovePost = commercialApiv1.ApprovePost
+ CommercialDeclinePost = commercialApiv1.DeclinePost
+ CommercialApproveComment = commercialApiv1.ApproveComment
+ CommercialDeclineComment = commercialApiv1.DeclineComment
+ CommercialDeclinePostAndBlock = commercialApiv1.DeclinePostAndBlock
+ CommercialDeclineCommentAndBlock = commercialApiv1.DeclineCommentAndBlock
+ CommercialApprovePostAndVerify = commercialApiv1.ApprovePostAndVerify
+ CommercialApproveCommentAndVerify = commercialApiv1.ApproveCommentAndVerify
+)
+
+// GetModerationHandler returns the appropriate moderation handler based on license
+func GetModerationHandler(fallback func() web.HandlerFunc) func() web.HandlerFunc {
+ return CommercialModerationPage
+}
+
+// GetModerationItemsHandler returns the appropriate moderation items handler based on license
+func GetModerationItemsHandler(fallback func() web.HandlerFunc) func() web.HandlerFunc {
+ return CommercialGetModerationItems
+}
+
+// GetModerationCountHandler returns the appropriate moderation count handler based on license
+func GetModerationCountHandler(fallback func() web.HandlerFunc) func() web.HandlerFunc {
+ return CommercialGetModerationCount
+}
+
+// GetApprovePostHandler returns the appropriate approve post handler based on license
+func GetApprovePostHandler(fallback func() web.HandlerFunc) func() web.HandlerFunc {
+ return CommercialApprovePost
+}
+
+// GetDeclinePostHandler returns the appropriate decline post handler based on license
+func GetDeclinePostHandler(fallback func() web.HandlerFunc) func() web.HandlerFunc {
+ return CommercialDeclinePost
+}
+
+// GetApproveCommentHandler returns the appropriate approve comment handler based on license
+func GetApproveCommentHandler(fallback func() web.HandlerFunc) func() web.HandlerFunc {
+ return CommercialApproveComment
+}
+
+// GetDeclineCommentHandler returns the appropriate decline comment handler based on license
+func GetDeclineCommentHandler(fallback func() web.HandlerFunc) func() web.HandlerFunc {
+ return CommercialDeclineComment
+}
+
+// GetDeclinePostAndBlockHandler returns the appropriate decline post and block handler based on license
+func GetDeclinePostAndBlockHandler(fallback func() web.HandlerFunc) func() web.HandlerFunc {
+ return CommercialDeclinePostAndBlock
+}
+
+// GetDeclineCommentAndBlockHandler returns the appropriate decline comment and block handler based on license
+func GetDeclineCommentAndBlockHandler(fallback func() web.HandlerFunc) func() web.HandlerFunc {
+ return CommercialDeclineCommentAndBlock
+}
+
+// GetApprovePostAndVerifyHandler returns the appropriate approve post and verify handler based on license
+func GetApprovePostAndVerifyHandler(fallback func() web.HandlerFunc) func() web.HandlerFunc {
+ return CommercialApprovePostAndVerify
+}
+
+// GetApproveCommentAndVerifyHandler returns the appropriate approve comment and verify handler based on license
+func GetApproveCommentAndVerifyHandler(fallback func() web.HandlerFunc) func() web.HandlerFunc {
+ return CommercialApproveCommentAndVerify
+}
\ No newline at end of file
diff --git a/commercial/services/sqlstore/postgres/moderation.go b/commercial/services/sqlstore/postgres/moderation.go
new file mode 100644
index 000000000..13c6cff61
--- /dev/null
+++ b/commercial/services/sqlstore/postgres/moderation.go
@@ -0,0 +1,317 @@
+package postgres
+
+import (
+ "context"
+ "fmt"
+ "time"
+
+ "github.com/getfider/fider/app"
+ "github.com/getfider/fider/app/models/cmd"
+ "github.com/getfider/fider/app/models/entity"
+ "github.com/getfider/fider/app/models/enum"
+ "github.com/getfider/fider/app/models/query"
+ "github.com/getfider/fider/app/pkg/bus"
+ "github.com/getfider/fider/app/pkg/dbx"
+ "github.com/getfider/fider/app/pkg/errors"
+ "github.com/getfider/fider/app/pkg/log"
+)
+
+func ApprovePost(ctx context.Context, c *cmd.ApprovePost) error {
+ return using(ctx, func(trx *dbx.Trx, tenant *entity.Tenant, user *entity.User) error {
+ _, err := trx.Execute(`
+ UPDATE posts SET is_approved = true
+ WHERE id = $1 AND tenant_id = $2`, c.PostID, tenant.ID)
+ if err != nil {
+ return errors.Wrap(err, "failed to approve post")
+ }
+ return nil
+ })
+}
+
+func DeclinePost(ctx context.Context, c *cmd.DeclinePost) error {
+ return using(ctx, func(trx *dbx.Trx, tenant *entity.Tenant, user *entity.User) error {
+ // Use existing query to get the post
+ getPost := &query.GetPostByID{PostID: c.PostID}
+ if err := bus.Dispatch(ctx, getPost); err != nil {
+ return err
+ }
+
+ // Use SetPostResponse to properly delete the post
+ setResponse := &cmd.SetPostResponse{
+ Post: getPost.Result,
+ Text: "Post declined during moderation",
+ Status: enum.PostDeleted,
+ }
+
+ return bus.Dispatch(ctx, setResponse)
+ })
+}
+
+func ApproveComment(ctx context.Context, c *cmd.ApproveComment) error {
+ return using(ctx, func(trx *dbx.Trx, tenant *entity.Tenant, user *entity.User) error {
+ _, err := trx.Execute(`
+ UPDATE comments SET is_approved = true
+ WHERE id = $1 AND tenant_id = $2`, c.CommentID, tenant.ID)
+ if err != nil {
+ return errors.Wrap(err, "failed to approve comment")
+ }
+ return nil
+ })
+}
+
+func DeclineComment(ctx context.Context, c *cmd.DeclineComment) error {
+ return using(ctx, func(trx *dbx.Trx, tenant *entity.Tenant, user *entity.User) error {
+ // Use existing delete command
+ deleteComment := &cmd.DeleteComment{CommentID: c.CommentID}
+ return bus.Dispatch(ctx, deleteComment)
+ })
+}
+
+func BulkApproveItems(ctx context.Context, c *cmd.BulkApproveItems) error {
+ return using(ctx, func(trx *dbx.Trx, tenant *entity.Tenant, user *entity.User) error {
+ if len(c.PostIDs) > 0 {
+ postIDsStr := ""
+ for i, id := range c.PostIDs {
+ if i > 0 {
+ postIDsStr += ","
+ }
+ postIDsStr += fmt.Sprintf("%d", id)
+ }
+ _, err := trx.Execute(fmt.Sprintf(`
+ UPDATE posts SET is_approved = true
+ WHERE id IN (%s) AND tenant_id = $1`, postIDsStr), tenant.ID)
+ if err != nil {
+ return errors.Wrap(err, "failed to bulk approve posts")
+ }
+ }
+
+ if len(c.CommentIDs) > 0 {
+ commentIDsStr := ""
+ for i, id := range c.CommentIDs {
+ if i > 0 {
+ commentIDsStr += ","
+ }
+ commentIDsStr += fmt.Sprintf("%d", id)
+ }
+ _, err := trx.Execute(fmt.Sprintf(`
+ UPDATE comments SET is_approved = true
+ WHERE id IN (%s) AND tenant_id = $1`, commentIDsStr), tenant.ID)
+ if err != nil {
+ return errors.Wrap(err, "failed to bulk approve comments")
+ }
+ }
+
+ return nil
+ })
+}
+
+func BulkDeclineItems(ctx context.Context, c *cmd.BulkDeclineItems) error {
+ return using(ctx, func(trx *dbx.Trx, tenant *entity.Tenant, user *entity.User) error {
+ if len(c.PostIDs) > 0 {
+ // Use existing commands to properly delete each post
+ for _, postID := range c.PostIDs {
+ // Use existing query to get the post
+ getPost := &query.GetPostByID{PostID: postID}
+ if err := bus.Dispatch(ctx, getPost); err != nil {
+ return errors.Wrap(err, "failed to get post for bulk decline")
+ }
+
+ // Use SetPostResponse to properly delete the post
+ setResponse := &cmd.SetPostResponse{
+ Post: getPost.Result,
+ Text: "Post declined during bulk moderation",
+ Status: enum.PostDeleted,
+ }
+
+ if err := bus.Dispatch(ctx, setResponse); err != nil {
+ return errors.Wrap(err, "failed to bulk decline post")
+ }
+ }
+ }
+
+ if len(c.CommentIDs) > 0 {
+ // Use existing delete command for each comment
+ for _, commentID := range c.CommentIDs {
+ deleteComment := &cmd.DeleteComment{CommentID: commentID}
+ if err := bus.Dispatch(ctx, deleteComment); err != nil {
+ return errors.Wrap(err, "failed to bulk decline comment")
+ }
+ }
+ }
+
+ return nil
+ })
+}
+
+type dbModerationPost struct {
+ ID int `db:"id"`
+ Number int `db:"number"`
+ Title string `db:"title"`
+ Slug string `db:"slug"`
+ Description string `db:"description"`
+ CreatedAt time.Time `db:"created_at"`
+ User *dbUser `db:"user"`
+}
+
+type dbModerationComment struct {
+ ID int `db:"id"`
+ PostID int `db:"post_id"`
+ PostNumber int `db:"post_number"`
+ PostSlug string `db:"post_slug"`
+ Content string `db:"content"`
+ CreatedAt time.Time `db:"created_at"`
+ User *dbUser `db:"user"`
+ PostTitle string `db:"post_title"`
+}
+
+type dbUser struct {
+ ID int `db:"user_id"`
+ Name string `db:"user_name"`
+ Email string `db:"user_email"`
+ Role enum.Role `db:"user_role"`
+ Status enum.UserStatus `db:"user_status"`
+ AvatarType enum.AvatarType `db:"user_avatar_type"`
+ AvatarBkey string `db:"user_avatar_bkey"`
+}
+
+func (u *dbUser) toModel(ctx context.Context) *entity.User {
+ return &entity.User{
+ ID: u.ID,
+ Name: u.Name,
+ Email: u.Email,
+ Role: u.Role,
+ Status: u.Status,
+ AvatarType: u.AvatarType,
+ AvatarBlobKey: u.AvatarBkey,
+ }
+}
+
+func GetModerationItems(ctx context.Context, q *query.GetModerationItems) error {
+ return using(ctx, func(trx *dbx.Trx, tenant *entity.Tenant, user *entity.User) error {
+ q.Result = make([]*query.ModerationItem, 0)
+
+ // Get unmoderated posts
+ var posts []*dbModerationPost
+
+ err := trx.Select(&posts, `
+ SELECT p.id, p.number, p.title, p.slug, p.description, p.created_at,
+ u.id AS user_id,
+ u.name AS user_name,
+ u.email AS user_email,
+ u.role AS user_role,
+ u.status AS user_status,
+ u.avatar_type AS user_avatar_type,
+ u.avatar_bkey AS user_avatar_bkey
+ FROM posts p
+ INNER JOIN users u ON u.id = p.user_id AND u.tenant_id = p.tenant_id
+ WHERE p.tenant_id = $1 AND p.is_approved = false and p.status <> $2
+ ORDER BY p.created_at DESC`, tenant.ID, enum.PostDeleted)
+ if err != nil {
+ return errors.Wrap(err, "failed to get unmoderated posts")
+ }
+
+ for _, post := range posts {
+ userWithEmail := &entity.UserWithEmail{
+ User: post.User.toModel(ctx),
+ }
+
+ q.Result = append(q.Result, &query.ModerationItem{
+ Type: "post",
+ ID: post.ID,
+ PostNumber: post.Number,
+ PostSlug: post.Slug,
+ Title: post.Title,
+ Content: post.Description,
+ CreatedAt: post.CreatedAt,
+ User: userWithEmail,
+ })
+ }
+
+ // Get unmoderated comments
+ var comments []*dbModerationComment
+
+ err = trx.Select(&comments, `
+ SELECT c.id, c.post_id, p.number as post_number, p.slug as post_slug, c.content, c.created_at,
+ u.id AS user_id,
+ u.name AS user_name,
+ u.email AS user_email,
+ u.role AS user_role,
+ u.status AS user_status,
+ u.avatar_type AS user_avatar_type,
+ u.avatar_bkey AS user_avatar_bkey,
+ p.title as post_title
+ FROM comments c
+ 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")
+ }
+
+ for _, comment := range comments {
+
+ userWithEmail := &entity.UserWithEmail{
+ User: comment.User.toModel(ctx),
+ }
+
+ q.Result = append(q.Result, &query.ModerationItem{
+ Type: "comment",
+ ID: comment.ID,
+ PostID: comment.PostID,
+ PostNumber: comment.PostNumber,
+ PostSlug: comment.PostSlug,
+ Content: comment.Content,
+ CreatedAt: comment.CreatedAt,
+ PostTitle: comment.PostTitle,
+ User: userWithEmail,
+ })
+ }
+
+ return nil
+ })
+}
+
+func GetModerationCount(ctx context.Context, q *query.GetModerationCount) error {
+ log.Info(ctx, "COMMERCIAL: GetModerationCount function called!")
+ return using(ctx, func(trx *dbx.Trx, tenant *entity.Tenant, user *entity.User) error {
+ var count int
+
+ log.Info(ctx, "COMMERCIAL: Getting the moderation count properly")
+
+ 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 AND c.deleted_at IS NULL)
+ `, tenant.ID, enum.PostDeleted)
+
+ if err != nil {
+ return errors.Wrap(err, "failed to get moderation count")
+ }
+
+ q.Result = count
+ return nil
+ })
+}
+
+func VerifyUser(ctx context.Context, c *cmd.VerifyUser) error {
+ return using(ctx, func(trx *dbx.Trx, tenant *entity.Tenant, user *entity.User) error {
+ // Verify and unblock the user (can't be both blocked and verified)
+ _, err := trx.Execute(`
+ UPDATE users SET is_verified = true, status = $1
+ WHERE id = $2 AND tenant_id = $3`, enum.UserActive, c.UserID, tenant.ID)
+ if err != nil {
+ return errors.Wrap(err, "failed to verify user")
+ }
+
+ return nil
+ })
+}
+
+func using(ctx context.Context, handler func(*dbx.Trx, *entity.Tenant, *entity.User) error) error {
+ trx, _ := ctx.Value(app.TransactionCtxKey).(*dbx.Trx)
+ tenant, _ := ctx.Value(app.TenantCtxKey).(*entity.Tenant)
+ user, _ := ctx.Value(app.UserCtxKey).(*entity.User)
+ return handler(trx, tenant, user)
+}
diff --git a/commercial/services/sqlstore/postgres/postgres.go b/commercial/services/sqlstore/postgres/postgres.go
new file mode 100644
index 000000000..ef7864289
--- /dev/null
+++ b/commercial/services/sqlstore/postgres/postgres.go
@@ -0,0 +1,37 @@
+package postgres
+
+import (
+ "github.com/getfider/fider/app/pkg/bus"
+)
+
+func init() {
+ bus.Register(CommercialService{})
+}
+
+type CommercialService struct{}
+
+func (s CommercialService) Name() string {
+ return "Commercial PostgreSQL"
+}
+
+func (s CommercialService) Category() string {
+ return "commercial-sqlstore"
+}
+
+func (s CommercialService) Enabled() bool {
+ return true
+}
+
+func (s CommercialService) Init() {
+ // Register commercial moderation handlers
+ // These should override the open source stub handlers
+ bus.AddHandler(ApprovePost)
+ bus.AddHandler(DeclinePost)
+ bus.AddHandler(ApproveComment)
+ bus.AddHandler(DeclineComment)
+ bus.AddHandler(BulkApproveItems)
+ bus.AddHandler(BulkDeclineItems)
+ bus.AddHandler(GetModerationItems)
+ bus.AddHandler(GetModerationCount)
+ bus.AddHandler(VerifyUser)
+}
diff --git a/main.go b/main.go
index 63e000006..e76ca8dd0 100644
--- a/main.go
+++ b/main.go
@@ -4,6 +4,8 @@ import (
"os"
"github.com/getfider/fider/app/cmd"
+ _ "github.com/getfider/fider/commercial"
+ _ "github.com/getfider/fider/commercial/services/sqlstore/postgres" // Import AFTER cmd to override open source handlers
_ "github.com/lib/pq"
)
diff --git a/tsconfig.json b/tsconfig.json
index 73790fe48..e1021294b 100755
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -14,8 +14,9 @@
"noUnusedLocals": true,
"paths": {
"@fider/*": ["./public/*"],
+ "@commercial/*": ["./commercial/*"],
"@locale/*": ["./locale/*"]
}
},
- "include": ["./public/**/*", "./locale/**/*", "index.d.ts", "./e2e/**/*"]
+ "include": ["./public/**/*", "./commercial/**/*", "./locale/**/*", "index.d.ts", "./e2e/**/*"]
}