Add support for removing/adding channel memmbers
This commit is contained in:
@@ -68,17 +68,11 @@ CREATE TABLE team_members (
|
||||
PRIMARY KEY (rel_team, rel_user)
|
||||
);
|
||||
|
||||
-- handles membership, visibility and level of access
|
||||
-- handles channel membership
|
||||
CREATE TABLE channel_members (
|
||||
rel_channel BIGINT UNSIGNED NOT NULL REFERENCES channels(id),
|
||||
rel_user BIGINT UNSIGNED NOT NULL REFERENCES users(id),
|
||||
|
||||
-- display only messages from-to to particular user/organisation
|
||||
messages_since DATETIME NULL,
|
||||
messages_until DATETIME NULL,
|
||||
|
||||
created_at DATETIME NOT NULL DEFAULT NOW(),
|
||||
|
||||
PRIMARY KEY (rel_channel, rel_user)
|
||||
);
|
||||
|
||||
|
||||
+13
-15
@@ -50,11 +50,7 @@ func (r channel) Find(ctx context.Context, filter *types.ChannelFilter) ([]*type
|
||||
sql += " ORDER BY name ASC"
|
||||
|
||||
rval := make([]*types.Channel, 0)
|
||||
if err := db.SelectContext(ctx, &rval, sql, params...); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return rval, nil
|
||||
}
|
||||
return rval, db.SelectContext(ctx, &rval, sql, params...)
|
||||
}
|
||||
|
||||
func (r channel) Create(ctx context.Context, mod *types.Channel) (*types.Channel, error) {
|
||||
@@ -67,11 +63,7 @@ func (r channel) Create(ctx context.Context, mod *types.Channel) (*types.Channel
|
||||
mod.SetMeta([]byte("{}"))
|
||||
}
|
||||
|
||||
if err := db.Insert("channels", mod); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return mod, nil
|
||||
}
|
||||
return mod, db.Insert("channels", mod)
|
||||
}
|
||||
|
||||
func (r channel) Update(ctx context.Context, mod *types.Channel) (*types.Channel, error) {
|
||||
@@ -80,11 +72,17 @@ func (r channel) Update(ctx context.Context, mod *types.Channel) (*types.Channel
|
||||
now := time.Now()
|
||||
mod.SetUpdatedAt(&now)
|
||||
|
||||
if err := db.Replace("channels", mod); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return mod, nil
|
||||
}
|
||||
return mod, db.Replace("channels", mod)
|
||||
}
|
||||
|
||||
func (r channel) AddMember(ctx context.Context, channelID, userID uint64) error {
|
||||
sql := `INSERT INTO channel_members (rel_channel, rel_user) VALUES (?, ?)`
|
||||
return exec(factory.Database.MustGet().ExecContext(ctx, sql, channelID, userID))
|
||||
}
|
||||
|
||||
func (r channel) RemoveMember(ctx context.Context, channelID, userID uint64) error {
|
||||
sql := `DELETE FROM channel_members WHERE rel_channel = ? AND rel_user = ?`
|
||||
return exec(factory.Database.MustGet().ExecContext(ctx, sql, channelID, userID))
|
||||
}
|
||||
|
||||
func (r channel) Archive(ctx context.Context, id uint64) error {
|
||||
|
||||
@@ -16,31 +16,31 @@ func TestChannel(t *testing.T) {
|
||||
|
||||
rpo := Channel()
|
||||
ctx := context.Background()
|
||||
att := types.Channel{}.New()
|
||||
chn := types.Channel{}.New()
|
||||
|
||||
var name1, name2 = "Test channel v1", "Test channel v2"
|
||||
|
||||
var aa []*types.Channel
|
||||
|
||||
att.SetName(name1)
|
||||
chn.SetName(name1)
|
||||
|
||||
att, err = rpo.Create(ctx, att)
|
||||
chn, err = rpo.Create(ctx, chn)
|
||||
must(t, err)
|
||||
if att.Name != name1 {
|
||||
if chn.Name != name1 {
|
||||
t.Fatal("Changes were not stored")
|
||||
}
|
||||
|
||||
att.SetName(name2)
|
||||
chn.SetName(name2)
|
||||
|
||||
att, err = rpo.Update(ctx, att)
|
||||
chn, err = rpo.Update(ctx, chn)
|
||||
must(t, err)
|
||||
if att.Name != name2 {
|
||||
if chn.Name != name2 {
|
||||
t.Fatal("Changes were not stored")
|
||||
}
|
||||
|
||||
att, err = rpo.FindByID(ctx, att.ID)
|
||||
chn, err = rpo.FindByID(ctx, chn.ID)
|
||||
must(t, err)
|
||||
if att.Name != name2 {
|
||||
if chn.Name != name2 {
|
||||
t.Fatal("Changes were not stored")
|
||||
}
|
||||
|
||||
@@ -50,7 +50,30 @@ func TestChannel(t *testing.T) {
|
||||
t.Fatal("No results found")
|
||||
}
|
||||
|
||||
must(t, rpo.Archive(ctx, att.ID))
|
||||
must(t, rpo.Unarchive(ctx, att.ID))
|
||||
must(t, rpo.Delete(ctx, att.ID))
|
||||
must(t, rpo.Archive(ctx, chn.ID))
|
||||
must(t, rpo.Unarchive(ctx, chn.ID))
|
||||
must(t, rpo.Delete(ctx, chn.ID))
|
||||
}
|
||||
|
||||
func TestChannelMembers(t *testing.T) {
|
||||
var err error
|
||||
|
||||
if testing.Short() {
|
||||
t.Skip("skipping test in short mode.")
|
||||
return
|
||||
}
|
||||
|
||||
rpo := Channel()
|
||||
ctx := context.Background()
|
||||
|
||||
chn := types.Channel{}.New()
|
||||
chn, err = rpo.Create(ctx, chn)
|
||||
must(t, err)
|
||||
|
||||
usr := types.User{}.New()
|
||||
usr, err = User().Create(ctx, usr)
|
||||
must(t, err)
|
||||
|
||||
must(t, rpo.AddMember(ctx, chn.ID, usr.ID))
|
||||
must(t, rpo.RemoveMember(ctx, chn.ID, usr.ID))
|
||||
}
|
||||
|
||||
@@ -23,3 +23,7 @@ func simpleDelete(ctx context.Context, tableName string, id uint64) (err error)
|
||||
_, err = db.ExecContext(ctx, sql, id)
|
||||
return err
|
||||
}
|
||||
|
||||
func exec(_ interface{}, err error) error {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -59,6 +59,11 @@ func (r user) Find(ctx context.Context, filter *types.UserFilter) ([]*types.User
|
||||
sql += " AND username LIKE ?"
|
||||
params = append(params, filter.Query+"%")
|
||||
}
|
||||
|
||||
if filter.MembersOfChannel > 0 {
|
||||
sql += " AND id IN (SELECT rel_user FROM channel_members WHERE rel_channel = ?)"
|
||||
params = append(params, filter.MembersOfChannel)
|
||||
}
|
||||
}
|
||||
|
||||
sql += " ORDER BY username ASC"
|
||||
|
||||
@@ -2,6 +2,7 @@ package types
|
||||
|
||||
type (
|
||||
UserFilter struct {
|
||||
Query string
|
||||
Query string
|
||||
MembersOfChannel uint64
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user