From 456fe2b6e4a0c0c1bec7f18b7ae9267ce446a59b Mon Sep 17 00:00:00 2001 From: Alexander Simmerl Date: Tue, 28 Mar 2017 13:24:46 +0200 Subject: [PATCH] Decouple invite path --- cmd/gateway-http/gateway-http.go | 3 ++- core/user.go | 43 ++++++++++++++++++++++++-------- handler/http/user.go | 13 ++++++++-- 3 files changed, 46 insertions(+), 13 deletions(-) diff --git a/cmd/gateway-http/gateway-http.go b/cmd/gateway-http/gateway-http.go index 1cdb0d0..0433b26 100644 --- a/cmd/gateway-http/gateway-http.go +++ b/cmd/gateway-http/gateway-http.go @@ -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), ), ), ) diff --git a/core/user.go b/core/user.go index 593ee10..8689d4d 100644 --- a/core/user.go +++ b/core/user.go @@ -19,14 +19,10 @@ 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 { @@ -34,8 +30,6 @@ func UserCreate( 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 @@ -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, @@ -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 { diff --git a/handler/http/user.go b/handler/http/user.go index cf759a1..58da6d1 100644 --- a/handler/http/user.go +++ b/handler/http/user.go @@ -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) @@ -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