Skip to content

Commit

Permalink
Decouple invite path
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Simmerl committed Mar 28, 2017
1 parent 66fe0b8 commit 456fe2b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
3 changes: 2 additions & 1 deletion cmd/gateway-http/gateway-http.go
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,8 @@ func main() {
handler.Wrap(
withApp,
handler.UserCreate(
core.UserCreate(connections, invites, sessions, users),
core.UserCreate(sessions, users),
core.UserCreateWithInvite(connections, invites, sessions, users),
),
),
)
Expand Down
43 changes: 33 additions & 10 deletions core/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,17 @@ type UserCreateFunc func(
currentApp *app.App,
origin Origin,
u *user.User,
invite bool,
conType connection.Type,
) (*user.User, error)

// UserCreate stores the provided user and creates a session.
func UserCreate(
connections connection.Service,
invites invite.Service,
sessions session.Service,
users user.Service,
) UserCreateFunc {
return func(
currentApp *app.App,
origin Origin,
u *user.User,
invite bool,
conType connection.Type,
) (*user.User, error) {
if err := userConstrainPrivate(origin, u.Private); err != nil {
return nil, err
Expand Down Expand Up @@ -63,14 +57,42 @@ func UserCreate(
return nil, err
}

if invite {
defer mapInvites(connections, invites, currentApp, u, conType)
}

return u, nil
}
}

// UserCreateWithInviteFunc stores the provided user, creates a session and
// sets up pending connections for open invites.
type UserCreateWithInviteFunc func(
currentApp *app.App,
origin Origin,
u *user.User,
conType connection.Type,
) (*user.User, error)

// UserCreateWithInvite stores the provided user and creates a session.
func UserCreateWithInvite(
connections connection.Service,
invites invite.Service,
sessions session.Service,
users user.Service,
) UserCreateWithInviteFunc {
return func(
currentApp *app.App,
origin Origin,
u *user.User,
conType connection.Type,
) (output *user.User, err error) {
defer func() {
if err == nil {
mapInvites(connections, invites, currentApp, u, conType)
}
}()

return UserCreate(sessions, users)(currentApp, origin, u)
}
}

// UserDeleteFunc disables the user.
type UserDeleteFunc func(
currentApp *app.App,
Expand Down Expand Up @@ -465,6 +487,7 @@ func UserUpdate(
// UsersFetchFunc retrieves the users for the given ids.
type UsersFetchFunc func(currentApp *app.App, ids ...uint64) (user.List, error)

// UsersFetch retrieves the users for the given ids.
func UsersFetch(users user.Service) UsersFetchFunc {
return func(currentApp *app.App, ids ...uint64) (user.List, error) {
if len(ids) == 0 {
Expand Down
13 changes: 11 additions & 2 deletions handler/http/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import (
)

// UserCreate stores the provided user and returns it with a valid session.
func UserCreate(fn core.UserCreateFunc) Handler {
func UserCreate(
createFn core.UserCreateFunc,
createWithInviteFn core.UserCreateWithInviteFunc,
) Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
var (
currentApp = appFromContext(ctx)
Expand All @@ -32,7 +35,13 @@ func UserCreate(fn core.UserCreateFunc) Handler {
return
}

u, err := fn(currentApp, origin, p.user, invite, conType)
var u *user.User

if invite {
u, err = createWithInviteFn(currentApp, origin, p.user, conType)
} else {
u, err = createFn(currentApp, origin, p.user)
}
if err != nil {
respondError(w, 0, err)
return
Expand Down

0 comments on commit 456fe2b

Please sign in to comment.