Open platform to collect and prioritize feedback
Userlist integration added - new signup will create company and user in userlist.com
Matt Robertsb9670de23380
diff --git a/.test.env b/.test.envindex a2236fb91..f1e0a7123 100644--- a/.test.env+++ b/.test.env@@ -34,4 +34,7 @@ EMAIL_SMTP_USERNAME=us3rEMAIL_SMTP_PASSWORD=p4ssEMAIL_MAILGUN_API=mys3cr3tk3y-EMAIL_MAILGUN_DOMAIN=mydomain.com\ No newline at end of file+EMAIL_MAILGUN_DOMAIN=mydomain.com++USER_LIST_ENABLED=true+USER_LIST_APIKEY=abcdefg\ No newline at end of filediff --git a/app/cmd/server.go b/app/cmd/server.goindex 23125f048..fcfbeec20 100644--- a/app/cmd/server.go+++ b/app/cmd/server.go@@ -30,11 +30,12 @@ import (_ "github.com/getfider/fider/app/services/log/sql"_ "github.com/getfider/fider/app/services/oauth"_ "github.com/getfider/fider/app/services/sqlstore/postgres"+ _ "github.com/getfider/fider/app/services/userlist"_ "github.com/getfider/fider/app/services/webhook")-//RunServer starts the Fider Server-//Returns an exitcode, 0 for OK and 1 for ERROR+// RunServer starts the Fider Server+// Returns an exitcode, 0 for OK and 1 for ERRORfunc RunServer() int {svcs := bus.Init()ctx := log.WithProperty(context.Background(), log.PropertyKeyTag, "BOOTSTRAP")diff --git a/app/handlers/signup.go b/app/handlers/signup.goindex 38f5a966e..b28df8c2d 100644--- a/app/handlers/signup.go+++ b/app/handlers/signup.go@@ -190,6 +190,11 @@ func VerifySignUpKey() web.HandlerFunc {return c.Failure(err)}+ // Handle userlist.+ if env.Config.UserList.Enabled {+ c.Enqueue(tasks.CreateUserListCompany(*c.Tenant(), *user))+ }+webutil.AddAuthUserCookie(c, user)c.Enqueue(tasks.SendWelcomeEmail(user.Name, user.Email, c.BaseURL()))diff --git a/app/models/cmd/userlist.go b/app/models/cmd/userlist.gonew file mode 100644index 000000000..932276980--- /dev/null+++ b/app/models/cmd/userlist.go@@ -0,0 +1,12 @@+package cmd++type CreateUserListCompany struct {+ Name string+ TenantId int+ SignedUpAt string+ BillingStatus string+ Subdomain string+ UserId int+ UserEmail string+ UserName string+}diff --git a/app/pkg/env/env.go b/app/pkg/env/env.goindex e9221d509..027036338 100644--- a/app/pkg/env/env.go+++ b/app/pkg/env/env.go@@ -68,6 +68,10 @@ type config struct {CDN struct {Host string `env:"CDN_HOST"`}+ UserList struct {+ Enabled bool `env:"USER_LIST_ENABLED,default=false"`+ ApiKey string `env:"USER_LIST_APIKEY"`+ }Log struct {Level string `env:"LOG_LEVEL,default=INFO"`Structured bool `env:"LOG_STRUCTURED,default=false"`diff --git a/app/services/userlist/userlist.go b/app/services/userlist/userlist.gonew file mode 100644index 000000000..1d53e74d2--- /dev/null+++ b/app/services/userlist/userlist.go@@ -0,0 +1,115 @@+// Company represents a company in UserList.com+package userlist++import (+ "bytes"+ "context"+ "encoding/json"+ "fmt"+ "net/http"+ "strconv"++ "github.com/getfider/fider/app/models/cmd"+ "github.com/getfider/fider/app/pkg/bus"+ "github.com/getfider/fider/app/pkg/env"+ "github.com/getfider/fider/app/pkg/errors"+)++// Company represents a company in UserList.com. It contains information about the company,+// including its identifier, name, sign-up date, custom properties, and associated users.+type Company struct {+ Identifier string `json:"identifier"`+ Name string `json:"name,omitempty"`+ SignedUpAt string `json:"signed_up_at,omitempty"`+ Properties map[string]interface{} `json:"properties,omitempty"`+ Users []UserListUser `json:"users,omitempty"`+}++// UserListUser represents a user in UserList.com. It contains information about the user,+// including their identifier, email, sign-up date, and custom properties.+type UserListUser struct {+ Identifier string `json:"identifier,omitempty"`+ Email string `json:"email,omitempty"`+ SignedUpAt string `json:"signed_up_at,omitempty"`+ Properties map[string]interface{} `json:"properties,omitempty"`+}++type CompanyCreateResponse struct {+ Identifier string `json:"id"`+}++const (+ UserListBaseUrl = "https://push.userlist.com"+)++func init() {+ bus.Register(Service{})+}++type Service struct{}++func (s Service) Name() string {+ return "UserList"+}++func (s Service) Category() string {+ return "userlist"+}++func (s Service) Enabled() bool {+ return env.Config.UserList.Enabled+}++func (s Service) Init() {+ bus.AddHandler(createUserListCompany)+}++func createUserListCompany(ctx context.Context, c *cmd.CreateUserListCompany) error {++ company := &Company{+ Identifier: strconv.Itoa(c.TenantId),+ Name: c.Name,+ SignedUpAt: c.SignedUpAt,+ Properties: map[string]interface{}{+ "billing_status": c.BillingStatus,+ "user_count": 1,+ "subdomain": c.Subdomain,+ },+ Users: []UserListUser{+ {+ Identifier: strconv.Itoa(c.UserId),+ Email: c.UserEmail,+ SignedUpAt: c.SignedUpAt,+ Properties: map[string]interface{}{+ "name": c.UserName,+ },+ },+ },+ }++ jsonContent, err := json.Marshal(company)+ if err != nil {+ return errors.Wrap(err, "failed to marshal company object")+ }++ req := &cmd.HTTPRequest{+ URL: fmt.Sprintf("%s/companies", UserListBaseUrl),+ Body: bytes.NewBuffer(jsonContent),+ Method: http.MethodPost,+ Headers: map[string]string{+ "Content-Type": "application/json",+ "Authorization": fmt.Sprintf("Push %s", env.Config.UserList.ApiKey),+ },+ }++ if err := bus.Dispatch(ctx, req); err != nil {+ return errors.Wrap(err, "Failed to create userlist company")+ }++ if req.ResponseStatusCode >= 300 {+ return errors.New("unexpected status code while creating company in userlist: %d", req.ResponseStatusCode)+ }++ return nil++}diff --git a/app/services/userlist/userlist_test.go b/app/services/userlist/userlist_test.gonew file mode 100644index 000000000..3f7df81d0--- /dev/null+++ b/app/services/userlist/userlist_test.go@@ -0,0 +1,50 @@+package userlist_test++import (+ "context"+ "testing"+ "time"++ "github.com/getfider/fider/app"+ "github.com/getfider/fider/app/models/cmd"+ "github.com/getfider/fider/app/models/entity"+ "github.com/getfider/fider/app/pkg/bus"+ "github.com/getfider/fider/app/pkg/env"+ "github.com/getfider/fider/app/services/httpclient/httpclientmock"+ "github.com/getfider/fider/app/services/userlist"++ . "github.com/getfider/fider/app/pkg/assert"+)++var ctx context.Context++func reset() {+ ctx = context.WithValue(context.Background(), app.TenantCtxKey, &entity.Tenant{+ Subdomain: "got",+ })+ bus.Init(userlist.Service{}, httpclientmock.Service{})+}++func TestCreatTenant_Success(t *testing.T) {+ RegisterT(t)+ env.Config.HostMode = "multi"+ reset()++ createCompanyCmd := &cmd.CreateUserListCompany{+ Name: "Fider",+ UserId: 1,+ UserEmail: "[email protected]",+ UserName: "Jon Snow",+ TenantId: 1,+ SignedUpAt: time.Now().Format(time.UnixDate),+ BillingStatus: "active",+ Subdomain: "got",+ }++ bus.Dispatch(ctx, createCompanyCmd)++ Expect(httpclientmock.RequestsHistory).HasLen(1)+ Expect(httpclientmock.RequestsHistory[0].URL.String()).Equals("https://push.userlist.com/companies")+ Expect(httpclientmock.RequestsHistory[0].Header.Get("Authorization")).Equals("Push abcdefg")+ Expect(httpclientmock.RequestsHistory[0].Header.Get("Content-Type")).Equals("application/json")+}diff --git a/app/tasks/userlist.go b/app/tasks/userlist.gonew file mode 100644index 000000000..f731b67bb--- /dev/null+++ b/app/tasks/userlist.go@@ -0,0 +1,35 @@+package tasks++import (+ "time"++ "github.com/getfider/fider/app/models/cmd"+ "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/pkg/bus"+ "github.com/getfider/fider/app/pkg/log"+ "github.com/getfider/fider/app/pkg/worker"+)++func CreateUserListCompany(tenant entity.Tenant, user entity.User) worker.Task {+ return describe("Create UserList Company", func(c *worker.Context) error {+ log.Debugf(c, "Sending new tenant @{Tenant} to userlist with user email @{User}", dto.Props{+ "Tenant": tenant.Name,+ "User": user.Email,+ })+ if err := bus.Dispatch(c, &cmd.CreateUserListCompany{+ Name: tenant.Name,+ TenantId: tenant.ID,+ SignedUpAt: time.Now().Format(time.RFC3339),+ BillingStatus: enum.BillingTrial.String(),+ Subdomain: tenant.Subdomain,+ UserId: user.ID,+ UserEmail: user.Email,+ UserName: user.Name,+ }); err != nil {+ return c.Failure(err)+ }+ return nil+ })+}