Skip to content

Commit

Permalink
Add feature flag on post creation with tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Mercier Mateo committed Nov 26, 2024
1 parent 782dcaf commit 3b44094
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
21 changes: 13 additions & 8 deletions app/actions/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package actions
import (
"context"
"time"
"fmt"

"github.com/getfider/fider/app/models/dto"
"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/env"
"github.com/getfider/fider/app/pkg/i18n"
"github.com/gosimple/slug"

Expand All @@ -29,14 +31,17 @@ type CreateNewPost struct {

// OnPreExecute prefetches Tags for later use
func (input *CreateNewPost) OnPreExecute(ctx context.Context) error {
input.Tags = make([]*entity.Tag, len(input.TagSlugs))
for i, slug := range input.TagSlugs {
getTag := &query.GetTagBySlug{Slug: slug}
if err := bus.Dispatch(ctx, getTag); err != nil {
return err
fmt.Println(env.Config.PostCreationWithTagsEnabled)
if env.Config.PostCreationWithTagsEnabled {
input.Tags = make([]*entity.Tag, len(input.TagSlugs))
for i, slug := range input.TagSlugs {
getTag := &query.GetTagBySlug{Slug: slug}
if err := bus.Dispatch(ctx, getTag); err != nil {
return err
}

input.Tags[i] = getTag.Result
}

input.Tags[i] = getTag.Result
}

return nil
Expand All @@ -46,7 +51,7 @@ func (input *CreateNewPost) OnPreExecute(ctx context.Context) error {
func (action *CreateNewPost) IsAuthorized(ctx context.Context, user *entity.User) bool {
if user == nil {
return false
} else if !user.IsCollaborator() {
} else if env.Config.PostCreationWithTagsEnabled && !user.IsCollaborator() {
for _, tag := range action.Tags {
if !tag.IsPublic {
return false
Expand Down
11 changes: 7 additions & 4 deletions app/handlers/apiv1/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"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/env"
"github.com/getfider/fider/app/pkg/web"
"github.com/getfider/fider/app/tasks"
)
Expand Down Expand Up @@ -60,10 +61,12 @@ func CreatePost() web.HandlerFunc {
return c.Failure(err)
}

for _, tag := range action.Tags {
assignTag := &cmd.AssignTag{Tag: tag, Post: newPost.Result}
if err := bus.Dispatch(c, assignTag); err != nil {
return c.Failure(err)
if env.Config.PostCreationWithTagsEnabled {
for _, tag := range action.Tags {
assignTag := &cmd.AssignTag{Tag: tag, Post: newPost.Result}
if err := bus.Dispatch(c, assignTag); err != nil {
return c.Failure(err)
}
}
}

Expand Down
13 changes: 7 additions & 6 deletions app/pkg/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ type config struct {
WriteTimeout time.Duration `env:"HTTP_WRITE_TIMEOUT,default=10s,strict"`
IdleTimeout time.Duration `env:"HTTP_IDLE_TIMEOUT,default=120s,strict"`
}
Port string `env:"PORT,default=3000"`
HostMode string `env:"HOST_MODE,default=single"`
HostDomain string `env:"HOST_DOMAIN"`
BaseURL string `env:"BASE_URL"`
Locale string `env:"LOCALE,default=en"`
JWTSecret string `env:"JWT_SECRET,required"`
Port string `env:"PORT,default=3000"`
HostMode string `env:"HOST_MODE,default=single"`
HostDomain string `env:"HOST_DOMAIN"`
BaseURL string `env:"BASE_URL"`
Locale string `env:"LOCALE,default=en"`
JWTSecret string `env:"JWT_SECRET,required"`
PostCreationWithTagsEnabled bool `env:"POST_CREATION_WITH_TAGS_ENABLED,default=false"`
Paddle struct {
IsSandbox bool `env:"PADDLE_SANDBOX,default=false"`
VendorID string `env:"PADDLE_VENDOR_ID"`
Expand Down

0 comments on commit 3b44094

Please sign in to comment.