summaryrefslogtreecommitdiff
path: root/server/channel/channel.go
diff options
context:
space:
mode:
Diffstat (limited to 'server/channel/channel.go')
-rw-r--r--server/channel/channel.go33
1 files changed, 31 insertions, 2 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))
}
})