Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow visitors to create posts with tags #1221

Merged
merged 9 commits into from
Dec 17, 2024
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"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this looks good. I was contemplating if we should have a nested AppFeatures struct, and move this into there so that we could add other things in future, but I think we probably don't need to do that...

Paddle struct {
IsSandbox bool `env:"PADDLE_SANDBOX,default=false"`
VendorID string `env:"PADDLE_VENDOR_ID"`
Expand Down
Loading