Skip to content

Commit

Permalink
refactor: remove models base pkg (#953)
Browse files Browse the repository at this point in the history
  • Loading branch information
goenning committed May 15, 2021
1 parent 877419a commit b3cedf3
Show file tree
Hide file tree
Showing 137 changed files with 1,759 additions and 2,029 deletions.
3 changes: 1 addition & 2 deletions Makefile
Expand Up @@ -2,8 +2,7 @@
##
## For more information, refer to https://suva.sh/posts/well-documented-makefiles/

LDFLAGS += -X main.buildtime=$(shell date -u "+%Y-%m-%dT%H:%M:%S")
LDFLAGS += -X main.buildnumber=${BUILDNUMBER}
LDFLAGS += -X github.com/getfider/fider/app/pkg/env.buildnumber=${BUILDNUMBER}

##@ Running

Expand Down
7 changes: 3 additions & 4 deletions app/actions/actions.go
Expand Up @@ -3,13 +3,12 @@ package actions
import (
"context"

"github.com/getfider/fider/app/models"
"github.com/getfider/fider/app/models/entity"
"github.com/getfider/fider/app/pkg/validate"
)

// Actionable is any action that the user can perform using the web app
type Actionable interface {
Initialize() interface{}
IsAuthorized(ctx context.Context, user *models.User) bool
Validate(ctx context.Context, user *models.User) *validate.Result
IsAuthorized(ctx context.Context, user *entity.User) bool
Validate(ctx context.Context, user *entity.User) *validate.Result
}
70 changes: 47 additions & 23 deletions app/actions/invite.go
Expand Up @@ -5,81 +5,79 @@ import (
"fmt"
"strings"

"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"
"github.com/getfider/fider/app/models"
"github.com/getfider/fider/app/pkg/errors"
"github.com/getfider/fider/app/pkg/validate"
)

// InviteUsers is used to invite new users into Fider
type InviteUsers struct {
Subject string `json:"subject"`
Message string `json:"message"`
Recipients []string `json:"recipients" format:"lower"`
IsSampleInvite bool
Model *models.InviteUsers
Invitations []*models.UserInvitation
}

// Initialize the model
func (input *InviteUsers) Initialize() interface{} {
input.Model = new(models.InviteUsers)
return input.Model
Invitations []*UserInvitation
}

// IsAuthorized returns true if current user is authorized to perform this action
func (input *InviteUsers) IsAuthorized(ctx context.Context, user *models.User) bool {
func (action *InviteUsers) IsAuthorized(ctx context.Context, user *entity.User) bool {
return user != nil && user.IsCollaborator()
}

// Validate if current model is valid
func (input *InviteUsers) Validate(ctx context.Context, user *models.User) *validate.Result {
func (action *InviteUsers) Validate(ctx context.Context, user *entity.User) *validate.Result {
result := validate.Success()

if input.Model.Subject == "" {
if action.Subject == "" {
result.AddFieldFailure("subject", "Subject is required.")
} else if len(input.Model.Subject) > 70 {
} else if len(action.Subject) > 70 {
result.AddFieldFailure("subject", "Subject must have less than 70 characters.")
}

if input.Model.Message == "" {
if action.Message == "" {
result.AddFieldFailure("message", "Message is required.")
} else if !strings.Contains(input.Model.Message, app.InvitePlaceholder) {
} else if !strings.Contains(action.Message, app.InvitePlaceholder) {
msg := fmt.Sprintf("Your message is missing the invitation link placeholder. Please add '%s' to your message.", app.InvitePlaceholder)
result.AddFieldFailure("message", msg)
}

//When it's a sample invite, we skip recipients validation
if !input.IsSampleInvite {
if !action.IsSampleInvite {

if len(input.Model.Recipients) == 0 {
if len(action.Recipients) == 0 {
result.AddFieldFailure("recipients", "At least one recipient is required.")
} else if len(input.Model.Recipients) > 30 {
} else if len(action.Recipients) > 30 {
result.AddFieldFailure("recipients", "Too many recipients. We limit at 30 recipients per invite.")
}

for _, email := range input.Model.Recipients {
for _, email := range action.Recipients {
if email != "" {
messages := validate.Email(email)
result.AddFieldFailure("recipients", messages...)
}
}

if result.Ok {
input.Invitations = make([]*models.UserInvitation, 0)
for _, email := range input.Model.Recipients {
action.Invitations = make([]*UserInvitation, 0)
for _, email := range action.Recipients {
if email != "" {
err := bus.Dispatch(ctx, &query.GetUserByEmail{Email: email})
if errors.Cause(err) == app.ErrNotFound {
input.Invitations = append(input.Invitations, &models.UserInvitation{
action.Invitations = append(action.Invitations, &UserInvitation{
Email: email,
VerificationKey: models.GenerateSecretKey(),
VerificationKey: entity.GenerateEmailVerificationKey(),
})
}
}
}

if len(input.Invitations) == 0 {
if len(action.Invitations) == 0 {
result.AddFieldFailure("recipients", "All these addresses have already been registered on this site.")
}
}
Expand All @@ -88,3 +86,29 @@ func (input *InviteUsers) Validate(ctx context.Context, user *models.User) *vali

return result
}

//UserInvitation is the model used to register an invite sent to an user
type UserInvitation struct {
Email string
VerificationKey string
}

//GetEmail returns the invited user's email
func (e *UserInvitation) GetEmail() string {
return e.Email
}

//GetName returns empty for this kind of process
func (e *UserInvitation) GetName() string {
return ""
}

//GetUser returns the current user performing this action
func (e *UserInvitation) GetUser() *entity.User {
return nil
}

//GetKind returns EmailVerificationKindUserInvitation
func (e *UserInvitation) GetKind() enum.EmailVerificationKind {
return enum.EmailVerificationKindUserInvitation
}
42 changes: 20 additions & 22 deletions app/actions/invite_test.go
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/getfider/fider/app"
"github.com/getfider/fider/app/actions"
"github.com/getfider/fider/app/models"
"github.com/getfider/fider/app/models/entity"
"github.com/getfider/fider/app/models/query"
. "github.com/getfider/fider/app/pkg/assert"
"github.com/getfider/fider/app/pkg/bus"
Expand All @@ -16,7 +16,7 @@ import (
func TestInviteUsers_Empty(t *testing.T) {
RegisterT(t)

action := &actions.InviteUsers{Model: &models.InviteUsers{}}
action := &actions.InviteUsers{}
result := action.Validate(context.Background(), nil)
ExpectFailed(result, "subject", "message", "recipients")
Expect(action.Invitations).IsNil()
Expand All @@ -25,11 +25,11 @@ func TestInviteUsers_Empty(t *testing.T) {
func TestInviteUsers_Oversized(t *testing.T) {
RegisterT(t)

action := &actions.InviteUsers{Model: &models.InviteUsers{
action := &actions.InviteUsers{
Subject: "Join us and share your ideas. Because we have a cool website and this subject needs to be very long",
Message: "Use this link to join %invite%",
Recipients: []string{"jon.snow@got.com"},
}}
}
result := action.Validate(context.Background(), nil)
ExpectFailed(result, "subject")
Expect(action.Invitations).IsNil()
Expand All @@ -38,11 +38,11 @@ func TestInviteUsers_Oversized(t *testing.T) {
func TestInviteUsers_MissingInvite(t *testing.T) {
RegisterT(t)

action := &actions.InviteUsers{Model: &models.InviteUsers{
action := &actions.InviteUsers{
Subject: "Share your feedback.",
Message: "Please!",
Recipients: []string{"jon.snow@got.com"},
}}
}
result := action.Validate(context.Background(), nil)
ExpectFailed(result, "message")
Expect(action.Invitations).IsNil()
Expand All @@ -56,11 +56,11 @@ func TestInviteUsers_TooManyRecipients(t *testing.T) {
recipients[i] = fmt.Sprintf("jon.snow%d@got.com", i)
}

action := &actions.InviteUsers{Model: &models.InviteUsers{
action := &actions.InviteUsers{
Subject: "Share your feedback.",
Message: "Use this link to join %invite%",
Recipients: recipients,
}}
}
result := action.Validate(context.Background(), nil)
ExpectFailed(result, "recipients")
Expect(action.Invitations).IsNil()
Expand All @@ -69,14 +69,14 @@ func TestInviteUsers_TooManyRecipients(t *testing.T) {
func TestInviteUsers_InvalidRecipient(t *testing.T) {
RegisterT(t)

action := &actions.InviteUsers{Model: &models.InviteUsers{
action := &actions.InviteUsers{
Subject: "Share your feedback.",
Message: "Use this link to join our community: %invite%",
Recipients: []string{
"jon.snow",
"@got.com",
},
}}
}
result := action.Validate(context.Background(), nil)
ExpectFailed(result, "recipients")
Expect(action.Invitations).IsNil()
Expand All @@ -89,15 +89,15 @@ func TestInviteUsers_Valid(t *testing.T) {
return app.ErrNotFound
})

action := &actions.InviteUsers{Model: &models.InviteUsers{
action := &actions.InviteUsers{
Subject: "Share your feedback.",
Message: "Use this link to join our community: %invite%",
Recipients: []string{
"",
"jon.snow@got.com",
"arya.stark@got.com",
},
}}
}

ExpectSuccess(action.Validate(context.Background(), nil))

Expand All @@ -115,21 +115,21 @@ func TestInviteUsers_IgnoreAlreadyRegistered(t *testing.T) {

bus.AddHandler(func(ctx context.Context, q *query.GetUserByEmail) error {
if q.Email == "tony.stark@avengers.com" {
q.Result = &models.User{Email: q.Email}
q.Result = &entity.User{Email: q.Email}
return nil
}
return app.ErrNotFound
})

action := &actions.InviteUsers{Model: &models.InviteUsers{
action := &actions.InviteUsers{
Subject: "Share your feedback.",
Message: "Use this link to join our community: %invite%",
Recipients: []string{
"tony.stark@avengers.com",
"jon.snow@got.com",
"arya.stark@got.com",
},
}}
}

ExpectSuccess(action.Validate(context.Background(), nil))

Expand All @@ -146,17 +146,17 @@ func TestInviteUsers_ShouldFail_WhenAllRecipientsIgnored(t *testing.T) {
RegisterT(t)

bus.AddHandler(func(ctx context.Context, q *query.GetUserByEmail) error {
q.Result = &models.User{Email: q.Email}
q.Result = &entity.User{Email: q.Email}
return nil
})

action := &actions.InviteUsers{Model: &models.InviteUsers{
action := &actions.InviteUsers{
Subject: "Share your feedback.",
Message: "Use this link to join our community: %invite%",
Recipients: []string{
"tony.stark@avengers.com",
},
}}
}

ExpectFailed(action.Validate(context.Background(), nil), "recipients")
}
Expand All @@ -166,10 +166,8 @@ func TestInviteUsers_SampleInvite_IgnoreRecipients(t *testing.T) {

action := &actions.InviteUsers{
IsSampleInvite: true,
Model: &models.InviteUsers{
Subject: "Share your feedback.",
Message: "Use this link to join our community: %invite%",
},
Subject: "Share your feedback.",
Message: "Use this link to join our community: %invite%",
}

ExpectSuccess(action.Validate(context.Background(), nil))
Expand Down

0 comments on commit b3cedf3

Please sign in to comment.