From ac98a625939442f570d11ea12defdb25f0c272e4 Mon Sep 17 00:00:00 2001 From: Denis Arh Date: Wed, 19 Jun 2019 17:44:08 +0200 Subject: [PATCH] Add default channel creation on setup --- messaging/autosetup.go | 36 ++++++++++++++++++++++++ messaging/internal/repository/channel.go | 6 ++-- messaging/messaging.go | 1 + 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/messaging/autosetup.go b/messaging/autosetup.go index 64f179cfe..90dff235e 100644 --- a/messaging/autosetup.go +++ b/messaging/autosetup.go @@ -4,8 +4,11 @@ import ( "context" "github.com/spf13/cobra" + "github.com/titpetric/factory" + "github.com/cortezaproject/corteza-server/messaging/internal/repository" "github.com/cortezaproject/corteza-server/messaging/internal/service" + "github.com/cortezaproject/corteza-server/messaging/types" "github.com/cortezaproject/corteza-server/pkg/cli" ) @@ -15,3 +18,36 @@ func accessControlSetup(ctx context.Context, cmd *cobra.Command, c *cli.Config) var ac = service.DefaultAccessControl return p.Grant(ctx, ac.Whitelist(), ac.DefaultRules()...) } + +// Add default channels when there are none +func makeDefaultChannels(ctx context.Context, cmd *cobra.Command, c *cli.Config) error { + db, err := factory.Database.Get(messaging) + if err != nil { + return err + } + + repo := repository.Channel(ctx, db) + + cc, err := repo.Find(nil) + if err != nil { + return err + } + + if len(cc) == 0 { + cc = types.ChannelSet{ + &types.Channel{Name: "General"}, + &types.Channel{Name: "Random"}, + } + + err = cc.Walk(func(c *types.Channel) error { + _, err := repo.Create(c) + return err + }) + + if err != nil { + return err + } + } + + return nil +} diff --git a/messaging/internal/repository/channel.go b/messaging/internal/repository/channel.go index fe5464175..7d3f1fe51 100644 --- a/messaging/internal/repository/channel.go +++ b/messaging/internal/repository/channel.go @@ -17,7 +17,7 @@ type ( FindByID(id uint64) (*types.Channel, error) FindByMemberSet(memberID ...uint64) (*types.Channel, error) - Find(filter *types.ChannelFilter) ([]*types.Channel, error) + Find(filter *types.ChannelFilter) (types.ChannelSet, error) Create(mod *types.Channel) (*types.Channel, error) Update(mod *types.Channel) (*types.Channel, error) @@ -112,11 +112,11 @@ func (r *channel) FindByMemberSet(memberIDs ...uint64) (*types.Channel, error) { return mod, isFound(r.db().Get(mod, sqlChannelGroupByMemberSet, types.ChannelTypeGroup, len(memberIDs), membersConcat), mod.ID > 0, ErrChannelNotFound) } -func (r *channel) Find(filter *types.ChannelFilter) ([]*types.Channel, error) { +func (r *channel) Find(filter *types.ChannelFilter) (types.ChannelSet, error) { // @todo: actual searching (filter.Query) not just a full select params := make([]interface{}, 0) - rval := make([]*types.Channel, 0) + rval := types.ChannelSet{} sql := sqlChannelSelect diff --git a/messaging/messaging.go b/messaging/messaging.go index 8d9fd52b9..6b9060019 100644 --- a/messaging/messaging.go +++ b/messaging/messaging.go @@ -49,6 +49,7 @@ func Configure() *cli.Config { if c.ProvisionOpt.AutoSetup { cli.HandleError(accessControlSetup(ctx, cmd, c)) + cli.HandleError(makeDefaultChannels(ctx, cmd, c)) } return