Added title bar and basic message and command input

This commit is contained in:
khlieng 2015-01-21 03:06:34 +01:00
parent 508a04cf4c
commit f42d6011c6
23 changed files with 399 additions and 83 deletions

View file

@ -5,63 +5,98 @@ import (
)
type ChannelStore struct {
data map[string]map[string][]string
lock sync.Mutex
users map[string]map[string][]string
userLock sync.Mutex
topic map[string]map[string]string
topicLock sync.Mutex
}
func NewChannelStore() *ChannelStore {
return &ChannelStore{
data: make(map[string]map[string][]string),
users: make(map[string]map[string][]string),
topic: make(map[string]map[string]string),
}
}
func (c *ChannelStore) GetUsers(server, channel string) []string {
c.lock.Lock()
c.userLock.Lock()
users := make([]string, len(c.data[server][channel]))
copy(users, c.data[server][channel])
users := make([]string, len(c.users[server][channel]))
copy(users, c.users[server][channel])
c.lock.Unlock()
c.userLock.Unlock()
return users
}
func (c *ChannelStore) SetUsers(users []string, server, channel string) {
c.lock.Lock()
c.userLock.Lock()
if _, ok := c.data[server]; !ok {
c.data[server] = make(map[string][]string)
if _, ok := c.users[server]; !ok {
c.users[server] = make(map[string][]string)
}
c.data[server][channel] = users
c.users[server][channel] = users
c.lock.Unlock()
c.userLock.Unlock()
}
func (c *ChannelStore) AddUser(user, server, channel string) {
c.lock.Lock()
c.userLock.Lock()
if _, ok := c.data[server]; !ok {
c.data[server] = make(map[string][]string)
if _, ok := c.users[server]; !ok {
c.users[server] = make(map[string][]string)
}
if users, ok := c.data[server][channel]; ok {
c.data[server][channel] = append(users, user)
if users, ok := c.users[server][channel]; ok {
c.users[server][channel] = append(users, user)
} else {
c.data[server][channel] = []string{user}
c.users[server][channel] = []string{user}
}
c.lock.Unlock()
c.userLock.Unlock()
}
func (c *ChannelStore) RemoveUser(user, server, channel string) {
c.lock.Lock()
defer c.lock.Unlock()
c.userLock.Lock()
c.removeUser(user, server, channel)
c.userLock.Unlock()
}
for i, u := range c.data[server][channel] {
func (c *ChannelStore) RemoveUserAll(user, server string) {
c.userLock.Lock()
for channel, _ := range c.users[server] {
c.removeUser(user, server, channel)
}
c.userLock.Unlock()
}
func (c *ChannelStore) GetTopic(server, channel string) string {
c.topicLock.Lock()
defer c.topicLock.Unlock()
return c.topic[server][channel]
}
func (c *ChannelStore) SetTopic(topic, server, channel string) {
c.topicLock.Lock()
if _, ok := c.topic[server]; !ok {
c.topic[server] = make(map[string]string)
}
c.topic[server][channel] = topic
c.topicLock.Unlock()
}
func (c *ChannelStore) removeUser(user, server, channel string) {
for i, u := range c.users[server][channel] {
if u == user {
users := c.data[server][channel]
c.data[server][channel] = append(users[:i], users[i+1:]...)
users := c.users[server][channel]
c.users[server][channel] = append(users[:i], users[i+1:]...)
return
}
}

View file

@ -18,6 +18,7 @@ type Channel struct {
Server string `json:"server"`
Name string `json:"name"`
Users []string `json:"users"`
Topic string `json:"topic,omitempty"`
}
type User struct {
@ -99,7 +100,7 @@ func (u User) GetChannels() []Channel {
}
func (u User) AddServer(server Server) {
db.Update(func(tx *bolt.Tx) error {
go db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("Servers"))
data, _ := json.Marshal(server)
@ -110,7 +111,7 @@ func (u User) AddServer(server Server) {
}
func (u User) AddChannel(channel Channel) {
db.Update(func(tx *bolt.Tx) error {
go db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("Channels"))
data, _ := json.Marshal(channel)
@ -121,16 +122,25 @@ func (u User) AddChannel(channel Channel) {
}
func (u User) RemoveServer(address string) {
db.Update(func(tx *bolt.Tx) error {
tx.Bucket([]byte("Channels")).Delete([]byte(u.UUID + ":" + address))
go db.Update(func(tx *bolt.Tx) error {
serverID := []byte(u.UUID + ":" + address)
tx.Bucket([]byte("Servers")).Delete(serverID)
b := tx.Bucket([]byte("Channels"))
c := b.Cursor()
for k, _ := c.Seek(serverID); bytes.HasPrefix(k, serverID); k, _ = c.Next() {
b.Delete(k)
}
return nil
})
}
func (u User) RemoveChannel(server, channel string) {
db.Update(func(tx *bolt.Tx) error {
tx.Bucket([]byte("Servers")).Delete([]byte(u.UUID + ":" + server + ":" + channel))
go db.Update(func(tx *bolt.Tx) error {
tx.Bucket([]byte("Channels")).Delete([]byte(u.UUID + ":" + server + ":" + channel))
return nil
})