diff options
| -rw-r--r-- | server/channel/channel.go | 33 | ||||
| -rw-r--r-- | server/server/command.go | 3 | ||||
| -rw-r--r-- | server/user/user.go | 8 |
3 files changed, 36 insertions, 8 deletions
diff --git a/server/channel/channel.go b/server/channel/channel.go index fdd4243..fc895eb 100644 --- a/server/channel/channel.go +++ b/server/channel/channel.go @@ -55,6 +55,7 @@ func (cs *ChannelKind) CreateChannel(name string) (*Channel, *proto.Fail) { err := cs.db.Update(func(tx *bolt.Tx) error { chm, _ := tx.CreateBucketIfNotExists([]byte("channel membership")) chm.CreateBucket([]byte(c.id)) + tx.CreateBucketIfNotExists([]byte("user channels")) return nil }) if err != nil { @@ -70,6 +71,28 @@ func DirectHandle(among map[string]bool) string { return strings.Join(slices.Sorted(maps.Keys(among)), "\x00") } +func (cs *ChannelKind) UserChannels(uid string) map[string]bool { + result := make(map[string]bool) + err := cs.db.View(func(tx *bolt.Tx) error { + channels := tx.Bucket([]byte("user channels")) + if channels == nil { + return nil + } + user := channels.Bucket([]byte(uid)) + if user == nil { + return nil + } + return user.ForEach(func(k, v []byte) error { + result[string(k)] = true + return nil + }) + }) + if err != nil { + log.Fatal("error updating database: ", err) + } + return result +} + func (cs *ChannelKind) GetDirect(among map[string]bool) *Channel { handle := DirectHandle(among) switch ch := cs.world.Lookup("direct-channel", handle).(type) { @@ -193,7 +216,6 @@ func (c *Channel) Join(u *user.User) *proto.Fail { return nil } c.SetMembership(u.Id(), c.GetDefaultMembership()) - // u.Channels[c.id] = true c.Put(proto.Object{"join", "", map[string]string {"f": u.Id()}}) return nil } @@ -203,7 +225,6 @@ func (c *Channel) Leave(u *user.User) *proto.Fail { return nil } c.SetMembership(u.Id(), Membership {Yes: false}) - // delete(u.Channels, c.id) c.Put(proto.Object{"leave", "", map[string]string {"f": u.Id()}}) return nil } @@ -247,14 +268,22 @@ func (c *Channel) GetMembership(uid string) Membership { func (c *Channel) SetMembership(uid string, m Membership) { err := c.kind.db.Update(func(tx *bolt.Tx) error { channels := tx.Bucket([]byte("channel membership")) + userChannels := tx.Bucket([]byte("user channels")) members, _ := channels.CreateBucketIfNotExists([]byte(c.id)) + user, _ := userChannels.CreateBucketIfNotExists([]byte(uid)) if m.Yes { + err := user.Put([]byte(c.id), []byte("yes")) + if err != nil { + return err + } + var buf bytes.Buffer writer := bufio.NewWriter(&buf) proto.WriteObject(writer, m.GetInfo()) writer.Flush() return members.Put([]byte(uid), buf.Bytes()) } else { + user.Delete([]byte(c.id)) return members.Delete([]byte(uid)) } }) diff --git a/server/server/command.go b/server/server/command.go index c1b28b9..e8ced39 100644 --- a/server/server/command.go +++ b/server/server/command.go @@ -147,9 +147,8 @@ func (s *server) SendRequest(r session.Request) { r.ReplyInvalid() return } - u := s.world.GetObject(r.From.UserId).(*user.User) var channels []proto.Object - for c := range u.Channels { + for c := range s.channelKind.UserChannels(r.From.UserId) { info := s.world.GetObject(c).InfoFor(r.From.UserId) channels = append(channels, info) } diff --git a/server/user/user.go b/server/user/user.go index 46eda35..7f272c1 100644 --- a/server/user/user.go +++ b/server/user/user.go @@ -22,7 +22,6 @@ type User struct { description string Stream session.Stream anonymous bool - Channels map[string]bool // TODO: remove } func Kind(world *object.World) *UserKind { @@ -45,7 +44,6 @@ func (us *UserKind) CreateUser(name string) (*User, *proto.Fail) { u.name = name u.id = proto.GenId() u.anonymous = true - u.Channels = make(map[string]bool) u.Save() return &u, nil } @@ -154,8 +152,10 @@ func (u *User) Delete() { } u.kind.world.PutObject(u.id, gone) err := u.kind.db.Update(func(tx *bolt.Tx) error { - bucket, _ := tx.CreateBucketIfNotExists([]byte("anonymous users")) - bucket.Delete([]byte(u.id)) + anons, _ := tx.CreateBucketIfNotExists([]byte("anonymous users")) + anons.Delete([]byte(u.id)) + channels, _ := tx.CreateBucketIfNotExists([]byte("user channels")) + channels.DeleteBucket([]byte(u.id)) return nil }) if err != nil { |
