3
0

Add default channel creation on setup

This commit is contained in:
Denis Arh
2019-06-19 17:44:08 +02:00
parent 6456a5b78a
commit ac98a62593
3 changed files with 40 additions and 3 deletions
+36
View File
@@ -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
}
+3 -3
View File
@@ -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
+1
View File
@@ -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