diff --git a/.gitignore b/.gitignore index 45095bd..d8a89e0 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ build/ .cache/ compile_commands.json CMakeLists.txt.user +tags diff --git a/go/Taskfile.yml b/go/Taskfile.yml index 08fc5e6..0c37f3a 100644 --- a/go/Taskfile.yml +++ b/go/Taskfile.yml @@ -11,6 +11,11 @@ tasks: - task: genproto - go generate ./... + format: + desc: Format go files + cmds: + - go fmt ./... + genproto: desc: Generate Go code from .proto files via docker vars: diff --git a/go/cmd/orion/main.go b/go/cmd/orion/main.go index 11395bc..09ec9fd 100644 --- a/go/cmd/orion/main.go +++ b/go/cmd/orion/main.go @@ -103,7 +103,7 @@ func main() { } defer firestoreClient.Close() - networkSvc := network.NewService(db.Pool(), aeroSvc, billingSvc, livestore.NewMembershipPublisher(firestoreClient)) + networkSvc := network.NewService(db.Pool(), aeroSvc, billingSvc, livestore.NewMembershipPublisher(firestoreClient), humanSvc) particleSvc := particle.NewService(db.Pool(), networkSvc) depotSvc := depot.NewService(db.Pool(), storageClient, depot.Config{ GoogleServiceAccountEmail: utils.MustGetEnv("GOOGLE_SERVICE_ACCOUNT_EMAIL"), diff --git a/go/internal/human/mocks/service.go b/go/internal/human/mocks/service.go new file mode 100644 index 0000000..274ef88 --- /dev/null +++ b/go/internal/human/mocks/service.go @@ -0,0 +1,131 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: ./service.go +// +// Generated by this command: +// +// mockgen -source ./service.go -destination ./mocks/service.go +// + +// Package mock_human is a generated GoMock package. +package mock_human + +import ( + context "context" + reflect "reflect" + time "time" + + human "github.com/flowy-live/llink/internal/human" + gomock "go.uber.org/mock/gomock" +) + +// MockService is a mock of Service interface. +type MockService struct { + ctrl *gomock.Controller + recorder *MockServiceMockRecorder + isgomock struct{} +} + +// MockServiceMockRecorder is the mock recorder for MockService. +type MockServiceMockRecorder struct { + mock *MockService +} + +// NewMockService creates a new mock instance. +func NewMockService(ctrl *gomock.Controller) *MockService { + mock := &MockService{ctrl: ctrl} + mock.recorder = &MockServiceMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockService) EXPECT() *MockServiceMockRecorder { + return m.recorder +} + +// GetByEmail mocks base method. +func (m *MockService) GetByEmail(ctx context.Context, email string) (*human.Human, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetByEmail", ctx, email) + ret0, _ := ret[0].(*human.Human) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetByEmail indicates an expected call of GetByEmail. +func (mr *MockServiceMockRecorder) GetByEmail(ctx, email any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetByEmail", reflect.TypeOf((*MockService)(nil).GetByEmail), ctx, email) +} + +// GetByID mocks base method. +func (m *MockService) GetByID(ctx context.Context, id string) (*human.Human, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetByID", ctx, id) + ret0, _ := ret[0].(*human.Human) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetByID indicates an expected call of GetByID. +func (mr *MockServiceMockRecorder) GetByID(ctx, id any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetByID", reflect.TypeOf((*MockService)(nil).GetByID), ctx, id) +} + +// GetOrCreateByEmail mocks base method. +func (m *MockService) GetOrCreateByEmail(ctx context.Context, email string) (*human.Human, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetOrCreateByEmail", ctx, email) + ret0, _ := ret[0].(*human.Human) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetOrCreateByEmail indicates an expected call of GetOrCreateByEmail. +func (mr *MockServiceMockRecorder) GetOrCreateByEmail(ctx, email any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetOrCreateByEmail", reflect.TypeOf((*MockService)(nil).GetOrCreateByEmail), ctx, email) +} + +// ListAll mocks base method. +func (m *MockService) ListAll(ctx context.Context) ([]*human.Human, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ListAll", ctx) + ret0, _ := ret[0].([]*human.Human) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ListAll indicates an expected call of ListAll. +func (mr *MockServiceMockRecorder) ListAll(ctx any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListAll", reflect.TypeOf((*MockService)(nil).ListAll), ctx) +} + +// UpdateEmailNotificationsEnabled mocks base method. +func (m *MockService) UpdateEmailNotificationsEnabled(ctx context.Context, id string, enabled bool) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "UpdateEmailNotificationsEnabled", ctx, id, enabled) + ret0, _ := ret[0].(error) + return ret0 +} + +// UpdateEmailNotificationsEnabled indicates an expected call of UpdateEmailNotificationsEnabled. +func (mr *MockServiceMockRecorder) UpdateEmailNotificationsEnabled(ctx, id, enabled any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateEmailNotificationsEnabled", reflect.TypeOf((*MockService)(nil).UpdateEmailNotificationsEnabled), ctx, id, enabled) +} + +// UpdateLastEmailNotificationSentAt mocks base method. +func (m *MockService) UpdateLastEmailNotificationSentAt(ctx context.Context, id string, t time.Time) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "UpdateLastEmailNotificationSentAt", ctx, id, t) + ret0, _ := ret[0].(error) + return ret0 +} + +// UpdateLastEmailNotificationSentAt indicates an expected call of UpdateLastEmailNotificationSentAt. +func (mr *MockServiceMockRecorder) UpdateLastEmailNotificationSentAt(ctx, id, t any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateLastEmailNotificationSentAt", reflect.TypeOf((*MockService)(nil).UpdateLastEmailNotificationSentAt), ctx, id, t) +} diff --git a/go/internal/human/service.go b/go/internal/human/service.go index 2180443..1a28421 100644 --- a/go/internal/human/service.go +++ b/go/internal/human/service.go @@ -9,6 +9,8 @@ import ( "github.com/jackc/pgx/v5/pgxpool" ) +//go:generate go tool mockgen -source ./service.go -destination ./mocks/service.go + var ErrNotFound = errors.New("human not found") type Service interface { diff --git a/go/internal/network/service.go b/go/internal/network/service.go index 15ce8bd..861d29a 100644 --- a/go/internal/network/service.go +++ b/go/internal/network/service.go @@ -16,6 +16,7 @@ import ( pbaero "github.com/flowy-live/llink/genproto/aero" "github.com/flowy-live/llink/internal/billing" "github.com/flowy-live/llink/internal/constants" + "github.com/flowy-live/llink/internal/human" "github.com/flowy-live/llink/internal/livestore" "github.com/flowy-live/llink/internal/utils" ) @@ -27,6 +28,10 @@ var ErrInvalidHumanId = errors.New("invalid humanId") var ErrCapacityExceeded = errors.New("active stream capacity exceeded") var ErrInvalidRetentionHours = errors.New("message retention hours must be between 24 and 336") +type humanLookup interface { + GetByID(ctx context.Context, id string) (*human.Human, error) +} + type Service interface { Reader @@ -54,17 +59,25 @@ type Service interface { type serviceImpl struct { *readerImpl - aeroSvc pbaero.PrimaryClient - billingSvc billing.Service - pub livestore.MembershipPublisher + aeroSvc pbaero.PrimaryClient + billingSvc billing.Service + pub livestore.MembershipPublisher + humanLookup humanLookup } -func NewService(pool *pgxpool.Pool, aeroSvc pbaero.PrimaryClient, billingSvc billing.Service, pub livestore.MembershipPublisher) Service { +func NewService( + pool *pgxpool.Pool, + aeroSvc pbaero.PrimaryClient, + billingSvc billing.Service, + pub livestore.MembershipPublisher, + humanLookup humanLookup, +) Service { return &serviceImpl{ - readerImpl: newReader(pool), - aeroSvc: aeroSvc, - billingSvc: billingSvc, - pub: pub, + readerImpl: newReader(pool), + aeroSvc: aeroSvc, + billingSvc: billingSvc, + pub: pub, + humanLookup: humanLookup, } } @@ -223,6 +236,14 @@ func (s *serviceImpl) InviteByEmail(ctx context.Context, networkID string, email } func (s *serviceImpl) AcceptInvitation(ctx context.Context, networkID, email, humanId string) error { + network, err := s.repo.getByID(ctx, networkID) + if err != nil { + if errors.Is(err, errNotFound) { + return ErrNotFound + } + return err + } + normalized, err := utils.NormalizeEmail(email) if err != nil { return fmt.Errorf("%w: %w", ErrInvalidEmail, err) @@ -231,6 +252,11 @@ func (s *serviceImpl) AcceptInvitation(ctx context.Context, networkID, email, hu return ErrInvalidHumanId } + prevMembersHumanIds, membersErr := s.repo.getMemberHumanIds(ctx, networkID) + if membersErr != nil { + flog.Warn("unable to get member human ids", "error", membersErr) + } + if err := s.mutateMembers(ctx, networkID, func(tx pgx.Tx) error { err := s.repo.deleteInvitation(ctx, tx, networkID, normalized) if err != nil { @@ -241,6 +267,40 @@ func (s *serviceImpl) AcceptInvitation(ctx context.Context, networkID, email, hu return err } + if membersErr == nil && len(prevMembersHumanIds) > 0 { + emailRecipients := make([]string, 0, len(prevMembersHumanIds)) + for _, memberHumanId := range prevMembersHumanIds { + if memberHumanId != "" { + human, err := s.humanLookup.GetByID(ctx, memberHumanId) + if err != nil { + flog.Warn("unable to find human", "error", err, "humanId", memberHumanId) + continue + } + + if human.Email == "" || !human.EmailNotificationsEnabled { + continue + } + emailRecipients = append(emailRecipients, human.Email) + } + } + + if len(emailRecipients) > 0 { + newMemberEmailPrefix := strings.Split(normalized, "@")[0] + _, err = s.aeroSvc.ShootEmail(ctx, &pbaero.ShootEmailRequest{ + ToEmails: emailRecipients, + Subject: fmt.Sprintf("A new member has joined %s", network.Name), + TemplateData: &pbaero.ShootEmailRequest_SimpleHtmlData{ + SimpleHtmlData: &pbaero.SimpleHtmlData{ + Html: buildNewMemberHTML(network.Name, newMemberEmailPrefix), + }, + }, + }) + if err != nil { + flog.Warn("unable to send email notification", "email", email, "network", network.Name) + } + } + } + s.mirrorAddMembership(ctx, humanId, networkID) return nil } @@ -253,8 +313,24 @@ func (s *serviceImpl) RevokeInvitation(ctx context.Context, networkID, email str return s.repo.deleteInvitation(ctx, s.pool, networkID, normalized) } +const ( + emailWebAppURL = "https://llink.flowy.live" + emailDesktopURL = "llink://" + emailDownloadURL = "https://flowylabs.ai/llink/download" +) + +// emailDesktopFooter is the shared secondary line offering the desktop app. +// The web app is always the primary CTA (no install required), so desktop is +// kept quiet here and shared across templates so the two can't drift apart. +func emailDesktopFooter() string { + return fmt.Sprintf(`
| - You've been invited to join %s on Flowy.llink + You've been invited to join %s on Flowy.llink | |
| - Launch (or download) the app to accept your invitation and connect with your team. + Open the app to accept your invitation. If you're new, you'll be prompted to create a free account first. | |
| + | - Launch Flowy.llink + Open Flowy.llink (web) |